/**
 * DevExtreme (esm/__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/
 */
import errors from "../../../core/errors";
import {
    sign
} from "../../../core/utils/math";
import query from "../../../data/query";
import tzData from "./timezones_data";
var getConvertedUntils = value => value.split("|").map(until => {
    if ("Infinity" === until) {
        return null
    }
    return 1e3 * parseInt(until, 36)
});
var parseTimezone = timeZoneConfig => {
    var {
        offsets: offsets
    } = timeZoneConfig;
    var {
        offsetIndices: offsetIndices
    } = timeZoneConfig;
    var {
        untils: untils
    } = timeZoneConfig;
    var offsetList = offsets.split("|").map(value => parseInt(value));
    var offsetIndexList = offsetIndices.split("").map(value => parseInt(value));
    var dateList = getConvertedUntils(untils).map((accumulator = 0, value => accumulator += value));
    var accumulator;
    return {
        offsetList: offsetList,
        offsetIndexList: offsetIndexList,
        dateList: dateList
    }
};
class TimeZoneCache {
    constructor() {
        this.map = new Map
    }
    tryGet(id) {
        if (!this.map.get(id)) {
            var config = timeZoneDataUtils.getTimezoneById(id);
            if (!config) {
                return false
            }
            var timeZoneInfo = parseTimezone(config);
            this.map.set(id, timeZoneInfo)
        }
        return this.map.get(id)
    }
}
var tzCache = new TimeZoneCache;
var timeZoneDataUtils = {
    _tzCache: tzCache,
    _timeZones: tzData.zones,
    getDisplayedTimeZones(timestamp) {
        var timeZones = this._timeZones.map(timezone => {
            var timeZoneInfo = parseTimezone(timezone);
            var offset = this.getUtcOffset(timeZoneInfo, timestamp);
            var title = "(GMT ".concat(this.formatOffset(offset), ") ").concat(this.formatId(timezone.id));
            return {
                offset: offset,
                title: title,
                id: timezone.id
            }
        });
        return query(timeZones).sortBy("offset").toArray()
    },
    formatOffset(offset) {
        var hours = Math.floor(offset);
        var minutesInDecimal = offset - hours;
        var signString = sign(offset) >= 0 ? "+" : "-";
        var hoursString = "0".concat(Math.abs(hours)).slice(-2);
        var minutesString = minutesInDecimal > 0 ? ":".concat(60 * minutesInDecimal) : ":00";
        return signString + hoursString + minutesString
    },
    formatId: id => id.split("/").join(" - ").split("_").join(" "),
    getTimezoneById(id) {
        if (!id) {
            return
        }
        var tzList = this._timeZones;
        for (var i = 0; i < tzList.length; i++) {
            var currentId = tzList[i].id;
            if (currentId === id) {
                return tzList[i]
            }
        }
        errors.log("W0009", id);
        return
    },
    getTimeZoneOffsetById(id, timestamp) {
        var timeZoneInfo = tzCache.tryGet(id);
        return timeZoneInfo ? this.getUtcOffset(timeZoneInfo, timestamp) : void 0
    },
    getTimeZoneDeclarationTuple(id, year) {
        var timeZoneInfo = tzCache.tryGet(id);
        return timeZoneInfo ? this.getTimeZoneDeclarationTupleCore(timeZoneInfo, year) : []
    },
    getTimeZoneDeclarationTupleCore(timeZoneInfo, year) {
        var {
            offsetList: offsetList
        } = timeZoneInfo;
        var {
            offsetIndexList: offsetIndexList
        } = timeZoneInfo;
        var {
            dateList: dateList
        } = timeZoneInfo;
        var tupleResult = [];
        for (var i = 0; i < dateList.length; i++) {
            var currentDate = dateList[i];
            var currentYear = new Date(currentDate).getFullYear();
            if (currentYear === year) {
                var offset = offsetList[offsetIndexList[i + 1]];
                tupleResult.push({
                    date: currentDate,
                    offset: -offset / 60
                })
            }
            if (currentYear > year) {
                break
            }
        }
        return tupleResult
    },
    getUtcOffset(timeZoneInfo, dateTimeStamp) {
        var {
            offsetList: offsetList
        } = timeZoneInfo;
        var {
            offsetIndexList: offsetIndexList
        } = timeZoneInfo;
        var {
            dateList: dateList
        } = timeZoneInfo;
        var lastIntervalStartIndex = dateList.length - 1 - 1;
        var index = lastIntervalStartIndex;
        while (index >= 0 && dateTimeStamp < dateList[index]) {
            index--
        }
        var offset = offsetList[offsetIndexList[index + 1]];
        return -offset / 60 || offset
    }
};
export default timeZoneDataUtils;
