"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LayoutInternalApi = void 0;
const tslib_1 = require("tslib");
const ModuleConstants_1 = require("../../Utilities/Constants/ModuleConstants");
const ArrayExtensions_1 = tslib_1.__importDefault(require("../../Utilities/Extensions/ArrayExtensions"));
const ApiBase_1 = require("../Implementation/ApiBase");
const LayoutHelpers_1 = require("../Implementation/LayoutHelpers");
class LayoutInternalApi extends ApiBase_1.ApiBase {
    getNormalizedLayout(layout) {
        return (0, LayoutHelpers_1.normalizeLayout)(layout);
    }
    sortColumnDefsByPivotAggregationOrder(columnDefs, pivotAggregationColumns) {
        if (!pivotAggregationColumns.length) {
            return columnDefs;
        }
        let sortPivotAggColsToIndexes = null;
        let sortPivotAggColsIndexToColId = null;
        // because aggrid does not respect the aggregation order provided by the layout
        // we have to sort the coldefs
        // see the test at #keep-initial-aggregation-order
        sortPivotAggColsIndexToColId = {};
        sortPivotAggColsToIndexes = pivotAggregationColumns.reduce((acc, colDef, index) => {
            acc[colDef.ColumnId] = index;
            sortPivotAggColsIndexToColId[index] = colDef.ColumnId;
            return acc;
        }, {});
        let currentColIndex = 0;
        const colIdsToIndexes = {};
        function colIdToIndex(colDef) {
            if (colDef.children) {
                colDef.children.forEach(colIdToIndex);
            }
            else {
                colIdsToIndexes[colDef.colId] = currentColIndex;
                currentColIndex++;
            }
        }
        columnDefs.forEach(colIdToIndex);
        const initialColIdsToIndexes = { ...colIdsToIndexes };
        let currentAggIndex = 0;
        Object.keys(colIdsToIndexes).forEach((colId) => {
            if (sortPivotAggColsToIndexes[colId] !== undefined) {
                // this column is used in aggregation
                // assume we have cols: a,b,X,c,Y - X and Y are used in the pivot aggregation - but the agg order is Y,X
                // so the current indexes are 0 1 2 3 4 - but we should have
                // 0 1 4 2 3 - because we want to swap X and Y so they are in the order in which they are used in the pivot aggregation
                const columnAtCurrentAggIndex = sortPivotAggColsIndexToColId[currentAggIndex];
                const newIndex = initialColIdsToIndexes[columnAtCurrentAggIndex];
                colIdsToIndexes[colId] = newIndex;
                currentAggIndex++;
            }
        });
        const sortFn = (colDefA, colDefB) => {
            if (colDefA.children || colDefB.children) {
                if (colDefA.children) {
                    colDefA.children.sort(sortFn);
                }
                if (colDefB.children) {
                    colDefB.children.sort(sortFn);
                }
                return 0;
            }
            const defA = colDefA;
            const defB = colDefB;
            const indexA = colIdsToIndexes[defA.colId];
            const indexB = colIdsToIndexes[defB.colId];
            return indexA - indexB;
        };
        columnDefs.sort(sortFn);
        return columnDefs;
    }
    /**
     * Compares 2 Layouts for equality
     * @param layout1 First Layout
     * @param layout2 Second Layout
     */
    areLayoutsEqual(layout1, layout2) {
        return (0, LayoutHelpers_1.areLayoutsEqual)(layout1, layout2);
    }
    /**
     * Returns what the layout supports.
     * This takes into account the data-source.
     */
    getLayoutSupportedFeatures() {
        const layoutSupportedFeatures = {
            RowGroupedColumns: true,
            TableAggregationColumns: true,
            PivotAggregationColumns: true,
            PivotColumns: true,
            ColumnFilters: true,
            ColumnSorts: true,
            GridFilter: true,
            RowSummaries: true,
        };
        if (this.getGridApi().isTreeDataGrid()) {
            //  layoutSupportedFeatures.RowGroupedColumns = false;
            layoutSupportedFeatures.PivotColumns = false;
        }
        if (this.getGridApi().getAgGridRowModelType() === 'viewport') {
            layoutSupportedFeatures.RowGroupedColumns = false;
            layoutSupportedFeatures.TableAggregationColumns = false;
            layoutSupportedFeatures.PivotAggregationColumns = false;
            layoutSupportedFeatures.PivotColumns = false;
        }
        if (!this.getFilterOptions().useAdaptableFiltering) {
            layoutSupportedFeatures.ColumnFilters = false;
            layoutSupportedFeatures.GridFilter = false;
        }
        if (this.getEntitlementApi().getEntitlementAccessLevelForModule(ModuleConstants_1.ColumnFilterModuleId) == 'Hidden') {
            layoutSupportedFeatures.GridFilter = false;
        }
        if (this.getEntitlementApi().getEntitlementAccessLevelForModule(ModuleConstants_1.GridFilterModuleId) == 'Hidden') {
            layoutSupportedFeatures.GridFilter = false;
        }
        if (this._adaptable?.getAgGridRowModelType?.() !== 'clientSide') {
            layoutSupportedFeatures.RowSummaries = false;
        }
        return layoutSupportedFeatures;
    }
    hasLayoutSpecificObjects() {
        const layoutTagOptions = this.getLayoutOptions().layoutTagOptions;
        if (!layoutTagOptions) {
            return false;
        }
        if (layoutTagOptions.autoCheckTagsForLayouts == true ||
            typeof layoutTagOptions?.isObjectAvailableInLayout === 'function') {
            return true;
        }
        return false;
    }
    isObjectAvailableInLayout(object, module, layout) {
        if (!this.hasLayoutSpecificObjects()) {
            return true;
        }
        const layoutTagOptions = this.getLayoutOptions().layoutTagOptions;
        if (layoutTagOptions?.autoCheckTagsForLayouts == true) {
            return ArrayExtensions_1.default.IsNullOrEmpty(object.Tags) ? true : object.Tags?.includes(layout.Name);
        }
        const context = {
            adaptableObject: object,
            module,
            layout,
            ...this.getAdaptableInternalApi().buildBaseContext(),
        };
        return layoutTagOptions?.isObjectAvailableInLayout(context);
    }
    showLayoutNotAssociatedObjects() {
        return this.getAdaptableState().Internal.Layout.ShowLayoutNotAssociatedObjects;
    }
    isLayoutGrouped(layout) {
        if (!layout) {
            layout = this.getLayoutApi().getCurrentLayout();
        }
        return (layout?.RowGroupedColumns?.length || layout?.PivotGroupedColumns?.length) > 0;
    }
    setupRowSummaries() {
        const rowSummaries = this.getAdaptableState().Internal?.RowSummary?.rowSummaries ?? [];
        const { top, bottom } = rowSummaries.reduce((acc, summaryRow) => {
            const row = summaryRow.RowData;
            if (summaryRow.Position === 'Bottom' || !summaryRow.Position) {
                acc.bottom.push(row);
            }
            else {
                acc.top.push(row);
            }
            return acc;
        }, { top: [], bottom: [] });
        const agGridAdapter = this.getAdaptableInternalApi().getAgGridAdapter();
        agGridAdapter.setGridOption('pinnedTopRowData', top);
        agGridAdapter.setGridOption('pinnedBottomRowData', bottom);
    }
    refreshLayout() {
        this._adaptable.refreshLayout();
    }
}
exports.LayoutInternalApi = LayoutInternalApi;
