import i18n from 'i18next';
import * as _ from 'lodash-es';
import React from 'react';
// eslint-disable-next-line no-restricted-imports
import { useTranslation } from 'react-i18next';
import { A_E } from './A_E';
import { BA } from './BA';
import { CA } from './CA';
import { EA } from './EA';
import { FA } from './FA';
import { fi_FI } from './fi-FI';
import { GC } from './GC';
import { I } from './I';
import { M } from './M';
import { O_E } from './O_E';
import { PA } from './PA';
import { SA } from './SA';
import { sv_FI } from './sv-FI';
import { TC } from './TC';
import { VA } from './VA';
import { Z } from './Z';
const examSpecificTranslations = {
    A_E,
    BA,
    BB: BA,
    CA,
    CB: CA,
    EA,
    EC: EA,
    FA,
    FC: FA,
    GC,
    I,
    M,
    N: M,
    O_E,
    SA,
    SC: SA,
    PA,
    PC: PA,
    TC,
    VA,
    VC: VA,
    Z
};
const resources = {
    'fi-FI': {
        translation: fi_FI,
        ...examSpecificTranslations
    },
    'sv-FI': {
        translation: sv_FI,
        ...examSpecificTranslations
    }
};
export function initI18n(language, examCode, dayCode) {
    var _a, _b, _c;
    const namespace = examCode ? examCode + (dayCode ? `_${dayCode}` : '') : 'translation';
    const i18nInstance = i18n
        .use({
        type: 'postProcessor',
        name: 'lowercase',
        process: (value, _key, options) => value.toLocaleLowerCase(options.lng)
    })
        .createInstance({
        compatibilityJSON: 'v4',
        resources,
        lng: language,
        fallbackLng: 'fi-FI',
        debug: false,
        defaultNS: namespace,
        fallbackNS: 'translation',
        interpolation: { escapeValue: false }
    }, _.noop);
    (_a = i18nInstance.services.formatter) === null || _a === void 0 ? void 0 : _a.add('range', (value) => {
        const [start, end] = value;
        return start == null ? end : end == null ? start : start === end ? start : `${start}–${end}`;
    });
    (_b = i18nInstance.services.formatter) === null || _b === void 0 ? void 0 : _b.add('first', (value) => _.first(value));
    (_c = i18nInstance.services.formatter) === null || _c === void 0 ? void 0 : _c.add('last', (value) => _.last(value));
    return i18nInstance;
}
export function useExamTranslation() {
    const { i18n, t: originalT } = useTranslation();
    const t = (key, options) => translate(i18n, key, options);
    t.raw = originalT;
    return { i18n, t };
}
/**
 * Custom wrapper for i18n.t that wraps the translated text in a span element
 * containing the correct lang attribute, if necessary.
 *
 * If you want to disable the wrapping, you can use `t.raw(…)`.
 */
function translate(instance, key, options) {
    const translation = instance.t(key, Array.isArray(key) ? key[0] : key, options);
    const namespace = instance.options.defaultNS.toString();
    const examHasCustomTranslations = namespace !== 'translation' && namespace in examSpecificTranslations;
    const hasCustomTranslationForKey = examHasCustomTranslations && hasTranslation(instance, namespace, key);
    return hasCustomTranslationForKey || !examHasCustomTranslations ? (translation) : (React.createElement("span", { lang: instance.language }, translation));
}
/**
 * A hacky way to check if we have a translation in the specified namespace.
 * Ideally, we'd probably want to use `i18n.exists()` for this purpose, but I
 * couldn't find a way to disable the fallback mechanism when using it.
 */
function hasTranslation(instance, namespace, key) {
    return Array.isArray(key)
        ? key.some(k => instance.getResource(instance.language, namespace, k) != null)
        : instance.getResource(instance.language, namespace, key) != null;
}
export const changeLanguage = (_i18n, language) => () => {
    if (_i18n.language !== language) {
        _i18n
            .changeLanguage(language)
            .then(_.noop)
            .catch(e => console.error('Changing language failed', e));
    }
};
export default i18n;
//# sourceMappingURL=index.js.map