/**
 * DevExtreme (cjs/__internal/grids/grid_core/data_controller/m_data_helper_mixin.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.DataHelperMixin = void 0;
var _extend = require("../../../../core/utils/extend");
var _data_source = require("../../../../data/data_source/data_source");
var _utils = require("../../../../data/data_source/utils");
var _data_controller = _interopRequireDefault(require("../../../../ui/collection/data_controller"));

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

function _inheritsLoose(subClass, superClass) {
    subClass.prototype = Object.create(superClass.prototype);
    subClass.prototype.constructor = subClass;
    _setPrototypeOf(subClass, superClass)
}

function _setPrototypeOf(o, p) {
    _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) {
        o.__proto__ = p;
        return o
    };
    return _setPrototypeOf(o, p)
}
const DATA_SOURCE_OPTIONS_METHOD = "_dataSourceOptions";
const DATA_SOURCE_CHANGED_METHOD = "_dataSourceChangedHandler";
const DATA_SOURCE_LOAD_ERROR_METHOD = "_dataSourceLoadErrorHandler";
const DATA_SOURCE_LOADING_CHANGED_METHOD = "_dataSourceLoadingChangedHandler";
const DATA_SOURCE_FROM_URL_LOAD_MODE_METHOD = "_dataSourceFromUrlLoadMode";
const SPECIFIC_DATA_SOURCE_OPTION = "_getSpecificDataSourceOption";
const NORMALIZE_DATA_SOURCE = "_normalizeDataSource";
const DataHelperMixin = Base => function(_Base) {
    _inheritsLoose(DataHelperMixin, _Base);

    function DataHelperMixin() {
        return _Base.apply(this, arguments) || this
    }
    var _proto = DataHelperMixin.prototype;
    _proto.postCtor = function() {
        this.on("disposing", () => {
            this._disposeDataSource()
        })
    };
    _proto._refreshDataSource = function() {
        this._initDataSource();
        this._loadDataSource()
    };
    _proto._initDataSource = function() {
        let dataSourceOptions = SPECIFIC_DATA_SOURCE_OPTION in this ? this._getSpecificDataSourceOption() : this.option("dataSource");
        let widgetDataSourceOptions;
        let dataSourceType;
        this._disposeDataSource();
        if (dataSourceOptions) {
            if (dataSourceOptions instanceof _data_source.DataSource) {
                this._isSharedDataSource = true;
                this._dataSource = dataSourceOptions
            } else {
                widgetDataSourceOptions = "_dataSourceOptions" in this ? this._dataSourceOptions() : {};
                dataSourceType = this._dataSourceType ? this._dataSourceType() : _data_source.DataSource;
                dataSourceOptions = (0, _utils.normalizeDataSourceOptions)(dataSourceOptions, {
                    fromUrlLoadMode: "_dataSourceFromUrlLoadMode" in this && this._dataSourceFromUrlLoadMode()
                });
                this._dataSource = new dataSourceType((0, _extend.extend)(true, {}, widgetDataSourceOptions, dataSourceOptions))
            }
            if (NORMALIZE_DATA_SOURCE in this) {
                this._dataSource = this._normalizeDataSource(this._dataSource)
            }
            this._addDataSourceHandlers();
            this._initDataController()
        }
    };
    _proto._initDataController = function() {
        var _a;
        const dataController = null === (_a = this.option) || void 0 === _a ? void 0 : _a.call(this, "_dataController");
        const dataSource = this._dataSource;
        if (dataController) {
            this._dataController = dataController
        } else {
            this._dataController = new _data_controller.default(dataSource)
        }
    };
    _proto._addDataSourceHandlers = function() {
        if (DATA_SOURCE_CHANGED_METHOD in this) {
            this._addDataSourceChangeHandler()
        }
        if ("_dataSourceLoadErrorHandler" in this) {
            this._addDataSourceLoadErrorHandler()
        }
        if ("_dataSourceLoadingChangedHandler" in this) {
            this._addDataSourceLoadingChangedHandler()
        }
        this._addReadyWatcher()
    };
    _proto._addReadyWatcher = function() {
        this.readyWatcher = function(isLoading) {
            this._ready && this._ready(!isLoading)
        }.bind(this);
        this._dataSource.on("loadingChanged", this.readyWatcher)
    };
    _proto._addDataSourceChangeHandler = function() {
        const dataSource = this._dataSource;
        this._proxiedDataSourceChangedHandler = function(e) {
            this._dataSourceChangedHandler(dataSource.items(), e)
        }.bind(this);
        dataSource.on("changed", this._proxiedDataSourceChangedHandler)
    };
    _proto._addDataSourceLoadErrorHandler = function() {
        this._proxiedDataSourceLoadErrorHandler = this._dataSourceLoadErrorHandler.bind(this);
        this._dataSource.on("loadError", this._proxiedDataSourceLoadErrorHandler)
    };
    _proto._addDataSourceLoadingChangedHandler = function() {
        this._proxiedDataSourceLoadingChangedHandler = this._dataSourceLoadingChangedHandler.bind(this);
        this._dataSource.on("loadingChanged", this._proxiedDataSourceLoadingChangedHandler)
    };
    _proto._loadDataSource = function() {
        const dataSource = this._dataSource;
        if (dataSource) {
            if (dataSource.isLoaded()) {
                this._proxiedDataSourceChangedHandler && this._proxiedDataSourceChangedHandler()
            } else {
                dataSource.load()
            }
        }
    };
    _proto._loadSingle = function(key, value) {
        key = "this" === key ? this._dataSource.key() || "this" : key;
        return this._dataSource.loadSingle(key, value)
    };
    _proto._isLastPage = function() {
        return !this._dataSource || this._dataSource.isLastPage() || !this._dataSource._pageSize
    };
    _proto._isDataSourceLoading = function() {
        return this._dataSource && this._dataSource.isLoading()
    };
    _proto._disposeDataSource = function() {
        if (this._dataSource) {
            if (this._isSharedDataSource) {
                delete this._isSharedDataSource;
                this._proxiedDataSourceChangedHandler && this._dataSource.off("changed", this._proxiedDataSourceChangedHandler);
                this._proxiedDataSourceLoadErrorHandler && this._dataSource.off("loadError", this._proxiedDataSourceLoadErrorHandler);
                this._proxiedDataSourceLoadingChangedHandler && this._dataSource.off("loadingChanged", this._proxiedDataSourceLoadingChangedHandler);
                if (this._dataSource._eventsStrategy) {
                    this._dataSource._eventsStrategy.off("loadingChanged", this.readyWatcher)
                }
            } else {
                this._dataSource.dispose()
            }
            delete this._dataSource;
            delete this._proxiedDataSourceChangedHandler;
            delete this._proxiedDataSourceLoadErrorHandler;
            delete this._proxiedDataSourceLoadingChangedHandler
        }
    };
    _proto.getDataSource = function() {
        return this._dataSource || null
    };
    return DataHelperMixin
}(Base);
exports.DataHelperMixin = DataHelperMixin;
