/**
 * DevExtreme (cjs/__internal/grids/grid_core/state_storing/m_state_storing.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.stateStoringModule = void 0;
var _common = require("../../../../core/utils/common");
var _deferred = require("../../../../core/utils/deferred");
var _extend = require("../../../../core/utils/extend");
var _type = require("../../../../core/utils/type");
var _m_state_storing_core = require("./m_state_storing_core");

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 getDataState = that => {
    const pagerView = that.getView("pagerView");
    const dataController = that.getController("data");
    const state = {
        allowedPageSizes: pagerView ? pagerView.getPageSizes() : void 0,
        filterPanel: {
            filterEnabled: that.option("filterPanel.filterEnabled")
        },
        filterValue: that.option("filterValue"),
        focusedRowKey: that.option("focusedRowEnabled") ? that.option("focusedRowKey") : void 0
    };
    return (0, _extend.extend)(state, dataController.getUserState())
};
const processLoadState = that => {
    const columnsController = that.getController("columns");
    const selectionController = that.getController("selection");
    const exportController = that.getController("export");
    const dataController = that.getController("data");
    if (columnsController) {
        columnsController.columnsChanged.add(() => {
            that.updateState({
                columns: columnsController.getUserState()
            })
        })
    }
    if (selectionController) {
        selectionController.selectionChanged.add(e => {
            that.updateState({
                selectedRowKeys: e.selectedRowKeys,
                selectionFilter: e.selectionFilter
            })
        })
    }
    if (dataController) {
        that._initialPageSize = that.option("paging.pageSize");
        that._initialFilterValue = that.option("filterValue");
        dataController.changed.add(() => {
            const state = getDataState(that);
            that.updateState(state)
        })
    }
    if (exportController) {
        exportController.selectionOnlyChanged.add(() => {
            that.updateState({
                exportSelectionOnly: exportController.selectionOnly()
            })
        })
    }
};
const DEFAULT_FILTER_VALUE = null;
const getFilterValue = (that, state) => {
    const filterSyncController = that.getController("filterSync");
    const columnsController = that.getController("columns");
    const hasFilterState = state.columns || void 0 !== state.filterValue;
    if (filterSyncController) {
        if (hasFilterState) {
            return state.filterValue || filterSyncController.getFilterValueFromColumns(state.columns)
        }
        return that._initialFilterValue || filterSyncController.getFilterValueFromColumns(columnsController.getColumns())
    }
    return null
};
const rowsView = Base => function(_Base) {
    _inheritsLoose(StateStoringRowsViewExtender, _Base);

    function StateStoringRowsViewExtender() {
        return _Base.apply(this, arguments) || this
    }
    var _proto = StateStoringRowsViewExtender.prototype;
    _proto.init = function() {
        _Base.prototype.init.call(this);
        this._dataController.stateLoaded.add(() => {
            if (this._dataController.isLoaded() && !this._dataController.getDataSource()) {
                this.setLoading(false);
                this.renderNoDataText();
                const columnHeadersView = this.component.getView("columnHeadersView");
                columnHeadersView && columnHeadersView.render();
                this.component._fireContentReadyAction()
            }
        })
    };
    return StateStoringRowsViewExtender
}(Base);
const stateStoring = Base => function(_Base2) {
    _inheritsLoose(StateStoringExtender, _Base2);

    function StateStoringExtender() {
        return _Base2.apply(this, arguments) || this
    }
    var _proto2 = StateStoringExtender.prototype;
    _proto2.init = function() {
        _Base2.prototype.init.apply(this, arguments);
        processLoadState(this);
        return this
    };
    _proto2.isLoading = function() {
        return _Base2.prototype.isLoading.call(this) || this.getDataController().isStateLoading()
    };
    _proto2.state = function(_state) {
        const result = _Base2.prototype.state.apply(this, arguments);
        if (void 0 !== _state) {
            this.applyState((0, _extend.extend)(true, {}, _state))
        }
        return result
    };
    _proto2.updateState = function(state) {
        if (this.isEnabled()) {
            const oldState = this.state();
            const newState = (0, _extend.extend)({}, oldState, state);
            const oldStateHash = (0, _common.getKeyHash)(oldState);
            const newStateHash = (0, _common.getKeyHash)(newState);
            if (!(0, _common.equalByValue)(oldStateHash, newStateHash)) {
                state = (0, _extend.extend)(true, {}, state);
                (0, _extend.extend)(this._state, state);
                this.save()
            }
        } else {
            (0, _extend.extend)(this._state, state)
        }
    };
    _proto2.applyState = function(state) {
        var _a;
        const {
            allowedPageSizes: allowedPageSizes
        } = state;
        const {
            searchText: searchText
        } = state;
        const {
            selectedRowKeys: selectedRowKeys
        } = state;
        const {
            selectionFilter: selectionFilter
        } = state;
        const scrollingMode = this.option("scrolling.mode");
        const isVirtualScrollingMode = "virtual" === scrollingMode || "infinite" === scrollingMode;
        const showPageSizeSelector = true === this.option("pager.visible") && this.option("pager.showPageSizeSelector");
        const hasHeight = null === (_a = this.getView("rowsView")) || void 0 === _a ? void 0 : _a.hasHeight();
        this.component.beginUpdate();
        if (this.getColumnsController()) {
            this.getColumnsController().setUserState(state.columns)
        }
        if (this.getExportController()) {
            this.getExportController().selectionOnly(state.exportSelectionOnly)
        }
        if (!this.option("selection.deferred")) {
            this.option("selectedRowKeys", selectedRowKeys || [])
        }
        this.option("selectionFilter", selectionFilter);
        if (allowedPageSizes && "auto" === this.option("pager.allowedPageSizes")) {
            this.option("pager").allowedPageSizes = allowedPageSizes
        }
        if (this.option("focusedRowEnabled")) {
            this.option("focusedRowIndex", -1);
            this.option("focusedRowKey", state.focusedRowKey || null)
        }
        this.component.endUpdate();
        this.option("searchPanel.text", searchText || "");
        this.option("filterValue", getFilterValue(this, state));
        this.option("filterPanel.filterEnabled", state.filterPanel ? state.filterPanel.filterEnabled : true);
        this.option("paging.pageIndex", (!isVirtualScrollingMode || hasHeight) && state.pageIndex || 0);
        this.option("paging.pageSize", (!isVirtualScrollingMode || showPageSizeSelector) && (0, _type.isDefined)(state.pageSize) ? state.pageSize : this._initialPageSize);
        this.getDataController() && this.getDataController().reset()
    };
    return StateStoringExtender
}(Base);
const columns = Base => function(_Base3) {
    _inheritsLoose(StateStoringColumnsExtender, _Base3);

    function StateStoringColumnsExtender() {
        return _Base3.apply(this, arguments) || this
    }
    var _proto3 = StateStoringColumnsExtender.prototype;
    _proto3._shouldReturnVisibleColumns = function() {
        const result = _Base3.prototype._shouldReturnVisibleColumns.apply(this, arguments);
        return result && (!this._stateStoringController.isEnabled() || this._stateStoringController.isLoaded())
    };
    return StateStoringColumnsExtender
}(Base);
const data = Base => function(_Base4) {
    _inheritsLoose(StateStoringDataExtender, _Base4);

    function StateStoringDataExtender() {
        return _Base4.apply(this, arguments) || this
    }
    var _proto4 = StateStoringDataExtender.prototype;
    _proto4.dispose = function() {
        clearTimeout(this._restoreStateTimeoutID);
        _Base4.prototype.dispose.call(this)
    };
    _proto4.callbackNames = function() {
        return _Base4.prototype.callbackNames.call(this).concat(["stateLoaded"])
    };
    _proto4._refreshDataSource = function() {
        if (this._stateStoringController.isEnabled() && !this._stateStoringController.isLoaded()) {
            clearTimeout(this._restoreStateTimeoutID);
            const deferred = new _deferred.Deferred;
            this._restoreStateTimeoutID = setTimeout(() => {
                this._stateStoringController.load().always(() => {
                    this._restoreStateTimeoutID = null
                }).done(() => {
                    _Base4.prototype._refreshDataSource.call(this);
                    this.stateLoaded.fire();
                    deferred.resolve()
                }).fail(error => {
                    this.stateLoaded.fire();
                    this._handleLoadError(error || "Unknown error");
                    deferred.reject()
                })
            });
            return deferred.promise()
        }
        if (!this.isStateLoading()) {
            _Base4.prototype._refreshDataSource.call(this)
        }
    };
    _proto4.isLoading = function() {
        return _Base4.prototype.isLoading.call(this) || this._stateStoringController.isLoading()
    };
    _proto4.isStateLoading = function() {
        return (0, _type.isDefined)(this._restoreStateTimeoutID)
    };
    _proto4.isLoaded = function() {
        return _Base4.prototype.isLoaded.call(this) && !this.isStateLoading()
    };
    return StateStoringDataExtender
}(Base);
const selection = Base => function(_Base5) {
    _inheritsLoose(StateStoringSelectionExtender, _Base5);

    function StateStoringSelectionExtender() {
        return _Base5.apply(this, arguments) || this
    }
    var _proto5 = StateStoringSelectionExtender.prototype;
    _proto5._fireSelectionChanged = function(options) {
        const isDeferredSelection = this.option("selection.deferred");
        if (this._stateStoringController.isLoading() && isDeferredSelection) {
            return
        }
        _Base5.prototype._fireSelectionChanged.apply(this, arguments)
    };
    return StateStoringSelectionExtender
}(Base);
const stateStoringModule = {
    defaultOptions: () => ({
        stateStoring: {
            enabled: false,
            storageKey: null,
            type: "localStorage",
            customLoad: null,
            customSave: null,
            savingTimeout: 2e3
        }
    }),
    controllers: {
        stateStoring: _m_state_storing_core.StateStoringController
    },
    extenders: {
        views: {
            rowsView: rowsView
        },
        controllers: {
            stateStoring: stateStoring,
            columns: columns,
            data: data,
            selection: selection
        }
    }
};
exports.stateStoringModule = stateStoringModule;
