/**
 * DevExtreme (cjs/__internal/scheduler/appointments/data_provider/m_appointment_data_source.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.AppointmentDataSource = void 0;
var _deferred = require("../../../../core/utils/deferred");

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, _toPropertyKey(descriptor.key), descriptor)
    }
}

function _createClass(Constructor, protoProps, staticProps) {
    if (protoProps) {
        _defineProperties(Constructor.prototype, protoProps)
    }
    if (staticProps) {
        _defineProperties(Constructor, staticProps)
    }
    Object.defineProperty(Constructor, "prototype", {
        writable: false
    });
    return Constructor
}

function _toPropertyKey(arg) {
    var key = _toPrimitive(arg, "string");
    return "symbol" === typeof key ? key : String(key)
}

function _toPrimitive(input, hint) {
    if ("object" !== typeof input || null === input) {
        return input
    }
    var prim = input[Symbol.toPrimitive];
    if (void 0 !== prim) {
        var res = prim.call(input, hint || "default");
        if ("object" !== typeof res) {
            return res
        }
        throw new TypeError("@@toPrimitive must return a primitive value.")
    }
    return ("string" === hint ? String : Number)(input)
}
const STORE_EVENTS = {
    updating: "updating",
    push: "push"
};
let AppointmentDataSource = function() {
    function AppointmentDataSource(dataSource) {
        this.setDataSource(dataSource);
        this._updatedAppointmentKeys = []
    }
    var _proto = AppointmentDataSource.prototype;
    _proto._getStoreKey = function(target) {
        const store = this._dataSource.store();
        return store.keyOf(target)
    };
    _proto.setDataSource = function(dataSource) {
        this._dataSource = dataSource;
        this.cleanState();
        this._initStoreChangeHandlers()
    };
    _proto._initStoreChangeHandlers = function() {
        const dataSource = this._dataSource;
        const store = null === dataSource || void 0 === dataSource ? void 0 : dataSource.store();
        if (store) {
            store.on(STORE_EVENTS.updating, key => {
                const keyName = store.key();
                if (keyName) {
                    this._updatedAppointmentKeys.push({
                        key: keyName,
                        value: key
                    })
                } else {
                    this._updatedAppointment = key
                }
            });
            store.on(STORE_EVENTS.push, pushItems => {
                const items = dataSource.items();
                const keyName = store.key();
                pushItems.forEach(pushItem => {
                    const itemExists = 0 !== items.filter(item => item[keyName] === pushItem.key).length;
                    if (itemExists) {
                        this._updatedAppointmentKeys.push({
                            key: keyName,
                            value: pushItem.key
                        })
                    } else {
                        const {
                            data: data
                        } = pushItem;
                        data && items.push(data)
                    }
                });
                dataSource.load()
            })
        }
    };
    _proto.getUpdatedAppointment = function() {
        return this._updatedAppointment
    };
    _proto.getUpdatedAppointmentKeys = function() {
        return this._updatedAppointmentKeys
    };
    _proto.cleanState = function() {
        this._updatedAppointment = null;
        this._updatedAppointmentKeys = []
    };
    _proto.add = function(rawAppointment) {
        return this._dataSource.store().insert(rawAppointment).done(() => this._dataSource.load())
    };
    _proto.update = function(target, data) {
        const key = this._getStoreKey(target);
        const d = new _deferred.Deferred;
        this._dataSource.store().update(key, data).done(result => this._dataSource.load().done(() => d.resolve(result)).fail(d.reject)).fail(d.reject);
        return d.promise()
    };
    _proto.remove = function(rawAppointment) {
        const key = this._getStoreKey(rawAppointment);
        return this._dataSource.store().remove(key).done(() => this._dataSource.load())
    };
    _proto.destroy = function() {
        var _a;
        const store = null === (_a = this._dataSource) || void 0 === _a ? void 0 : _a.store();
        if (store) {
            store.off(STORE_EVENTS.updating);
            store.off(STORE_EVENTS.push)
        }
    };
    _createClass(AppointmentDataSource, [{
        key: "keyName",
        get: function() {
            const store = this._dataSource.store();
            return store.key()
        }
    }, {
        key: "isDataSourceInit",
        get: function() {
            return !!this._dataSource
        }
    }]);
    return AppointmentDataSource
}();
exports.AppointmentDataSource = AppointmentDataSource;
