"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTimeByZone = exports.getAllTimeZone = exports.getLocaleTimeZone = void 0;
const lodash_1 = __importDefault(require("lodash"));
const localeTimeZone = require('../config/locale-time-zone.json');
const TIME_ZONE_START_POS = 19;
function getTimeZoneList() {
    const list = [];
    for (const locale in localeTimeZone) {
        if (localeTimeZone[locale]) {
            localeTimeZone[locale].forEach((element) => {
                const idx = list.findIndex((item) => item.key === element.key);
                if (idx === -1) {
                    list.push(element);
                }
            });
        }
    }
    return list;
}
exports.default = getTimeZoneList;
function getLocaleTimeZone() {
    const list = [];
    const cloned = lodash_1.default.cloneDeep(localeTimeZone);
    for (const curLocale in cloned) {
        if (cloned[curLocale]) {
            cloned[curLocale].forEach((element) => {
                const idx = list.findIndex((item) => item.key === element.key);
                if (idx === -1) {
                    const idxByValue = list.findIndex((item) => item.value === element.value);
                    if (idxByValue > -1) {
                        Object.assign(list[idxByValue], { country: `${list[idxByValue].country}/${element.country}` });
                    }
                    else {
                        list.push(element);
                    }
                }
            });
        }
    }
    return list;
}
exports.getLocaleTimeZone = getLocaleTimeZone;
function getAllTimeZone(locale) {
    if (locale) {
        const cloned = lodash_1.default.cloneDeep(localeTimeZone);
        const curLocaleTimeZones = cloned[locale] || [];
        const list = getLocaleTimeZone();
        curLocaleTimeZones.forEach((item) => {
            Object.assign(item, { isCurLocale: true });
        });
        list.forEach((item) => {
            const idx = curLocaleTimeZones.findIndex((it) => it.value === item.value);
            if (idx === -1) {
                curLocaleTimeZones.push(item);
            }
        });
        return curLocaleTimeZones;
    }
    return getLocaleTimeZone();
}
exports.getAllTimeZone = getAllTimeZone;
function getTimeByZone(timezone, time) {
    if (!time) {
        return '';
    }
    let startTimeStr = time;
    startTimeStr =
        startTimeStr.substr(0, TIME_ZONE_START_POS) +
            timezone +
            startTimeStr.substr(TIME_ZONE_START_POS + 3, startTimeStr.length);
    return startTimeStr;
}
exports.getTimeByZone = getTimeByZone;
