import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Button } from "../../components/ui/button";
import { useEffect, useRef, useState } from "react";
import { Dialog, DialogContent } from "../../components/ui/dialog";
import { StyledDialogTitle } from "../../components/ui/styled-dialog-title";
import DialogButtons from "./DialogButtons";
import { ScrollingContentTree } from "../ScrollingContentTree";
import { rootItemId } from "../../config/config";
import { FolderTree } from "lucide-react";
function normalizeItemId(id) {
    if (!id)
        return undefined;
    return id.toLowerCase().replace(/[{}]/g, "");
}
export function SharedFolderSelectorDialog(props) {
    if (!props)
        return null;
    const normalizedInitialFolderId = normalizeItemId(props.initialFolderId);
    const openedAtRef = useRef(Date.now());
    const suppressNextImplicitCloseRef = useRef(false);
    const selectionRequestRef = useRef(0);
    const [selectedItem, setSelectedItem] = useState(null);
    useEffect(() => {
        let isMounted = true;
        const initializeSelection = async () => {
            if (!normalizedInitialFolderId)
                return;
            const requestId = ++selectionRequestRef.current;
            // Set immediate selection for visual highlight while permissions are loading.
            setSelectedItem({
                id: normalizedInitialFolderId,
                version: 0,
                language: props.language,
                canCreate: false,
            });
            const item = await props.editContext.itemsRepository.getItem({
                id: normalizedInitialFolderId,
                version: 0,
                language: props.language,
            });
            if (!isMounted || selectionRequestRef.current !== requestId)
                return;
            setSelectedItem({
                id: normalizedInitialFolderId,
                version: 0,
                language: props.language,
                canCreate: item?.canCreate ?? false,
            });
        };
        initializeSelection();
        return () => {
            isMounted = false;
        };
    }, [
        normalizedInitialFolderId,
        props.editContext.itemsRepository,
        props.language,
    ]);
    const handleOk = () => {
        props.onClose?.(selectedItem ?? null);
    };
    const handleCancel = () => {
        props.onClose?.(null);
    };
    return (_jsx(Dialog, { open: true, onOpenChange: (open) => {
            if (!open) {
                const elapsedSinceOpen = Date.now() - openedAtRef.current;
                const shouldIgnoreImplicitClose = suppressNextImplicitCloseRef.current || elapsedSinceOpen < 500;
                if (shouldIgnoreImplicitClose) {
                    suppressNextImplicitCloseRef.current = false;
                    return;
                }
                props.onClose?.(null);
            }
        }, children: _jsxs(DialogContent, { className: "flex flex-col overflow-hidden border-none p-0 shadow-2xl", style: { width: "94vw", maxWidth: "1200px", height: "90vh" }, onInteractOutside: (event) => {
                event.preventDefault();
            }, onPointerDownOutside: (event) => {
                event.preventDefault();
            }, onEscapeKeyDown: (event) => {
                event.preventDefault();
                event.stopPropagation();
                suppressNextImplicitCloseRef.current = true;
            }, children: [_jsx(StyledDialogTitle, { icon: _jsx(FolderTree, { strokeWidth: 1 }), title: props.header ?? "Select Shared Folder" }), _jsxs("div", { className: "flex h-full flex-1 flex-col overflow-hidden", children: [_jsx("div", { className: "relative min-h-0 flex-1 px-6 pt-6 pb-0", children: _jsx("div", { className: "h-full overflow-hidden rounded-xl bg-white p-1", children: _jsx(ScrollingContentTree, { selectedItemId: selectedItem?.id ?? normalizedInitialFolderId, expandedItemId: normalizedInitialFolderId, rootItemId: rootItemId, onSelectionChange: async (node) => {
                                        if (!node[0]) {
                                            selectionRequestRef.current += 1;
                                            setSelectedItem(null);
                                            return;
                                        }
                                        const requestId = ++selectionRequestRef.current;
                                        const nextSelection = {
                                            id: node[0].id,
                                            version: 0,
                                            language: props.language,
                                        };
                                        // The tree updates its selected row synchronously. Clear the
                                        // previous folder's permission at the same time so the user
                                        // cannot submit that stale folder while this lookup is in
                                        // flight.
                                        setSelectedItem({ ...nextSelection, canCreate: false });
                                        const item = await props.editContext.itemsRepository.getItem(node[0]);
                                        if (selectionRequestRef.current !== requestId)
                                            return;
                                        setSelectedItem({
                                            ...nextSelection,
                                            canCreate: item?.canCreate ?? false,
                                        });
                                    } }) }) }), _jsxs(DialogButtons, { hideBorder: true, className: "mt-4", children: [_jsx(Button, { onClick: handleCancel, variant: "ghost", children: "Cancel" }), _jsx(Button, { onClick: handleOk, variant: "default", disabled: !selectedItem || !selectedItem.canCreate, className: "min-w-[140px]", children: props.label ?? "Use folder" })] })] })] }) }));
}
//# sourceMappingURL=SharedFolderSelectorDialog.js.map