"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizeLayoutModel = exports.normalizePivotLayoutModel = exports.normalizeTableLayoutModel = exports.isGroupColumn = exports.DEFAULT_COL_WIDTH = exports.GROUP_COLUMN_ID__MULTI_PREFIX = exports.GROUP_COLUMN_ID__SINGLE = void 0;
const isPivotLayoutModel_1 = require("./isPivotLayoutModel");
exports.GROUP_COLUMN_ID__SINGLE = 'ag-Grid-AutoColumn';
exports.GROUP_COLUMN_ID__MULTI_PREFIX = 'ag-Grid-AutoColumn-';
exports.DEFAULT_COL_WIDTH = 200;
function isGroupColumn(colId) {
    return colId.startsWith(exports.GROUP_COLUMN_ID__MULTI_PREFIX) || colId === exports.GROUP_COLUMN_ID__SINGLE;
}
exports.isGroupColumn = isGroupColumn;
function normalizeTableLayoutModel(layout, options) {
    layout = structuredClone(layout);
    if (!layout.ColumnVisibility) {
        layout.ColumnVisibility = {};
    }
    if (!layout.TableAggregationColumns) {
        layout.TableAggregationColumns = [];
    }
    if (!layout.ColumnSorts) {
        layout.ColumnSorts = [];
    }
    if (!layout.ColumnPinning) {
        layout.ColumnPinning = {};
    }
    if (!layout.ColumnWidths) {
        layout.ColumnWidths = {};
    }
    if (!layout.RowGroupedColumns) {
        layout.RowGroupedColumns = [];
    }
    if (!('SuppressAggFuncInHeader' in layout)) {
        // make it an own property
        layout.SuppressAggFuncInHeader = undefined;
    }
    if (!('GrandTotalRow' in layout)) {
        // make it an own property
        layout.GrandTotalRow = undefined;
    }
    const ColumnOrderSet = new Set(layout.TableColumns);
    if (layout.RowGroupedColumns && layout.RowGroupedColumns.length && layout.TableColumns) {
        // the layout.TableColumns might not include the group columns
        //
        // but we want to include those columnIds in the TableColumns
        // so the layout is properly specified
        let displayType = layout.RowGroupDisplayType;
        if (!displayType) {
            layout.TableColumns.some((columnId) => {
                if (columnId === exports.GROUP_COLUMN_ID__SINGLE) {
                    displayType = 'single';
                    return true;
                }
                else if (columnId.startsWith(exports.GROUP_COLUMN_ID__MULTI_PREFIX)) {
                    displayType = 'multi';
                    return true;
                }
            });
            displayType = displayType || 'single';
            layout.RowGroupDisplayType = displayType;
        }
        if (displayType === 'single') {
            if (!ColumnOrderSet.has(exports.GROUP_COLUMN_ID__SINGLE)) {
                layout.TableColumns = [exports.GROUP_COLUMN_ID__SINGLE, ...layout.TableColumns];
            }
        }
        else {
            const GroupedCols = new Set(layout.RowGroupedColumns);
            [...layout.RowGroupedColumns].reverse().forEach((colId) => {
                const groupColId = `${exports.GROUP_COLUMN_ID__MULTI_PREFIX}${colId}`;
                if (!ColumnOrderSet.has(groupColId) && !ColumnOrderSet.has(colId)) {
                    // add the col to the beginning of the layout.OrderedColumns
                    layout.TableColumns.unshift(colId);
                    ColumnOrderSet.add(colId);
                    // the .flatMap below will handle the rest
                }
            });
            const MappedColumnOrder = layout.TableColumns.flatMap((colId) => {
                if (GroupedCols.has(colId)) {
                    // if the column is grouped by
                    const groupColId = `${exports.GROUP_COLUMN_ID__MULTI_PREFIX}${colId}`;
                    // but there's no column for this group in the layout
                    if (!ColumnOrderSet.has(groupColId)) {
                        // then we assume that the current column is basically the group column
                        // so we return both the group column and the current column
                        // but make the current column hidden
                        layout.ColumnVisibility[colId] = false;
                        return [groupColId, colId];
                    }
                }
                // if this is a group column
                if (colId.startsWith(exports.GROUP_COLUMN_ID__MULTI_PREFIX)) {
                    const simpleColId = colId.replace(exports.GROUP_COLUMN_ID__MULTI_PREFIX, '');
                    // but the corresponding simple column is not in the layout
                    // then let's include it, with visibility set to false
                    if (!ColumnOrderSet.has(simpleColId)) {
                        layout.ColumnVisibility[simpleColId] = false;
                        return [colId, simpleColId];
                    }
                }
                return colId;
            });
            layout.TableColumns = [...MappedColumnOrder];
        }
        if (layout.RowGroupedColumns && layout.RowGroupedColumns.length && !layout.RowGroupValues) {
            layout.RowGroupValues = {
                RowGroupDisplay: 'always-collapsed',
            };
        }
    }
    if (options?.isTree && !layout.TableColumns.includes(exports.GROUP_COLUMN_ID__SINGLE)) {
        layout.TableColumns.unshift(exports.GROUP_COLUMN_ID__SINGLE);
    }
    return layout;
}
exports.normalizeTableLayoutModel = normalizeTableLayoutModel;
function normalizePivotLayoutModel(layout) {
    layout = structuredClone(layout);
    if (!layout.ColumnVisibility) {
        layout.ColumnVisibility = {};
    }
    if (!layout.PivotAggregationColumns) {
        layout.PivotAggregationColumns = [];
    }
    if (!layout.ColumnSorts) {
        layout.ColumnSorts = [];
    }
    if (!layout.ColumnPinning) {
        layout.ColumnPinning = {};
    }
    if (!layout.ColumnWidths) {
        layout.ColumnWidths = {};
    }
    if (!layout.PivotGroupedColumns) {
        layout.PivotGroupedColumns = [];
    }
    if (layout.PivotExpandLevel === undefined) {
        layout.PivotExpandLevel = -1;
    }
    if (!layout.PivotColumns) {
        layout.PivotColumns = [];
    }
    if (!layout.RowGroupValues) {
        layout.RowGroupValues = {
            RowGroupDisplay: 'always-collapsed',
        };
    }
    if (!('SuppressAggFuncInHeader' in layout)) {
        // make it an own property
        layout.SuppressAggFuncInHeader = undefined;
    }
    if (!('GrandTotalRow' in layout)) {
        // make it an own property
        layout.GrandTotalRow = undefined;
    }
    if (!('PivotGrandTotal' in layout)) {
        // make it an own property
        layout.PivotGrandTotal = undefined;
    }
    if (!('PivotColumnTotal' in layout)) {
        // make it an own property
        layout.PivotColumnTotal = undefined;
    }
    return layout;
}
exports.normalizePivotLayoutModel = normalizePivotLayoutModel;
function normalizeLayoutModel(layout, options) {
    if ((0, isPivotLayoutModel_1.isPivotLayoutModel)(layout)) {
        return normalizePivotLayoutModel(layout);
    }
    return normalizeTableLayoutModel(layout, options);
}
exports.normalizeLayoutModel = normalizeLayoutModel;
//@ts-ignore
globalThis.normalizeTableLayoutModel = normalizeTableLayoutModel;
