/*
 * Copyright (C) Contributors to the Suwayomi project
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

import { DAYJS_LOCALES, DayJsLocale } from '@/lib/dayjs/Locales.ts';

export const getDayJsLocale = (locale: string): DayJsLocale => {
    if (DAYJS_LOCALES.includes(locale as DayJsLocale)) {
        return locale as DayJsLocale;
    }

    if (DAYJS_LOCALES.includes(locale.toLowerCase() as DayJsLocale)) {
        return locale.toLowerCase() as DayJsLocale;
    }

    const convertedLocale = locale.split('-')[0].toLowerCase();
    if (DAYJS_LOCALES.includes(convertedLocale as DayJsLocale)) {
        return convertedLocale as DayJsLocale;
    }

    return 'en';
};

export const importDayJsLocale = async (locale: string): Promise<DayJsLocale> => {
    const dayjsLocale = getDayJsLocale(locale);

    try {
        // eslint-disable-next-line @typescript-eslint/no-use-before-define
        await localesToImport[dayjsLocale]();
    } catch (e) {
        // ignore - dayjs falls back to "en" by default
    }

    return dayjsLocale;
};

/**
 * **IMPORTANT:**
 *
 * This is generated by the `dayjs:gen-locales-imports` script
 */
const localesToImport: Record<(typeof DAYJS_LOCALES)[number], () => Promise<unknown>> = {
    af: () => import('dayjs/locale/af.js'),
    am: () => import('dayjs/locale/am.js'),
    'ar-dz': () => import('dayjs/locale/ar-dz.js'),
    'ar-iq': () => import('dayjs/locale/ar-iq.js'),
    'ar-kw': () => import('dayjs/locale/ar-kw.js'),
    'ar-ly': () => import('dayjs/locale/ar-ly.js'),
    'ar-ma': () => import('dayjs/locale/ar-ma.js'),
    'ar-sa': () => import('dayjs/locale/ar-sa.js'),
    'ar-tn': () => import('dayjs/locale/ar-tn.js'),
    ar: () => import('dayjs/locale/ar.js'),
    az: () => import('dayjs/locale/az.js'),
    be: () => import('dayjs/locale/be.js'),
    bg: () => import('dayjs/locale/bg.js'),
    bi: () => import('dayjs/locale/bi.js'),
    bm: () => import('dayjs/locale/bm.js'),
    'bn-bd': () => import('dayjs/locale/bn-bd.js'),
    bn: () => import('dayjs/locale/bn.js'),
    bo: () => import('dayjs/locale/bo.js'),
    br: () => import('dayjs/locale/br.js'),
    bs: () => import('dayjs/locale/bs.js'),
    ca: () => import('dayjs/locale/ca.js'),
    cs: () => import('dayjs/locale/cs.js'),
    cv: () => import('dayjs/locale/cv.js'),
    cy: () => import('dayjs/locale/cy.js'),
    da: () => import('dayjs/locale/da.js'),
    'de-at': () => import('dayjs/locale/de-at.js'),
    'de-ch': () => import('dayjs/locale/de-ch.js'),
    de: () => import('dayjs/locale/de.js'),
    dv: () => import('dayjs/locale/dv.js'),
    el: () => import('dayjs/locale/el.js'),
    'en-au': () => import('dayjs/locale/en-au.js'),
    'en-ca': () => import('dayjs/locale/en-ca.js'),
    'en-gb': () => import('dayjs/locale/en-gb.js'),
    'en-ie': () => import('dayjs/locale/en-ie.js'),
    'en-il': () => import('dayjs/locale/en-il.js'),
    'en-in': () => import('dayjs/locale/en-in.js'),
    'en-nz': () => import('dayjs/locale/en-nz.js'),
    'en-sg': () => import('dayjs/locale/en-sg.js'),
    'en-tt': () => import('dayjs/locale/en-tt.js'),
    en: () => import('dayjs/locale/en.js'),
    eo: () => import('dayjs/locale/eo.js'),
    'es-do': () => import('dayjs/locale/es-do.js'),
    'es-mx': () => import('dayjs/locale/es-mx.js'),
    'es-pr': () => import('dayjs/locale/es-pr.js'),
    'es-us': () => import('dayjs/locale/es-us.js'),
    es: () => import('dayjs/locale/es.js'),
    et: () => import('dayjs/locale/et.js'),
    eu: () => import('dayjs/locale/eu.js'),
    fa: () => import('dayjs/locale/fa.js'),
    fi: () => import('dayjs/locale/fi.js'),
    fo: () => import('dayjs/locale/fo.js'),
    'fr-ca': () => import('dayjs/locale/fr-ca.js'),
    'fr-ch': () => import('dayjs/locale/fr-ch.js'),
    fr: () => import('dayjs/locale/fr.js'),
    fy: () => import('dayjs/locale/fy.js'),
    ga: () => import('dayjs/locale/ga.js'),
    gd: () => import('dayjs/locale/gd.js'),
    gl: () => import('dayjs/locale/gl.js'),
    'gom-latn': () => import('dayjs/locale/gom-latn.js'),
    gu: () => import('dayjs/locale/gu.js'),
    he: () => import('dayjs/locale/he.js'),
    hi: () => import('dayjs/locale/hi.js'),
    hr: () => import('dayjs/locale/hr.js'),
    ht: () => import('dayjs/locale/ht.js'),
    hu: () => import('dayjs/locale/hu.js'),
    'hy-am': () => import('dayjs/locale/hy-am.js'),
    id: () => import('dayjs/locale/id.js'),
    is: () => import('dayjs/locale/is.js'),
    'it-ch': () => import('dayjs/locale/it-ch.js'),
    it: () => import('dayjs/locale/it.js'),
    ja: () => import('dayjs/locale/ja.js'),
    jv: () => import('dayjs/locale/jv.js'),
    ka: () => import('dayjs/locale/ka.js'),
    kk: () => import('dayjs/locale/kk.js'),
    km: () => import('dayjs/locale/km.js'),
    kn: () => import('dayjs/locale/kn.js'),
    ko: () => import('dayjs/locale/ko.js'),
    ku: () => import('dayjs/locale/ku.js'),
    ky: () => import('dayjs/locale/ky.js'),
    lb: () => import('dayjs/locale/lb.js'),
    lo: () => import('dayjs/locale/lo.js'),
    lt: () => import('dayjs/locale/lt.js'),
    lv: () => import('dayjs/locale/lv.js'),
    me: () => import('dayjs/locale/me.js'),
    mi: () => import('dayjs/locale/mi.js'),
    mk: () => import('dayjs/locale/mk.js'),
    ml: () => import('dayjs/locale/ml.js'),
    mn: () => import('dayjs/locale/mn.js'),
    mr: () => import('dayjs/locale/mr.js'),
    'ms-my': () => import('dayjs/locale/ms-my.js'),
    ms: () => import('dayjs/locale/ms.js'),
    mt: () => import('dayjs/locale/mt.js'),
    my: () => import('dayjs/locale/my.js'),
    nb: () => import('dayjs/locale/nb.js'),
    ne: () => import('dayjs/locale/ne.js'),
    'nl-be': () => import('dayjs/locale/nl-be.js'),
    nl: () => import('dayjs/locale/nl.js'),
    nn: () => import('dayjs/locale/nn.js'),
    'oc-lnc': () => import('dayjs/locale/oc-lnc.js'),
    'pa-in': () => import('dayjs/locale/pa-in.js'),
    pl: () => import('dayjs/locale/pl.js'),
    'pt-br': () => import('dayjs/locale/pt-br.js'),
    pt: () => import('dayjs/locale/pt.js'),
    rn: () => import('dayjs/locale/rn.js'),
    ro: () => import('dayjs/locale/ro.js'),
    ru: () => import('dayjs/locale/ru.js'),
    rw: () => import('dayjs/locale/rw.js'),
    sd: () => import('dayjs/locale/sd.js'),
    se: () => import('dayjs/locale/se.js'),
    si: () => import('dayjs/locale/si.js'),
    sk: () => import('dayjs/locale/sk.js'),
    sl: () => import('dayjs/locale/sl.js'),
    sq: () => import('dayjs/locale/sq.js'),
    'sr-cyrl': () => import('dayjs/locale/sr-cyrl.js'),
    sr: () => import('dayjs/locale/sr.js'),
    ss: () => import('dayjs/locale/ss.js'),
    'sv-fi': () => import('dayjs/locale/sv-fi.js'),
    sv: () => import('dayjs/locale/sv.js'),
    sw: () => import('dayjs/locale/sw.js'),
    ta: () => import('dayjs/locale/ta.js'),
    te: () => import('dayjs/locale/te.js'),
    tet: () => import('dayjs/locale/tet.js'),
    tg: () => import('dayjs/locale/tg.js'),
    th: () => import('dayjs/locale/th.js'),
    tk: () => import('dayjs/locale/tk.js'),
    'tl-ph': () => import('dayjs/locale/tl-ph.js'),
    tlh: () => import('dayjs/locale/tlh.js'),
    tr: () => import('dayjs/locale/tr.js'),
    tzl: () => import('dayjs/locale/tzl.js'),
    'tzm-latn': () => import('dayjs/locale/tzm-latn.js'),
    tzm: () => import('dayjs/locale/tzm.js'),
    'ug-cn': () => import('dayjs/locale/ug-cn.js'),
    uk: () => import('dayjs/locale/uk.js'),
    ur: () => import('dayjs/locale/ur.js'),
    'uz-latn': () => import('dayjs/locale/uz-latn.js'),
    uz: () => import('dayjs/locale/uz.js'),
    vi: () => import('dayjs/locale/vi.js'),
    'x-pseudo': () => import('dayjs/locale/x-pseudo.js'),
    yo: () => import('dayjs/locale/yo.js'),
    'zh-cn': () => import('dayjs/locale/zh-cn.js'),
    'zh-hk': () => import('dayjs/locale/zh-hk.js'),
    'zh-tw': () => import('dayjs/locale/zh-tw.js'),
    zh: () => import('dayjs/locale/zh.js'),
};
