/**
 * Extract Korean initial consonants (초성) from a given string.
 * This is useful for fuzzy searching Korean words.
 */
const CHO_SUNG = ['ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ'];

export function getChoseong(str: string): string {
    let result = '';
    for (let i = 0; i < str.length; i++) {
        const code = str.charCodeAt(i) - 44032; // 0xAC00 (가)

        // Check if the character is a complete Korean syllable
        if (code > -1 && code < 11172) {
            result += CHO_SUNG[Math.floor(code / 588)];
        } else {
            // Keep the character as-is if it's not a complete Korean syllable (e.g., english, numbers, spaces, isolated consonants)
            result += str.charAt(i);
        }
    }
    return result;
}
