import localizationService from './localizationService/defaultLocalizationConfig.js';
import { getQueryStringParameterByName } from 'talon/utils';

const DEFAULT_FORM_FACTOR = 'Large';

/**
 * This pattern is used to configure the framework (base path, etc...)
 * from the HTML file, the provider implementation being typically
 * generated from the template metadata.
 */
let configProvider;

function register(providerImpl) {
    if (!configProvider) {
        configProvider = providerImpl;
    } else {
        throw new Error('ConfigProvider can only be set once at initilization time');
    }
}

function getBasePath() {
    return (configProvider && configProvider.getBasePath()) || '';
}

function getMode() {
    return configProvider && configProvider.getMode();
}

function getLwcFallback() {
    const lwcFallback = getQueryStringParameterByName('talon.lwc.fallback');
    return lwcFallback && lwcFallback.toLowerCase().trim() === 'false' ? false : true;
}

function getLocale() {
    const langLocale = (configProvider && configProvider.getLocale()) || '';
    const [language, country = ''] = langLocale.split('_');
    const lang = langLocale.replace('_', '-');

    return {
        userLocaleLang: 'en',
        userLocaleCountry: 'US',
        language,
        country,
        variant: '',
        langLocale,
        nameOfMonths: [
            {
                fullName: 'January',
                shortName: 'Jan',
            },
            {
                fullName: 'February',
                shortName: 'Feb',
            },
            {
                fullName: 'March',
                shortName: 'Mar',
            },
            {
                fullName: 'April',
                shortName: 'Apr',
            },
            {
                fullName: 'May',
                shortName: 'May',
            },
            {
                fullName: 'June',
                shortName: 'Jun',
            },
            {
                fullName: 'July',
                shortName: 'Jul',
            },
            {
                fullName: 'August',
                shortName: 'Aug',
            },
            {
                fullName: 'September',
                shortName: 'Sep',
            },
            {
                fullName: 'October',
                shortName: 'Oct',
            },
            {
                fullName: 'November',
                shortName: 'Nov',
            },
            {
                fullName: 'December',
                shortName: 'Dec',
            },
            {
                fullName: '',
                shortName: '',
            },
        ],
        nameOfWeekdays: [
            {
                fullName: 'Sunday',
                shortName: 'SUN',
            },
            {
                fullName: 'Monday',
                shortName: 'MON',
            },
            {
                fullName: 'Tuesday',
                shortName: 'TUE',
            },
            {
                fullName: 'Wednesday',
                shortName: 'WED',
            },
            {
                fullName: 'Thursday',
                shortName: 'THU',
            },
            {
                fullName: 'Friday',
                shortName: 'FRI',
            },
            {
                fullName: 'Saturday',
                shortName: 'SAT',
            },
        ],
        labelForToday: 'Today',
        firstDayOfWeek: 1,
        timezone: 'America/Los_Angeles',
        dateFormat: 'MMM d, yyyy',
        shortDateFormat: 'M/d/yyyy',
        longDateFormat: 'MMMM d, yyyy',
        datetimeFormat: 'MMM d, yyyy, h:mm:ss a',
        shortDatetimeFormat: 'M/d/yyyy, h:mm a',
        timeFormat: 'h:mm:ss a',
        shortTimeFormat: 'h:mm a',
        numberFormat: '#,##0.###',
        decimal: '.',
        grouping: ',',
        zero: '0',
        percentFormat: '#,##0%',
        currencyFormat: '¤#,##0.00',
        currencyCode: 'USD',
        currency: '$',
        dir: 'ltr',
        lang,
        isEasternNameStyle: false,
    };
}

function getLocalizationService() {
    return localizationService;
}

function getPathPrefix() {
    return getBasePath();
}

function getToken() {
    return null;
}

function getUser() {
    return configProvider && configProvider.getUser();
}

function getFormFactor() {
    // TODO issue #216
    return (
        (configProvider && configProvider.getFormFactor && configProvider.getFormFactor()) ||
        DEFAULT_FORM_FACTOR
    );
}

function getIconSvgTemplates() {
    return configProvider && configProvider.iconSvgTemplates;
}

function getOneConfig() {
    return {
        densitySetting: '',
    };
}
/**
 * TODO: remove this when we implement the Site scoped module resolver
 */
function getAppId() {
    return configProvider && configProvider.getAppId();
}

/**
 * TODO: implement sanitizeDOM
 */
function sanitizeDOM(dirty, config) {
    return (configProvider && configProvider.sanitizeDOM(dirty, config)) || dirty;
}

/**
 * TODO: implement coreInfo
 */
function getCoreInfo() {
    return (
        (configProvider && configProvider.getCoreInfo()) || {
            untrustedContentDomain: '',
            localhostPort: '',
            securePort: '',
        }
    );
}

export {
    getCoreInfo,
    sanitizeDOM,
    getAppId,
    getOneConfig,
    getIconSvgTemplates,
    getFormFactor,
    getUser,
    getLocale,
    getLwcFallback,
    getToken,
    getPathPrefix,
    getLocalizationService,
    register,
    register as default,
    getBasePath,
    getMode,
};
