import { r as registerInstance, h, c as getElement } from './core-1db051ba.js';
import { d as datetimeFormatOptions, T as Tunnel } from './i18n.provider-15e382c4.js';
import { S as STANDARD_LOCALES, T as TRANSLATIONS } from './translations-c4959f9b.js';

const Locale = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        /**
         * The local code to use globally
         */
        this.locale = 'en-US';
    }
    getLanguage() {
        const langEl = this.element.getAttribute('lang') ? this.element : this.element.closest('[lang]');
        return langEl ? langEl.getAttribute('lang') : this.locale.split('-')[0];
    }
    getDateFormat() {
        if (!this.locale)
            return 'dd/MM/yyyy';
        let format = STANDARD_LOCALES[this.locale];
        // Covers case of partial locale code provided
        if (!format) {
            const localeKey = Object.keys(STANDARD_LOCALES).find(key => key.includes(this.locale));
            format = (localeKey) ? STANDARD_LOCALES[localeKey] : 'dd/MM/yyyy';
        }
        return format;
    }
    getDictionary() {
        return this.dictionary ? this.dictionary : TRANSLATIONS[this.getLanguage()] || TRANSLATIONS['en'];
    }
    translateFactory(dictionary) {
        return (value, variables = {}) => {
            // We have no matching translation string
            if (!dictionary[value]) {
                return value;
            }
            let transformedString = dictionary[value];
            /* Variable Handling Usage:
                this.translator('Sort column {column} ascending', {column: 'Street'})
            */
            // Interpolate provided variables into the translated string
            Object.keys(variables).forEach(key => {
                const matchedVariables = new RegExp(`{${key}}`, 'g');
                transformedString = transformedString
                    .replace(matchedVariables, variables[key]);
            });
            // Scrub undefined variables from the translated string
            const unMatchedVariables = new RegExp(`\\{([^}]+)\\}`, 'g');
            const excessWhiteSpace = new RegExp(`\\s{2,}`, 'g');
            transformedString = transformedString
                .replace(unMatchedVariables, '')
                .replace(excessWhiteSpace, ' ')
                .trim();
            return transformedString;
        };
    }
    componentWillLoad() {
        this.render();
    }
    render() {
        const lang = this.getLanguage();
        const intlState = {
            dictionary: this.getDictionary(),
            dateFormat: this.getDateFormat(),
            dateFormatter: Intl.DateTimeFormat(this.locale).format,
            datetimeFormatter: Intl.DateTimeFormat(this.locale, datetimeFormatOptions).format,
            language: lang,
            locale: this.locale,
            numberFormatter: Intl.NumberFormat(this.locale).format,
            timezoneOverride: this.timezone,
            translator: this.translateFactory(this.getDictionary())
        };
        return (h(Tunnel.Provider, { state: intlState }, h("slot", null)));
    }
    get element() { return getElement(this); }
};

export { Locale as anj_locale };
