"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CalculatedColumnApiImpl = void 0;
const tslib_1 = require("tslib");
const CalculatedColumnRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/CalculatedColumnRedux"));
const ApiBase_1 = require("./ApiBase");
const ModuleConstants = tslib_1.__importStar(require("../../Utilities/Constants/ModuleConstants"));
const CalculatedColumnInternalApi_1 = require("../Internal/CalculatedColumnInternalApi");
const ArrayExtensions_1 = tslib_1.__importDefault(require("../../Utilities/Extensions/ArrayExtensions"));
class CalculatedColumnApiImpl extends ApiBase_1.ApiBase {
    constructor(_adaptable) {
        super(_adaptable);
        this.internalApi = new CalculatedColumnInternalApi_1.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_1.default.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);
        }
    }
}
exports.CalculatedColumnApiImpl = CalculatedColumnApiImpl;
