import { assertExists } from "https://deno.land/std@0.224.0/assert/assert_exists.ts";

import { enumerate } from "./utils.ts";

export function initialize_prompt() {
	console.log("Please input initial state of the game. Format is :");
	console.log("  letter    [a-z]    known letter");
	console.log("  dot       .        unknown letter");
	console.log("example : .rb..");
	console.log("");
	const inputted = parse_initialization_until_correct();
	console.log("");
	console.log("From now on, please try the following guesses and report results. Format is :");
	console.log("  plus     +    correct");
	console.log("  minus    -    wrong placement");
	console.log("  dot      .    incorrect");
	console.log("example : -++..");
	console.log();
	return inputted;
}

function parse_initialization_until_correct() {
	let input = prompt("initial :");
	while (true) {
		assertExists(input);
		const parsed = parse_initialization(input);
		if (parsed === null) input = prompt("Invalid, please try again :");
		else return parsed;
	}
}

function parse_initialization(input: string) {
	const code_a = "a".charCodeAt(0);
	const code_z = "z".charCodeAt(0);
	let length = 0;
	const constraints = [] as [number, string][];
	for (const [index, char] of enumerate(input.trim().toLowerCase())) {
		length += 1;
		if (char === ".") continue;
		const code = char.charCodeAt(0);
		if (code < code_a) return null;
		if (code > code_z) return null;
		constraints.push([index, char]);
	}
	return { length, constraints };
}
