"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GridApiImpl = void 0;
const tslib_1 = require("tslib");
const ApiBase_1 = require("./ApiBase");
const GridRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/GridRedux"));
const SystemRedux_1 = require("../../Redux/ActionsReducers/SystemRedux");
const ModuleConstants = tslib_1.__importStar(require("../../Utilities/Constants/ModuleConstants"));
const GridInternalApi_1 = require("../Internal/GridInternalApi");
const ArrayExtensions_1 = tslib_1.__importDefault(require("../../Utilities/Extensions/ArrayExtensions"));
const windowFactory_1 = require("../../View/Components/Popups/WindowPopups/windowFactory");
const RowSummary_1 = require("../../PredefinedConfig/Common/RowSummary");
const logDeprecation_1 = require("../../Utilities/logDeprecation");
class GridApiImpl extends ApiBase_1.ApiBase {
    constructor(_adaptable) {
        super(_adaptable);
        this.internalApi = new GridInternalApi_1.GridInternalApi(_adaptable);
    }
    getVariant() {
        return this.getAdaptableVariant();
    }
    getGridState() {
        return this.getAdaptableState().Grid;
    }
    loadGridData(dataSource) {
        // we intentionally set an initial empty array,
        // to prevent `cellchanged` events from being triggered for rows that have
        // the same primary key with existing rows
        // see #testprevent_cellchanged_event_on_load_grid_data
        this._adaptable.agGridAdapter.setGridOption('rowData', []);
        this._adaptable.setGridData(dataSource);
        const allRowNodes = this.getAllRowNodes();
        this.internalApi.fireGridDataChangedEvent(dataSource, allRowNodes, 'Load');
    }
    getGridData() {
        return this._adaptable.getGridData();
    }
    getFilteredData() {
        return this._adaptable.getFilteredData();
    }
    getVisibleData() {
        const data = [];
        this.getAdaptableInternalApi().forAllVisibleRowNodesDo((rowNode) => {
            if (!this.isGroupRowNode(rowNode)) {
                data.push(rowNode.data);
            }
        });
        return data;
    }
    async updateGridData(dataRows, dataUpdateConfig) {
        const rowNodes = await this._adaptable.updateRows(dataRows, dataUpdateConfig);
        this.internalApi.fireGridDataChangedEvent(dataRows, rowNodes, 'Update');
        return rowNodes;
    }
    async addOrUpdateGridData(dataRows, dataUpdateConfig) {
        const { added, updated } = await this._adaptable.addOrUpdateRows(dataRows, dataUpdateConfig);
        if (ArrayExtensions_1.default.IsNotNullOrEmpty(updated)) {
            this.internalApi.fireGridDataChangedEvent(dataRows, updated, 'Update');
        }
        if (ArrayExtensions_1.default.IsNotNullOrEmpty(added)) {
            this.internalApi.fireGridDataChangedEvent(dataRows, added, 'Add');
        }
        return {
            addedRows: added,
            updatedRows: updated,
        };
    }
    async addGridData(dataRows, dataUpdateConfig) {
        const rowNodes = await this._adaptable.addRows(dataRows, dataUpdateConfig);
        this.internalApi.fireGridDataChangedEvent(dataRows, rowNodes, 'Add');
        return rowNodes;
    }
    undoCellEdit(cellDataChangedInfo) {
        // for the reason of this hacky solution see the comments in DataService
        this.getAdaptableInternalApi().getDataService().logUndoChange(cellDataChangedInfo);
        const cellUpdateRequest = {
            columnId: cellDataChangedInfo.column.columnId,
            newValue: cellDataChangedInfo.oldValue,
            primaryKeyValue: cellDataChangedInfo.primaryKeyValue,
            rowNode: cellDataChangedInfo.rowNode,
        };
        this.setCellValue(cellUpdateRequest);
        return true;
    }
    async deleteGridData(dataRows, dataUpdateConfig) {
        if (this.checkArrayExists(dataRows)) {
            const rowNodes = await this._adaptable.deleteRows(dataRows, dataUpdateConfig);
            this.internalApi.fireGridDataChangedEvent(dataRows, rowNodes, 'Delete');
            return rowNodes;
        }
        return [];
    }
    setCellValue(cellUpdateRequest) {
        var _a;
        const abColumn = this.getColumnApi().getColumnWithColumnId(cellUpdateRequest.columnId);
        if (!abColumn) {
            this.logWarn(`setCellValue() - column not found for columnId: ${cellUpdateRequest.columnId}`);
            return;
        }
        const rowNode = (_a = cellUpdateRequest.rowNode) !== null && _a !== void 0 ? _a : this.getRowNodeForPrimaryKey(cellUpdateRequest.primaryKeyValue);
        if (!rowNode) {
            this.logWarn(`setCellValue() - rowNode not found for primaryKeyValue: ${cellUpdateRequest.primaryKeyValue}`);
            return;
        }
        this._adaptable.setDataValue(cellUpdateRequest.newValue, abColumn, cellUpdateRequest.primaryKeyValue, rowNode);
        this.refreshCells([rowNode], [abColumn.columnId]);
    }
    setCellValues(cellUpdateRequests) {
        cellUpdateRequests === null || cellUpdateRequests === void 0 ? void 0 : cellUpdateRequests.forEach((cellUpdateRequest) => this.setCellValue(cellUpdateRequest));
    }
    getSelectedCellInfo() {
        return this.getGridState().SelectedCellInfo;
    }
    getSelectedRowInfo() {
        return this.getGridState().SelectedRowInfo;
    }
    getCellDisplayValue(primaryKeyValue, columnId) {
        const rowNode = this.getRowNodeForPrimaryKey(primaryKeyValue);
        return rowNode ? this.getDisplayValueFromRowNode(rowNode, columnId) : undefined;
    }
    getCellRawValue(primaryKeyValue, columnId) {
        const rowNode = this.getRowNodeForPrimaryKey(primaryKeyValue);
        return rowNode ? this.getRawValueFromRowNode(rowNode, columnId) : undefined;
    }
    getCellNormalisedValue(primaryKeyValue, columnId) {
        const rowNode = this.getRowNodeForPrimaryKey(primaryKeyValue);
        return rowNode ? this.getNormalisedValueFromRowNode(rowNode, columnId) : undefined;
    }
    hideFilterForm() {
        this._adaptable.hideColumnFilterForm();
    }
    applyGridFiltering() {
        this._adaptable.applyColumnFiltering();
    }
    clearGridFiltering() {
        // slightly round the houses but we have to call ColumnFilterAPI as it does it properly
        this.getAdaptableApi().columnFilterApi.clearColumnFilters();
    }
    getColumnSorts() {
        return this.getAdaptableState().Grid.ColumnSorts;
    }
    getColumnSortForColumn(columnId) {
        let columnSorts = this.getColumnSorts();
        return columnSorts === null || columnSorts === void 0 ? void 0 : columnSorts.find((cs) => cs.ColumnId == columnId);
    }
    setAdaptableSorting(columnSorts) {
        this._adaptable.setColumnSort(columnSorts);
        this.setColumnSorts(columnSorts);
    }
    clearAdaptableSorting() {
        this._adaptable.clearColumnSort();
        this.clearColumnSorts();
    }
    setColumnSorts(columnSorts) {
        this.dispatchAction(GridRedux.GridSetSort(columnSorts));
    }
    clearColumnSorts() {
        this.dispatchAction(GridRedux.GridClearSort());
    }
    selectRow(primaryKeyValue, clearSelection) {
        this.selectNode(this.getRowNodeForPrimaryKey(primaryKeyValue), clearSelection);
    }
    selectRows(primaryKeyValues, clearSelection) {
        let nodes = [];
        primaryKeyValues.forEach((pkValue) => {
            nodes.push(this.getRowNodeForPrimaryKey(pkValue));
        });
        this.selectNodes(nodes, clearSelection);
    }
    selectNode(rowNode, clearSelection) {
        this._adaptable.selectNode(rowNode, clearSelection);
    }
    selectNodes(rowNodes, clearSelection) {
        this._adaptable.selectNodes(rowNodes, clearSelection);
    }
    deSelectRow(primaryKeyValue, clearSelection) {
        this._adaptable.deSelectNode(this.getRowNodeForPrimaryKey(primaryKeyValue), clearSelection);
    }
    deSelectRows(primaryKeyValues, clearSelection) {
        let nodes = [];
        primaryKeyValues.forEach((pkValue) => {
            nodes.push(this.getRowNodeForPrimaryKey(pkValue));
        });
        this.deSelectNodes(nodes, clearSelection);
    }
    deSelectNode(rowNode, clearSelection) {
        this._adaptable.deSelectNode(rowNode, clearSelection);
    }
    deSelectNodes(rowNodes, clearSelection) {
        this._adaptable.deSelectNodes(rowNodes, clearSelection);
    }
    getSelectionStartEndNodes(gridCellRange) {
        let startNode;
        let endNode;
        startNode = gridCellRange.primaryKeyValueEnd
            ? this._adaptable.getRowNodeForPrimaryKey(gridCellRange.primaryKeyValueStart)
            : this._adaptable.getRowNodeByIndex(gridCellRange.rowIndexStart);
        if (gridCellRange.primaryKeyValueEnd) {
            endNode = this._adaptable.getRowNodeForPrimaryKey(gridCellRange.primaryKeyValueEnd);
        }
        else if (gridCellRange.rowIndexEnd) {
            endNode = this._adaptable.getRowNodeByIndex(gridCellRange.rowIndexEnd);
        }
        else {
            endNode = startNode;
        }
        return [startNode, endNode];
    }
    selectCellRange(gridCellRange, clearSelection) {
        if (gridCellRange == undefined) {
            return;
        }
        if (gridCellRange.primaryKeyValueStart == undefined &&
            gridCellRange.rowIndexStart == undefined) {
            return;
        }
        const [startNode, endNode] = this.getSelectionStartEndNodes(gridCellRange);
        if (startNode && endNode) {
            this._adaptable.selectCells(gridCellRange.columnIds, startNode, endNode, clearSelection);
        }
    }
    selectCellRangeByQuery(query, gridCellRange, clearSelection) {
        const filteredRowNodes = [];
        const isRowNodeInQuery = (rowNode) => this.getAdaptableApi()
            .internalApi.getQueryLanguageService()
            .evaluateBooleanExpression(query, 'GridInfo', rowNode);
        if (gridCellRange) {
            const [startNode, endNode] = this.getSelectionStartEndNodes(gridCellRange);
            for (let rowIndex = startNode.rowIndex; rowIndex <= endNode.rowIndex; rowIndex++) {
                const rowNode = this._adaptable.getRowNodeByIndex(rowIndex);
                isRowNodeInQuery(rowNode) && filteredRowNodes.push(rowNode);
            }
        }
        else {
            // include all row
            this._adaptable.forAllRowNodesDo((rowNode) => {
                isRowNodeInQuery(rowNode) && filteredRowNodes.push(rowNode);
            });
        }
        if (!filteredRowNodes.length) {
            return;
        }
        let preapredGridCellRange = gridCellRange;
        if (!preapredGridCellRange) {
            /**
             * Select all rows and all columns
             */
            const currentLayout = this.getLayoutApi().getCurrentLayout();
            preapredGridCellRange = {
                columnIds: currentLayout.Columns,
            };
        }
        // ranges of row with consecutive indexes
        const rowRanges = filteredRowNodes.reduce((acc, rowNode, index, collection) => {
            // add start if there is none
            const prevRange = acc[acc.length - 1];
            if (prevRange.startNode === undefined) {
                prevRange.startNode = rowNode;
            }
            // add end if next is not consecutive
            const nextNode = collection[index + 1];
            if (nextNode && nextNode.rowIndex - 1 !== rowNode.rowIndex) {
                prevRange.endNode = rowNode;
                // start a new one
                acc.push({});
            }
            // if at the end close last range
            if (index === collection.length - 1) {
                prevRange.endNode = rowNode;
            }
            return acc;
        }, [{}]);
        let preparedClearSelection = clearSelection;
        rowRanges.forEach((range) => {
            // clear only on first range selection
            this._adaptable.selectCells(preapredGridCellRange.columnIds, range.startNode, range.endNode, preparedClearSelection);
            preparedClearSelection = false;
        });
    }
    selectColumn(columnId) {
        this.selectColumns([columnId]);
    }
    selectColumns(columnIds) {
        this.getColumnApi().selectColumns(columnIds);
    }
    getFirstRowNode() {
        return this._adaptable.getFirstRowNode();
    }
    getFirstDisplayedRowNode() {
        return this._adaptable.getFirstDisplayedRowNode();
    }
    getVisibleRowNodes(config) {
        const rowNodes = [];
        this.getAdaptableInternalApi().forAllVisibleRowNodesDo((rowNode) => rowNodes.push(rowNode), config);
        return rowNodes;
    }
    getAllRowNodes(config) {
        return this._adaptable.getAllRowNodes(config);
    }
    getGroupRowNodes(config) {
        return this._adaptable.getGroupRowNodes(config);
    }
    getGridCellFromRowNode(rowNode, columnId) {
        return this._adaptable.getGridCellFromRowNode(rowNode, columnId);
    }
    getRawValueFromRowNode(rowNode, columnId) {
        return this._adaptable.getRawValueFromRowNode(rowNode, columnId);
    }
    getDisplayValueFromRowNode(rowNode, columnId) {
        return this._adaptable.getDisplayValueFromRowNode(rowNode, columnId);
    }
    getDisplayValueFromRawValue(rowNode, columnId, rawValue) {
        return this._adaptable.getDisplayValueFromRawValue(rowNode, columnId, rawValue);
    }
    getNormalisedValueFromRowNode(rowNode, columnId) {
        const rawValue = this._adaptable.getRawValueFromRowNode(rowNode, columnId);
        const abColumn = this.getColumnApi().getColumnWithColumnId(columnId);
        return this._adaptable.getNormalisedValueFromRawValue(rawValue, abColumn);
    }
    getRowNodesForPrimaryKeys(primaryKeyValues) {
        return this._adaptable.getRowNodesForPrimaryKeys(primaryKeyValues);
    }
    getRowNodeForPrimaryKey(primaryKeyValue) {
        return this._adaptable.getRowNodeForPrimaryKey(primaryKeyValue);
    }
    getPrimaryKeyValueForRowNode(rowNode) {
        return this._adaptable.getPrimaryKeyValueFromRowNode(rowNode);
    }
    getRowNodeForIndex(index) {
        return this._adaptable.getRowNodeByIndex(index);
    }
    getPrimaryKeyValuesForRowNodes(rowNodes) {
        return rowNodes.map((rowNode) => this.getPrimaryKeyValueForRowNode(rowNode));
    }
    setRowGroupColumns(columnIds) {
        this._adaptable.setRowGroupColumns(columnIds);
    }
    clearRowGroupColumns() {
        this._adaptable.clearRowGroupColumns();
    }
    expandAllRowGroups() {
        this._adaptable.expandAllRowGroups();
    }
    collapseAllRowGroups() {
        this._adaptable.collapseAllRowGroups();
    }
    getExpandRowGroupsKeys() {
        return this._adaptable.getExpandRowGroupsKeys();
    }
    expandRowGroupsForValues(columnValues) {
        this._adaptable.expandRowGroupsForValues(columnValues);
    }
    isGridPivotable() {
        return !this.getAdaptableInternalApi().isGridInTreeMode();
    }
    isGridGroupable() {
        return !this.getAdaptableInternalApi().isGridInTreeMode();
    }
    isGridRowSelectable() {
        return this._adaptable.isGridRowSelectable();
    }
    isGridRangeSelectable() {
        return this._adaptable.isGridRangeSelectable();
    }
    isGridRowGrouped() {
        return this._adaptable.isGridGroupingActive();
    }
    isGridInPivotMode() {
        return this.getLayoutApi().isCurrentLayoutPivot();
    }
    isGroupRowNode(rowNode) {
        return this._adaptable.isGroupRowNode(rowNode);
    }
    isSummaryNode(rowNode) {
        var _a;
        return !!((_a = rowNode === null || rowNode === void 0 ? void 0 : rowNode.data) === null || _a === void 0 ? void 0 : _a[RowSummary_1.ROW_SUMMARY_ROW_ID]);
    }
    isQuickFilterAvailable() {
        return this._adaptable.isQuickFilterAvailable();
    }
    redrawGrid() {
        this._adaptable.redrawBody();
        this._adaptable.refreshHeader();
    }
    getGridCellsForRawValue(columnId, rawValue) {
        const gridCells = this._adaptable.getGridCellsForColumn(columnId);
        if (!gridCells) {
            return undefined;
        }
        const returnValues = [];
        gridCells.forEach((gc) => {
            if (gc.rawValue === rawValue) {
                returnValues.push(gc);
            }
        });
        return returnValues;
    }
    getCellRawValueCount(columnId, rawValue) {
        const gridCells = this.getGridCellsForRawValue(columnId, rawValue);
        return gridCells === null || gridCells === void 0 ? void 0 : gridCells.length;
    }
    getGridCellsForDisplayValue(columnId, displayValue) {
        const gridCells = this._adaptable.getGridCellsForColumn(columnId);
        if (!gridCells) {
            return undefined;
        }
        const returnGridCells = [];
        gridCells.forEach((gc) => {
            if (gc.displayValue === displayValue) {
                returnGridCells.push(gc);
            }
        });
        return returnGridCells;
    }
    jumpToRow(primaryKeyValue) {
        const node = this._adaptable.getRowNodeForPrimaryKey(primaryKeyValue);
        this._adaptable.jumpToRow(node);
    }
    jumpToColumn(columnId) {
        this._adaptable.jumpToColumn(columnId);
    }
    jumpToCell(primaryKeyValue, columnId) {
        const node = this._adaptable.getRowNodeForPrimaryKey(primaryKeyValue);
        this._adaptable.jumpToCell(columnId, node);
    }
    highlightCell(cellHighlightInfo) {
        this.dispatchAction((0, SystemRedux_1.SystemHighlightCellAdd)(cellHighlightInfo));
        if (cellHighlightInfo.timeout) {
            setTimeout(() => {
                this.unHighlightCell(cellHighlightInfo.primaryKeyValue, cellHighlightInfo.columnId);
            }, cellHighlightInfo.timeout);
        }
    }
    unHighlightCell(primaryKeyValue, columnId) {
        this.dispatchAction((0, SystemRedux_1.SystemHighlightCellDelete)(primaryKeyValue, columnId));
    }
    unHighlightAllCells() {
        this.dispatchAction((0, SystemRedux_1.SystemHighlightCellDeleteAll)());
    }
    highlightRow(rowHighlightInfo) {
        this.dispatchAction((0, SystemRedux_1.SystemHighlightRowAdd)(rowHighlightInfo));
        if (rowHighlightInfo.timeout) {
            setTimeout(() => {
                this.unHighlightRow(rowHighlightInfo.primaryKeyValue);
            }, rowHighlightInfo.timeout);
        }
    }
    highlightRows(rowHighlightInfos) {
        this.dispatchAction((0, SystemRedux_1.SystemHighlightRowsAdd)(rowHighlightInfos));
        if (rowHighlightInfos.timeout) {
            setTimeout(() => {
                this.unHighlightRows(rowHighlightInfos.primaryKeyValues);
            }, rowHighlightInfos.timeout);
        }
    }
    unHighlightRow(primaryKeyValue) {
        this.dispatchAction((0, SystemRedux_1.SystemHighlightRowDelete)(primaryKeyValue));
    }
    unHighlightRows(primaryKeyValues) {
        this.dispatchAction((0, SystemRedux_1.SystemHighlightRowsDelete)(primaryKeyValues));
    }
    unHighlightAllRows() {
        this.dispatchAction((0, SystemRedux_1.SystemHighlightRowDeleteAll)());
    }
    refreshCells(rowNodes, columnIds, suppressFlash = true) {
        this._adaptable.refreshCells(rowNodes, columnIds, suppressFlash);
    }
    refreshAllCells(forceUpdate = false) {
        this._adaptable.refreshAllCells(forceUpdate);
    }
    refreshGridCell(gridCell) {
        this.refreshCells([gridCell.rowNode], [gridCell.column.columnId]);
    }
    refreshGridCells(gridCells) {
        gridCells.forEach((gc) => {
            this.refreshGridCell(gc);
        });
    }
    refreshColumn(columnId) {
        this._adaptable.refreshCells(null, [columnId], true);
    }
    refreshColumns(columnIds) {
        this._adaptable.refreshCells(null, columnIds, true);
    }
    refreshRowByPrimaryKey(primaryKey) {
        const rowNode = this.getRowNodeForPrimaryKey(primaryKey);
        this.refreshRowNode(rowNode);
    }
    refreshRowNode(rowNode) {
        this._adaptable.redrawRow(rowNode);
    }
    refreshRowNodes(rowNodes) {
        this._adaptable.redrawRows(rowNodes);
    }
    refreshGroupRowNodes() {
        // see https://www.ag-grid.com/javascript-data-grid/client-side-row-stages/#refreshing-the-client-side-model
        this.getAgGridApi().refreshClientSideRowModel('group');
        this._adaptable.updateRowGroupsExpandedState();
    }
    isCellEditable(gridCell) {
        // Note if cell.editable in AG Grid is a function (and nothing is provided in Adaptble) this will return true.
        // If not Grid Cell or Column then return false - GridCell.column may be undefined for cells from synthetic columns created by AG Grid (ex. autoGroup columns)
        if (!gridCell || !gridCell.column || !gridCell.rowNode) {
            return false;
        }
        if (gridCell.rowNode && this.isSummaryNode(gridCell.rowNode)) {
            return false;
        }
        // checking `gridCell.column.readOnly` might return misleading results
        // because cell editability is defined by EditOptions.isCellEditable() as well
        const agGridColumn = this.getColumnApi().internalApi.getAgGridColumnForAdaptableColumn(gridCell.column.columnId);
        // this will invoke the colDef.editable callback
        // there we check both Adaptable and AG Grid editability
        return agGridColumn === null || agGridColumn === void 0 ? void 0 : agGridColumn.isCellEditable(gridCell.rowNode);
    }
    isCellEdited(gridCell) {
        if (!gridCell) {
            return false;
        }
        const cellDataChangedInfo = this.getDataChangeHistoryApi().getDataChangeForGridCell(gridCell);
        return cellDataChangedInfo != null;
    }
    isEveryCellEditable(gridCells) {
        for (let gridCell of gridCells) {
            if (!this.isCellEditable(gridCell)) {
                return false;
            }
        }
        return true;
    }
    getRowCount() {
        return this._adaptable.getRowCount();
    }
    getVisibleRowCount() {
        return this._adaptable.getVisibleRowCount();
    }
    getRowsInViewport() {
        return this._adaptable.getRowsInViewport();
    }
    getColumnCount() {
        return this._adaptable.getColumnCount();
    }
    getVisibleColumnCount() {
        return this._adaptable.getVisibleColumnCount();
    }
    selectAll() {
        this._adaptable.selectAll();
    }
    deselectAll() {
        this._adaptable.deselectAll();
    }
    destroy() {
        (0, logDeprecation_1.logDeprecationExternal)(this._adaptable.logger, 'GridApi', 'destroy', 'AdaptableApi', 'destroy');
        this.getAdaptableApi().destroy();
    }
    getGridContainerElement() {
        return this._adaptable.getAgGridContainerElement();
    }
    openGridInfoSettingsPanel() {
        this.showModulePopup(ModuleConstants.GridInfoModuleId);
    }
    getAgGridRowModelType() {
        return this._adaptable.getAgGridRowModelType();
    }
    showTransposedView(transposeConfig = {}) {
        var _a, _b, _c, _d, _e;
        const transposedColumnId = (_a = transposeConfig.transposedColumnId) !== null && _a !== void 0 ? _a : this.getAdaptableApi().optionsApi.getPrimaryKey();
        const hideTransposedColumn = (_b = transposeConfig.hideTransposedColumn) !== null && _b !== void 0 ? _b : true;
        const visibleColumns = (_c = transposeConfig.visibleColumns) !== null && _c !== void 0 ? _c : false;
        const visibleRows = (_d = transposeConfig.visibleRows) !== null && _d !== void 0 ? _d : false;
        const autosize = (_e = transposeConfig.autosize) !== null && _e !== void 0 ? _e : true;
        this.getAdaptableInternalApi().showPopupWindow({
            id: windowFactory_1.WINDOW_SHOW_TRANSPOSED_VIEW,
            factoryId: windowFactory_1.WINDOW_SHOW_TRANSPOSED_VIEW,
            title: 'Transposed View',
            icon: 'grid',
            popupProps: {
                transposedColumnId,
                hideTransposedColumn,
                visibleColumns,
                visibleRows,
                autosize,
            },
        });
    }
    getAllAgGridColumns() {
        return this._adaptable.getAllGridColumns();
    }
    updateAgGridColumnState(columnState) {
        const columnExists = this.getColumnApi().doesColumnExist(columnState.colId);
        if (!columnExists) {
            this.logWarn(`Column with id ${columnState.colId} does not exist, could NOT update configuration`);
            return;
        }
        const agGridApi = this._adaptable.agGridAdapter.getAgGridApi();
        agGridApi.applyColumnState({
            state: [columnState],
        });
    }
    updateAgGridColumnStates(columnStates) {
        const existingColumnStates = columnStates.filter((cc) => this.getColumnApi().doesColumnExist(cc.colId));
        const notExistingColumnIds = columnStates
            .filter((cc) => !this.getColumnApi().doesColumnExist(cc.colId))
            .map((cc) => cc.colId);
        notExistingColumnIds.forEach((colId) => {
            this.logWarn(`Column with id ${colId} does not exist, could NOT update configuration`);
        });
        const agGridApi = this._adaptable.agGridAdapter.getAgGridApi();
        agGridApi.applyColumnState({
            state: existingColumnStates,
        });
    }
    setAgGridColumnDefinitions(columnDefinitions) {
        const agGridApi = this._adaptable.agGridAdapter.getAgGridApi();
        agGridApi.setGridOption('columnDefs', columnDefinitions);
        this._adaptable.updateColumnModelAndRefreshGrid({ skipColDefsRefresh: true });
    }
    updateAgGridColumnDefinition(columnDefinitionToBeUpdated) {
        const currentColDefs = [...this._adaptable.agGridAdapter.getAgGridApi().getColumnDefs()];
        const columnId = columnDefinitionToBeUpdated.colId;
        const doesColumnExist = this.getColumnApi().doesColumnExist(columnId);
        if (!doesColumnExist) {
            this.logWarn(`Column with id ${columnId} does not exist, will add it instead!`);
            const newColDefs = [...currentColDefs, columnDefinitionToBeUpdated];
            this.setAgGridColumnDefinitions(newColDefs);
            return;
        }
        const updatedColDefs = this._adaptable.agGridAdapter.traverseColDefs(currentColDefs, (colDef) => {
            return colDef.colId === columnId ? Object.assign(Object.assign({}, colDef), columnDefinitionToBeUpdated) : colDef;
        });
        this.setAgGridColumnDefinitions(updatedColDefs);
    }
    updateAgGridColumnDefinitions(columnDefinitionsToBeUpdated) {
        const currentColDefs = [...this._adaptable.agGridAdapter.getAgGridApi().getColumnDefs()];
        const updatedColDefs = this._adaptable.agGridAdapter.traverseColDefs(currentColDefs, (colDef) => {
            const newColDef = newColumnDefinitions.find((c) => c.colId === colDef.colId);
            return newColDef ? Object.assign(Object.assign({}, colDef), newColDef) : colDef;
        });
        // find out new columns which are not in the current column definitions
        const currentColIds = [];
        this._adaptable.agGridAdapter.traverseColDefs(currentColDefs, (colDef) => {
            currentColIds.push(colDef.colId);
            return colDef;
        });
        const newColumnDefinitions = columnDefinitionsToBeUpdated.filter((c) => !currentColIds.includes(c.colId));
        if (newColumnDefinitions.length) {
            this.logWarn(`Columns with ids ${newColumnDefinitions
                .map((c) => c.colId)
                .join(', ')} do not exist, will add them instead!`);
        }
        this.setAgGridColumnDefinitions([...updatedColDefs, ...newColumnDefinitions]);
    }
    removeAgGridColumnDefinition(columnId) {
        const doesColumnExist = this.getColumnApi().doesColumnExist(columnId);
        if (!doesColumnExist) {
            this.logWarn(`Column with id ${columnId} does not exist!`);
            return;
        }
        const currentColDefs = this._adaptable.agGridAdapter.getAgGridApi().getColumnDefs();
        const updatedColDefs = this._adaptable.agGridAdapter
            .traverseColDefs(currentColDefs, (colDef) => {
            return colDef.colId === columnId ? null : colDef;
        })
            .filter(Boolean);
        this.setAgGridColumnDefinitions(updatedColDefs);
    }
    addAgGridColumnDefinition(newColumnDefinition) {
        const currentColDefs = this._adaptable.agGridAdapter.getAgGridApi().getColumnDefs();
        // just in case check of there is not already an existing column with the same id and eliminate it
        const sanitizedColDefs = this._adaptable.agGridAdapter
            .traverseColDefs(currentColDefs, (colDef) => {
            return colDef.colId === newColumnDefinition.colId ? null : colDef;
        })
            .filter(Boolean);
        this.setAgGridColumnDefinitions([...sanitizedColDefs, newColumnDefinition]);
    }
}
exports.GridApiImpl = GridApiImpl;
