"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useColumnOptions = useColumnOptions;
const react_1 = require("react");
const consoleWarning_1 = require("../../../utils/helpers/consoleWarning");
const DataTableTr_1 = require("../tr/DataTableTr");
function useColumnOptions(columnDefinitions, options) {
    const { stickyColumns, hasSelection, hasDetailsPanel, layout, columnDisplay, } = options;
    const hasStickyStart = (stickyColumns === null || stickyColumns === void 0 ? void 0 : stickyColumns.start) === 1;
    const stickyExpansion = hasStickyStart && hasDetailsPanel;
    const stickySelection = hasStickyStart && hasSelection;
    const stickySelectionOffset = stickyExpansion ? DataTableTr_1.ACTION_CELL_WIDTH : 0;
    const stickyFirstColumnOffset = (stickyExpansion ? DataTableTr_1.ACTION_CELL_WIDTH : 0) +
        (stickySelection ? DataTableTr_1.ACTION_CELL_WIDTH : 0);
    const visibleColumns = (0, react_1.useMemo)(() => {
        return orderColumnsAndFilterByVisibility(columnDefinitions, columnDisplay);
    }, [columnDefinitions, columnDisplay]);
    const columns = (0, react_1.useMemo)(() => {
        return visibleColumns.map((colDef, index) => {
            const isFirstSticky = hasStickyStart && index === 0;
            const isLastSticky = (stickyColumns === null || stickyColumns === void 0 ? void 0 : stickyColumns.end) === 1 && index === visibleColumns.length - 1;
            return {
                isSticky: isFirstSticky
                    ? "start"
                    : isLastSticky
                        ? "end"
                        : false,
                isStickyLast: isFirstSticky && !isLastSticky,
                stickyLeftOffset: isFirstSticky ? stickyFirstColumnOffset : undefined,
                colDef,
            };
        });
    }, [visibleColumns, hasStickyStart, stickyColumns, stickyFirstColumnOffset]);
    const totalColSpan = columns.length +
        (layout === "fixed" ? 1 : 0) +
        (hasSelection ? 1 : 0) +
        (hasDetailsPanel ? 1 : 0);
    return {
        stickyStart: {
            selection: stickySelection,
            expansion: stickyExpansion,
            selectionOffset: stickySelectionOffset,
            firstColumnOffset: stickyFirstColumnOffset,
        },
        columns,
        totalColSpan,
    };
}
function orderColumnsAndFilterByVisibility(columns, columnDisplay) {
    if (!columnDisplay) {
        return columns;
    }
    const columnMap = new Map(columns.map((col) => [col.id, col]));
    return columnDisplay.reduce((acc, { id, visible }) => {
        const col = columnMap.get(id);
        if (!col) {
            (0, consoleWarning_1.consoleWarning)(`DataGrid: Column with id "${id}" not found in column definitions. Please check your columnDisplay configuration.`);
        }
        if (col && visible) {
            acc.push(col);
        }
        return acc;
    }, []);
}
//# sourceMappingURL=useColumnOptions.js.map