// utils/transliterate.ts

// Define the type for the transliteration map
type TransliterationMap = {
    [key: string]: string;
};

export const transliterationMap: TransliterationMap = {
    // Russian/Ukrainian/Bulgarian Cyrillic
    'а': 'a', 'б': 'b', 'в': 'v', 'г': 'g', 'д': 'd',
    'е': 'e', 'ё': 'jo', 'ж': 'zh', 'з': 'z', 'и': 'i',
    'й': 'j',        // 'й' -> 'j'
    'к': 'k', 'л': 'l', 'м': 'm', 'н': 'n',
    'о': 'o', 'п': 'p', 'р': 'r', 'с': 's', 'т': 't',
    'у': 'u', 'ф': 'f', 'х': 'h',      // 'х' -> 'h' (not 'kh')
    'ц': 'c',        // 'ц' -> 'c' (not 'ts')
    'ч': 'ch', 'ш': 'sh', 'щ': 'shch',
    'ъ': '', 'ы': 'y', 'ь': '',        // Soft sign becomes empty
    'э': 'je',       // 'э' -> 'je' (crucial for your example)
    'ю': 'ju',       // 'ю' -> 'ju'
    'я': 'ja',       // 'я' -> 'ja'

    // Capital letters
    'А': 'A', 'Б': 'B', 'В': 'V', 'Г': 'G', 'Д': 'D',
    'Е': 'E', 'Ё': 'Jo', 'Ж': 'Zh', 'З': 'Z', 'И': 'I',
    'Й': 'J', 'К': 'K', 'Л': 'L', 'М': 'M', 'Н': 'N',
    'О': 'O', 'П': 'P', 'Р': 'R', 'С': 'S', 'Т': 'T',
    'У': 'U', 'Ф': 'F', 'Х': 'H', 'Ц': 'C',
    'Ч': 'Ch', 'Ш': 'Sh', 'Щ': 'Shch',
    'Ъ': '', 'Ы': 'Y', 'Ь': '',
    'Э': 'Je',       // 'Э' -> 'Je'
    'Ю': 'Ju', 'Я': 'Ja'
};


/**
 * Transliterates Cyrillic text to Latin characters
 * @param text - The input text to transliterate
 * @returns Transliterated text with Latin characters
 */
export function transliterate(text: string): string {
    if (!text) return '';

    return text.split('').map((char: string): string => {
        return transliterationMap[char] || char;
    }).join('');
}

// Optional: Add a more comprehensive version with options
interface TransliterateOptions {
    preserveCase?: boolean;
    ignoreUnknown?: boolean;
}

/**
 * Advanced transliteration with options
 * @param text - The input text to transliterate
 * @param options - Configuration options
 * @returns Transliterated text
 */
export function transliterateAdvanced(
    text: string,
    options: TransliterateOptions = {}
): string {
    if (!text) return '';

    const { preserveCase = false, ignoreUnknown = true } = options;

    return text.split('').map((char: string): string => {
        const transliterated = transliterationMap[char];

        if (transliterated !== undefined) {
            return transliterated;
        }

        // Handle unknown characters
        if (!ignoreUnknown) {
            // You might want to log or handle unknown characters differently
            console.warn(`Unknown character found: ${char}`);
        }

        return char;
    }).join('');
}

export const containsCyrillic = (text: string): boolean => {
    // First, decode the URL-encoded string
    try {
        const decodedText = decodeURIComponent(text);

        const cyrillicPattern = /[а-яА-ЯёЁіІїЇєЄґҐ]/;
        return cyrillicPattern.test(decodedText);
    } catch (e) {
        // If decoding fails, test the original text
        console.error('Error decoding URL:', e);
        const cyrillicPattern = /[а-яА-ЯёЁіІїЇєЄґҐ]/;
        return cyrillicPattern.test(text);
    }
};