"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataChangeHistoryApiImpl = void 0;
const tslib_1 = require("tslib");
const ApiBase_1 = require("./ApiBase");
const InternalRedux_1 = require("../../Redux/ActionsReducers/InternalRedux");
const ModuleConstants = tslib_1.__importStar(require("../../Utilities/Constants/ModuleConstants"));
class DataChangeHistoryApiImpl extends ApiBase_1.ApiBase {
    getDataChangeHistoryMode() {
        return this.getAdaptableState().Internal.DataChangeHistory.currentMode;
    }
    activateDataChangeHistory(forceReset) {
        const currentMode = this.getDataChangeHistoryMode();
        if (currentMode === 'ACTIVE' && !forceReset) {
            return;
        }
        if (forceReset || currentMode === 'INACTIVE') {
            this.dispatchAction((0, InternalRedux_1.DataChangeHistoryEnable)());
        }
        else if (currentMode === 'SUSPENDED') {
            this.dispatchAction((0, InternalRedux_1.DataChangeHistoryResume)());
        }
    }
    deactivateDataChangeHistory() {
        if (this.getDataChangeHistoryMode() === 'ACTIVE' ||
            this.getDataChangeHistoryMode() === 'SUSPENDED') {
            this.dispatchAction((0, InternalRedux_1.DataChangeHistoryDisable)());
        }
    }
    suspendDataChangeHistory() {
        if (this.getDataChangeHistoryMode() === 'ACTIVE') {
            this.dispatchAction((0, InternalRedux_1.DataChangeHistorySuspend)());
        }
    }
    getDataChangeHistoryLog() {
        const changeLog = this.getAdaptableState().Internal.DataChangeHistory.logs ?? {};
        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, InternalRedux_1.DataChangeHistoryAdd)(dataChangeInfo, uniqueKey));
    }
    undoDataChangeHistoryEntry(dataChangeInfo) {
        const uniqueKey = this.getDataChangeHistoryKey(dataChangeInfo);
        this.dispatchAction((0, InternalRedux_1.DataChangeHistoryUndo)(dataChangeInfo, uniqueKey));
    }
    clearDataChangeHistoryEntry(dataChangeInfo) {
        const uniqueKey = this.getDataChangeHistoryKey(dataChangeInfo);
        this.dispatchAction((0, InternalRedux_1.DataChangeHistoryClearRow)(dataChangeInfo, uniqueKey));
    }
    openDataChangeHistorySettingsPanel() {
        this.showModulePopup(ModuleConstants.DataChangeHistoryModuleId);
    }
    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 });
        }
    };
}
exports.DataChangeHistoryApiImpl = DataChangeHistoryApiImpl;
