import { ApiBase } from './ApiBase';
import { DataChangeHistoryClearRow, DataChangeHistoryDisable, DataChangeHistoryEnable, DataChangeHistoryResume, DataChangeHistorySuspend, DataChangeHistoryUndo, DataChangeHistoryAdd, } from '../../Redux/ActionsReducers/InternalRedux';
import * as ModuleConstants from '../../Utilities/Constants/ModuleConstants';
export class DataChangeHistoryApiImpl extends 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().Internal.DataChangeHistory.currentMode;
    }
    activateDataChangeHistory(forceReset) {
        const currentMode = this.getDataChangeHistoryMode();
        if (currentMode === 'ACTIVE' && !forceReset) {
            return;
        }
        if (forceReset || currentMode === 'INACTIVE') {
            this.dispatchAction(DataChangeHistoryEnable());
        }
        else if (currentMode === 'SUSPENDED') {
            this.dispatchAction(DataChangeHistoryResume());
        }
    }
    deactivateDataChangeHistory() {
        if (this.getDataChangeHistoryMode() === 'ACTIVE' ||
            this.getDataChangeHistoryMode() === 'SUSPENDED') {
            this.dispatchAction(DataChangeHistoryDisable());
        }
    }
    suspendDataChangeHistory() {
        if (this.getDataChangeHistoryMode() === 'ACTIVE') {
            this.dispatchAction(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(DataChangeHistoryAdd(dataChangeInfo, uniqueKey));
    }
    undoDataChangeHistoryEntry(dataChangeInfo) {
        const uniqueKey = this.getDataChangeHistoryKey(dataChangeInfo);
        this.dispatchAction(DataChangeHistoryUndo(dataChangeInfo, uniqueKey));
    }
    clearDataChangeHistoryEntry(dataChangeInfo) {
        const uniqueKey = this.getDataChangeHistoryKey(dataChangeInfo);
        this.dispatchAction(DataChangeHistoryClearRow(dataChangeInfo, uniqueKey));
    }
    openDataChangeHistorySettingsPanel() {
        this.showModulePopup(ModuleConstants.DataChangeHistoryModuleId);
    }
}
