import { getLocale } from 'talon/configProvider';
import { assert } from 'talon/utils';

/**
 * Resolves '@salesforce/i18n' resources.
 */
export default {
    scope: 'i18n',

    resolve(resource) {
        const localeValues = getLocale();
        assert(localeValues, `Can't resolve @salesforce/i18n/${resource} because locale is falsy`);

        const parts = resource.split('.');
        switch (parts[0]) {
            case 'lang':
            case 'dir':
            case 'firstDayOfWeek':
                return localeValues[parts[0]];
            case 'locale':
                return getLanguageTag(localeValues.userLocaleLang, localeValues.userLocaleCountry);
            case 'timeZone':
                return localeValues.timezone;
            case 'currency':
                return localeValues.currencyCode;
            case 'dateTime':
                switch (parts[1]) {
                    case 'shortDateFormat':
                    case 'shortTimeFormat':
                    case 'longDateFormat':
                        return localeValues[parts[1]];
                    case 'shortDateTimeFormat':
                        return localeValues.shortDatetimeFormat;
                    case 'mediumDateFormat':
                        return localeValues.dateFormat;
                    case 'mediumDateTimeFormat':
                        return localeValues.datetimeFormat;
                    case 'mediumTimeFormat':
                        return localeValues.timeFormat;
                    default:
                        return undefined;
                }
            case 'number':
                switch (parts[1]) {
                    case 'numberFormat':
                    case 'percentFormat':
                    case 'currencyFormat':
                        return localeValues[parts[1]];
                    case 'currencySymbol':
                        return localeValues.currency;
                    default:
                        return undefined;
                }
            default:
                return undefined;
        }
    },
};

/**
 * Get a valid BCP 47 tag for the given language and country.
 *
 * @param {string} lang The given languge
 * @param {string} country The given country
 * @returns A valid BCP 47 language tag string, or undefined if the given language or country is invalid
 */
function getLanguageTag(lang, country) {
    if (!lang) {
        return undefined;
    }

    // All SFDC supported locales:
    // https://git.soma.salesforce.com/sfdc-commons/sfdc-i18n/blob/master/src/main/resources/sfdc/i18n/config/g11n.xml
    // Special cases which the locales cannot be recognized by browswers.
    switch (lang) {
        case 'no':
            lang = 'nb'; // Norwegian
            break;
        case 'sh':
            switch (country) {
                case 'BA':
                case 'ME':
                case 'CS':
                    return 'hr'; // Croatian
                default:
                // do nothing
            }
            break;
        case 'tl':
            return 'fil'; // Filipino
        default:
        // do nothing
    }

    const locale = country && country.length > 0 ? `${lang}-${country}` : lang;
    return Intl.DateTimeFormat.supportedLocalesOf(locale)[0];
}
