"use strict";
/**
 * Resolve language from dataset `config.language` for synthesis, TTS, and Presidio.
 * UI often stores an object `{ name, code, SpeechSynthesisLanguage }` or a string.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDatasetLanguageLabelForSynthesis = getDatasetLanguageLabelForSynthesis;
exports.getDatasetLanguageForPresidio = getDatasetLanguageForPresidio;
exports.getDatasetDefaultTtsLocale = getDatasetDefaultTtsLocale;
function getDatasetLanguageLabelForSynthesis(datasetConfig) {
    if (!datasetConfig || typeof datasetConfig !== 'object') {
        return 'English';
    }
    const lang = datasetConfig.language;
    if (typeof lang === 'string' && lang.trim()) {
        return lang.trim();
    }
    if (lang && typeof lang === 'object') {
        const o = lang;
        if (typeof o.name === 'string' && o.name.trim()) {
            return o.name.trim();
        }
        if (typeof o.label === 'string' && o.label.trim()) {
            return o.label.trim();
        }
    }
    return 'English';
}
const LANGUAGE_NAME_TO_PRESIDIO = {
    english: 'en',
    french: 'fr',
    spanish: 'es',
    german: 'de',
    italian: 'it',
    portuguese: 'pt',
    dutch: 'nl',
    polish: 'pl',
    russian: 'ru',
    chinese: 'zh',
    japanese: 'ja',
    korean: 'ko',
    arabic: 'ar',
    hindi: 'hi',
};
const LANGUAGE_NAME_TO_BCP47 = {
    english: 'en-US',
    french: 'fr-FR',
    spanish: 'es-ES',
    german: 'de-DE',
    italian: 'it-IT',
    portuguese: 'pt-PT',
    dutch: 'nl-NL',
    polish: 'pl-PL',
    russian: 'ru-RU',
    chinese: 'zh-CN',
    japanese: 'ja-JP',
    korean: 'ko-KR',
    arabic: 'ar-SA',
    hindi: 'hi-IN',
};
function presidioFromName(name) {
    const t = name.trim().toLowerCase();
    return LANGUAGE_NAME_TO_PRESIDIO[t] ?? 'en';
}
/** Presidio analyzer language code (ISO 639-1 where possible). */
function getDatasetLanguageForPresidio(datasetConfig) {
    if (!datasetConfig || typeof datasetConfig !== 'object') {
        return 'en';
    }
    const lang = datasetConfig.language;
    if (typeof lang === 'string' && lang.trim()) {
        const t = lang.trim();
        if (/^[a-z]{2}$/i.test(t)) {
            return t.toLowerCase();
        }
        return presidioFromName(t);
    }
    if (lang && typeof lang === 'object') {
        const o = lang;
        if (typeof o.name === 'string' && o.name.trim()) {
            return presidioFromName(o.name);
        }
        const code = typeof o.code === 'string' ? o.code : '';
        if (code && /^[a-z]{2}/i.test(code)) {
            return code.slice(0, 2).toLowerCase();
        }
        const ss = typeof o.SpeechSynthesisLanguage === 'string' ? o.SpeechSynthesisLanguage : '';
        if (ss && /^[a-z]{2}/i.test(ss)) {
            return ss.slice(0, 2).toLowerCase();
        }
    }
    return 'en';
}
/** Default locale for text-to-speech (e.g. ElevenLabs / Azure). */
function getDatasetDefaultTtsLocale(datasetConfig) {
    if (!datasetConfig || typeof datasetConfig !== 'object') {
        return 'en-US';
    }
    const lang = datasetConfig.language;
    if (typeof lang === 'string' && lang.trim()) {
        const t = lang.trim();
        if (/^[a-z]{2}(-[A-Z]{2})?$/i.test(t)) {
            return t;
        }
        const fromName = LANGUAGE_NAME_TO_BCP47[t.toLowerCase()];
        return fromName ?? 'en-US';
    }
    if (lang && typeof lang === 'object') {
        const o = lang;
        const ss = typeof o.SpeechSynthesisLanguage === 'string' ? o.SpeechSynthesisLanguage.trim() : '';
        if (ss) {
            return ss;
        }
        const code = typeof o.code === 'string' ? o.code.trim() : '';
        if (code && /^[a-z]{2}/i.test(code)) {
            return code;
        }
        if (typeof o.name === 'string' && o.name.trim()) {
            const b = LANGUAGE_NAME_TO_BCP47[o.name.trim().toLowerCase()];
            if (b) {
                return b;
            }
        }
    }
    return 'en-US';
}
//# sourceMappingURL=datasetDynamicDatasetLanguage.js.map