import * as CalculatedColumnRedux from '../../Redux/ActionsReducers/CalculatedColumnRedux';
import { ApiBase } from './ApiBase';
import * as ModuleConstants from '../../Utilities/Constants/ModuleConstants';
import { CalculatedColumnInternalApi } from '../Internal/CalculatedColumnInternalApi';
import ArrayExtensions from '../../Utilities/Extensions/ArrayExtensions';
export class CalculatedColumnApiImpl extends ApiBase {
    constructor(_adaptable) {
        super(_adaptable);
        this.internalApi = new CalculatedColumnInternalApi(_adaptable);
    }
    getCalculatedColumnState() {
        return this.getAdaptableState().CalculatedColumn;
    }
    getCalculatedColumns() {
        return this.getCalculatedColumnState().CalculatedColumns ?? [];
    }
    getAggregatedCalculatedColumns() {
        return this.getCalculatedColumns()?.filter((calculatedColumn) => calculatedColumn.Query?.AggregatedScalarExpression);
    }
    getCalculatedColumnById(id) {
        return this.getCalculatedColumns().find((column) => column?.Uuid === id);
    }
    addCalculatedColumn(calculatedColumn) {
        this.addUidToAdaptableObject(calculatedColumn);
        this.dispatchAction(CalculatedColumnRedux.CalculatedColumnAdd(calculatedColumn));
        return this.getCalculatedColumnById(calculatedColumn.Uuid);
    }
    getCalculatedColumnForColumnId(columnId) {
        return this.getCalculatedColumns().find((column) => column.ColumnId === columnId);
    }
    editCalculatedColumn(calculatedColumn) {
        this.dispatchAction(CalculatedColumnRedux.CalculatedColumnEdit(calculatedColumn));
        return this.getCalculatedColumnById(calculatedColumn.Uuid);
    }
    deleteCalculatedColumn(column) {
        let calcColumn = this.getCalculatedColumns().find((cc) => cc.ColumnId == column);
        this.dispatchAction(CalculatedColumnRedux.CalculatedColumnDelete(calcColumn));
    }
    openCalculatedColumnSettingsPanel() {
        this.showModulePopup(ModuleConstants.CalculatedColumnModuleId);
    }
    refreshAggregatedCalculatedColumns() {
        const aggregatedCalculatedColumns = this.getAggregatedCalculatedColumns();
        if (ArrayExtensions.IsNotNullOrEmpty(aggregatedCalculatedColumns)) {
            aggregatedCalculatedColumns.forEach((calculatedColumn) => {
                this.getAdaptableInternalApi()
                    .getCalculatedColumnExpressionService()
                    .createAggregatedScalarLiveValue(calculatedColumn);
            });
            // refresh calculated columns
            const aggCalculatedColumnIds = aggregatedCalculatedColumns.map((calculatedColumn) => calculatedColumn.ColumnId);
            if (this.getLayoutApi().internalApi.isLayoutGrouped()) {
                this.getGridApi().refreshGroupRowNodes();
            }
            else {
                this.getGridApi().refreshColumns(aggCalculatedColumnIds);
            }
        }
    }
    refreshAggregatedCalculatedColumn(columnId) {
        const aggregatedCalculatedColumn = this.getCalculatedColumnForColumnId(columnId);
        if (!aggregatedCalculatedColumn?.Query?.AggregatedScalarExpression) {
            this.logWarn(`Aggregated calculated column with ID=${columnId} was NOT found (and refreshed)!`);
            return;
        }
        this.getAdaptableInternalApi()
            .getCalculatedColumnExpressionService()
            .createAggregatedScalarLiveValue(aggregatedCalculatedColumn);
        this.getGridApi().refreshColumn(columnId);
        if (this.getLayoutApi().internalApi.isLayoutGrouped()) {
            this.getGridApi().refreshGroupRowNodes();
        }
        else {
            this.getGridApi().refreshColumn(columnId);
        }
    }
}
