import { getDefaultConfig } from './defaultConfig';

// Closure to hold the APIs if and when available
let configProvided = false;
const { assign, freeze } = Object;
let PROVIDED_IMPL = getDefaultConfig();

function resolveServiceApiProps(serviceAPI = {}) {
    const serviceApiMap = {
        getCoreInfo:
            serviceAPI.getInitializer &&
            serviceAPI.getInitializer('coreInfoConfig'),
        getPathPrefix: serviceAPI.getPathPrefix,
        getToken: serviceAPI.getToken,
        getLocale: serviceAPI.getLocale,
        getLocalizationService: serviceAPI.getLocalizationService,
        iconSvgTemplates: serviceAPI.iconSvgTemplates,
        getOneConfig:
            serviceAPI.getInitializer && serviceAPI.getInitializer('oneConfig'),
    };
    return Object.keys(serviceApiMap).reduce((seed, prop) => {
        if (serviceApiMap[prop] !== undefined) {
            seed[prop] = serviceApiMap[prop];
        }
        return seed;
    }, {});
}

export default function configProviderService(serviceAPI) {
    if (!configProvided) {
        PROVIDED_IMPL = freeze(
            assign({}, PROVIDED_IMPL, resolveServiceApiProps(serviceAPI))
        );
        configProvided = true;
    } else {
        throw new Error(
            'ConfigProvider can only be set once at initilization time'
        );
    }

    return { name: 'lightning-config-provider' };
}

export function getPathPrefix() {
    return (
        (PROVIDED_IMPL &&
            PROVIDED_IMPL.getPathPrefix &&
            PROVIDED_IMPL.getPathPrefix()) ||
        ''
    );
}

export function getToken(name) {
    return (
        PROVIDED_IMPL && PROVIDED_IMPL.getToken && PROVIDED_IMPL.getToken(name)
    );
}

// There is currently no other way to get the grouping and decimal separators.
// This function should be removed once we have a way to get these values
export function getLocale() {
    const locale =
        PROVIDED_IMPL && PROVIDED_IMPL.getLocale && PROVIDED_IMPL.getLocale();
    if (locale) {
        return {
            decimal: locale.decimal,
            grouping: locale.grouping,
        };
    }
    return {
        decimal: '.',
        grouping: ',',
    };
}

export function getLocalizationService() {
    return (
        PROVIDED_IMPL &&
        PROVIDED_IMPL.getLocalizationService &&
        PROVIDED_IMPL.getLocalizationService()
    );
}

export function getCoreInfo() {
    return (
        (PROVIDED_IMPL && PROVIDED_IMPL.getCoreInfo) || {
            untrustedContentDomain: '.a.forceusercontent.com',
            localhostPort: '',
            securePort: '',
            internalAppVersion: 218, // earliest version when map proxying was implemented
        }
    );
}

export function getIconSvgTemplates() {
    return PROVIDED_IMPL && PROVIDED_IMPL.iconSvgTemplates;
}

export function getOneConfig() {
    return (
        (PROVIDED_IMPL && PROVIDED_IMPL.getOneConfig) || {
            densitySetting: '',
        }
    );
}
