import { AdaptableModuleBase } from './AdaptableModuleBase';
import * as ModuleConstants from '../Utilities/Constants/ModuleConstants';
import * as CalculatedColumnRedux from '../Redux/ActionsReducers/CalculatedColumnRedux';
import { CalculatedColumnWizard } from '../View/CalculatedColumn/Wizard/CalculatedColumnWizard';
import ArrayExtensions from '../Utilities/Extensions/ArrayExtensions';
import { getCalculatedColumnSettingsTags } from '../View/CalculatedColumn/Utilities/getCalculatedColumnSettingsTags';
import { getExpressionViewItems } from '../Utilities/getExpressionViewItems';
import { getObjectTagsViewItems } from '../Utilities/getObjectTagsViewItems';
export class CalculatedColumnModule extends 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.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.isModuleAvailable()) {
            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.isModuleAvailable()) {
            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 = getCalculatedColumnSettingsTags(calculateColumn.CalculatedColumnSettings);
        let items = [
            {
                name: 'Settings',
                label: 'Column Id',
                values: [calculateColumn.ColumnId],
            },
            calculateColumn.FriendlyName && {
                name: 'Settings',
                label: 'Column Name',
                values: [calculateColumn.FriendlyName],
            },
            getExpressionViewItems(calculateColumn.Query, this.api),
        ].filter(Boolean);
        if (ArrayExtensions.IsNotNullOrEmpty(settingsTags)) {
            items.push({
                name: 'Settings',
                values: settingsTags,
            });
        }
        items.push(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,
        };
    }
}
