import { ApiBase } from '../Implementation/ApiBase';
import { AG_GRID_GROUPED_COLUMN } from '../../Utilities/Constants/GeneralConstants';
import uniq from 'lodash/uniq';
import StringExtensions from '../../Utilities/Extensions/StringExtensions';
export function getAutoRowGroupColumnIdFor(columnId) {
    return `${AG_GRID_GROUPED_COLUMN}-${columnId}`;
}
export class ColumnInternalApi extends ApiBase {
    /**
     * Retrieves 'ColumnType' property for a given Column
     *   isCumulativeAggregate(input: string): boolean {
     *     return !!this.getNodesFromExpression(input, 'CUMUL')?.length;
     *   }
     * @param columnId Column to use
     */
    getAgGridColumnType(columnId) {
        return this._adaptable.getAgGridColumnType(columnId);
    }
    getAllAgGridColumnTypes() {
        let colTypes = [];
        this.getColumnApi()
            .getColumns()
            .forEach((c) => {
            const agGridColType = this.getAgGridColumnType(c.columnId);
            if (Array.isArray(agGridColType)) {
                colTypes.push(...agGridColType);
            }
            else {
                colTypes.push(agGridColType);
            }
        });
        return uniq(colTypes).filter((c) => StringExtensions.IsNotNullOrEmptyOrWhiteSpace(c));
    }
    /**
     * Retrieves a list providing the Friendly Name / Caption for all columns
     */
    getAllColumnFriendlyNames() {
        return this.getColumnApi()
            .getColumns()
            .map((c) => {
            return c.friendlyName;
        });
    }
    getAutoRowGroupColumnIdFor(columnId) {
        return getAutoRowGroupColumnIdFor(columnId);
    }
    /**
     * Retrieves AG Grid's field property for the column
     * @param columnId columnId to look up
     */
    getAgGridColumnFieldForAdaptableColumn(columnId) {
        const agGridColumn = this.getAgGridColumnForAdaptableColumn(columnId);
        return agGridColumn.getColDef().field || '';
    }
    /**
     * Retrieves AG Grid's column for an Adaptable column
     * @param columnId columnId to look up
     */
    getAgGridColumnForAdaptableColumn(columnId) {
        return this._adaptable.getAgGridColumnForColumnId(columnId);
    }
    getActiveColumnComparator(columnId, customSort, customSortComparer) {
        if ((!customSort || customSort?.IsSuspended) && !customSortComparer) {
            // defaults to AG-Grid column definition comparator if no CustomSort is defined&active
            const colDefComparator = this._adaptable.agGridColumnAdapter.getUserColDefProperty(columnId, 'comparator');
            return colDefComparator;
        }
        // CustomSort Comparer function takes precedence over CustomSort SortedValues
        const comparerFunction = customSortComparer
            ? customSortComparer.comparer
            : this.getCustomSortApi().internalApi.getDefaultCustomSortComparer(customSort.ColumnId, customSort.SortedValues);
        return comparerFunction;
    }
    isSpecialColumn(columnId, column = null) {
        if (column) {
            return column.isCalculatedColumn || column.isFreeTextColumn || column.isActionColumn;
        }
        else
            return (this.getColumnApi().isCalculatedColumn(columnId) ||
                this.getColumnApi().isFreeTextColumn(columnId) ||
                this.getColumnApi().isActionColumn(columnId) ||
                this.getColumnApi().isFdc3Column(columnId));
    }
}
