/*!
 * Built by ShiftSoftware
 * Copyright (c)
 */
'use strict';

require('./index-CmK48vN3.js');
var types = require('./types-DV_cOCRi.js');

const version = "0.2.4";

// This file is auto-generated. Do not edit it manually.
// Use command: yarn create:locale-mapper
// It is generated by the script located at: generate-locale-mapper.mjs
const localeMapper = {
    "-": [
        "locales/"
    ],
    "errors": [
        "locales/errors/"
    ],
    "forms": [
        "locales/forms/",
        "locales/forms/contactUs/",
        "locales/forms/demo/",
        "locales/forms/testDriveDemo/",
        "locales/forms/vehicleQuotation/"
    ],
    "forms.contactUs": [
        "locales/forms/contactUs/"
    ],
    "forms.demo": [
        "locales/forms/demo/"
    ],
    "forms.testDriveDemo": [
        "locales/forms/testDriveDemo/"
    ],
    "forms.vehicleQuotation": [
        "locales/forms/vehicleQuotation/"
    ],
    "general": [
        "locales/general/"
    ],
    "generalTicketTypes": [
        "locales/generalTicketTypes/"
    ],
    "partLookup.deadStock": [
        "locales/partLookup/deadStock/"
    ],
    "partLookup": [
        "locales/partLookup/deadStock/",
        "locales/partLookup/distributor/",
        "locales/partLookup/manufacturer/"
    ],
    "partLookup.distributor": [
        "locales/partLookup/distributor/"
    ],
    "partLookup.manufacturer": [
        "locales/partLookup/manufacturer/"
    ],
    "vehicleLookup.accessories": [
        "locales/vehicleLookup/accessories/"
    ],
    "vehicleLookup": [
        "locales/vehicleLookup/accessories/",
        "locales/vehicleLookup/claimableItems/",
        "locales/vehicleLookup/paintThickness/",
        "locales/vehicleLookup/saleInformation/",
        "locales/vehicleLookup/serviceHistory/",
        "locales/vehicleLookup/specification/",
        "locales/vehicleLookup/warranty/"
    ],
    "vehicleLookup.claimableItems": [
        "locales/vehicleLookup/claimableItems/"
    ],
    "vehicleLookup.paintThickness": [
        "locales/vehicleLookup/paintThickness/"
    ],
    "vehicleLookup.serviceHistory": [
        "locales/vehicleLookup/serviceHistory/"
    ],
    "vehicleLookup.saleInformation": [
        "locales/vehicleLookup/saleInformation/"
    ],
    "vehicleLookup.specification": [
        "locales/vehicleLookup/specification/"
    ],
    "vehicleLookup.warranty": [
        "locales/vehicleLookup/warranty/"
    ],
    "forms*": [
        "locales/forms/"
    ]
};

const cachedLocales = {};
async function getSharedLocal(languageKey) {
    const [errors, globalKeys] = await Promise.all([getLocaleLanguage(languageKey, 'errors', types.errorsSchema), getLocaleLanguage(languageKey, '-', types.globalSchema)]);
    return Object.assign({ errors }, globalKeys);
}
async function getSharedFormLocal(languageKey) {
    const [errors, globalKeys, sharedFormKeys] = await Promise.all([
        getLocaleLanguage(languageKey, 'errors', types.errorsSchema),
        getLocaleLanguage(languageKey, '-', types.globalSchema),
        getLocaleLanguage(languageKey, 'forms*', types.formsSchema),
    ]);
    return Object.assign(Object.assign(Object.assign({}, globalKeys), sharedFormKeys), errors);
}
async function getLocaleLanguage(languageKey, component, schema) {
    const languageFile = types.languageMapper[languageKey] || types.languageMapper.en;
    const localeFiles = localeMapper[component];
    if (!localeFiles || !localeFiles.length)
        throw new Error(`Locale file not found for component: ${component}`);
    if (localeFiles.length === 1) {
        const localeFile = localeFiles[0] + languageFile;
        const response = await requestLocaleFile(localeFile);
        try {
            schema.validateSync(response, { strict: true, abortEarly: false });
        }
        catch (error) {
            console.error(`Failed to parse locale file: ${localeFile}`);
            console.error(error);
        }
        return response;
    }
    else {
        const localeFilePromises = localeFiles.map(localeFile => requestLocaleFile(localeFile + languageFile));
        const localeFilesResponses = await Promise.all(localeFilePromises);
        const responseBluePrint = schema.getDefault();
        const parsedResponseMapper = {};
        recursiveParser(component, responseBluePrint, parsedResponseMapper, languageFile);
        try {
            schema.validateSync(parsedResponseMapper, { strict: true, abortEarly: false });
        }
        catch (error) {
            console.error(`Failed to parse locale file component: ${component}`);
            console.error(error);
        }
        return localeFilesResponses;
    }
}
function isPlainObject(value) {
    return typeof value === 'object' && value !== null && !Array.isArray(value);
}
function recursiveParser(parents, bluePrint, parsedResponseMapper, languageFile) {
    let mappedSelf = false;
    Object.entries(bluePrint).forEach(([key, value]) => {
        if (typeof value === 'string' && !mappedSelf) {
            if (localeMapper[parents]) {
                const cacheKey = localeMapper[parents][0] + languageFile;
                if (cachedLocales[cacheKey])
                    Object.assign(parsedResponseMapper, cachedLocales[cacheKey]);
            }
            mappedSelf = true;
        }
        else if (isPlainObject(value)) {
            const childMapperKey = parents + '.' + key;
            const isNestedObject = Object.entries(value).some(([_, nestedValue]) => isPlainObject(nestedValue));
            if (isNestedObject) {
                parsedResponseMapper[key] = {};
                recursiveParser(childMapperKey, value, parsedResponseMapper[key], languageFile);
            }
            else if (localeMapper[childMapperKey] && !!localeMapper[childMapperKey].length) {
                const cacheKey = localeMapper[childMapperKey][0] + languageFile;
                if (cachedLocales[cacheKey])
                    parsedResponseMapper[key] = cachedLocales[cacheKey];
            }
        }
    });
}
async function requestLocaleFile(localeFile) {
    if (cachedLocales[localeFile])
        return await cachedLocales[localeFile];
    try {
        const fetchPromise = (fetch(`https://cdn.jsdelivr.net/npm/adp-web-components@${version}/dist/${localeFile}`)).then(res => {
            if (!res.ok)
                delete cachedLocales[localeFile];
            return res.json();
        });
        cachedLocales[localeFile] = fetchPromise;
        const result = await fetchPromise;
        cachedLocales[localeFile] = result;
        return result;
    }
    catch (error) {
        delete cachedLocales[localeFile];
        return {};
    }
}

exports.getLocaleLanguage = getLocaleLanguage;
exports.getSharedFormLocal = getSharedFormLocal;
exports.getSharedLocal = getSharedLocal;
exports.version = version;
