import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import React, { useCallback, useMemo, useState } from "react";
import { Check, ChevronDown } from "lucide-react";
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuShortcut, DropdownMenuTrigger, } from "../components/ui/dropdown-menu";
import { cn } from "../lib/utils";
import { useEditContext } from "./client/editContext";
function getSortedWorkspaces(workspaces) {
    const sorted = [...(workspaces ?? [])]
        .filter((workspace) => !workspace.hidden)
        .sort((a, b) => {
        if (a.id === "home")
            return -1;
        if (b.id === "home")
            return 1;
        if (a.id === "settings")
            return 1;
        if (b.id === "settings")
            return -1;
        return (a.sortOrder ?? 100) - (b.sortOrder ?? 100);
    });
    return sorted;
}
function renderWorkspaceIcon(icon, className = "size-4") {
    if (!React.isValidElement(icon)) {
        return icon;
    }
    const iconElement = icon;
    return React.cloneElement(iconElement, {
        className: cn(iconElement.props.className, className, "text-neutral-grey-100"),
        strokeWidth: iconElement.props.strokeWidth ?? 1.5,
    });
}
export function WorkspaceTitleSwitcher({ className, variant = "default", }) {
    const editContext = useEditContext();
    const [open, setOpen] = useState(false);
    const compact = variant === "compact";
    const workspaces = useMemo(() => getSortedWorkspaces(editContext?.visibleWorkspaces), [editContext?.visibleWorkspaces]);
    const shortcutByWorkspaceId = useMemo(() => {
        const shortcuts = new Map();
        workspaces
            .filter((workspace) => workspace.id !== "home")
            .slice(0, 9)
            .forEach((workspace, index) => {
            shortcuts.set(workspace.id, `${index + 1}`);
        });
        return shortcuts;
    }, [workspaces]);
    const currentWorkspace = workspaces.find((workspace) => workspace.id === editContext?.workspaceId) ??
        editContext?.workspace ??
        workspaces[0];
    const handleSelect = useCallback((workspaceId) => {
        setOpen(false);
        if (!editContext || workspaceId === editContext.workspaceId)
            return;
        editContext.switchWorkspace(workspaceId, workspaceId === "home" ? { skipNavigationHistory: true } : undefined);
    }, [editContext]);
    if (!editContext || !currentWorkspace || workspaces.length <= 1) {
        return null;
    }
    return (_jsxs(DropdownMenu, { open: open, onOpenChange: setOpen, children: [_jsx(DropdownMenuTrigger, { asChild: true, children: _jsxs("button", { type: "button", "aria-label": `Workspace switcher: ${currentWorkspace.title}`, "aria-haspopup": "menu", "aria-expanded": open, "data-testid": "workspace-title-switcher", className: cn("focus-visible:ring-neutral-grey-100 text-neutral-grey-100 flex h-[34px] shrink-0 items-center gap-2 rounded-md bg-white p-2 text-xs shadow-[0_0_6px_0_var(--color-neutral-grey-15)] transition-shadow hover:bg-white focus:outline-none focus-visible:ring-2 focus-visible:ring-offset-2", compact && "h-8 w-8 justify-center gap-0 p-1.5", className), title: currentWorkspace.title, children: [_jsx("span", { className: "flex size-6 shrink-0 items-center justify-center", children: renderWorkspaceIcon(currentWorkspace.icon, "size-5") }), !compact && (_jsxs(_Fragment, { children: [_jsx("span", { className: "min-w-0 truncate text-xs font-medium", children: currentWorkspace.title }), _jsx(ChevronDown, { className: cn("text-neutral-grey-100 ml-auto size-3.5 shrink-0 transition-transform", open && "rotate-180"), strokeWidth: 1.75 })] }))] }) }), _jsx(DropdownMenuContent, { align: "start", sideOffset: 6, className: "flex min-w-44 flex-col gap-1 p-1", children: workspaces.map((workspace) => {
                    const active = workspace.id === editContext.workspaceId;
                    const numberKey = shortcutByWorkspaceId.get(workspace.id);
                    return (_jsxs(DropdownMenuItem, { "aria-current": active ? "page" : undefined, "data-testid": `workspace-title-switcher-${workspace.id}`, className: cn("h-8 gap-2 rounded-md px-2 text-xs", active
                            ? "bg-neutral-grey-5 text-neutral-grey-100 font-medium"
                            : "text-neutral-grey-100"), onSelect: () => handleSelect(workspace.id), children: [_jsx("span", { className: "flex size-4 shrink-0 items-center justify-center", children: renderWorkspaceIcon(workspace.icon, "size-4") }), _jsx("span", { className: "min-w-0 flex-1 truncate", children: workspace.title }), numberKey && (_jsxs(DropdownMenuShortcut, { className: "ml-3 tracking-normal", children: ["Alt+", numberKey] })), active && (_jsx(Check, { className: "text-neutral-grey-100 size-3.5 shrink-0", strokeWidth: 2 }))] }, workspace.id));
                }) })] }));
}
//# sourceMappingURL=WorkspaceTitleSwitcher.js.map