"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.useDataTableDetailsPanel = void 0;
exports.DataTableDetailsPanelProvider = DataTableDetailsPanelProvider;
exports.getDataTableDetailsPanelId = getDataTableDetailsPanelId;
const react_1 = __importStar(require("react"));
const helpers_1 = require("../../../utils/helpers");
const hooks_1 = require("../../../utils/hooks");
const DataTableRoot_context_1 = require("../root/DataTableRoot.context");
const { Provider: DataTableDetailsPanelContextProvider, useContext: useDataTableDetailsPanel, } = (0, helpers_1.createStrictContext)({
    name: "DataTableDetailsPanelContext",
    errorMessage: "useDataTableDetailsPanel must be used within a DataTableDetailsPanelProvider.",
});
exports.useDataTableDetailsPanel = useDataTableDetailsPanel;
function DataTableDetailsPanelProvider({ children, detailsPanel, }) {
    var _a;
    const { expandedRowIds, defaultExpandedRowIds = [], onExpandedRowIdsChange, getContent, isRowExpandable, getHeight, showExpandAll = false, } = detailsPanel || {};
    const [expandedIds, setExpandedIds] = (0, hooks_1.useControllableState)({
        value: expandedRowIds,
        defaultValue: defaultExpandedRowIds,
        onChange: onExpandedRowIdsChange,
    });
    /* TODO: False is just fallback until auto and root is merged */
    const tableContext = (0, DataTableRoot_context_1.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_1.default.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 = (0, react_1.useCallback)((id) => expandableIds.has(id), [expandableIds]);
    const isExpanded = (0, react_1.useCallback)((id) => isDetailsPanelExpandableById(id) && expandedIds.includes(id), [expandedIds, isDetailsPanelExpandableById]);
    const toggleExpansion = (0, react_1.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 = (0, react_1.useCallback)(() => {
        setExpandedIds(isAllExpanded ? [] : Array.from(expandableIds));
    }, [expandableIds, isAllExpanded, setExpandedIds]);
    return (react_1.default.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}`;
}
//# sourceMappingURL=useTableDetailsPanel.js.map