import { ApiBase } from '../Implementation/ApiBase';
import ObjectFactory, { CreateEmptyFreeTextColumn } from '../../Utilities/ObjectFactory';
import { AdaptableNumberEditor } from '../../agGrid/editors/AdaptableNumberEditor';
import { AdaptableDateEditor } from '../../agGrid/editors/AdaptableDateEditor';
import ArrayExtensions from '../../Utilities/Extensions/ArrayExtensions';
import { FREE_TEXT_COLUMN_TYPE, } from '../../AdaptableState/Common/AdaptableColumn';
export class FreeTextColumnInternalApi extends 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 = CreateEmptyFreeTextColumn(defaultSpecialColumnSettings).FreeTextColumnSettings;
        return this.getFreeTextColumnApi()
            .getFreeTextColumns()
            .map((freeTextColumn) => {
            const freeTextColumnSettings = {
                ...defaultFreeTextColumnSettings,
                ...freeTextColumn.FreeTextColumnSettings,
            };
            const dataTypeEditor = freeTextColumnSettings.DataType === 'number'
                ? AdaptableNumberEditor
                : freeTextColumnSettings.DataType === 'date'
                    ? AdaptableDateEditor
                    : null;
            const columnTypes = [FREE_TEXT_COLUMN_TYPE];
            if (ArrayExtensions.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.CreateInternalAlertDefinitionForMessages('Error'),
            };
            this.getAlertApi().displayAdaptableAlertNotification(alert);
            return false;
        }
        else {
            return true;
        }
    }
}
