/**
 * DevExtreme (esm/__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/
 */
import {
    fromPromise
} from "../../../../core/utils/deferred";
import {
    extend
} from "../../../../core/utils/extend";
import {
    each
} from "../../../../core/utils/iterator";
import {
    sessionStorage
} from "../../../../core/utils/storage";
import {
    isDefined,
    isEmptyObject,
    isPlainObject
} from "../../../../core/utils/type";
import {
    getWindow
} from "../../../../core/utils/window";
import eventsEngine from "../../../../events/core/events_engine";
import errors from "../../../../ui/widget/ui.errors";
import modules from "../m_modules";
var DATE_REGEX = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/;
var parseDates = function parseDates(state) {
    if (!state) {
        return
    }
    each(state, (key, value) => {
        if (isPlainObject(value) || Array.isArray(value)) {
            parseDates(value)
        } else if ("string" === typeof value) {
            var 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]))
            }
        }
    })
};
var getStorage = function(options) {
    var storage = "sessionStorage" === options.type ? sessionStorage() : getWindow().localStorage;
    if (!storage) {
        throw new Error("E1007")
    }
    return storage
};
var getUniqueStorageKey = function(options) {
    return isDefined(options.storageKey) ? options.storageKey : "storage"
};
export class StateStoringController extends modules.ViewController {
    getDataController() {
        return this.getController("data")
    }
    getExportController() {
        return this.getController("export")
    }
    getColumnsController() {
        return this.getController("columns")
    }
    init() {
        this._state = {};
        this._isLoaded = false;
        this._isLoading = false;
        this._windowUnloadHandler = () => {
            if (void 0 !== this._savingTimeoutID) {
                this._saveState(this.state())
            }
        };
        eventsEngine.on(getWindow(), "unload", this._windowUnloadHandler);
        return this
    }
    optionChanged(args) {
        switch (args.name) {
            case "stateStoring":
                if (this.isEnabled() && !this.isLoading()) {
                    this.load()
                }
                args.handled = true;
                break;
            default:
                super.optionChanged(args)
        }
    }
    dispose() {
        clearTimeout(this._savingTimeoutID);
        eventsEngine.off(getWindow(), "unload", this._windowUnloadHandler)
    }
    _loadState() {
        var options = this.option("stateStoring");
        if ("custom" === options.type) {
            return options.customLoad && options.customLoad()
        }
        try {
            return JSON.parse(getStorage(options).getItem(getUniqueStorageKey(options)))
        } catch (e) {
            errors.log("W1022", "State storing", e.message)
        }
    }
    _saveState(state) {
        var 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) {
            errors.log(e.message)
        }
    }
    publicMethods() {
        return ["state"]
    }
    isEnabled() {
        return this.option("stateStoring.enabled")
    }
    isLoaded() {
        return this._isLoaded
    }
    isLoading() {
        return this._isLoading
    }
    load() {
        this._isLoading = true;
        var loadResult = fromPromise(this._loadState());
        loadResult.always(() => {
            this._isLoaded = true;
            this._isLoading = false
        }).done(state => {
            if (null !== state && !isEmptyObject(state)) {
                this.state(state)
            }
        });
        return loadResult
    }
    state(state) {
        if (!arguments.length) {
            return extend(true, {}, this._state)
        }
        this._state = extend({}, state);
        parseDates(this._state)
    }
    save() {
        var that = this;
        clearTimeout(that._savingTimeoutID);
        that._savingTimeoutID = setTimeout(() => {
            that._saveState(that.state());
            that._savingTimeoutID = void 0
        }, that.option("stateStoring.savingTimeout"))
    }
}
export default {
    StateStoringController: StateStoringController
};
