import { assertExists } from "https://deno.land/std@0.224.0/assert/assert_exists.ts";
import { GuessResult } from "../game/game.ts";
import { async_next, Awaitable, LineReader } from "../utils.ts";
import { Guessing } from "./guesser.ts";

export class InputGuesser implements Guessing {
	length;
	lines;

	constructor(length: number) {
		this.length = length;
		this.lines = new LineReader(Deno.stdin.readable);
	}

	declare_properties(): string[] {
		return [];
	}

	async next_line() {
		const value = await this.lines.read();
		assertExists(value);
		return value;
	}

	async guess(try_: (guess: string, ...properties: unknown[]) => Awaitable<GuessResult>) {
		let input = "";

		while (true) {
			input = await this.next_line();
			if (input.length === this.length && are_letters(input)) break;
			console.log("Word is length", this.length, "and composed of letters.");
		}

		return await try_(input.slice(0));
	}
}

function are_letters(text: string) {
	const letters = "azertyuiopqsdfghjklmwxcvbn";
	for (const char of text) if (!letters.includes(char)) return false;
	return true;
}
