/**
 * Test file for the lang-codes package
 * This file demonstrates the usage and validates the functionality
 */

import {
	allLanguages,
	findBy,
	getCodes,
	getNames,
	hasLanguage,
	type Language,
	type LanguageCodeType,
	langs, // Legacy API
} from "./index";

function runTests() {
	console.log("🧪 Testing lang-codes package...\n");

	// Test 1: allLanguages()
	console.log("1️⃣ Testing allLanguages()");
	const allLangs = allLanguages();
	console.log(`   ✅ Total languages: ${allLangs.length}`);
	console.log(
		`   ✅ First language: ${allLangs[0].name} (${allLangs[0]["1"]})`,
	);
	console.log();

	// Test 2: hasLanguage()
	console.log("2️⃣ Testing hasLanguage()");
	console.log(`   ✅ Has English (ISO 639-1): ${hasLanguage("1", "en")}`);
	console.log(`   ✅ Has Spanish (name): ${hasLanguage("name", "Spanish")}`);
	console.log(`   ✅ Has Klingon (name): ${hasLanguage("name", "Klingon")}`);
	console.log();

	// Test 3: getCodes()
	console.log("3️⃣ Testing getCodes()");
	const iso1Codes = getCodes("1");
	const iso2Codes = getCodes("2");
	const invalidCodes = getCodes("invalid" as LanguageCodeType);
	console.log(`   ✅ ISO 639-1 codes count: ${iso1Codes?.length}`);
	console.log(`   ✅ ISO 639-2 codes count: ${iso2Codes?.length}`);
	console.log(`   ✅ Invalid type returns: ${invalidCodes}`);
	console.log(
		`   ✅ Sample ISO 639-1 codes: ${iso1Codes?.slice(0, 5).join(", ")}`,
	);
	console.log();

	// Test 4: getNames()
	console.log("4️⃣ Testing getNames()");
	const englishNames = getNames(false);
	const localNames = getNames(true);
	console.log(`   ✅ English names count: ${englishNames.length}`);
	console.log(`   ✅ Local names count: ${localNames.length}`);
	console.log(
		`   ✅ Sample English names: ${englishNames.slice(0, 3).join(", ")}`,
	);
	console.log(`   ✅ Sample local names: ${localNames.slice(0, 3).join(", ")}`);
	console.log();

	// Test 5: findBy()
	console.log("5️⃣ Testing findBy()");
	const english = findBy("1", "en");
	const spanish = findBy("name", "Spanish");
	const french = findBy("local", "Français");
	const notFound = findBy("name", "Klingon");

	console.log(
		`   ✅ English by ISO 639-1: ${english?.name} (${english?.local})`,
	);
	console.log(`   ✅ Spanish by name: ${spanish?.name} (${spanish?.["1"]})`);
	console.log(`   ✅ French by local name: ${french?.name} (${french?.["1"]})`);
	console.log(`   ✅ Klingon (not found): ${notFound}`);
	console.log();

	// Test 6: Legacy API
	console.log("6️⃣ Testing Legacy API");
	const legacyAll = langs.all();
	const legacyHas = langs.has("1", "de");
	const legacyCodes = langs.codes("2");
	const legacyNames = langs.names(false);
	const legacyWhere = langs.where("name", "German");

	console.log(`   ✅ Legacy all() count: ${legacyAll.length}`);
	console.log(`   ✅ Legacy has() German: ${legacyHas}`);
	console.log(`   ✅ Legacy codes() count: ${legacyCodes?.length}`);
	console.log(`   ✅ Legacy names() count: ${legacyNames.length}`);
	console.log(
		`   ✅ Legacy where() German: ${legacyWhere?.name} (${legacyWhere?.["1"]})`,
	);
	console.log();

	// Test 7: Type validation
	console.log("7️⃣ Testing Type Safety");
	const language: Language | undefined = findBy("name", "English");
	if (language) {
		// Type-safe access to language properties
		const name: string = language.name;
		const local: string = language.local;
		const iso1: string = language["1"];
		const _iso2: string = language["2"];
		console.log(`   ✅ Type-safe access: ${name} (${iso1}) - ${local}`);
	}

	// Test various code types
	const codeTypes: LanguageCodeType[] = ["1", "2", "2B", "2T", "3", 1, 2, 3];
	console.log(`   ✅ Valid code types: ${codeTypes.join(", ")}`);
	console.log();

	console.log("🎉 All tests completed successfully!");
}

// Run the tests
runTests();
