/**
 * DevExtreme (cjs/__internal/scheduler/m_utils_time_zone.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 _date = require("../core/utils/date");
var _date2 = _interopRequireDefault(require("../../core/utils/date"));
var _m_date_adapter = _interopRequireDefault(require("./m_date_adapter"));
var _m_utils_timezones_data = _interopRequireDefault(require("./timezones/m_utils_timezones_data"));

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}
const toMs = _date2.default.dateToMilliseconds;
const MINUTES_IN_HOUR = 60;
const MS_IN_MINUTE = 6e4;
const createUTCDateWithLocalOffset = date => {
    if (!date) {
        return null
    }
    return new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()))
};
const createDateFromUTCWithLocalOffset = date => {
    const result = (0, _m_date_adapter.default)(date);
    const timezoneOffsetBeforeInMin = result.getTimezoneOffset();
    result.addTime(result.getTimezoneOffset("minute"));
    result.subtractMinutes(timezoneOffsetBeforeInMin - result.getTimezoneOffset());
    return result.source
};
const getTimeZones = function() {
    let date = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : new Date;
    const dateInUTC = createUTCDate(date);
    return _m_utils_timezones_data.default.getDisplayedTimeZones(dateInUTC.getTime())
};
const createUTCDate = date => new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes()));
const getTimezoneOffsetChangeInMinutes = (startDate, endDate, updatedStartDate, updatedEndDate) => getDaylightOffset(updatedStartDate, updatedEndDate) - getDaylightOffset(startDate, endDate);
const getTimezoneOffsetChangeInMs = (startDate, endDate, updatedStartDate, updatedEndDate) => getTimezoneOffsetChangeInMinutes(startDate, endDate, updatedStartDate, updatedEndDate) * toMs("minute");
const getDaylightOffset = (startDate, endDate) => new Date(startDate).getTimezoneOffset() - new Date(endDate).getTimezoneOffset();
const getDaylightOffsetInMs = (startDate, endDate) => getDaylightOffset(startDate, endDate) * toMs("minute");
const calculateTimezoneByValue = function(timezone) {
    let date = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : new Date;
    if ("string" === typeof timezone) {
        const dateUtc = createUTCDate(date);
        return _m_utils_timezones_data.default.getTimeZoneOffsetById(timezone, dateUtc.getTime())
    }
    return timezone
};
const _getDaylightOffsetByTimezone = (startDate, endDate, timeZone) => calculateTimezoneByValue(timeZone, startDate) - calculateTimezoneByValue(timeZone, endDate);
const getCorrectedDateByDaylightOffsets = (convertedOriginalStartDate, convertedDate, date, timeZone, startDateTimezone) => {
    const daylightOffsetByCommonTimezone = _getDaylightOffsetByTimezone(convertedOriginalStartDate, convertedDate, timeZone);
    const daylightOffsetByAppointmentTimezone = _getDaylightOffsetByTimezone(convertedOriginalStartDate, convertedDate, startDateTimezone);
    const diff = daylightOffsetByCommonTimezone - daylightOffsetByAppointmentTimezone;
    return new Date(date.getTime() - diff * toMs("hour"))
};
const correctRecurrenceExceptionByTimezone = function(exception, exceptionByStartDate, timeZone, startDateTimeZone) {
    let isBackConversion = arguments.length > 4 && void 0 !== arguments[4] ? arguments[4] : false;
    let timezoneOffset = (exception.getTimezoneOffset() - exceptionByStartDate.getTimezoneOffset()) / 60;
    if (startDateTimeZone) {
        timezoneOffset = _getDaylightOffsetByTimezone(exceptionByStartDate, exception, startDateTimeZone)
    } else if (timeZone) {
        timezoneOffset = _getDaylightOffsetByTimezone(exceptionByStartDate, exception, timeZone)
    }
    return new Date(exception.getTime() + (isBackConversion ? -1 : 1) * timezoneOffset * toMs("hour"))
};
const isTimezoneChangeInDate = date => {
    const startDayDate = new Date(new Date(date).setHours(0, 0, 0, 0));
    const endDayDate = new Date(new Date(date).setHours(23, 59, 59, 0));
    return startDayDate.getTimezoneOffset() - endDayDate.getTimezoneOffset() !== 0
};
const getDateWithoutTimezoneChange = date => {
    const clonedDate = new Date(date);
    if (isTimezoneChangeInDate(clonedDate)) {
        const result = new Date(clonedDate);
        return new Date(result.setDate(result.getDate() + 1))
    }
    return clonedDate
};
const isSameAppointmentDates = (startDate, endDate) => {
    endDate = new Date(endDate.getTime() - 1);
    return _date2.default.sameDate(startDate, endDate)
};
const getClientTimezoneOffset = function() {
    let date = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : new Date;
    return 6e4 * date.getTimezoneOffset()
};
const getDiffBetweenClientTimezoneOffsets = function() {
    let firstDate = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : new Date;
    let secondDate = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : new Date;
    return getClientTimezoneOffset(firstDate) - getClientTimezoneOffset(secondDate)
};
const isEqualLocalTimeZone = function(timeZoneName) {
    let date = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : new Date;
    if (Intl) {
        const localTimeZoneName = Intl.DateTimeFormat().resolvedOptions().timeZone;
        if (localTimeZoneName === timeZoneName) {
            return true
        }
    }
    return isEqualLocalTimeZoneByDeclaration(timeZoneName, date)
};
const hasDSTInLocalTimeZone = () => {
    const [startDate, endDate] = getExtremeDates();
    return startDate.getTimezoneOffset() !== endDate.getTimezoneOffset()
};
const isEqualLocalTimeZoneByDeclaration = (timeZoneName, date) => {
    const year = date.getFullYear();
    const getOffset = date => -date.getTimezoneOffset() / 60;
    const getDateAndMoveHourBack = dateStamp => new Date(dateStamp - 36e5);
    const configTuple = _m_utils_timezones_data.default.getTimeZoneDeclarationTuple(timeZoneName, year);
    const [summerTime, winterTime] = configTuple;
    const noDSTInTargetTimeZone = configTuple.length < 2;
    if (noDSTInTargetTimeZone) {
        const targetTimeZoneOffset = _m_utils_timezones_data.default.getTimeZoneOffsetById(timeZoneName, date);
        const localTimeZoneOffset = getOffset(date);
        if (targetTimeZoneOffset !== localTimeZoneOffset) {
            return false
        }
        return !hasDSTInLocalTimeZone()
    }
    const localSummerOffset = getOffset(new Date(summerTime.date));
    const localWinterOffset = getOffset(new Date(winterTime.date));
    if (localSummerOffset !== summerTime.offset) {
        return false
    }
    if (localSummerOffset === getOffset(getDateAndMoveHourBack(summerTime.date))) {
        return false
    }
    if (localWinterOffset !== winterTime.offset) {
        return false
    }
    if (localWinterOffset === getOffset(getDateAndMoveHourBack(winterTime.date))) {
        return false
    }
    return true
};
const getExtremeDates = () => {
    const nowDate = new Date(Date.now());
    const startDate = new Date;
    const endDate = new Date;
    startDate.setFullYear(nowDate.getFullYear(), 0, 1);
    endDate.setFullYear(nowDate.getFullYear(), 6, 1);
    return [startDate, endDate]
};
const setOffsetsToDate = (targetDate, offsetsArray) => {
    const newDateMs = offsetsArray.reduce((result, offset) => result + offset, targetDate.getTime());
    return new Date(newDateMs)
};
const addOffsetsWithoutDST = function(date) {
    for (var _len = arguments.length, offsets = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
        offsets[_key - 1] = arguments[_key]
    }
    const newDate = _date.dateUtilsTs.addOffsets(date, offsets);
    const daylightShift = getDaylightOffsetInMs(date, newDate);
    if (!daylightShift) {
        return newDate
    }
    const correctLocalDate = _date.dateUtilsTs.addOffsets(newDate, [-daylightShift]);
    const daylightSecondShift = getDaylightOffsetInMs(newDate, correctLocalDate);
    return !daylightSecondShift ? correctLocalDate : newDate
};
const utils = {
    getDaylightOffset: getDaylightOffset,
    getDaylightOffsetInMs: getDaylightOffsetInMs,
    getTimezoneOffsetChangeInMinutes: getTimezoneOffsetChangeInMinutes,
    getTimezoneOffsetChangeInMs: getTimezoneOffsetChangeInMs,
    calculateTimezoneByValue: calculateTimezoneByValue,
    getCorrectedDateByDaylightOffsets: getCorrectedDateByDaylightOffsets,
    isSameAppointmentDates: isSameAppointmentDates,
    correctRecurrenceExceptionByTimezone: correctRecurrenceExceptionByTimezone,
    getClientTimezoneOffset: getClientTimezoneOffset,
    getDiffBetweenClientTimezoneOffsets: getDiffBetweenClientTimezoneOffsets,
    createUTCDateWithLocalOffset: createUTCDateWithLocalOffset,
    createDateFromUTCWithLocalOffset: createDateFromUTCWithLocalOffset,
    createUTCDate: createUTCDate,
    isTimezoneChangeInDate: isTimezoneChangeInDate,
    getDateWithoutTimezoneChange: getDateWithoutTimezoneChange,
    hasDSTInLocalTimeZone: hasDSTInLocalTimeZone,
    isEqualLocalTimeZone: isEqualLocalTimeZone,
    isEqualLocalTimeZoneByDeclaration: isEqualLocalTimeZoneByDeclaration,
    getTimeZones: getTimeZones,
    setOffsetsToDate: setOffsetsToDate,
    addOffsetsWithoutDST: addOffsetsWithoutDST
};
var _default = utils;
exports.default = _default;
