"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CalculatedColumnModule = void 0;
const tslib_1 = require("tslib");
const AdaptableModuleBase_1 = require("./AdaptableModuleBase");
const ModuleConstants = tslib_1.__importStar(require("../Utilities/Constants/ModuleConstants"));
const CalculatedColumnRedux = tslib_1.__importStar(require("../Redux/ActionsReducers/CalculatedColumnRedux"));
const CalculatedColumnWizard_1 = require("../View/CalculatedColumn/Wizard/CalculatedColumnWizard");
const ArrayExtensions_1 = tslib_1.__importDefault(require("../Utilities/Extensions/ArrayExtensions"));
const getCalculatedColumnSettingsTags_1 = require("../View/CalculatedColumn/Utilities/getCalculatedColumnSettingsTags");
const getExpressionViewItems_1 = require("../Utilities/getExpressionViewItems");
const getObjectTagsViewItems_1 = require("../Utilities/getObjectTagsViewItems");
class CalculatedColumnModule extends AdaptableModuleBase_1.AdaptableModuleBase {
    constructor(api) {
        super(ModuleConstants.CalculatedColumnModuleId, ModuleConstants.CalculatedColumnFriendlyName, 'calculated-column', 'CalculatedColumnPopup', 'Create bespoke columns whose cell value is derived dynamically from an Expression', api);
        this.isListeningToCellDataChanges = false;
    }
    onAdaptableReady() {
        this.checkListenToCellDataChanged();
    }
    shouldListenToDataChanges() {
        return ArrayExtensions_1.default.IsNotNullOrEmpty(this.api.calculatedColumnApi.getCalculatedColumns());
    }
    checkListenToCellDataChanged() {
        if (!this.isListeningToCellDataChanges) {
            if (this.shouldListenToDataChanges()) {
                this.setupCellDataChangeListener();
                this.setupRowDataChangeListener();
                this.api.calculatedColumnApi.refreshAggregatedCalculatedColumns();
                this.isListeningToCellDataChanges = true;
            }
        }
    }
    setupCellDataChangeListener() {
        this.api.internalApi
            .getDataService()
            .on('CellDataChanged', (cellDataChangedInfo) => {
            if (cellDataChangedInfo.trigger !== 'aggChange') {
                this.api.internalApi
                    .getCalculatedColumnExpressionService()
                    .listentoCellDataChange(cellDataChangedInfo);
            }
        });
    }
    setupRowDataChangeListener() {
        this.api.internalApi
            .getDataService()
            .on('RowDataChanged', (rowDataChangedInfo) => {
            this.api.internalApi
                .getCalculatedColumnExpressionService()
                .listentoRowDataChange(rowDataChangedInfo);
        });
    }
    onAdaptableStateReloaded() {
        this.api.calculatedColumnApi.refreshAggregatedCalculatedColumns();
    }
    getModuleAdaptableObjects() {
        return this.api.calculatedColumnApi.getCalculatedColumns();
    }
    getExplicitlyReferencedColumnIds(calculatedColumn) {
        return (this.api.expressionApi.getColumnsFromExpression(this.api.expressionApi.getAdaptableQueryExpression(calculatedColumn.Query)) ?? []);
    }
    getReferencedNamedQueryNames(calculatedColumn) {
        return (this.api.namedQueryApi.internalApi.getReferencedNamedQueryNames(this.api.expressionApi.getAdaptableQueryExpression(calculatedColumn.Query)) ?? []);
    }
    createColumnMenuItems(column) {
        if (!column) {
            return;
        }
        if (!this.isModuleVisible()) {
            return;
        }
        if (this.isModuleEditable() && column.isCalculatedColumn) {
            const popupParam = {
                column: column,
                action: 'Edit',
                source: 'ColumnMenu',
            };
            return [
                this.createMenuItemShowPopup('calculated-column-edit', 'Edit Calculated Column', this.moduleInfo.Popup, this.moduleInfo.Glyph, popupParam),
            ];
        }
    }
    createContextMenuItems(menuContext) {
        if (!menuContext.adaptableColumn) {
            return;
        }
        if (!this.isModuleVisible()) {
            return;
        }
        if (this.isModuleEditable() &&
            menuContext.adaptableColumn.isCalculatedColumn &&
            !menuContext.isRowGroupColumn &&
            menuContext.isSingleSelectedCell) {
            const popupParam = {
                column: menuContext.adaptableColumn,
                action: 'Edit',
                source: 'ContextMenu',
            };
            return [
                this.createMenuItemShowPopup('calculated-column-edit', 'Edit Calculated Column', this.moduleInfo.Popup, this.moduleInfo.Glyph, popupParam),
            ];
        }
    }
    getTeamSharingAction() {
        return {
            ModuleEntities: this.api.calculatedColumnApi.getCalculatedColumns(),
            AddAction: CalculatedColumnRedux.CalculatedColumnAdd,
            EditAction: CalculatedColumnRedux.CalculatedColumnEdit,
        };
    }
    toView(calculateColumn) {
        const settingsTags = (0, getCalculatedColumnSettingsTags_1.getCalculatedColumnSettingsTags)(calculateColumn.CalculatedColumnSettings);
        let items = [
            {
                name: 'Settings',
                label: 'Column Id',
                values: [calculateColumn.ColumnId],
            },
            calculateColumn.FriendlyName && {
                name: 'Settings',
                label: 'Column Name',
                values: [calculateColumn.FriendlyName],
            },
            (0, getExpressionViewItems_1.getExpressionViewItems)(calculateColumn.Query, this.api),
        ].filter(Boolean);
        if (ArrayExtensions_1.default.IsNotNullOrEmpty(settingsTags)) {
            items.push({
                name: 'Settings',
                values: settingsTags,
            });
        }
        items.push((0, getObjectTagsViewItems_1.getObjectTagsViewItems)(calculateColumn, this.api));
        return {
            items: items,
            abObject: calculateColumn,
        };
    }
    toViewAll() {
        return this.getModuleAdaptableObjects().map((calculatedColumn) => this.toView(calculatedColumn));
    }
    getViewProperties() {
        return {
            getDeleteAction: CalculatedColumnRedux.CalculatedColumnDelete,
            getEditWizard: () => CalculatedColumnWizard_1.CalculatedColumnWizard,
        };
    }
}
exports.CalculatedColumnModule = CalculatedColumnModule;
