"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 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(() => {
        var _a;
        const getFriendlyName = (columnId) => adaptableApi.columnApi.getFriendlyNameForColumnId(columnId);
        const pivotPreviewOptionColumn = (_a = adaptableApi.optionsApi.getLayoutOptions()) === null || _a === void 0 ? void 0 : _a.pivotPreviewColumns;
        let pivotPreviewExtraColumns = [];
        if (typeof pivotPreviewOptionColumn === 'function') {
            const pivotPreviewColumnsContext = Object.assign({ 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 = Object.assign(Object.assign({}, columns), { [aggColumn]: { field: aggColumn, header: getFriendlyName(aggColumn) }, [pivotColumnId]: { field: pivotColumnId, header: getFriendlyName(pivotColumnId) } });
            data = rows.filter((row) => (row === null || row === void 0 ? void 0 : row[pivotColumnId]) === pivotValue);
        }
        else {
            columns = Object.assign(Object.assign({}, 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 Object.assign(Object.assign({}, columns), extraColumns.reduce((acc, colId) => (Object.assign(Object.assign({}, acc), { [colId]: { field: colId, header: getFriendlyName(colId) } })), {}));
    }, [props.popupProps]);
    return (React.createElement(InfiniteTable_1.DataSource, { data: data, primaryKey: columnId },
        React.createElement(InfiniteTable_1.InfiniteTable, { columnTypes: {
                default: {
                    defaultFlex: 1,
                },
            }, domProps: tableDOMProps, columns: columns })));
};
exports.PivotDetailsPopoup = PivotDetailsPopoup;
