import { useLayoutStore } from './store/layoutStore';
import { $themeConfig } from './theme.config';

/** Alinea códigos BCP-47 (es-MX, es_ES) con los de `countryList` (es, en, …). */
function normalizeLocaleCode(code) {
    if (code == null || code === '') return null;
    let c = String(code).trim().toLowerCase();
    if (c.includes('-')) c = c.split('-')[0];
    if (c.includes('_')) c = c.split('_')[0];
    return c || null;
}

export default {
    init() {
        const store = useLayoutStore();

        const enResetKey = 'i18n_reset_accidental_en_v1';
        if (localStorage.getItem('i18n_locale') === 'en' && !localStorage.getItem(enResetKey)) {
            localStorage.removeItem('i18n_locale');
            localStorage.setItem(enResetKey, '1');
        }

        let val = localStorage.getItem('dark_mode');
        if (!val) {
            val = $themeConfig.theme;
        }
        store.toggleDarkMode(val);

        const ranchMenuResetKey = 'ranch_menu_vertical_v1';
        if (!localStorage.getItem(ranchMenuResetKey)) {
            localStorage.setItem('menu_style', 'vertical');
            localStorage.setItem(ranchMenuResetKey, '1');
        }

        val = localStorage.getItem('menu_style');
        if (!val) {
            val = $themeConfig.navigation;
        }
        store.toggleMenuStyle(val);

        val = localStorage.getItem('layout_style');
        if (!val) {
            val = $themeConfig.layout;
        }
        store.toggleLayoutStyle(val);

        store.initThemeColor();

        const savedLocale = localStorage.getItem('i18n_locale');
        if (!savedLocale) {
            const defaultCode = $themeConfig.lang;
            const item = store.countryList.find((i) => i.code === defaultCode);
            if (item) {
                this.toggleLanguage(item);
            }
        } else {
            this.toggleLanguage();
        }
    },

    toggleLanguage(item) {
        const store = useLayoutStore();
        let lang = null;

        if (item) {
            lang = item;
        } else {
            const raw = localStorage.getItem('i18n_locale');
            let code = normalizeLocaleCode(raw);
            if (!code) {
                code = normalizeLocaleCode(store.locale);
            }
            if (code && raw !== code) {
                localStorage.setItem('i18n_locale', code);
            }

            const found = code ? store.countryList.find((d) => d.code === code) : null;
            if (found) {
                lang = found;
            }
        }

        if (!lang) {
            lang =
                store.countryList.find((d) => d.code === $themeConfig.lang) ||
                store.countryList.find((d) => d.code === 'es');
        }

        store.toggleLocale(lang.code);
        return lang;
    },

    toggleMode(mode) {
        const store = useLayoutStore();
        if (!mode) {
            let val = localStorage.getItem('dark_mode');
            mode = val || 'light';
        }
        store.toggleDarkMode(mode || 'light');
        return mode;
    },
};
