import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState } from "react";
import ContentTree from "../ContentTree";
import { ItemSearch } from "../ui/ItemSearch";
import { SimpleTabs } from "../ui/SimpleTabs";
const CONTENT_ROOT_ID = "0de95ae4-41ab-4d01-9eb0-67441b7c2450"; // /sitecore/content
export function ItemTreeSelector({ selectedItems, onItemsChange, language, height = "400px", }) {
    const [activeTab, setActiveTab] = useState(0);
    // Handle special language values like "*" (all languages) or "mixed" (multi-language) - default to "en" for tree browsing
    const effectiveLanguage = language === "*" || language === "mixed" || !language ? "en" : language;
    const handleItemSelected = (item) => {
        // Check if item is already selected
        const isAlreadySelected = selectedItems.some((selected) => selected.id === item.id && selected.language === item.language);
        if (!isAlreadySelected) {
            onItemsChange([...selectedItems, item]);
        }
    };
    const handleTreeSelectionChange = (items) => {
        // Convert tree items to ItemDescriptor format, preserving name and path
        const descriptors = items.map((item) => ({
            id: item.id,
            language: item.language || effectiveLanguage,
            version: item.version || 1,
            name: item.name,
            path: item.path || item.idPath,
        }));
        onItemsChange(descriptors);
    };
    const tabs = [
        {
            id: "browse",
            label: "Browse",
            content: (_jsx("div", { className: "h-full overflow-auto p-2", children: _jsx(ContentTree, { language: effectiveLanguage, selectionMode: "multiple", selectedItemIds: selectedItems.map((item) => item.id), onSelectionChange: handleTreeSelectionChange, rootItemIds: [CONTENT_ROOT_ID], hideRootNodes: true }) })),
        },
        {
            id: "search",
            label: "Search",
            content: (_jsxs("div", { className: "flex h-full flex-col p-2", children: [_jsx(ItemSearch, { language: effectiveLanguage, itemSelected: handleItemSelected, autoFocus: activeTab === 1, rootItemIds: [CONTENT_ROOT_ID] }), _jsxs("div", { className: "mt-2 text-xs text-gray-500", children: ["Selected: ", selectedItems.length, " item", selectedItems.length !== 1 ? "s" : ""] })] })),
        },
    ];
    return (_jsxs("div", { className: "flex flex-col rounded border", style: { height }, children: [_jsx("div", { className: "border-b", children: _jsx(SimpleTabs, { tabs: tabs, activeTab: activeTab, setActiveTab: setActiveTab, className: "px-2", hideContent: true }) }), _jsx("div", { className: "flex-1 overflow-hidden", children: tabs[activeTab]?.content })] }));
}
//# sourceMappingURL=ItemTreeSelector.js.map