/**
 * DevExtreme (cjs/__internal/scheduler/timezones/m_utils_timezones_data.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.default = void 0;
var _errors = _interopRequireDefault(require("../../../core/errors"));
var _math = require("../../../core/utils/math");
var _query = _interopRequireDefault(require("../../../data/query"));
var _timezones_data = _interopRequireDefault(require("./timezones_data"));

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}
const getConvertedUntils = value => value.split("|").map(until => {
    if ("Infinity" === until) {
        return null
    }
    return 1e3 * parseInt(until, 36)
});
const parseTimezone = timeZoneConfig => {
    const {
        offsets: offsets
    } = timeZoneConfig;
    const {
        offsetIndices: offsetIndices
    } = timeZoneConfig;
    const {
        untils: untils
    } = timeZoneConfig;
    const offsetList = offsets.split("|").map(value => parseInt(value));
    const offsetIndexList = offsetIndices.split("").map(value => parseInt(value));
    const dateList = (value = untils, value.split("|").map(until => {
        if ("Infinity" === until) {
            return null
        }
        return 1e3 * parseInt(until, 36)
    })).map((accumulator = 0, value => accumulator += value));
    var accumulator;
    var value;
    return {
        offsetList: offsetList,
        offsetIndexList: offsetIndexList,
        dateList: dateList
    }
};
let TimeZoneCache = function() {
    function TimeZoneCache() {
        this.map = new Map
    }
    var _proto = TimeZoneCache.prototype;
    _proto.tryGet = function(id) {
        if (!this.map.get(id)) {
            const config = timeZoneDataUtils.getTimezoneById(id);
            if (!config) {
                return false
            }
            const timeZoneInfo = parseTimezone(config);
            this.map.set(id, timeZoneInfo)
        }
        return this.map.get(id)
    };
    return TimeZoneCache
}();
const tzCache = new TimeZoneCache;
const timeZoneDataUtils = {
    _tzCache: tzCache,
    _timeZones: _timezones_data.default.zones,
    getDisplayedTimeZones(timestamp) {
        const timeZones = this._timeZones.map(timezone => {
            const timeZoneInfo = parseTimezone(timezone);
            const offset = this.getUtcOffset(timeZoneInfo, timestamp);
            const title = "(GMT ".concat(this.formatOffset(offset), ") ").concat(this.formatId(timezone.id));
            return {
                offset: offset,
                title: title,
                id: timezone.id
            }
        });
        return (0, _query.default)(timeZones).sortBy("offset").toArray()
    },
    formatOffset(offset) {
        const hours = Math.floor(offset);
        const minutesInDecimal = offset - hours;
        const signString = (0, _math.sign)(offset) >= 0 ? "+" : "-";
        const hoursString = "0".concat(Math.abs(hours)).slice(-2);
        const minutesString = minutesInDecimal > 0 ? ":".concat(60 * minutesInDecimal) : ":00";
        return signString + hoursString + minutesString
    },
    formatId: id => id.split("/").join(" - ").split("_").join(" "),
    getTimezoneById(id) {
        if (!id) {
            return
        }
        const tzList = this._timeZones;
        for (let i = 0; i < tzList.length; i++) {
            const currentId = tzList[i].id;
            if (currentId === id) {
                return tzList[i]
            }
        }
        _errors.default.log("W0009", id);
        return
    },
    getTimeZoneOffsetById(id, timestamp) {
        const timeZoneInfo = tzCache.tryGet(id);
        return timeZoneInfo ? this.getUtcOffset(timeZoneInfo, timestamp) : void 0
    },
    getTimeZoneDeclarationTuple(id, year) {
        const timeZoneInfo = tzCache.tryGet(id);
        return timeZoneInfo ? this.getTimeZoneDeclarationTupleCore(timeZoneInfo, year) : []
    },
    getTimeZoneDeclarationTupleCore(timeZoneInfo, year) {
        const {
            offsetList: offsetList
        } = timeZoneInfo;
        const {
            offsetIndexList: offsetIndexList
        } = timeZoneInfo;
        const {
            dateList: dateList
        } = timeZoneInfo;
        const tupleResult = [];
        for (let i = 0; i < dateList.length; i++) {
            const currentDate = dateList[i];
            const currentYear = new Date(currentDate).getFullYear();
            if (currentYear === year) {
                const offset = offsetList[offsetIndexList[i + 1]];
                tupleResult.push({
                    date: currentDate,
                    offset: -offset / 60
                })
            }
            if (currentYear > year) {
                break
            }
        }
        return tupleResult
    },
    getUtcOffset(timeZoneInfo, dateTimeStamp) {
        const {
            offsetList: offsetList
        } = timeZoneInfo;
        const {
            offsetIndexList: offsetIndexList
        } = timeZoneInfo;
        const {
            dateList: dateList
        } = timeZoneInfo;
        const lastIntervalStartIndex = dateList.length - 1 - 1;
        let index = lastIntervalStartIndex;
        while (index >= 0 && dateTimeStamp < dateList[index]) {
            index--
        }
        const offset = offsetList[offsetIndexList[index + 1]];
        return -offset / 60 || offset
    }
};
var _default = timeZoneDataUtils;
exports.default = _default;
