"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 = require("../../Utilities/ObjectFactory");
const AdaptableNumberEditor_1 = require("../../agGrid/editors/AdaptableNumberEditor");
const AdaptableDateEditor_1 = require("../../agGrid/editors/AdaptableDateEditor");
const GeneralConstants_1 = require("../../Utilities/Constants/GeneralConstants");
const ArrayExtensions_1 = tslib_1.__importDefault(require("../../Utilities/Extensions/ArrayExtensions"));
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);
            var colsToRefresh = [cellDataChangedInfo.column.columnId];
            const refencedCalcColumns = this.getAdaptableApi().calculatedColumnApi.internalApi.getCalculatedColumnsReferencingColumnId(cellDataChangedInfo.column.columnId);
            refencedCalcColumns.forEach((calculatedColumn) => {
                colsToRefresh.push(calculatedColumn.ColumnId);
            });
            const suppressFlash = false;
            this.getGridApi().refreshCells([cellDataChangedInfo.rowNode], colsToRefresh, suppressFlash);
        }
    }
    /**
     * 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) => {
            var _a;
            const freeTextColumnSettings = Object.assign(Object.assign({}, defaultFreeTextColumnSettings), freeTextColumn.FreeTextColumnSettings);
            const dataTypeEditor = freeTextColumnSettings.DataType === 'Number'
                ? AdaptableNumberEditor_1.AdaptableNumberEditor
                : freeTextColumnSettings.DataType === 'Date'
                    ? AdaptableDateEditor_1.AdaptableDateEditor
                    : null;
            const columnTypes = [
                GeneralConstants_1.AB_SPECIAL_COLUMN,
                this.getGridApi().internalApi.getAgGridDataType(freeTextColumnSettings.DataType),
            ];
            if (ArrayExtensions_1.default.IsNotNullOrEmpty((_a = freeTextColumn === null || freeTextColumn === void 0 ? void 0 : freeTextColumn.FreeTextColumnSettings) === null || _a === void 0 ? void 0 : _a.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 !== null && dataTypeEditor !== void 0 ? dataTypeEditor : (freeTextColumn.TextEditor && freeTextColumn.TextEditor == 'Large'
                    ? 'agLargeTextCellEditor'
                    : 'agTextCellEditor'),
                type: columnTypes,
                valueSetter: (params) => {
                    return (params.data[freeTextColumn.ColumnId] = params.newValue);
                },
                // use a Boolean Cell Data Type if the Free Text Column is boolean
                cellDataType: freeTextColumnSettings.DataType && freeTextColumnSettings.DataType == 'Boolean'
                    ? 'boolean'
                    : undefined,
                valueGetter: (params) => this.getFreeTextColumnApi().getFreeTextColumnValueForRowNode(freeTextColumn, params.node),
            };
            if (freeTextColumnSettings.HeaderToolTip) {
                newColDef.headerTooltip = freeTextColumnSettings.HeaderToolTip;
            }
            this.getAdaptableApi().logInfo('Setting up FreeText Column: ' + freeTextColumn.ColumnId);
            return newColDef;
        });
    }
}
exports.FreeTextColumnInternalApi = FreeTextColumnInternalApi;
