/**
 * DevExtreme (cjs/__internal/grids/grid_core/state_storing/m_state_storing_core.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.default = exports.StateStoringController = void 0;
var _deferred = require("../../../../core/utils/deferred");
var _extend = require("../../../../core/utils/extend");
var _iterator = require("../../../../core/utils/iterator");
var _storage = require("../../../../core/utils/storage");
var _type = require("../../../../core/utils/type");
var _window = require("../../../../core/utils/window");
var _events_engine = _interopRequireDefault(require("../../../../events/core/events_engine"));
var _ui = _interopRequireDefault(require("../../../../ui/widget/ui.errors"));
var _m_modules = _interopRequireDefault(require("../m_modules"));

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 DATE_REGEX = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/;
const parseDates = function(state) {
    if (!state) {
        return
    }(0, _iterator.each)(state, (key, value) => {
        if ((0, _type.isPlainObject)(value) || Array.isArray(value)) {
            parseDates(value)
        } else if ("string" === typeof value) {
            const date = DATE_REGEX.exec(value);
            if (date) {
                state[key] = new Date(Date.UTC(+date[1], +date[2] - 1, +date[3], +date[4], +date[5], +date[6]))
            }
        }
    })
};
const getStorage = function(options) {
    const storage = "sessionStorage" === options.type ? (0, _storage.sessionStorage)() : (0, _window.getWindow)().localStorage;
    if (!storage) {
        throw new Error("E1007")
    }
    return storage
};
const getUniqueStorageKey = function(options) {
    return (0, _type.isDefined)(options.storageKey) ? options.storageKey : "storage"
};
let StateStoringController = function(_modules$ViewControll) {
    _inheritsLoose(StateStoringController, _modules$ViewControll);

    function StateStoringController() {
        return _modules$ViewControll.apply(this, arguments) || this
    }
    var _proto = StateStoringController.prototype;
    _proto.getDataController = function() {
        return this.getController("data")
    };
    _proto.getExportController = function() {
        return this.getController("export")
    };
    _proto.getColumnsController = function() {
        return this.getController("columns")
    };
    _proto.init = function() {
        this._state = {};
        this._isLoaded = false;
        this._isLoading = false;
        this._windowUnloadHandler = () => {
            if (void 0 !== this._savingTimeoutID) {
                this._saveState(this.state())
            }
        };
        _events_engine.default.on((0, _window.getWindow)(), "unload", this._windowUnloadHandler);
        return this
    };
    _proto.optionChanged = function(args) {
        const that = this;
        switch (args.name) {
            case "stateStoring":
                if (that.isEnabled() && !that.isLoading()) {
                    that.load()
                }
                args.handled = true;
                break;
            default:
                _modules$ViewControll.prototype.optionChanged.call(this, args)
        }
    };
    _proto.dispose = function() {
        clearTimeout(this._savingTimeoutID);
        _events_engine.default.off((0, _window.getWindow)(), "unload", this._windowUnloadHandler)
    };
    _proto._loadState = function() {
        const options = this.option("stateStoring");
        if ("custom" === options.type) {
            return options.customLoad && options.customLoad()
        }
        try {
            return JSON.parse(getStorage(options).getItem(getUniqueStorageKey(options)))
        } catch (e) {
            _ui.default.log("W1022", "State storing", e.message)
        }
    };
    _proto._saveState = function(state) {
        const options = this.option("stateStoring");
        if ("custom" === options.type) {
            options.customSave && options.customSave(state);
            return
        }
        try {
            getStorage(options).setItem(getUniqueStorageKey(options), JSON.stringify(state))
        } catch (e) {
            _ui.default.log(e.message)
        }
    };
    _proto.publicMethods = function() {
        return ["state"]
    };
    _proto.isEnabled = function() {
        return this.option("stateStoring.enabled")
    };
    _proto.isLoaded = function() {
        return this._isLoaded
    };
    _proto.isLoading = function() {
        return this._isLoading
    };
    _proto.load = function() {
        this._isLoading = true;
        const loadResult = (0, _deferred.fromPromise)(this._loadState());
        loadResult.always(() => {
            this._isLoaded = true;
            this._isLoading = false
        }).done(state => {
            if (null !== state && !(0, _type.isEmptyObject)(state)) {
                this.state(state)
            }
        });
        return loadResult
    };
    _proto.state = function(_state) {
        const that = this;
        if (!arguments.length) {
            return (0, _extend.extend)(true, {}, that._state)
        }
        that._state = (0, _extend.extend)({}, _state);
        parseDates(that._state)
    };
    _proto.save = function() {
        const that = this;
        clearTimeout(that._savingTimeoutID);
        that._savingTimeoutID = setTimeout(() => {
            that._saveState(that.state());
            that._savingTimeoutID = void 0
        }, that.option("stateStoring.savingTimeout"))
    };
    return StateStoringController
}(_m_modules.default.ViewController);
exports.StateStoringController = StateStoringController;
var _default = {
    StateStoringController: StateStoringController
};
exports.default = _default;
