/**
 * 语言检测工具函数集合
 *
 * 提供完整的语言检测、判断和转换功能，支持多种检测方式：
 * 1. 浏览器语言设置检测
 * 2. Ant Design ConfigProvider 的 locale 检测
 * 3. localStorage 中保存的用户选择检测
 * 4. 语言代码转换和标准化
 */ function _class_call_check(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function");
    }
}
function _defineProperties(target, props) {
    for(var i = 0; i < props.length; i++){
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;
        if ("value" in descriptor) descriptor.writable = true;
        Object.defineProperty(target, descriptor.key, descriptor);
    }
}
function _create_class(Constructor, protoProps, staticProps) {
    if (protoProps) _defineProperties(Constructor.prototype, protoProps);
    if (staticProps) _defineProperties(Constructor, staticProps);
    return Constructor;
}
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
/**
 * 支持的语言类型
 */ export var SUPPORTED_LANGUAGES = [
    'zh-CN',
    'en-US'
];
/**
 * 语言映射配置
 */ export var LANGUAGE_MAP = {
    'zh-CN': {
        code: 'zh-CN',
        name: '简体中文',
        nativeName: '简体中文',
        flag: '🇨🇳',
        antdLocale: 'zh_CN'
    },
    'en-US': {
        code: 'en-US',
        name: 'English',
        nativeName: 'English',
        flag: '🇺🇸',
        antdLocale: 'en_US'
    }
};
/**
 * 检测浏览器语言设置
 *
 * @returns 检测到的语言代码，如果无法检测则返回 null
 *
 * @example
 * ```ts
 * const browserLang = detectBrowserLanguage();
 * console.log(browserLang); // 'zh-CN' | 'en-US' | null
 * ```
 */ export function detectBrowserLanguage() {
    if (typeof navigator === 'undefined') {
        return null;
    }
    // 获取浏览器语言列表
    var browserLanguages = navigator.languages || [
        navigator.language
    ];
    var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
    try {
        for(var _iterator = browserLanguages[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
            var lang = _step.value;
            var langLower = lang.toLowerCase();
            // 检测中文
            if (langLower.startsWith('zh')) {
                return 'zh-CN';
            }
            // 检测英文
            if (langLower.startsWith('en')) {
                return 'en-US';
            }
        }
    } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
    } finally{
        try {
            if (!_iteratorNormalCompletion && _iterator.return != null) {
                _iterator.return();
            }
        } finally{
            if (_didIteratorError) {
                throw _iteratorError;
            }
        }
    }
    return null;
}
/**
 * 检测 Ant Design ConfigProvider 的 locale
 *
 * @returns 检测到的语言代码，如果无法检测则返回 null
 *
 * @example
 * ```ts
 * const antdLang = detectAntdLocale();
 * console.log(antdLang); // 'zh-CN' | 'en-US' | null
 * ```
 */ export function detectAntdLocale() {
    if (typeof document === 'undefined') {
        return null;
    }
    try {
        var _document_querySelector;
        // 尝试从 DOM 属性中获取 Ant Design locale
        var antdLocale = (_document_querySelector = document.querySelector('[data-antd-locale]')) === null || _document_querySelector === void 0 ? void 0 : _document_querySelector.getAttribute('data-antd-locale');
        if (antdLocale) {
            var localeLower = antdLocale.toLowerCase();
            if (localeLower.includes('zh')) {
                return 'zh-CN';
            }
            if (localeLower.includes('en')) {
                return 'en-US';
            }
        }
    } catch (error) {
    // 忽略错误，继续其他检测方式
    }
    return null;
}
/**
 * 从 localStorage 中获取用户保存的语言选择
 *
 * @param key - localStorage 中的键名，默认为 'md-editor-language'
 * @returns 保存的语言代码，如果没有保存则返回 null
 *
 * @example
 * ```ts
 * const savedLang = getSavedLanguage();
 * console.log(savedLang); // 'zh-CN' | 'en-US' | null
 * ```
 */ export function getSavedLanguage() {
    var key = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : 'md-editor-language';
    if (typeof window === 'undefined') {
        return null;
    }
    try {
        var savedLanguage = localStorage.getItem(key);
        if (savedLanguage && SUPPORTED_LANGUAGES.includes(savedLanguage)) {
            return savedLanguage;
        }
    } catch (error) {
    // 忽略 localStorage 访问错误
    }
    return null;
}
/**
 * 保存用户语言选择到 localStorage
 *
 * @param language - 要保存的语言代码
 * @param key - localStorage 中的键名，默认为 'md-editor-language'
 *
 * @example
 * ```ts
 * saveLanguage('en-US');
 * ```
 */ export function saveLanguage(language) {
    var key = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 'md-editor-language';
    if (typeof window === 'undefined') {
        return;
    }
    try {
        localStorage.setItem(key, language);
    } catch (error) {
        // 忽略 localStorage 访问错误
        console.warn('Failed to save language preference:', error);
    }
}
/**
 * 智能语言检测
 *
 * 按优先级检测用户语言偏好：
 * 1. localStorage 中保存的用户选择
 * 2. Ant Design ConfigProvider 的 locale
 * 3. 浏览器语言设置
 * 4. 默认语言
 *
 * @param defaultLanguage - 默认语言，当所有检测方式都失败时使用
 * @returns 检测到的语言代码
 *
 * @example
 * ```ts
 * const detectedLang = detectUserLanguage();
 * console.log(detectedLang); // 'zh-CN' | 'en-US'
 *
 * // 指定默认语言
 * const lang = detectUserLanguage('en-US');
 * ```
 */ export function detectUserLanguage() {
    var defaultLanguage = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : 'zh-CN';
    // 1. 检查 localStorage 中保存的用户选择
    var savedLanguage = getSavedLanguage();
    if (savedLanguage) {
        return savedLanguage;
    }
    // 2. 检查 Ant Design ConfigProvider 的 locale
    var antdLanguage = detectAntdLocale();
    if (antdLanguage) {
        return antdLanguage;
    }
    // 3. 检测浏览器语言
    var browserLanguage = detectBrowserLanguage();
    if (browserLanguage) {
        return browserLanguage;
    }
    // 4. 返回默认语言
    return defaultLanguage;
}
/**
 * 验证语言代码是否有效
 *
 * @param language - 要验证的语言代码
 * @returns 是否为有效的语言代码
 *
 * @example
 * ```ts
 * isValidLanguage('zh-CN'); // true
 * isValidLanguage('fr-FR'); // false
 * ```
 */ export function isValidLanguage(language) {
    return SUPPORTED_LANGUAGES.includes(language);
}
/**
 * 标准化语言代码
 *
 * @param language - 输入的语言代码
 * @param fallback - 当无法标准化时返回的默认值
 * @returns 标准化后的语言代码
 *
 * @example
 * ```ts
 * normalizeLanguage('zh'); // 'zh-CN'
 * normalizeLanguage('en'); // 'en-US'
 * normalizeLanguage('fr'); // 'zh-CN' (fallback)
 * ```
 */ export function normalizeLanguage(language) {
    var fallback = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 'zh-CN';
    var langLower = language.toLowerCase();
    if (langLower.startsWith('zh')) {
        return 'zh-CN';
    }
    if (langLower.startsWith('en')) {
        return 'en-US';
    }
    return fallback;
}
/**
 * 获取语言信息
 *
 * @param language - 语言代码
 * @returns 语言信息对象
 *
 * @example
 * ```ts
 * const langInfo = getLanguageInfo('zh-CN');
 * console.log(langInfo); // { code: 'zh-CN', name: '简体中文', flag: '🇨🇳', ... }
 * ```
 */ export function getLanguageInfo(language) {
    return LANGUAGE_MAP[language];
}
/**
 * 获取所有支持的语言信息
 *
 * @returns 所有支持的语言信息数组
 *
 * @example
 * ```ts
 * const allLanguages = getAllLanguages();
 * console.log(allLanguages); // [{ code: 'zh-CN', name: '简体中文', ... }, ...]
 * ```
 */ export function getAllLanguages() {
    return SUPPORTED_LANGUAGES.map(function(lang) {
        return getLanguageInfo(lang);
    });
}
/**
 * 语言切换工具类
 */ export var LanguageManager = /*#__PURE__*/ function() {
    "use strict";
    function LanguageManager() {
        var key = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : 'md-editor-language', defaultLanguage = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 'zh-CN';
        _class_call_check(this, LanguageManager);
        _define_property(this, "key", void 0);
        _define_property(this, "currentLanguage", void 0);
        this.key = key;
        this.currentLanguage = detectUserLanguage(defaultLanguage);
    }
    _create_class(LanguageManager, [
        {
            /**
   * 获取当前语言
   */ key: "getCurrentLanguage",
            value: function getCurrentLanguage() {
                return this.currentLanguage;
            }
        },
        {
            /**
   * 设置语言
   */ key: "setLanguage",
            value: function setLanguage(language) {
                if (isValidLanguage(language)) {
                    this.currentLanguage = language;
                    saveLanguage(language, this.key);
                }
            }
        },
        {
            /**
   * 切换语言
   */ key: "toggleLanguage",
            value: function toggleLanguage() {
                var newLanguage = this.currentLanguage === 'zh-CN' ? 'en-US' : 'zh-CN';
                this.setLanguage(newLanguage);
                return newLanguage;
            }
        },
        {
            /**
   * 检测并更新语言
   */ key: "detectAndUpdate",
            value: function detectAndUpdate() {
                var detectedLanguage = detectUserLanguage(this.currentLanguage);
                if (detectedLanguage !== this.currentLanguage) {
                    this.setLanguage(detectedLanguage);
                }
                return this.currentLanguage;
            }
        }
    ]);
    return LanguageManager;
}();
/**
 * 创建语言管理器实例
 *
 * @param key - localStorage 键名
 * @param defaultLanguage - 默认语言
 * @returns 语言管理器实例
 *
 * @example
 * ```ts
 * const langManager = createLanguageManager();
 * langManager.setLanguage('en-US');
 * const current = langManager.getCurrentLanguage();
 * ```
 */ export function createLanguageManager() {
    var key = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : 'md-editor-language', defaultLanguage = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 'zh-CN';
    return new LanguageManager(key, defaultLanguage);
}
