"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getWorkLanguage = getWorkLanguage;
exports.setWorkLanguage = setWorkLanguage;
exports.detectOsDefaultWorkLanguage = detectOsDefaultWorkLanguage;
exports.resolveWorkLanguage = resolveWorkLanguage;
const language_1 = require("@longrun-ai/kernel/types/language");
let workingLanguage = 'en';
function getWorkLanguage() {
    return workingLanguage;
}
function setWorkLanguage(language) {
    workingLanguage = language;
}
function normalizeLocaleLike(value) {
    // Examples: "en_US.UTF-8" -> "en-US", "zh_CN" -> "zh-CN"
    const stripped = value.split('.')[0] ?? value;
    return stripped.replace(/_/g, '-');
}
function detectOsDefaultWorkLanguage(env) {
    const candidates = [];
    const lang = env.LANG;
    if (typeof lang === 'string' && lang.trim() !== '')
        candidates.push(lang);
    for (const raw of candidates) {
        const normalized = normalizeLocaleLike(raw);
        const parsed = (0, language_1.normalizeLanguageCode)(normalized);
        if (parsed)
            return parsed;
    }
    try {
        const locale = Intl.DateTimeFormat().resolvedOptions().locale;
        const parsed = (0, language_1.normalizeLanguageCode)(locale);
        if (parsed)
            return parsed;
    }
    catch {
        // ignore
    }
    return null;
}
function resolveWorkLanguage(options) {
    const detected = detectOsDefaultWorkLanguage(options.env);
    if (detected)
        return { language: detected, source: 'os' };
    return { language: 'en', source: 'default' };
}
