"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ColumnApiImpl = exports.isAutoPivotColumn = exports.isAutoRowGroupColumn = void 0;
const tslib_1 = require("tslib");
const ApiBase_1 = require("./ApiBase");
const GeneralConstants = tslib_1.__importStar(require("../../Utilities/Constants/GeneralConstants"));
const ModuleConstants = tslib_1.__importStar(require("../../Utilities/Constants/ModuleConstants"));
const ColumnInternalApi_1 = require("../Internal/ColumnInternalApi");
const ObjectFactory_1 = require("../../Utilities/ObjectFactory");
const ArrayExtensions_1 = tslib_1.__importDefault(require("../../Utilities/Extensions/ArrayExtensions"));
const logDeprecation_1 = require("../../Utilities/logDeprecation");
const GeneralConstants_1 = require("../../Utilities/Constants/GeneralConstants");
function isAutoRowGroupColumn(columnId) {
    // put this here as there might be other indicators we are not aware of
    // perhaps with non auto groups ?
    //https://www.ag-grid.com/javascript-grid-grouping/
    return columnId === null || columnId === void 0 ? void 0 : columnId.startsWith(GeneralConstants.AG_GRID_GROUPED_COLUMN);
}
exports.isAutoRowGroupColumn = isAutoRowGroupColumn;
function isAutoPivotColumn(columnId) {
    // put this here as there might be other indicators we are not aware of?
    return columnId === null || columnId === void 0 ? void 0 : columnId.startsWith(GeneralConstants.AG_GRID_PIVOT_COLUMN);
}
exports.isAutoPivotColumn = isAutoPivotColumn;
class ColumnApiImpl extends ApiBase_1.ApiBase {
    constructor(_adaptable) {
        super(_adaptable);
        this.internalApi = new ColumnInternalApi_1.ColumnInternalApi(_adaptable);
    }
    getColumns() {
        var _a;
        return (_a = this.getGridApi().getGridState().Columns) !== null && _a !== void 0 ? _a : [];
    }
    getStandardColumns() {
        var _a;
        const cols = (_a = this.getColumns()) === null || _a === void 0 ? void 0 : _a.filter((c) => !this.isActionRowButtonColumn(c.columnId));
        return cols !== null && cols !== void 0 ? cols : [];
    }
    getNonSpecialColumns() {
        var _a;
        const cols = (_a = this.getStandardColumns()) === null || _a === void 0 ? void 0 : _a.filter((c) => !this.getColumnApi().isSpecialColumn(c.columnId));
        return cols !== null && cols !== void 0 ? cols : [];
    }
    getVisibleColumns() {
        const layout = this.getLayoutApi().getCurrentLayout();
        const visibleCols = layout.Columns.reduce((acc, colId) => {
            acc[colId] = true;
            return acc;
        }, {});
        return this.getColumns().filter((c) => visibleCols[c.columnId]);
    }
    selectColumn(columnId) {
        this._adaptable.selectColumn(columnId);
    }
    selectColumns(columnIds) {
        this._adaptable.selectColumns(columnIds);
    }
    addColumnToSelection(columnId) {
        this._adaptable.selectColumn(columnId, { keepExistingSelection: true });
    }
    addColumnsToSelection(columnIds) {
        this._adaptable.selectColumns(columnIds, { keepExistingSelection: true });
    }
    selectAllColumns() {
        this._adaptable.selectAll();
    }
    autosizeColumn(columnId) {
        this._adaptable.autoSizeColumn(columnId);
    }
    autosizeColumns(columnIds) {
        this._adaptable.autoSizeColumns(columnIds);
    }
    autosizeAllColumns() {
        this._adaptable.autoSizeAllColumns();
    }
    hideColumn(columnId) {
        this._adaptable.hideColumn(columnId);
    }
    showColumn(columnId) {
        this._adaptable.showColumn(columnId);
    }
    isAutoRowGroupColumn(columnId) {
        return isAutoRowGroupColumn(columnId);
    }
    isAutoPivotColumn(columnId) {
        return isAutoPivotColumn(columnId);
    }
    isActionRowButtonColumn(columnId) {
        return columnId === GeneralConstants_1.ADAPTABLE_ROW_ACTION_BUTTONS;
    }
    isCalculatedColumn(columnId) {
        return (this.getAdaptableApi()
            .calculatedColumnApi.getCalculatedColumns()
            .find((cc) => cc.ColumnId == columnId) != null);
    }
    isFreeTextColumn(columnId) {
        return (this.getAdaptableApi()
            .freeTextColumnApi.getFreeTextColumns()
            .find((cc) => cc.ColumnId == columnId) != null);
    }
    isActionColumn(columnId) {
        var _a;
        return (((_a = this.getAdaptableApi()
            .actionColumnApi.getActionColumns()) === null || _a === void 0 ? void 0 : _a.find((cc) => cc.columnId == columnId)) != null);
    }
    getColumnWithColumnId(columnId, logWarning) {
        if (columnId == undefined) {
            return;
        }
        // just return null if no columns rather than logging a warning - otherwise get lots at startup
        if (ArrayExtensions_1.default.IsNullOrEmpty(this.getColumns())) {
            return;
        }
        if (this.isAutoPivotColumn(columnId)) {
            return;
        }
        const foundColumn = this.getColumns().find((c) => c.columnId == columnId);
        if (foundColumn) {
            return foundColumn;
        }
        if (logWarning) {
            this.logMissingColumnWarning(columnId);
        }
        return undefined;
    }
    hasNumberDataType(columnId) {
        const column = this.getColumnWithColumnId(columnId);
        if (!column) {
            return false;
        }
        return (column === null || column === void 0 ? void 0 : column.dataType) == 'Number';
    }
    hasBooleanDataType(columnId) {
        const column = this.getColumnWithColumnId(columnId);
        if (!column) {
            return false;
        }
        return (column === null || column === void 0 ? void 0 : column.dataType) == 'Boolean';
    }
    hasArrayDataType(columnId) {
        return this.hasStringArrayDataType(columnId) || this.hasNumericArrayDataType(columnId);
    }
    hasStringArrayDataType(columnId) {
        const column = this.getColumnWithColumnId(columnId);
        if (!column) {
            return false;
        }
        return (column === null || column === void 0 ? void 0 : column.dataType) == 'StringArray';
    }
    hasNumericArrayDataType(columnId) {
        const column = this.getColumnWithColumnId(columnId);
        if (!column) {
            return false;
        }
        return ((column === null || column === void 0 ? void 0 : column.dataType) == 'NumberArray' ||
            (column === null || column === void 0 ? void 0 : column.dataType) == 'TupleNumberArray' ||
            (column === null || column === void 0 ? void 0 : column.dataType) == 'ObjectNumberArray');
    }
    hasDateDataType(columnId) {
        const column = this.getColumnWithColumnId(columnId);
        if (!column) {
            return false;
        }
        return (column === null || column === void 0 ? void 0 : column.dataType) == 'Date';
    }
    getColumnDataTypeForColumnId(columnId) {
        const column = this.getColumnWithColumnId(columnId); // this.getColumns().find(c => c.ColumnId == columnId);
        if (!column) {
            return undefined;
        }
        return column.dataType;
    }
    getFriendlyNameForColumnId(columnId) {
        if (this.isAutoPivotColumn(columnId)) {
            return GeneralConstants.EMPTY_STRING;
        }
        const foundColumn = this.getColumns().find((c) => c.columnId == columnId);
        if (foundColumn) {
            return foundColumn.friendlyName;
        }
        let result = columnId + GeneralConstants.MISSING_COLUMN;
        if (this.isAutoRowGroupColumn(columnId)) {
            const currentLayout = this.getLayoutApi().getCurrentLayout();
            if (currentLayout === null || currentLayout === void 0 ? void 0 : currentLayout.RowGroupedColumns) {
                result = `[Grouped column: ${currentLayout.RowGroupedColumns.join(', ')}]`;
            }
        }
        this.logMissingColumnWarning(columnId);
        return result;
    }
    doesColumnExist(columnId) {
        const foundColumn = this.getColumns().find((c) => c.columnId == columnId);
        return foundColumn ? true : false;
    }
    getFriendlyNamesForColumnIds(columnIds) {
        const friendlyNames = [];
        if (ArrayExtensions_1.default.IsNullOrEmpty(columnIds)) {
            return friendlyNames;
        }
        columnIds.forEach((c) => {
            friendlyNames.push(this.getFriendlyNameForColumnId(c));
        });
        return friendlyNames;
    }
    getColumnIdForFriendlyName(friendlyName) {
        if (friendlyName.includes(GeneralConstants.MISSING_COLUMN)) {
            return friendlyName.replace(GeneralConstants.MISSING_COLUMN, ''); // Ids should stay "pure"
        }
        const foundColumn = this.getColumns().find((c) => c.friendlyName == friendlyName);
        if (foundColumn) {
            return foundColumn.columnId;
        }
        this.logMissingColumnWarning(friendlyName);
        return friendlyName + GeneralConstants.MISSING_COLUMN;
    }
    getColumnIdsForFriendlyNames(friendlyNames) {
        const columnIds = [];
        if (ArrayExtensions_1.default.IsNullOrEmpty(friendlyNames)) {
            return columnIds;
        }
        friendlyNames.forEach((c) => {
            columnIds.push(this.getColumnIdForFriendlyName(c));
        });
        return columnIds;
    }
    getColumnsWithFriendlyNames(friendlyNames) {
        return friendlyNames
            .map((friendlyName) => this.getColumns().find((x) => x.friendlyName == friendlyName))
            .filter(Boolean);
    }
    getColumnsWithColumnIds(columnIds, logWarning = true) {
        let returnCols = [];
        columnIds.forEach((c) => {
            const column = this.getColumnWithColumnId(c, logWarning);
            if (column) {
                returnCols.push(column);
            }
        });
        return returnCols;
    }
    isColumnInGrid(columnId) {
        return !!this.getColumnWithColumnId(columnId, false);
    }
    getColumnWithFriendlyName(columnFriendlyName, logWarning = true) {
        // just return null if no columns rather than logging a warning - otherwise get lots at startup
        if (ArrayExtensions_1.default.IsNullOrEmpty(this.getColumns())) {
            return null;
        }
        const foundColumn = this.getColumns().find((c) => c.friendlyName == columnFriendlyName);
        if (foundColumn) {
            return foundColumn;
        }
        if (logWarning) {
            this.logMissingColumnWarning(columnFriendlyName);
        }
        return null;
    }
    getColumnsOfType(dataType) {
        switch (dataType) {
            case 'Boolean':
                return this.getBooleanColumns();
            case 'Date':
                return this.getDateColumns();
            case 'Number':
                return this.getNumericColumns();
            case 'String':
                return this.getStringColumns();
            case 'StringArray':
                return this.getStringArrayColumns();
            case 'NumberArray':
                return this.getNumberArrayColumns();
            case 'ObjectNumberArray':
                return this.getObjectNumberArrayColumns();
            case 'TupleNumberArray':
                return this.getTupleNumberArrayColumns();
            default:
                return this.getColumns();
        }
    }
    getNumericColumns() {
        return this.getColumns().filter((c) => c.dataType == 'Number');
    }
    getNumericArrayColumns() {
        return this.getNumberArrayColumns()
            .concat(this.getTupleNumberArrayColumns())
            .concat(this.getObjectNumberArrayColumns());
    }
    getNumberArrayColumns() {
        return this.getColumns().filter((c) => c.dataType == 'NumberArray');
    }
    getTupleNumberArrayColumns() {
        return this.getColumns().filter((c) => c.dataType == 'TupleNumberArray');
    }
    getObjectNumberArrayColumns() {
        return this.getColumns().filter((c) => c.dataType == 'ObjectNumberArray');
    }
    getStringColumns() {
        return this.getColumns().filter((c) => c.dataType == 'String');
    }
    getStringArrayColumns() {
        return this.getColumns().filter((c) => c.dataType == 'StringArray');
    }
    getDateColumns() {
        return this.getColumns().filter((c) => c.dataType == 'Date');
    }
    getBooleanColumns() {
        return this.getColumns().filter((c) => c.dataType == 'Boolean');
    }
    getArrayColumns() {
        return this.getColumns().filter((c) => c.dataType == 'NumberArray' ||
            c.dataType == 'TupleNumberArray' ||
            c.dataType == 'ObjectNumberArray' ||
            c.dataType == 'StringArray');
    }
    getSortableColumns() {
        return this.getColumns().filter((c) => c.sortable);
    }
    getFilterableColumns() {
        return this.getColumns().filter((c) => c.filterable);
    }
    getGroupableColumns() {
        return this.getColumns().filter((c) => c.groupable);
    }
    getPivotableColumns() {
        return this.getColumns().filter((c) => c.pivotable);
    }
    getAggregatableColumns() {
        return this.getColumns().filter((c) => c.aggregatable && c.dataType == 'Number');
    }
    getQueryableColumns() {
        return this.getColumns().filter((c) => c.queryable);
    }
    getExportableColumns() {
        return this.getColumns().filter((c) => c.exportable);
    }
    logMissingColumnWarning(columnId) {
        if (this.getColumnOptions().showMissingColumnsWarning === true) {
            if (!this.isAutoRowGroupColumn(columnId) &&
                !this.isCalculatedColumn(columnId) &&
                !this.isFreeTextColumn(columnId) &&
                !this.isActionColumn(columnId)) {
                this.logWarn(`No column found named '${columnId}'`);
            }
        }
    }
    getPrimaryKeyColumn() {
        return this.getColumnWithColumnId(this.getOptions().primaryKey);
    }
    getDefaultAggFunc(columnId) {
        var _a, _b, _c;
        const abColumn = this.getColumnWithColumnId(columnId);
        const availableAggregationFunctions = abColumn.availableAggregationFunctions;
        const agColumn = (_b = (_a = this.internalApi.getAgGridColumnForAdaptableColumn(columnId)) === null || _a === void 0 ? void 0 : _a.getColDef) === null || _b === void 0 ? void 0 : _b.call(_a);
        return (_c = agColumn === null || agColumn === void 0 ? void 0 : agColumn.defaultAggFunc) !== null && _c !== void 0 ? _c : availableAggregationFunctions[0];
    }
    isSpecialColumn(columnId) {
        return (this.isCalculatedColumn(columnId) ||
            this.isFreeTextColumn(columnId) ||
            this.isActionColumn(columnId));
    }
    openColumnInfoSettingsPanel() {
        this.showModulePopup(ModuleConstants.ColumnInfoModuleId);
    }
    getColumnTypes() {
        const columnTypes = this.getOptions().columnOptions.columnTypes;
        if (typeof columnTypes === 'function') {
            return columnTypes((0, ObjectFactory_1.createBaseContext)(this.getAdaptableApi()));
        }
        else {
            return columnTypes;
        }
    }
    getColumnsByColumnType(columnType) {
        const abColumns = this.getColumns();
        return abColumns.filter((c) => {
            var _a;
            return ((_a = c.columnTypes) !== null && _a !== void 0 ? _a : []).includes(columnType);
        });
    }
    getRowGroupedColumns() {
        var _a;
        return (_a = this.getLayoutApi()
            .getCurrentRowGroupsColumnIds()) === null || _a === void 0 ? void 0 : _a.map((n) => {
            return this.getColumnWithColumnId(n);
        });
    }
    getDistinctDisplayValuesForColumn(columnId) {
        return this.getGridApi().internalApi.getDistinctDisplayValuesForColumn(columnId);
    }
    setColumnCaption(columnId, caption) {
        return this.getLayoutApi().setColumnCaption(columnId, caption);
    }
    updateColumnConfiguration(columnConfig) {
        (0, logDeprecation_1.logDeprecationExternal)(this.getAdatableLogger(), 'ColumnApi', 'updateColumnConfiguration', 'GridApi', 'updateAgGridColumnState');
        this.getGridApi().updateAgGridColumnState(columnConfig);
    }
    updateColumnConfigurations(columnConfigs) {
        (0, logDeprecation_1.logDeprecationExternal)(this.getAdatableLogger(), 'ColumnApi', 'updateColumnConfigurations', 'GridApi', 'updateAgGridColumnStates');
        this.getGridApi().updateAgGridColumnStates(columnConfigs);
    }
    setColumnDefinitions(columnDefinitions) {
        (0, logDeprecation_1.logDeprecationExternal)(this.getAdatableLogger(), 'ColumnApi', 'setColumnDefinitions', 'GridApi', 'setAgGridColumnDefinitions');
        this.getGridApi().setAgGridColumnDefinitions(columnDefinitions);
    }
}
exports.ColumnApiImpl = ColumnApiImpl;
