import React, { useCallback } from "react";
import { createStrictContext } from "../../../utils/helpers/index.js";
import { useControllableState } from "../../../utils/hooks/index.js";
import { useDataTableContext } from "../root/DataTableRoot.context.js";
const { Provider: DataTableDetailsPanelContextProvider, useContext: useDataTableDetailsPanel, } = createStrictContext({
    name: "DataTableDetailsPanelContext",
    errorMessage: "useDataTableDetailsPanel must be used within a DataTableDetailsPanelProvider.",
});
function DataTableDetailsPanelProvider({ children, detailsPanel, }) {
    var _a;
    const { expandedRowIds, defaultExpandedRowIds = [], onExpandedRowIdsChange, getContent, isRowExpandable, getHeight, showExpandAll = false, } = detailsPanel || {};
    const [expandedIds, setExpandedIds] = useControllableState({
        value: expandedRowIds,
        defaultValue: defaultExpandedRowIds,
        onChange: onExpandedRowIdsChange,
    });
    /* TODO: False is just fallback until auto and root is merged */
    const tableContext = useDataTableContext(false);
    const { itemDetails } = (_a = tableContext === null || tableContext === void 0 ? void 0 : tableContext.tableItems) !== null && _a !== void 0 ? _a : {
        itemDetails: new Map(),
    };
    const expandableIds = React.useMemo(() => {
        if (!getContent) {
            return new Set();
        }
        const ids = new Set();
        for (const { rowData, id, level } of itemDetails.values()) {
            /* We only allow Master - Details pattern on top level rows */
            if (level > 0) {
                continue;
            }
            if (!isRowExpandable || isRowExpandable(rowData)) {
                ids.add(id);
            }
        }
        return ids;
    }, [getContent, isRowExpandable, itemDetails]);
    const isDetailsPanelExpandableById = useCallback((id) => expandableIds.has(id), [expandableIds]);
    const isExpanded = useCallback((id) => isDetailsPanelExpandableById(id) && expandedIds.includes(id), [expandedIds, isDetailsPanelExpandableById]);
    const toggleExpansion = useCallback((id) => {
        if (!isDetailsPanelExpandableById(id)) {
            return;
        }
        setExpandedIds((currentExpandedIds) => currentExpandedIds.includes(id)
            ? currentExpandedIds.filter((expandedId) => expandedId !== id)
            : [...currentExpandedIds, id]);
    }, [isDetailsPanelExpandableById, setExpandedIds]);
    const isAllExpanded = expandableIds.size > 0 &&
        Array.from(expandableIds).every((key) => expandedIds.includes(key));
    const toggleAll = useCallback(() => {
        setExpandedIds(isAllExpanded ? [] : Array.from(expandableIds));
    }, [expandableIds, isAllExpanded, setExpandedIds]);
    return (React.createElement(DataTableDetailsPanelContextProvider, { isExpanded: isExpanded, isDetailsPanelExpandable: isDetailsPanelExpandableById, toggleExpansion: toggleExpansion, toggleAll: toggleAll, isAllExpanded: isAllExpanded, getDetailsPanelContent: getContent, getDetailsPanelHeight: getHeight, showExpandAll: showExpandAll, enableDetailsPanel: !!getContent }, children));
}
function getDataTableDetailsPanelId(tableId, rowId) {
    return `${tableId}-expansion-${rowId}`;
}
export { DataTableDetailsPanelProvider, getDataTableDetailsPanelId, useDataTableDetailsPanel, };
//# sourceMappingURL=useTableDetailsPanel.js.map