"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BulkUpdateModule = void 0;
const tslib_1 = require("tslib");
const AdaptableModuleBase_1 = require("./AdaptableModuleBase");
const ModuleConstants = tslib_1.__importStar(require("../Utilities/Constants/ModuleConstants"));
const PreviewHelper_1 = require("../Utilities/Helpers/PreviewHelper");
const StringExtensions_1 = tslib_1.__importDefault(require("../Utilities/Extensions/StringExtensions"));
const ArrayExtensions_1 = tslib_1.__importDefault(require("../Utilities/Extensions/ArrayExtensions"));
const ObjectFactory_1 = tslib_1.__importDefault(require("../Utilities/ObjectFactory"));
class BulkUpdateModule extends AdaptableModuleBase_1.AdaptableModuleBase {
    constructor(api) {
        super(ModuleConstants.BulkUpdateModuleId, ModuleConstants.BulkUpdateFriendlyName, 'edit-table', 'BulkUpdatePopup', 'Update multiple cell simultaneously with a new or existing value', api);
    }
    getViewAccessLevel() {
        return 'Full';
    }
    createContextMenuItems(menuContext) {
        let menuItemShowPopup = undefined;
        if (!menuContext.isRowGroupColumn && this.isModuleEditable()) {
            if (menuContext.adaptableColumn &&
                menuContext.isSelectedCell &&
                menuContext.isSingleSelectedColumn &&
                this.api.gridApi.isEveryCellEditable(menuContext.selectedCellInfo.gridCells)) {
                let popUpParams = {
                    source: 'ContextMenu',
                };
                menuItemShowPopup = this.createMainMenuItemShowPopup({
                    Name: 'bulk-update-apply',
                    Label: 'Apply Bulk Update',
                    ComponentName: this.moduleInfo.Popup,
                    Icon: this.moduleInfo.Glyph,
                    PopupParams: popUpParams,
                });
            }
        }
        return [menuItemShowPopup];
    }
    checkCorrectCellSelection() {
        let selectedCellInfo = this.api.gridApi.getSelectedCellInfo();
        if (this.api.layoutApi.isCurrentLayoutPivot()) {
            return {
                IsValid: false,
                Alert: {
                    alertType: 'generic',
                    header: 'Bulk Update Error',
                    message: 'Cannot edit while Grid is in Pivot Mode.',
                    alertDefinition: ObjectFactory_1.default.CreateInternalAlertDefinitionForMessages('Error'),
                },
            };
        }
        if (selectedCellInfo == null || ArrayExtensions_1.default.IsNullOrEmpty(selectedCellInfo.columns)) {
            return {
                IsValid: false,
                Alert: {
                    alertType: 'generic',
                    header: 'Bulk Update Error',
                    message: 'No cells are selected.\nPlease select some cells.',
                    alertDefinition: ObjectFactory_1.default.CreateInternalAlertDefinitionForMessages('Error'),
                },
            };
        }
        if (ArrayExtensions_1.default.NotCorrectLength(selectedCellInfo.columns, 1)) {
            return {
                IsValid: false,
                Alert: {
                    alertType: 'generic',
                    header: 'Bulk Update Error',
                    message: 'Bulk Update only supports single column edit.\nPlease adjust cell selection.',
                    alertDefinition: ObjectFactory_1.default.CreateInternalAlertDefinitionForMessages('Error'),
                },
            };
        }
        let selectedColumn = selectedCellInfo.columns[0];
        if (ArrayExtensions_1.default.IsNotNullOrEmpty(selectedCellInfo.gridCells) &&
            !this.api.gridApi.isEveryCellEditable(selectedCellInfo.gridCells)) {
            return {
                IsValid: false,
                Alert: {
                    alertType: 'generic',
                    header: 'Bulk Update Error',
                    message: 'Bulk Update is not permitted on readonly cells.\nPlease adjust the cell selection.',
                    alertDefinition: ObjectFactory_1.default.CreateInternalAlertDefinitionForMessages('Error'),
                },
            };
        }
        return { IsValid: true, Column: selectedColumn };
    }
    buildPreviewValues(bulkUpdateValue) {
        let previewResults = [];
        if (StringExtensions_1.default.IsNullOrEmpty(String(bulkUpdateValue))) {
            return null;
        }
        let selectedCellInfo = this.api.gridApi.getSelectedCellInfo();
        let column;
        if (!this.api.layoutApi.isCurrentLayoutPivot()) {
            if (selectedCellInfo != null && selectedCellInfo.columns.length > 0) {
                column = this.api.columnApi.getColumnWithColumnId(selectedCellInfo.columns[0].columnId);
                let typedBulkUpdateValue;
                switch (column.dataType) {
                    case 'number':
                        typedBulkUpdateValue = Number(bulkUpdateValue);
                        break;
                    case 'text':
                        typedBulkUpdateValue = bulkUpdateValue;
                        break;
                    case 'date':
                        typedBulkUpdateValue = new Date(bulkUpdateValue);
                        break;
                }
                selectedCellInfo.gridCells.forEach((selectedCell) => {
                    const cellDataChangedInfo = this.api.internalApi.buildCellDataChangedInfo({
                        oldValue: selectedCell.rawValue,
                        newValue: typedBulkUpdateValue,
                        column: selectedCell.column,
                        primaryKeyValue: selectedCell.primaryKeyValue,
                        rowNode: selectedCell.rowNode,
                        trigger: 'edit',
                    });
                    const validationRules = this.api.internalApi
                        .getValidationService()
                        .getValidationRulesForDataChange(cellDataChangedInfo);
                    const previewResult = {
                        id: selectedCell.primaryKeyValue,
                        initialValue: selectedCell.rawValue,
                        computedValue: typedBulkUpdateValue,
                        validationRules: validationRules,
                        rowNode: selectedCell.rowNode,
                    };
                    previewResults.push(previewResult);
                });
            }
        }
        return {
            column: column,
            previewResults: previewResults,
            previewValidationSummary: PreviewHelper_1.PreviewHelper.GetPreviewValidationSummary(previewResults),
        };
    }
}
exports.BulkUpdateModule = BulkUpdateModule;
