"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.simplifyLayoutModel = exports.simplifyPivotLayoutModel = exports.simplifyTableLayoutModel = void 0;
const isPivotLayoutModel_1 = require("./isPivotLayoutModel");
const normalizeLayoutModel_1 = require("./normalizeLayoutModel");
function simplifyTableLayoutModel(layout) {
    layout = structuredClone(layout);
    if (layout.ColumnVisibility && !Object.keys(layout.ColumnVisibility).length) {
        delete layout.ColumnVisibility;
    }
    if (layout.ColumnSizing && !Object.keys(layout.ColumnSizing).length) {
        delete layout.ColumnSizing;
    }
    if (layout.ColumnSorts && !layout.ColumnSorts.length) {
        delete layout.ColumnSorts;
    }
    if (layout.Ignore_ColumnFilters && !layout.Ignore_ColumnFilters) {
        delete layout.Ignore_ColumnFilters;
    }
    if (layout.RowGroupedColumns && !layout.RowGroupedColumns.length) {
        delete layout.RowGroupedColumns;
    }
    if (!layout.RowGroupedColumns && layout.RowGroupValues) {
        // delete layout.RowGroupValues;
    }
    if (!layout.SuppressAggFuncInHeader) {
        delete layout.SuppressAggFuncInHeader;
    }
    // this is the default value
    // if (layout.RowGroupValues && layout.RowGroupValues.RowGroupDisplay === 'always-collapsed') {
    //   delete layout.RowGroupValues;
    // }
    // if it's a property, but is undefined
    // make sure we delete it
    if (!layout.RowGroupValues && Object.prototype.hasOwnProperty.call(layout, 'RowGroupValues')) {
        delete layout.RowGroupValues;
    }
    // if it's a property, but is undefined
    // make sure we delete it
    if (!layout.ColumnGroupValues &&
        Object.prototype.hasOwnProperty.call(layout, 'ColumnGroupValues')) {
        delete layout.ColumnGroupValues;
    }
    if (layout.TableAggregationColumns && !layout.TableAggregationColumns.length) {
        delete layout.TableAggregationColumns;
    }
    if (layout.ColumnPinning && !Object.keys(layout.ColumnPinning).length) {
        delete layout.ColumnPinning;
    }
    if (!layout.RowGroupDisplayType &&
        layout.TableColumns &&
        layout.TableColumns.length &&
        layout.RowGroupedColumns &&
        layout.RowGroupedColumns.length) {
        let displayType = 'single';
        layout.TableColumns.some((columnId) => {
            if (columnId === normalizeLayoutModel_1.AUTO_GROUP_COLUMN_ID__SINGLE) {
                displayType = 'single';
                return true;
            }
            else if (columnId.startsWith(normalizeLayoutModel_1.AUTO_GROUP_COLUMN_ID__MULTI_PREFIX)) {
                displayType = 'multi';
                return true;
            }
        });
        layout.RowGroupDisplayType = displayType;
    }
    if (layout.GrandTotalRow === undefined) {
        delete layout.GrandTotalRow;
    }
    return layout;
}
exports.simplifyTableLayoutModel = simplifyTableLayoutModel;
function simplifyPivotLayoutModel(layout) {
    layout = structuredClone(layout);
    if (layout.ColumnSizing && !Object.keys(layout.ColumnSizing).length) {
        delete layout.ColumnSizing;
    }
    if (!layout.SuppressAggFuncInHeader) {
        delete layout.SuppressAggFuncInHeader;
    }
    if (layout.PivotAggregationColumns && !layout.PivotAggregationColumns.length) {
        delete layout.PivotAggregationColumns;
    }
    if (layout.ColumnSorts && !layout.ColumnSorts.length) {
        delete layout.ColumnSorts;
    }
    if (layout.GrandTotalRow === undefined) {
        delete layout.GrandTotalRow;
    }
    if (layout.PivotColumnTotal === undefined) {
        delete layout.PivotColumnTotal;
    }
    if (layout.PivotGrandTotal === undefined) {
        delete layout.PivotGrandTotal;
    }
    // if it's a property, but is undefined
    // make sure we delete it
    if (!layout.ColumnGroupValues &&
        Object.prototype.hasOwnProperty.call(layout, 'ColumnGroupValues')) {
        delete layout.ColumnGroupValues;
    }
    return layout;
}
exports.simplifyPivotLayoutModel = simplifyPivotLayoutModel;
function simplifyLayoutModel(layout) {
    if ((0, isPivotLayoutModel_1.isPivotLayoutModel)(layout)) {
        return simplifyPivotLayoutModel(layout);
    }
    return simplifyTableLayoutModel(layout);
}
exports.simplifyLayoutModel = simplifyLayoutModel;
