"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataChangeHistoryApiImpl = void 0;
const tslib_1 = require("tslib");
const ApiBase_1 = require("./ApiBase");
const SystemRedux_1 = require("../../Redux/ActionsReducers/SystemRedux");
const ModuleConstants = tslib_1.__importStar(require("../../Utilities/Constants/ModuleConstants"));
class DataChangeHistoryApiImpl extends ApiBase_1.ApiBase {
    constructor() {
        super(...arguments);
        this.getDataChangeHistoryKey = (dataChangeInfo) => {
            const columnId = dataChangeInfo.column.columnId;
            const primaryKeyValue = dataChangeInfo.primaryKeyValue;
            const changedAt = dataChangeInfo.changedAt;
            const showLastDataChangeOnly = this.getDataChangeHistoryOptions().showLastDataChangeOnly;
            if (showLastDataChangeOnly) {
                return JSON.stringify({ columnId, primaryKeyValue });
            }
            else {
                return JSON.stringify({ columnId, primaryKeyValue, changedAt });
            }
        };
    }
    getDataChangeHistoryMode() {
        return this.getAdaptableState().System.DataChangeHistory.currentMode;
    }
    activateDataChangeHistory(forceReset) {
        const currentMode = this.getDataChangeHistoryMode();
        if (currentMode === 'ACTIVE' && !forceReset) {
            return;
        }
        if (forceReset || currentMode === 'INACTIVE') {
            this.dispatchAction((0, SystemRedux_1.SystemDataChangeHistoryEnable)());
        }
        else if (currentMode === 'SUSPENDED') {
            this.dispatchAction((0, SystemRedux_1.SystemDataChangeHistoryResume)());
        }
    }
    deactivateDataChangeHistory() {
        if (this.getDataChangeHistoryMode() === 'ACTIVE' ||
            this.getDataChangeHistoryMode() === 'SUSPENDED') {
            this.dispatchAction((0, SystemRedux_1.SystemDataChangeHistoryDisable)());
        }
    }
    suspendDataChangeHistory() {
        if (this.getDataChangeHistoryMode() === 'ACTIVE') {
            this.dispatchAction((0, SystemRedux_1.SystemDataChangeHistorySuspend)());
        }
    }
    getDataChangeHistoryLog() {
        var _a;
        const changeLog = (_a = this.getAdaptableState().System.DataChangeHistory.logs) !== null && _a !== void 0 ? _a : {};
        return Object.values(changeLog);
    }
    getDataChangeForGridCell(gridCell) {
        const allChanges = this.getDataChangeHistoryLog();
        return allChanges.find((c) => c.primaryKeyValue == gridCell.primaryKeyValue &&
            c.column.columnId == gridCell.column.columnId);
    }
    addDataChangeHistoryEntry(dataChangeInfo) {
        const uniqueKey = this.getDataChangeHistoryKey(dataChangeInfo);
        this.dispatchAction((0, SystemRedux_1.SystemDataChangeHistoryAdd)(dataChangeInfo, uniqueKey));
    }
    undoDataChangeHistoryEntry(dataChangeInfo) {
        const uniqueKey = this.getDataChangeHistoryKey(dataChangeInfo);
        this.dispatchAction((0, SystemRedux_1.SystemDataChangeHistoryUndo)(dataChangeInfo, uniqueKey));
    }
    clearDataChangeHistoryEntry(dataChangeInfo) {
        const uniqueKey = this.getDataChangeHistoryKey(dataChangeInfo);
        this.dispatchAction((0, SystemRedux_1.SystemDataChangeHistoryClearRow)(dataChangeInfo, uniqueKey));
    }
    openDataChangeHistorySettingsPanel() {
        this.showModulePopup(ModuleConstants.DataChangeHistoryModuleId);
    }
}
exports.DataChangeHistoryApiImpl = DataChangeHistoryApiImpl;
