"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PivotDetailsPopoup = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const InfiniteTable_1 = require("../../components/InfiniteTable");
const AdaptableContext_1 = require("../AdaptableContext");
const tableDOMProps = {
    style: {
        height: '100%',
        minWidth: '10rem',
        minHeight: 300,
    },
};
const columnTypes = {
    default: {
        defaultFlex: 1,
    },
};
const PivotDetailsPopoup = (props) => {
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const adaptableApi = adaptable.api;
    const primaryKey = adaptableApi.optionsApi.getPrimaryKey();
    const { hasPivotValue, rows, rowGroups, columnId, layout, aggColumn, pivotValue, pivotColumnId, } = props.popupProps;
    let data = rows;
    const columns = React.useMemo(() => {
        const getFriendlyName = (columnId) => adaptableApi.columnApi.getFriendlyNameForColumnId(columnId);
        const pivotPreviewOptionColumn = adaptableApi.optionsApi.getLayoutOptions()?.pivotPreviewColumns;
        let pivotPreviewExtraColumns = [];
        if (typeof pivotPreviewOptionColumn === 'function') {
            const pivotPreviewColumnsContext = {
                layout: layout,
                columnId: columnId,
                ...adaptableApi.internalApi.buildBaseContext(),
            };
            pivotPreviewExtraColumns = pivotPreviewOptionColumn(pivotPreviewColumnsContext);
        }
        else if (Array.isArray(pivotPreviewOptionColumn)) {
            pivotPreviewExtraColumns = pivotPreviewOptionColumn;
        }
        let columns = {
            [primaryKey]: { field: primaryKey, header: getFriendlyName(primaryKey) },
        };
        if (hasPivotValue) {
            // pivot_license_MIT License_stargazers_count
            // splitting by _ does not work
            columns = {
                ...columns,
                [aggColumn]: { field: aggColumn, header: getFriendlyName(aggColumn) },
                [pivotColumnId]: { field: pivotColumnId, header: getFriendlyName(pivotColumnId) },
            };
            data = rows.filter((row) => row?.[pivotColumnId] === pivotValue);
        }
        else {
            columns = {
                ...columns,
                [columnId]: { field: columnId, header: getFriendlyName(columnId) },
            };
        }
        const extraColumns = [];
        if (Array.isArray(rowGroups) && rowGroups.length > 0) {
            extraColumns.push(...rowGroups);
        }
        if (Array.isArray(pivotPreviewExtraColumns) && pivotPreviewExtraColumns.length > 0) {
            extraColumns.push(...pivotPreviewExtraColumns);
        }
        return {
            ...columns,
            ...extraColumns.reduce((acc, colId) => ({
                ...acc,
                [colId]: { field: colId, header: getFriendlyName(colId) },
            }), {}),
        };
    }, [props.popupProps]);
    return (React.createElement(InfiniteTable_1.DataSource, { data: data, primaryKey: columnId },
        React.createElement(InfiniteTable_1.InfiniteTableGrid, { columnTypes: columnTypes, domProps: tableDOMProps, columns: columns })));
};
exports.PivotDetailsPopoup = PivotDetailsPopoup;
