"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlusMinusModule = void 0;
const tslib_1 = require("tslib");
const PlusMinusRedux = tslib_1.__importStar(require("../Redux/ActionsReducers/PlusMinusRedux"));
const ModuleConstants = tslib_1.__importStar(require("../Utilities/Constants/ModuleConstants"));
const ArrayExtensions_1 = require("../Utilities/Extensions/ArrayExtensions");
const Helper_1 = require("../Utilities/Helpers/Helper");
const AdaptableModuleBase_1 = require("./AdaptableModuleBase");
const ModuleConstants_1 = require("../Utilities/Constants/ModuleConstants");
const getObjectTagsViewItems_1 = require("../Utilities/getObjectTagsViewItems");
const getRuleViewItems_1 = require("../Utilities/getRuleViewItems");
const getScopeViewItems_1 = require("../Utilities/getScopeViewItems");
const MovePlusMinus_1 = require("../View/PlusMinus/MovePlusMinus");
const PlusMinusWizard_1 = require("../View/PlusMinus/Wizard/PlusMinusWizard");
const AdaptableLogger_1 = require("../agGrid/AdaptableLogger");
class PlusMinusModule extends AdaptableModuleBase_1.AdaptableModuleBase {
    constructor(api) {
        super(ModuleConstants.PlusMinusModuleId, ModuleConstants.PlusMinusFriendlyName, 'add-circle', 'PlusMinusPopup', 'Configure how a cell value will edit when the + or - keys are pressed', api);
        this.shouldHandleKeyDown = false;
        this.adaptable = api.internalApi.getAdaptableInstance();
    }
    getModuleAdaptableObjects(config) {
        return this.api.plusMinusApi.getAllPlusMinus(config);
    }
    getExplicitlyReferencedColumnIds(plusMinusNudge) {
        if (this.api.columnScopeApi.scopeHasColumns(plusMinusNudge.Scope)) {
            return this.api.columnScopeApi
                .getColumnsInScope(plusMinusNudge.Scope)
                .map((adaptableColumn) => adaptableColumn.columnId);
        }
        return [];
    }
    getReferencedNamedQueryNames(plusMinusNudge) {
        if (!plusMinusNudge.Rule?.BooleanExpression) {
            return [];
        }
        return this.api.namedQueryApi.internalApi.getReferencedNamedQueryNames(plusMinusNudge.Rule?.BooleanExpression);
    }
    onAdaptableReady() {
        let plusMinusNudges = this.api.plusMinusApi.getAllPlusMinus();
        this.shouldHandleKeyDown = ArrayExtensions_1.ArrayExtensions.IsNotNullOrEmpty(plusMinusNudges);
        if (this.shouldHandleKeyDown) {
            this.setupKeyDownListener();
        }
    }
    setupKeyDownListener() {
        this.adaptable._on('KeyDown', (keyDownEvent) => {
            this.handleKeyDown(keyDownEvent);
        });
    }
    checkListenToKeyDown() {
        const oldShouldHandleKeyDown = this.shouldHandleKeyDown;
        const newShouldHandleKeyDown = ArrayExtensions_1.ArrayExtensions.IsNotNullOrEmpty(this.api.plusMinusApi.getAllPlusMinus());
        if (!oldShouldHandleKeyDown && newShouldHandleKeyDown) {
            this.setupKeyDownListener();
        }
        this.shouldHandleKeyDown = newShouldHandleKeyDown;
    }
    createColumnMenuItems(column) {
        if (column && this.isModuleEditable() && column.dataType == 'number') {
            let popupParam = {
                column: column,
                action: 'New',
                source: 'ColumnMenu',
            };
            return [
                this.createMenuItemShowPopup('plus-minus-add', 'Create Plus/Minus Rule', this.moduleInfo.Popup, this.moduleInfo.Glyph, popupParam),
            ];
        }
    }
    handleKeyDown(keyEvent) {
        if (this.shouldHandleKeyDown) {
            //it's a special key so we handle the string representation of the key '
            let keyEventString = Helper_1.Helper.getStringRepresentionFromKey(keyEvent);
            if (keyEventString == '-' || keyEventString == '+') {
                let plusMinusNudges = this.api.plusMinusApi.getAllActivePlusMinus();
                if (ArrayExtensions_1.ArrayExtensions.IsNotNullOrEmpty(plusMinusNudges)) {
                    let side = keyEventString == '-' ? 'down' : 'up';
                    let selectedCellInfo = this.api.gridApi.getSelectedCellInfo();
                    let isPlusMinusApplicable = this.applyPlusMinus(plusMinusNudges, selectedCellInfo.gridCells, side);
                    if (isPlusMinusApplicable) {
                        keyEvent.preventDefault();
                    }
                }
            }
        }
    }
    applyPlusMinus(plusMinusNudges, cellsToUpdate, direction) {
        let shouldApplyPlusMinus = false;
        let successfulValues = [];
        let failedPreventEdits = [];
        let side = direction == 'up' ? 1 : -1;
        cellsToUpdate.forEach((gridCell) => {
            if (gridCell.column &&
                gridCell.column.dataType == 'number' &&
                this.api.gridApi.isCellEditable(gridCell)) {
                let foundRule;
                let replacementGridCell;
                plusMinusNudges.forEach((pmr) => {
                    if (!foundRule) {
                        if (this.api.columnScopeApi.isColumnInScope(gridCell.column, pmr.Scope)) {
                            //for aggrid as we are getting strings sometimes
                            if (typeof gridCell.rawValue != 'number') {
                                gridCell.rawValue = parseFloat(gridCell.rawValue);
                            }
                            const isSatisfiedExpression = this.isPlusMinusNudgeApplied(pmr, gridCell);
                            if (isSatisfiedExpression) {
                                foundRule = pmr;
                                shouldApplyPlusMinus = true;
                                let computedValue = gridCell.rawValue + pmr.NudgeValue * side;
                                //avoid the 0.0000000000x??
                                //   computedValue=   parseFloat(computedValue.toFixed(12));
                                replacementGridCell = {
                                    primaryKeyValue: gridCell.primaryKeyValue,
                                    column: gridCell.column,
                                    rawValue: computedValue,
                                    displayValue: computedValue,
                                    normalisedValue: computedValue,
                                    rowNode: gridCell.rowNode,
                                    isPivotCell: false,
                                    isRowGroupCell: false,
                                };
                            }
                        }
                    }
                });
                if (foundRule && replacementGridCell) {
                    const dataChangedEvent = this.api.internalApi.buildCellDataChangedInfo({
                        oldValue: Number(gridCell.rawValue),
                        newValue: replacementGridCell.rawValue,
                        column: gridCell.column,
                        primaryKeyValue: gridCell.primaryKeyValue,
                        rowNode: gridCell.rowNode,
                        trigger: 'edit',
                    });
                    let validationRules = this.api.internalApi
                        .getValidationService()
                        .getValidationRulesForDataChange(dataChangedEvent);
                    if (validationRules.length > 0) {
                        failedPreventEdits.push(validationRules[0]);
                    }
                    else {
                        successfulValues.push(replacementGridCell);
                    }
                }
            }
        });
        this.ShowErrorPreventMessage(failedPreventEdits);
        if (ArrayExtensions_1.ArrayExtensions.IsNotNullOrEmpty(successfulValues)) {
            const cellUpdateRequests = [];
            successfulValues.forEach((gridCell) => {
                cellUpdateRequests.push({
                    columnId: gridCell.column.columnId,
                    newValue: gridCell.normalisedValue,
                    primaryKeyValue: gridCell.primaryKeyValue,
                    rowNode: gridCell.rowNode,
                });
            });
            this.api.plusMinusApi.applyPlusMinus(cellUpdateRequests);
        }
        return shouldApplyPlusMinus;
    }
    isPlusMinusNudgeApplied(plusMinusNudge, gridCell) {
        if (plusMinusNudge.Rule?.BooleanExpression) {
            return this.evaluateExpression(plusMinusNudge, gridCell.rowNode);
        }
        return true;
    }
    evaluateExpression(plusMinusNudge, node) {
        try {
            return this.api.internalApi
                .getQueryLanguageService()
                .evaluateBooleanExpression(plusMinusNudge.Rule?.BooleanExpression, ModuleConstants_1.PlusMinusModuleId, node);
        }
        catch (error) {
            (0, AdaptableLogger_1.errorOnce)(error.message);
            return false;
        }
    }
    ShowErrorPreventMessage(failedRules) {
        if (failedRules.length > 0) {
            let failedMessages = [];
            failedRules.forEach((fr) => {
                let failedMessage = this.api.internalApi.getValidationService().createValidationMessage(fr) + '\n';
                let existingMessage = failedMessages.find((f) => f == failedMessage);
                if (existingMessage == null) {
                    failedMessages.push(failedMessage);
                }
            });
            this.api.alertApi.showAlertError('Nudge(s) failed rule', failedMessages.toString());
        }
    }
    getTeamSharingAction() {
        return {
            ModuleEntities: this.api.plusMinusApi.getAllPlusMinus(),
            AddAction: PlusMinusRedux.PlusMinusNudgeAdd,
            EditAction: PlusMinusRedux.PlusMinusNudgeEdit,
        };
    }
    toView(plusMinus) {
        return {
            abObject: plusMinus,
            items: [
                {
                    name: 'Target',
                    ...(0, getScopeViewItems_1.getScopeViewItems)(plusMinus.Scope, this.api),
                },
                plusMinus.Rule && {
                    name: 'Settings',
                    label: 'Rule',
                    values: (0, getRuleViewItems_1.getRuleViewItems)(plusMinus.Rule, this.api).values,
                },
                {
                    name: 'Settings',
                    label: 'Nudge Value',
                    values: [plusMinus.NudgeValue],
                },
                (0, getObjectTagsViewItems_1.getObjectTagsViewItems)(plusMinus, this.api),
            ].filter(Boolean),
        };
    }
    toViewAll() {
        return this.getModuleAdaptableObjects({
            includeLayoutNotExtendedObjects: this.showLayoutNonExtendedObjects(),
        }).map((plusMinus) => this.toView(plusMinus));
    }
    getViewProperties() {
        return {
            actions: [MovePlusMinus_1.MovePlusMinus],
            getEditAction: PlusMinusRedux.PlusMinusNudgeEdit,
            getDeleteAction: PlusMinusRedux.PlusMinusNudgeDelete,
            getSuspendAction: PlusMinusRedux.PlusMinusNudgeSuspend,
            getUnSuspendAction: PlusMinusRedux.PlusMinusNudgeUnSuspend,
            getSuspendAllAction: PlusMinusRedux.PlusMinusNudgeSuspendAll,
            getUnSuspendAllAction: PlusMinusRedux.PlusMinusNudgeUnSuspendAll,
            getEditWizard() {
                return PlusMinusWizard_1.PlusMinusWizard;
            },
        };
    }
    containsLayoutExtensions() {
        return true;
    }
}
exports.PlusMinusModule = PlusMinusModule;
