'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const core = require('./core-859ae5a6.js');
const i18n_provider = require('./i18n.provider-ca81b0a0.js');
const translations = require('./translations-6b7628ed.js');

const Locale = class {
    constructor(hostRef) {
        core.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 = translations.STANDARD_LOCALES[this.locale];
        // Covers case of partial locale code provided
        if (!format) {
            const localeKey = Object.keys(translations.STANDARD_LOCALES).find(key => key.includes(this.locale));
            format = (localeKey) ? translations.STANDARD_LOCALES[localeKey] : 'dd/MM/yyyy';
        }
        return format;
    }
    getDictionary() {
        return this.dictionary ? this.dictionary : translations.TRANSLATIONS[this.getLanguage()] || translations.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, i18n_provider.datetimeFormatOptions).format,
            language: lang,
            locale: this.locale,
            numberFormatter: Intl.NumberFormat(this.locale).format,
            timezoneOverride: this.timezone,
            translator: this.translateFactory(this.getDictionary())
        };
        return (core.h(i18n_provider.Tunnel.Provider, { state: intlState }, core.h("slot", null)));
    }
    get element() { return core.getElement(this); }
};

exports.anj_locale = Locale;
