import { getKeysUpToLevel } from "./levels";

/**
 * Word lists per level.
 * Levels 1-2: Only F/J/D/K available -- no real German words possible, use letter combos.
 * Levels 3+: Mix of real German words and combos using only learned letters.
 * All words max 5 characters, no spaces.
 */
export const wordsByLevel: Record<number, string[]> = {
	// Level 1: f, j
	1: ["ff", "jj", "fj", "jf", "fjf", "jfj", "fjfj", "jfjf", "ffj", "jjf"],
	// Level 2: f, j, d, k
	2: ["fdk", "kdf", "djf", "fkd", "dkf", "jdk", "kfj", "dfjk", "kjdf", "fkjd"],
	// Level 3: f, j, d, k, s, l
	3: ["sdf", "sdk", "lsd", "flsk", "sldf", "dsl", "kls", "flds", "jskl", "lkds"],
	// Level 4: f, j, d, k, s, l, a, ö -- "aas", "das", "lass", "als" are real words
	4: ["als", "das", "aas", "lass", "fad", "dal", "lad", "alfa", "salö", "aöds"],
	// Level 5: f, j, d, k, s, l, a, ö, g, h -- "lag", "sah", "glas" are real words
	5: ["glas", "lag", "gah", "hag", "gal", "sah", "hals", "has", "gash", "flags"],
	// Level 6: f, j, d, k, s, l, a, ö, g, h, e, i -- "eis", "die", "sie", "lied", "fies"
	6: ["eis", "die", "sie", "lied", "fies", "dies", "sei", "geld", "held", "lieh"],
	// Level 7: +r, u -- bekannt: f,j,d,k,s,l,a,ö,g,h,e,i,r,u
	7: ["ruhe", "reis", "reif", "rief", "drei", "fuhr", "ufer", "krug", "raus", "seid"],
	// Level 8: +t, z -- bekannt: ...+t,z
	8: ["zeit", "ziel", "tier", "tief", "fest", "rest", "sitz", "reist", "hitze", "kreuz"],
	// Level 9: +w, o -- bekannt: ...+w,o
	9: ["wort", "rose", "dorf", "wohl", "dort", "rost", "ohr", "trotz", "woher", "forst"],
	// Level 10: +q, p -- bekannt: ...+q,p
	10: ["topf", "kopf", "post", "opfer", "sport", "preis", "quer", "pfad", "prost", "stopp"],
	// Level 11: +n, b -- bekannt: ...+n,b
	11: ["bein", "nein", "blind", "braun", "brot", "nebst", "brand", "oben", "born", "binde"],
	// Level 12: +v, m -- bekannt: ...+v,m
	12: ["vorn", "helm", "form", "viel", "mein", "warm", "stimm", "vater", "mund", "monat"],
	// Level 13: +c, x -- bekannt: ...+c,x
	13: ["dach", "nach", "buch", "fach", "noch", "text", "wachs", "mach", "nixe", "hexe"],
	// Level 14: +ä, ü -- bekannt: ...+ä,ü
	14: ["über", "übel", "grün", "kühl", "spät", "wählt", "würde", "trägt", "fährt", "hüpft"],
	// Level 15: +y, "," -- bekannt: ...+y (Komma in Wörtern selten, echte Wörter mit y)
	15: ["baby", "typ", "yoga", "party", "handy", "hobby", "yacht", "lyrik", "idyll", "hymne"],
	// Level 16: +"." -- Punkt als Satzzeichen üben, kurze Wörter mit Punkt
	16: ["da.", "so.", "gut.", "ja.", "hier.", "dort.", "fein.", "wohl.", "mein.", "doch."],
};

/**
 * Returns `count` random words from the given level's word list.
 * If count <= list length: sampling without replacement.
 * If count > list length: sampling with replacement.
 * Returns empty array for non-existent levels.
 */
export function getWordsForLevel(level: number, count: number): string[] {
	const list = wordsByLevel[level];
	if (!list || list.length === 0) return [];

	if (count <= list.length) {
		// Shuffle and take first `count` (without replacement)
		const shuffled = [...list];
		for (let i = shuffled.length - 1; i > 0; i--) {
			const j = Math.floor(Math.random() * (i + 1));
			[shuffled[i], shuffled[j]] = [shuffled[j]!, shuffled[i]!];
		}
		return shuffled.slice(0, count);
	}

	// With replacement
	const result: string[] = [];
	for (let i = 0; i < count; i++) {
		result.push(list[Math.floor(Math.random() * list.length)]!);
	}
	return result;
}

// Validation helper (used in tests)
export function validateWordList(): boolean {
	for (const [levelStr, words] of Object.entries(wordsByLevel)) {
		const level = Number(levelStr);
		const allowedKeys = getKeysUpToLevel(level).filter((k) => k !== " ");
		for (const word of words) {
			for (const char of word) {
				if (!allowedKeys.includes(char)) return false;
			}
		}
	}
	return true;
}
