"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isPivotLayout = exports.pivotLayoutModelToPivotLayout = exports.tableLayoutModelToTableLayout = exports.pivotLayoutToPivotLayoutModel = exports.tableLayoutToTableLayoutModel = exports.checkForDuplicateColumns = exports.normalizePivotLayout = exports.normalizeTableLayout = exports.normalizeLayout = exports.areLayoutsEqual = exports.layoutModelToLayoutState = exports.layoutStateToLayoutModel = void 0;
const tslib_1 = require("tslib");
const isEqual_1 = tslib_1.__importDefault(require("lodash/isEqual"));
const isPivotLayoutModel_1 = require("../../layout-manager/src/isPivotLayoutModel");
const normalizeLayoutModel_1 = require("../../layout-manager/src/normalizeLayoutModel");
const simplifyLayoutModel_1 = require("../../layout-manager/src/simplifyLayoutModel");
const AggregationColumns_1 = require("../../AdaptableState/Common/AggregationColumns");
const AdaptableHelper_1 = require("../../Utilities/Helpers/AdaptableHelper");
const layoutStateToLayoutModel = (layout) => {
    if ((0, exports.isPivotLayout)(layout)) {
        return (0, exports.pivotLayoutToPivotLayoutModel)(layout);
    }
    return (0, exports.tableLayoutToTableLayoutModel)(layout);
};
exports.layoutStateToLayoutModel = layoutStateToLayoutModel;
const layoutModelToLayoutState = (layoutModel, defaults) => {
    if ((0, isPivotLayoutModel_1.isPivotLayoutModel)(layoutModel)) {
        return (0, exports.pivotLayoutModelToPivotLayout)(layoutModel, defaults);
    }
    return (0, exports.tableLayoutModelToTableLayout)(layoutModel, defaults);
};
exports.layoutModelToLayoutState = layoutModelToLayoutState;
function cleanupAdaptableObjectPrimitives(layout) {
    const l = layout;
    for (const key in l) {
        if (typeof l[key] === 'object') {
            (0, AdaptableHelper_1.removeAdaptableObjectPrimitivesInlineDeep)(l[key]);
        }
    }
}
/**
 * Compares 2 Layouts for equality
 * @param layout1 First Layout
 * @param layout2 Second Layout
 */
