/**
 * DevExtreme (ui/scheduler/timeZoneCalculator.js)
 * Version: 20.2.3 (build 20316-1558)
 * Build date: Wed Nov 11 2020
 *
 * Copyright (c) 2012 - 2020 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.TimeZoneCalculator = exports.PathTimeZoneConversion = void 0;
var _type = require("../../core/utils/type");
var _date = _interopRequireDefault(require("../../core/utils/date"));

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        "default": obj
    }
}

function _classCallCheck(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function")
    }
}

function _defineProperties(target, props) {
    for (var i = 0; i < props.length; i++) {
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;
        if ("value" in descriptor) {
            descriptor.writable = true
        }
        Object.defineProperty(target, descriptor.key, descriptor)
    }
}

function _createClass(Constructor, protoProps, staticProps) {
    if (protoProps) {
        _defineProperties(Constructor.prototype, protoProps)
    }
    if (staticProps) {
        _defineProperties(Constructor, staticProps)
    }
    return Constructor
}
var toMs = _date.default.dateToMilliseconds;
var PathTimeZoneConversion = {
    fromSourceToAppointment: "toAppointment",
    fromAppointmentToSource: "fromAppointment",
    fromSourceToGrid: "toGrid",
    fromGridToSource: "fromGrid"
};
exports.PathTimeZoneConversion = PathTimeZoneConversion;
var TimeZoneCalculator = function() {
    function TimeZoneCalculator(options) {
        _classCallCheck(this, TimeZoneCalculator);
        this.options = options
    }
    _createClass(TimeZoneCalculator, [{
        key: "createDate",
        value: function(sourceDate, info) {
            var date = new Date(sourceDate);
            switch (info.path) {
                case PathTimeZoneConversion.fromSourceToAppointment:
                    return this._getConvertedDate(date, info.appointmentTimeZone, true);
                case PathTimeZoneConversion.fromAppointmentToSource:
                    return this._getConvertedDate(date, info.appointmentTimeZone, true, true);
                case PathTimeZoneConversion.fromSourceToGrid:
                    return this._getConvertedDate(date, info.appointmentTimeZone, false);
                case PathTimeZoneConversion.fromGridToSource:
                    return this._getConvertedDate(date, info.appointmentTimeZone, false, true)
            }
            throw new Error("not specified pathTimeZoneConversion")
        }
    }, {
        key: "getOffsets",
        value: function(date, appointmentTimezone) {
            var clientOffset = -this._getClientOffset(date) / toMs("hour");
            var commonOffset = this._getCommonOffset(date);
            var appointmentOffset = this._getAppointmentOffset(date, appointmentTimezone);
            return {
                client: clientOffset,
                common: !(0, _type.isDefined)(commonOffset) ? clientOffset : commonOffset,
                appointment: "number" !== typeof appointmentOffset ? clientOffset : appointmentOffset
            }
        }
    }, {
        key: "_getClientOffset",
        value: function(date) {
            return this.options.getClientOffset(date)
        }
    }, {
        key: "_getCommonOffset",
        value: function(date) {
            return this.options.getCommonOffset(date)
        }
    }, {
        key: "_getAppointmentOffset",
        value: function(date, appointmentTimezone) {
            return this.options.getAppointmentOffset(date, appointmentTimezone)
        }
    }, {
        key: "_getConvertedDate",
        value: function(date, appointmentTimezone, useAppointmentTimeZone, isBack) {
            var newDate = new Date(date.getTime());
            var offsets = this.getOffsets(newDate, appointmentTimezone);
            if (useAppointmentTimeZone && !!appointmentTimezone) {
                return this._getConvertedDateByOffsets(date, offsets.client, offsets.appointment, isBack)
            }
            return this._getConvertedDateByOffsets(date, offsets.client, offsets.common, isBack)
        }
    }, {
        key: "_getConvertedDateByOffsets",
        value: function(date, clientOffset, targetOffset, isBack) {
            var direction = isBack ? -1 : 1;
            var utcDate = date.getTime() - direction * clientOffset * toMs("hour");
            return new Date(utcDate + direction * targetOffset * toMs("hour"))
        }
    }]);
    return TimeZoneCalculator
}();
exports.TimeZoneCalculator = TimeZoneCalculator;
