"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FreeTextColumnInternalApi = void 0;
const tslib_1 = require("tslib");
const ApiBase_1 = require("../Implementation/ApiBase");
const ObjectFactory_1 = tslib_1.__importStar(require("../../Utilities/ObjectFactory"));
const AdaptableNumberEditor_1 = require("../../agGrid/editors/AdaptableNumberEditor");
const AdaptableDateEditor_1 = require("../../agGrid/editors/AdaptableDateEditor");
const ArrayExtensions_1 = tslib_1.__importDefault(require("../../Utilities/Extensions/ArrayExtensions"));
const AdaptableColumn_1 = require("../../AdaptableState/Common/AdaptableColumn");
class FreeTextColumnInternalApi extends ApiBase_1.ApiBase {
    /**
     * Checks if a User Edit was in a Free Text Column
     * @param cellDataChangedInfo change to check
     */
    handleFreeTextColumnDataChange(cellDataChangedInfo) {
        let freeTextColumn = this.getFreeTextColumnApi()
            .getFreeTextColumns()
            .find((fc) => fc.ColumnId == cellDataChangedInfo.column.columnId);
        if (freeTextColumn) {
            let freeTextStoredValue = {
                PrimaryKey: cellDataChangedInfo.primaryKeyValue,
                FreeText: cellDataChangedInfo.newValue,
            };
            this.getFreeTextColumnApi().setStoredValue(freeTextColumn.ColumnId, freeTextStoredValue);
        }
    }
    /**
     * Returns all the references in the AdaptableState of the given FreeTextColumn
     * @param freeTextColumn FreeTextColumn to be checked
     */
    getFreeTextColumnModuleReferences(freeTextColumn) {
        const references = [];
        this.getAdaptableInternalApi()
            .getModules()
            .forEach((module) => {
            if (module
                .getModuleFreeTextColumnReferences()
                .some((moduleReference) => moduleReference.Uuid === freeTextColumn.Uuid)) {
                references.push(module.moduleInfo.FriendlyName);
            }
        });
        return references;
    }
    getColDefsForFreeTextColumns() {
        const defaultSpecialColumnSettings = this.getGridApi().internalApi.deriveSpecialColumnSettingsFromAgGridDefaultColDef();
        const defaultFreeTextColumnSettings = (0, ObjectFactory_1.CreateEmptyFreeTextColumn)(defaultSpecialColumnSettings).FreeTextColumnSettings;
        return this.getFreeTextColumnApi()
            .getFreeTextColumns()
            .map((freeTextColumn) => {
            const freeTextColumnSettings = {
                ...defaultFreeTextColumnSettings,
                ...freeTextColumn.FreeTextColumnSettings,
            };
            const dataTypeEditor = freeTextColumnSettings.DataType === 'number'
                ? AdaptableNumberEditor_1.AdaptableNumberEditor
                : freeTextColumnSettings.DataType === 'date'
                    ? AdaptableDateEditor_1.AdaptableDateEditor
                    : null;
            const columnTypes = [AdaptableColumn_1.FREE_TEXT_COLUMN_TYPE];
            if (ArrayExtensions_1.default.IsNotNullOrEmpty(freeTextColumn?.FreeTextColumnSettings?.ColumnTypes)) {
                columnTypes.push(...freeTextColumn.FreeTextColumnSettings.ColumnTypes);
            }
            const newColDef = {
                headerName: freeTextColumn.FriendlyName || freeTextColumn.ColumnId,
                colId: freeTextColumn.ColumnId,
                editable: true,
                hide: false,
                width: freeTextColumnSettings.Width,
                enableValue: freeTextColumnSettings.Aggregatable,
                filter: freeTextColumnSettings.Filterable,
                floatingFilter: freeTextColumnSettings.Filterable,
                resizable: freeTextColumnSettings.Resizable,
                enableRowGroup: freeTextColumnSettings.Groupable,
                sortable: freeTextColumnSettings.Sortable,
                enablePivot: freeTextColumnSettings.Pivotable,
                suppressHeaderMenuButton: freeTextColumnSettings.SuppressMenu,
                suppressMovable: freeTextColumnSettings.SuppressMovable,
                cellEditor: dataTypeEditor ??
                    (freeTextColumn.TextEditor && freeTextColumn.TextEditor == 'Large'
                        ? 'agLargeTextCellEditor'
                        : 'agTextCellEditor'),
                type: columnTypes,
                valueSetter: (params) => {
                    return (params.data[freeTextColumn.ColumnId] = params.newValue);
                },
                cellDataType: freeTextColumnSettings.DataType,
                valueGetter: (params) => this.getFreeTextColumnApi().getFreeTextColumnValueForRowNode(freeTextColumn, params.node),
            };
            if (freeTextColumnSettings.HeaderToolTip) {
                newColDef.headerTooltip = freeTextColumnSettings.HeaderToolTip;
            }
            this.logInfo('Setting up FreeText Column: ' + freeTextColumn.ColumnId);
            return newColDef;
        });
    }
    validateDeletedFreeTextColumn(freeTextColumn) {
        const freeTextColumnReferences = this.getFreeTextColumnModuleReferences(freeTextColumn);
        if (freeTextColumnReferences.length) {
            const alert = {
                alertType: 'generic',
                header: 'Free Text Column',
                message: `This Free Text Column cannot be deleted. It is still referenced by: ${freeTextColumnReferences.join(', ')}.`,
                alertDefinition: ObjectFactory_1.default.CreateInternalAlertDefinitionForMessages('Error'),
            };
            this.getAlertApi().displayAdaptableAlertNotification(alert);
            return false;
        }
        else {
            return true;
        }
    }
}
exports.FreeTextColumnInternalApi = FreeTextColumnInternalApi;
