import debounce from 'lodash/debounce';
import { MergeStateFunction } from './AdaptableReduxMerger';
import { AdaptableLogger } from '../../agGrid/AdaptableLogger';
const checkStatus = (response) => {
    const error = new Error(response.statusText);
    if (response.status >= 200 && response.status < 300) {
        return response;
    }
    throw error;
};
const persistState = (state, config) => {
    return new Promise((resolve, reject) => {
        try {
            localStorage.setItem(config.adaptableStateKey, JSON.stringify(state));
            resolve();
        }
        catch (ex) {
            reject(ex);
        }
    });
};
const loadState = ({ adaptableId, adaptableStateKey }) => {
    const jsonState = localStorage.getItem(adaptableStateKey);
    const parsedJsonState = JSON.parse(jsonState) || {};
    return Promise.resolve(parsedJsonState);
};
class AdaptableReduxLocalStorageEngine {
    constructor(config) {
        this.adaptableId = config.adaptableId;
        this.adaptableStateKey = config.adaptableStateKey;
        this.userName = config.userName;
        this.initialState = config.initialState;
        this.loadState = config.loadState;
        this.persistState = config.persistState;
        this.save =
            config.debounceStateDelay > 0
                ? debounce(this.save, config.debounceStateDelay, {
                    maxWait: 1000,
                })
                : this.save;
    }
    setStateKey(adaptableStateKey) {
        this.adaptableStateKey = adaptableStateKey;
    }
    load(initialState = this.initialState) {
        return (this.loadState || loadState)({
            adaptableId: this.adaptableId,
            adaptableStateKey: this.adaptableStateKey,
            userName: this.userName,
        }).then((parsedJsonState) => {
            // we need to merge the Initial Adaptable State
            return Promise.resolve(initialState)
                .then((parsedInitialState) => {
                return MergeStateFunction(parsedInitialState, parsedJsonState);
            })
                .catch((err) => AdaptableLogger.consoleErrorBase(`AdaptableId: ${this.adaptableId}`, err));
        });
    }
    save(state, getState) {
        return this.saveNow(state, getState);
    }
    saveNow(state, getState) {
        const config = {
            adaptableId: this.adaptableId,
            adaptableStateKey: this.adaptableStateKey,
            userName: this.userName,
        };
        let result = state;
        if (getState) {
            result = getState(state, config);
        }
        const promise = (this.persistState || persistState)(result, config)?.catch(rejectWithMessage);
        if (!(promise instanceof Promise)) {
            AdaptableLogger.consoleWarnBase(`AdaptableId: ${this.adaptableId}`, 'stateOptions.persistState should return a promise, it returned', 'Error', promise);
        }
        return promise;
    }
}
function rejectWithMessage(error) {
    return Promise.reject(error.message);
}
export function createEngine(config) {
    return new AdaptableReduxLocalStorageEngine(config);
}
