import { jsx as _jsx } from "react/jsx-runtime";
import { useEffect, useState } from "react";
import ContentTree from "./ContentTree";
import { contentItemId } from "../config/config";
import { useEditContext } from "./client/editContext";
export function ScrollingContentTree({ selectedItemId, onSelectionChange, rootItemId, rootItemIds, visibleRootItemIds, expandedItemId, scrollToSelected = true, hideRootNodes, }) {
    const [selectedItem, setSelectdItem] = useState(null);
    const [expandedItem, setExpandedItem] = useState();
    const editContext = useEditContext();
    const effectiveHideRootNodes = hideRootNodes !== undefined
        ? hideRootNodes
        : !rootItemIds || rootItemIds?.length === 1;
    const effectiveRootItemIds = rootItemIds ?? [rootItemId ?? contentItemId];
    // SelectedItemIds array
    const selectedItemIds = selectedItemId ? [selectedItemId] : [];
    useEffect(() => {
        const loadItem = async () => {
            if (!selectedItemId)
                return;
            const item = await editContext.itemsRepository.getItem({
                id: selectedItemId,
                language: editContext.currentItemDescriptor?.language ?? "en",
                version: 0,
            });
            if (item)
                setSelectdItem(item);
        };
        loadItem();
    }, [selectedItemId]);
    useEffect(() => {
        const loadItem = async () => {
            if (!expandedItemId && !selectedItemId) {
                setExpandedItem(null);
                return;
            }
            const item = await editContext.itemsRepository.getItem({
                id: expandedItemId ?? selectedItemId ?? "",
                language: editContext.currentItemDescriptor?.language ?? "en",
                version: 0,
            });
            if (item)
                setExpandedItem(item);
        };
        loadItem();
    }, [expandedItemId, selectedItemId]);
    return (_jsx("div", { className: "absolute inset-0 overflow-auto", children: _jsx(ContentTree, { language: editContext.currentItemDescriptor?.language ?? "en", rootItemIds: effectiveRootItemIds, visibleRootItemIds: visibleRootItemIds, expandIdPath: expandedItem?.idPath, selectedItemIds: selectedItemIds, selectionMode: "single", className: "h-full p-1", onSelectionChange: onSelectionChange, scrollToSelected: scrollToSelected, hideRootNodes: effectiveHideRootNodes }) }));
}
//# sourceMappingURL=ScrollingContentTree.js.map