"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataChangeHistoryModule = void 0;
const tslib_1 = require("tslib");
const AdaptableModuleBase_1 = require("./AdaptableModuleBase");
const ModuleConstants = tslib_1.__importStar(require("../Utilities/Constants/ModuleConstants"));
const DataChangeHistoryStatusBarContent_1 = require("../View/DataChangeHistory/DataChangeHistoryStatusBarContent");
const core_1 = require("ag-grid-community");
class DataChangeHistoryModule extends AdaptableModuleBase_1.AdaptableModuleBase {
    constructor(api) {
        super(ModuleConstants.DataChangeHistoryModuleId, ModuleConstants.DataChangeHistoryFriendlyName, 'target', 'DataChangeHistoryPopup', 'Provides an overview of all previous changes, giving the possibility to undo specific changes', api);
    }
    onAdaptableReady() {
        this.api.internalApi
            .getDataService()
            .on('CellDataChanged', (cellDataChangedInfo) => {
            if (cellDataChangedInfo.trigger !== 'undo' && cellDataChangedInfo.trigger !== 'aggChange') {
                if (this.shouldLogDataChange(cellDataChangedInfo)) {
                    this.api.dataChangeHistoryApi.addDataChangeHistoryEntry(cellDataChangedInfo);
                }
            }
        });
    }
    isModuleAvailable() {
        const isAdaptableModuleAvailable = super.isModuleAvailable();
        const isAgGridModuleAvailable = this.api.internalApi
            .getAdaptableInstance()
            .agGridAdapter.isModulePresent(core_1.ModuleNames.ClientSideRowModelModule);
        if (isAdaptableModuleAvailable && !isAgGridModuleAvailable) {
            this.api.logWarn(`Data Change History is NOT available due to missing required AG Grid module: ${core_1.ModuleNames.ClientSideRowModelModule}`);
        }
        return isAdaptableModuleAvailable && isAgGridModuleAvailable;
    }
    getPopupMaxWidth() {
        return 1000;
    }
    hasNamedQueryReferences() {
        return false;
    }
    shouldLogDataChange(cellDataChangedInfo) {
        // we log changes only if the change tracking is active
        if (this.api.internalApi.getState().System.DataChangeHistory.currentMode !== 'ACTIVE') {
            return false;
        }
        if (this.api.optionsApi.getDataChangeHistoryOptions().showDataChange) {
            return this.api.optionsApi.getDataChangeHistoryOptions().showDataChange(cellDataChangedInfo);
        }
        return true;
    }
    getViewProperties() {
        return {
            getStatusBarPanelProps: () => {
                return {
                    triggerActionOnWrapperClick: false,
                    content: DataChangeHistoryStatusBarContent_1.DataChangeHistoryStatusBarContent,
                };
            },
        };
    }
}
exports.DataChangeHistoryModule = DataChangeHistoryModule;