const areLayoutsEqual = (layout1, layout2) => {
    if (!layout1 && !layout2) {
        return true;
    }
    if (!layout1 || !layout2) {
        return false;
    }
    return (0, isEqual_1.default)((0, exports.normalizeLayout)(layout1), (0, exports.normalizeLayout)(layout2));
};
exports.areLayoutsEqual = areLayoutsEqual;
const normalizeLayout = (layout, options) => {
    if ((0, exports.isPivotLayout)(layout)) {
        return (0, exports.normalizePivotLayout)(layout);
    }
    return (0, exports.normalizeTableLayout)(layout, options);
};
exports.normalizeLayout = normalizeLayout;
const normalizeTableLayout = (tableLayout, options) => {
    const Source = tableLayout.Source;
    const Uuid = tableLayout.Uuid;
    const AdaptableVersion = tableLayout.AdaptableVersion;
    const ColumnFilters = tableLayout.ColumnFilters;
    const GridFilter = tableLayout.GridFilter;
    let model = (0, exports.tableLayoutToTableLayoutModel)(tableLayout);
    model = (0, normalizeLayoutModel_1.normalizeTableLayoutModel)(model, options);
    tableLayout = (0, exports.tableLayoutModelToTableLayout)(model, tableLayout);
    if (Source) {
        tableLayout.Source = Source;
    }
    if (Uuid) {
        tableLayout.Uuid = Uuid;
    }
    if (AdaptableVersion) {
        tableLayout.AdaptableVersion = AdaptableVersion;
    }
    if (ColumnFilters) {
        tableLayout.ColumnFilters = ColumnFilters;
    }
    if (GridFilter) {
        tableLayout.GridFilter = GridFilter;
    }
    return tableLayout;
};
exports.normalizeTableLayout = normalizeTableLayout;
const normalizePivotLayout = (pivotLayout) => {
    const Source = pivotLayout.Source;
    const Uuid = pivotLayout.Uuid;
    const AdaptableVersion = pivotLayout.AdaptableVersion;
    const ColumnFilters = pivotLayout.ColumnFilters;
    const GridFilter = pivotLayout.GridFilter;
    let model = (0, exports.pivotLayoutToPivotLayoutModel)(pivotLayout);
    model = (0, normalizeLayoutModel_1.normalizePivotLayoutModel)(model);
    pivotLayout = (0, exports.pivotLayoutModelToPivotLayout)(model, pivotLayout);
    if (Source) {
        pivotLayout.Source = Source;
    }
    if (Uuid) {
        pivotLayout.Uuid = Uuid;
    }
    if (AdaptableVersion) {
        pivotLayout.AdaptableVersion = AdaptableVersion;
    }
    if (ColumnFilters) {
        pivotLayout.ColumnFilters = ColumnFilters;
    }
    if (GridFilter) {
        pivotLayout.GridFilter = GridFilter;
    }
    return pivotLayout;
};
exports.normalizePivotLayout = normalizePivotLayout;
const errorOnceMessages = new Map();
const errorOnce = (message) => {
    if (errorOnceMessages.get(message)) {
        return;
    }
    console.error(message);
    errorOnceMessages.set(message, true);
};
const checkForDuplicateColumns = (layout) => {
    if (layout.TableColumns) {
        const set = new Set(layout.TableColumns);
        if (set.size !== layout.TableColumns.length) {
            layout.TableColumns.reduce((acc, col) => {
                acc[col] = (acc[col] || 0) + 1;
                if (acc[col] > 1) {
                    // if you change this message, make sure you also update the associated test error-on-duplicate-column.spec.ts
                    errorOnce(`Duplicate column "${col}" in layout "${layout.Name}"!`);
                }
                return acc;
            }, {});
        }
    }
};
exports.checkForDuplicateColumns = checkForDuplicateColumns;
const tableLayoutToTableLayoutModel = (tableLayout) => {
    tableLayout = structuredClone(tableLayout);
    cleanupAdaptableObjectPrimitives(tableLayout);
    if (tableLayout.RowGroupValues) {
        // @ts-ignore
        delete tableLayout.RowGroupValues.Source;
        // @ts-ignore
        delete tableLayout.RowGroupValues.Uuid;
        // @ts-ignore
        delete tableLayout.RowGroupValues.AdaptableVersion;
    }
    delete tableLayout.ColumnHeaders;
    const TableAggregationColumns = (tableLayout.TableAggregationColumns || []).map(({ ColumnId, AggFunc }) => {
        return {
            ColumnId,
            AggFunc: toAggFunc(AggFunc),
        };
    });
    const result = (0, simplifyLayoutModel_1.simplifyTableLayoutModel)({
        SuppressAggFuncInHeader: tableLayout.SuppressAggFuncInHeader,
        TableColumns: tableLayout.TableColumns,
        ColumnWidths: tableLayout.ColumnWidths,
        ColumnVisibility: tableLayout.ColumnVisibility,
        ColumnSorts: tableLayout.ColumnSorts,
        // ColumnHeaders: tableLayout.ColumnHeaders,
        RowGroupedColumns: tableLayout.RowGroupedColumns,
        ColumnPinning: tableLayout.ColumnPinning,
        RowGroupValues: tableLayout.RowGroupValues
            ? tableLayout.RowGroupValues.RowGroupDefaultBehavior === 'always-collapsed' ||
                tableLayout.RowGroupValues.RowGroupDefaultBehavior === 'always-expanded'
                ? {
                    RowGroupDisplay: tableLayout.RowGroupValues.RowGroupDefaultBehavior,
                }
                : {
                    RowGroupDisplay: tableLayout.RowGroupValues.RowGroupDefaultBehavior,
                    Values: tableLayout.RowGroupValues.ExceptionGroupKeys || [],
                }
            : undefined,
        TableAggregationColumns: TableAggregationColumns,
        GrandTotalRow: tableLayout.GrandTotalRow,
    });
    if (tableLayout.RowGroupDisplayType) {
        result.RowGroupDisplayType = tableLayout.RowGroupDisplayType;
    }
    if (tableLayout.ColumnFilters) {
        result.ColumnFilters = tableLayout.ColumnFilters;
    }
    if (tableLayout.GridFilter) {
        result.GridFilter = tableLayout.GridFilter;
    }
    return result;
};
exports.tableLayoutToTableLayoutModel = tableLayoutToTableLayoutModel;
const pivotLayoutToPivotLayoutModel = (pivotLayout) => {
    pivotLayout = structuredClone(pivotLayout);
    cleanupAdaptableObjectPrimitives(pivotLayout);
    delete pivotLayout.ColumnHeaders;
    const result = (0, simplifyLayoutModel_1.simplifyPivotLayoutModel)({
        SuppressAggFuncInHeader: pivotLayout.SuppressAggFuncInHeader,
        ColumnWidths: pivotLayout.ColumnWidths,
        ColumnVisibility: pivotLayout.ColumnVisibility,
        ColumnSorts: pivotLayout.ColumnSorts,
        PivotGroupedColumns: pivotLayout.PivotGroupedColumns,
        ColumnPinning: pivotLayout.ColumnPinning,
        PivotColumns: pivotLayout.PivotColumns,
        PivotExpandLevel: pivotLayout.PivotExpandLevel,
        PivotAggregationColumns: (pivotLayout.PivotAggregationColumns || []).map(({ ColumnId, AggFunc, Total }) => {
            return {
                ColumnId,
                AggFunc: toAggFunc(AggFunc),
                Total,
            };
        }),
        GrandTotalRow: pivotLayout.GrandTotalRow,
        PivotGrandTotal: pivotLayout.PivotGrandTotal,
        PivotColumnTotal: pivotLayout.PivotColumnTotal,
        RowGroupValues: pivotLayout.RowGroupValues
            ? pivotLayout.RowGroupValues.RowGroupDefaultBehavior === 'always-collapsed' ||
                pivotLayout.RowGroupValues.RowGroupDefaultBehavior === 'always-expanded'
                ? {
                    RowGroupDisplay: pivotLayout.RowGroupValues.RowGroupDefaultBehavior,
                }
                : {
                    RowGroupDisplay: pivotLayout.RowGroupValues.RowGroupDefaultBehavior,
                    Values: pivotLayout.RowGroupValues.ExceptionGroupKeys || [],
                }
            : undefined,
    });
    if (pivotLayout.ColumnFilters) {
        result.ColumnFilters = pivotLayout.ColumnFilters;
    }
    if (pivotLayout.GridFilter) {
        result.GridFilter = pivotLayout.GridFilter;
    }
    return result;
};
exports.pivotLayoutToPivotLayoutModel = pivotLayoutToPivotLayoutModel;
function toAggFunc(aggFunc) {
    if ((0, AggregationColumns_1.isWeightedAverageAggregation)(aggFunc)) {
        return { aggFunc: AggregationColumns_1.WEIGHTED_AVERAGE_AGG_FN_NAME, weightedColumnId: aggFunc.weightedColumnId };
    }
    return { aggFunc };
}
const tableLayoutModelToTableLayout = (layoutModel, defaults) => {
    const tableLayout = {
        ...defaults,
        TableColumns: layoutModel.TableColumns,
        ColumnWidths: layoutModel.ColumnWidths,
        ColumnVisibility: layoutModel.ColumnVisibility,
        ColumnSorts: layoutModel.ColumnSorts,
    };
    if (layoutModel.RowGroupDisplayType) {
        tableLayout.RowGroupDisplayType = layoutModel.RowGroupDisplayType;
    }
    if (layoutModel.RowGroupedColumns) {
        tableLayout.RowGroupedColumns = layoutModel.RowGroupedColumns;
    }
    else {
        delete tableLayout.RowGroupedColumns;
    }
    if (layoutModel.ColumnPinning) {
        tableLayout.ColumnPinning = layoutModel.ColumnPinning;
    }
    else {
        delete tableLayout.ColumnPinning;
    }
    if (layoutModel.RowGroupValues) {
        tableLayout.RowGroupValues = {
            RowGroupDefaultBehavior: layoutModel.RowGroupValues.RowGroupDisplay,
        };
        if ((layoutModel.RowGroupValues.RowGroupDisplay === 'collapsed' ||
            layoutModel.RowGroupValues.RowGroupDisplay === 'expanded') &&
            layoutModel.RowGroupValues.Values) {
            // @ts-ignore
            tableLayout.RowGroupValues.ExceptionGroupKeys = layoutModel.RowGroupValues.Values;
        }
    }
    else {
        delete tableLayout.RowGroupValues;
    }
    if (layoutModel.TableAggregationColumns) {
        tableLayout.TableAggregationColumns = layoutModel.TableAggregationColumns.map(({ ColumnId, AggFunc }) => {
            return {
                ColumnId,
                AggFunc: toAggregationColumnValue(AggFunc),
            };
        });
    }
    else {
        delete tableLayout.TableAggregationColumns;
    }
    if (layoutModel.GrandTotalRow) {
        tableLayout.GrandTotalRow = layoutModel.GrandTotalRow;
    }
    else {
        delete tableLayout.GrandTotalRow;
    }
    if (layoutModel.ColumnFilters) {
        tableLayout.ColumnFilters = layoutModel.ColumnFilters;
    }
    if (layoutModel.GridFilter) {
        tableLayout.GridFilter = layoutModel.GridFilter;
    }
    cleanupAdaptableObjectPrimitives(tableLayout);
    return tableLayout;
};
exports.tableLayoutModelToTableLayout = tableLayoutModelToTableLayout;
function toAggregationColumnValue(columnAggregationModel) {
    return columnAggregationModel &&
        typeof columnAggregationModel.aggFunc === 'string' &&
        (0, AggregationColumns_1.isWeightedAverageAggFuncName)(columnAggregationModel.aggFunc)
        ? {
            type: 'weightedAverage',
            weightedColumnId: columnAggregationModel.weightedColumnId,
        }
        : columnAggregationModel.aggFunc;
}
const pivotLayoutModelToPivotLayout = (layoutModel, defaults) => {
    const pivotLayout = {
        ...defaults,
        PivotColumns: layoutModel.PivotColumns,
        TableColumns: layoutModel.TableColumns,
        ColumnWidths: layoutModel.ColumnWidths,
        ColumnVisibility: layoutModel.ColumnVisibility,
        ColumnSorts: layoutModel.ColumnSorts,
        ColumnPinning: layoutModel.ColumnPinning,
        PivotExpandLevel: layoutModel.PivotExpandLevel,
    };
    if (layoutModel.PivotGroupedColumns) {
        pivotLayout.PivotGroupedColumns = layoutModel.PivotGroupedColumns;
    }
    else {
        delete pivotLayout.PivotGroupedColumns;
    }
    if (layoutModel.RowGroupValues) {
        pivotLayout.RowGroupValues = {
            RowGroupDefaultBehavior: layoutModel.RowGroupValues.RowGroupDisplay,
        };
        if ((layoutModel.RowGroupValues.RowGroupDisplay === 'collapsed' ||
            layoutModel.RowGroupValues.RowGroupDisplay === 'expanded') &&
            layoutModel.RowGroupValues.Values) {
            // @ts-ignore
            pivotLayout.RowGroupValues.ExceptionGroupKeys = layoutModel.RowGroupValues.Values;
        }
    }
    else {
        delete pivotLayout.RowGroupValues;
    }
    if (layoutModel.GrandTotalRow) {
        pivotLayout.GrandTotalRow = layoutModel.GrandTotalRow;
    }
    else {
        delete pivotLayout.GrandTotalRow;
    }
    if (layoutModel.PivotGrandTotal) {
        pivotLayout.PivotGrandTotal = layoutModel.PivotGrandTotal;
    }
    else {
        delete pivotLayout.PivotGrandTotal;
    }
    if (layoutModel.PivotColumnTotal) {
        pivotLayout.PivotColumnTotal = layoutModel.PivotColumnTotal;
    }
    else {
        delete pivotLayout.PivotColumnTotal;
    }
    if (layoutModel.PivotAggregationColumns) {
        pivotLayout.PivotAggregationColumns = (layoutModel.PivotAggregationColumns || []).map(({ ColumnId, AggFunc, Total }) => ({
            ColumnId,
            AggFunc: toAggregationColumnValue(AggFunc),
            Total,
        }));
    }
    else {
        delete pivotLayout.PivotAggregationColumns;
    }
    if (layoutModel.ColumnFilters) {
        pivotLayout.ColumnFilters = layoutModel.ColumnFilters;
    }
    if (layoutModel.GridFilter) {
        pivotLayout.GridFilter = layoutModel.GridFilter;
    }
    return pivotLayout;
};
exports.pivotLayoutModelToPivotLayout = pivotLayoutModelToPivotLayout;
const isPivotLayout = (layout) => Array.isArray(layout.PivotColumns);
exports.isPivotLayout = isPivotLayout;
