/**
 * DevExtreme (cjs/ui/scheduler/instanceFactory.js)
 * Version: 21.2.0 (build 21200-0321)
 * Build date: Mon Jul 19 2021
 *
 * Copyright (c) 2012 - 2021 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.getModel = exports.getTimeZoneCalculator = exports.getAppointmentDataProvider = exports.getResourceManager = exports.disposeFactoryInstances = exports.createInstance = exports.createFactoryInstances = void 0;
var _type = require("../../core/utils/type");
var _resourceManager = require("./resources/resourceManager");
var _appointmentDataProvider = require("./appointments/DataProvider/appointmentDataProvider");
var _timeZoneCalculator = require("./timeZoneCalculator");
var _utils = _interopRequireDefault(require("./utils.timeZone"));

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

function _extends() {
    _extends = Object.assign || function(target) {
        for (var i = 1; i < arguments.length; i++) {
            var source = arguments[i];
            for (var key in source) {
                if (Object.prototype.hasOwnProperty.call(source, key)) {
                    target[key] = source[key]
                }
            }
        }
        return target
    };
    return _extends.apply(this, arguments)
}
var Names = {
    timeZoneCalculator: "timeZoneCalculator",
    resourceManager: "resourceManager",
    appointmentDataProvider: "appointmentDataProvider",
    model: "model"
};
var factoryInstances = {};
var tailIndex = -1;
var createFactoryInstances = function(options) {
    var key = (0, _type.isDefined)(options.key) ? options.key : ++tailIndex;
    createModel(key, options.model);
    var timeZoneCalculator = createTimeZoneCalculator(key, options.timeZone);
    var resourceManager = createResourceManager(key, options.resources);
    createAppointmentDataProvider(key, _extends({}, options, {
        timeZoneCalculator: timeZoneCalculator,
        resourceManager: resourceManager
    }));
    return key
};
exports.createFactoryInstances = createFactoryInstances;
var createInstance = function(name, key, callback) {
    if (!(0, _type.isDefined)(factoryInstances[name])) {
        factoryInstances[name] = {}
    }
    var result = callback();
    factoryInstances[name][key] = result;
    return result
};
exports.createInstance = createInstance;
var getInstance = function(name, key) {
    return factoryInstances[name] ? factoryInstances[name][key] : void 0
};
var removeInstance = function(name, key) {
    if (getInstance(name, key)) {
        factoryInstances[name] = null
    }
};
var createResourceManager = function(key, resources) {
    return createInstance(Names.resourceManager, key, (function() {
        var resourceManager = getInstance(Names.resourceManager, key);
        if ((0, _type.isDefined)(resourceManager)) {
            resourceManager.setResources(resources);
            return resourceManager
        }
        return new _resourceManager.ResourceManager(resources)
    }))
};
var createAppointmentDataProvider = function(key, options) {
    return createInstance(Names.appointmentDataProvider, key, (function() {
        return new _appointmentDataProvider.AppointmentDataProvider(_extends({}, options, {
            key: key
        }))
    }))
};
var createTimeZoneCalculator = function(key, currentTimeZone) {
    return createInstance(Names.timeZoneCalculator, key, (function() {
        return new _timeZoneCalculator.TimeZoneCalculator({
            getClientOffset: function(date) {
                return _utils.default.getClientTimezoneOffset(date)
            },
            getCommonOffset: function(date, timeZone) {
                return _utils.default.calculateTimezoneByValue(timeZone || currentTimeZone, date)
            },
            getAppointmentOffset: function(date, appointmentTimezone) {
                return _utils.default.calculateTimezoneByValue(appointmentTimezone, date)
            }
        })
    }))
};
var createModel = function(key, options) {
    return createInstance(Names.model, key, (function() {
        return options
    }))
};
var disposeFactoryInstances = function(key) {
    Object.getOwnPropertyNames(Names).forEach((function(name) {
        removeInstance(name, key)
    }))
};
exports.disposeFactoryInstances = disposeFactoryInstances;
var getResourceManager = function(key) {
    return getInstance(Names.resourceManager, key)
};
exports.getResourceManager = getResourceManager;
var getAppointmentDataProvider = function() {
    var key = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : 0;
    return getInstance(Names.appointmentDataProvider, key)
};
exports.getAppointmentDataProvider = getAppointmentDataProvider;
var getTimeZoneCalculator = function(key) {
    return getInstance(Names.timeZoneCalculator, key)
};
exports.getTimeZoneCalculator = getTimeZoneCalculator;
var getModel = function(key) {
    return getInstance(Names.model, key)
};
exports.getModel = getModel;
