import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React, { useState, useCallback } from "react";
import { cn } from "../../lib/utils";
import { Popover, PopoverContent, PopoverTrigger, } from "../../components/ui/popover";
function renderIcon(icon) {
    const iconElement = icon;
    if (!iconElement || !React.isValidElement(iconElement)) {
        return icon;
    }
    return React.cloneElement(iconElement, {
        className: cn("h-4 w-4", iconElement.props?.className),
        strokeWidth: iconElement.props?.strokeWidth ?? 1.5,
    });
}
/**
 * Mobile-only popover button to switch workspaces (Editor, AI Agents, Translation Management, etc.).
 * Renders a single trigger button showing the current workspace icon; opens a list to pick another.
 */
export function MobileWorkspacePopover({ workspaces, currentWorkspaceId, onWorkspaceSelect, showLabels = false, }) {
    const [open, setOpen] = useState(false);
    const handleSelect = useCallback((workspaceId) => {
        onWorkspaceSelect(workspaceId);
        setOpen(false);
    }, [onWorkspaceSelect]);
    if (workspaces.length === 0)
        return null;
    const currentWorkspace = workspaces.find((w) => w.id === currentWorkspaceId) ?? workspaces[0];
    const currentIcon = currentWorkspace?.icon;
    const currentTitle = currentWorkspace?.title;
    return (_jsxs(Popover, { open: open, onOpenChange: setOpen, children: [_jsx(PopoverTrigger, { asChild: true, children: _jsxs("button", { type: "button", "aria-label": "Change workspace", "aria-haspopup": "listbox", "aria-expanded": open, "data-testid": "workspace-mobile-popover", className: cn("flex aspect-square min-w-7 min-h-7 max-h-11 flex-col items-center justify-center rounded border border-gray-300 bg-gray-100 p-1 text-gray-900", showLabels ? "w-14 shrink-0" : ""), children: [_jsx("div", { className: cn("relative", showLabels ? "p-0.5" : ""), children: renderIcon(currentIcon) }), showLabels && currentTitle && (_jsx("span", { className: "mt-0.5 w-full truncate px-0.5 text-center text-[10px] leading-tight whitespace-nowrap", children: currentTitle }))] }) }), _jsx(PopoverContent, { side: "right", align: "start", className: "w-auto min-w-40 p-1", children: _jsx("div", { className: "flex flex-col gap-0.5", role: "listbox", "aria-label": "Choose workspace", children: workspaces.map((workspace) => {
                        const active = workspace.id === currentWorkspaceId;
                        return (_jsxs("button", { type: "button", role: "option", "aria-selected": active, "data-testid": `workspace-mobile-${workspace.id}`, className: cn("flex items-center gap-2 rounded px-3 py-2 text-left text-sm", "hover:bg-gray-100", active && "bg-gray-100 font-medium text-gray-900", !active && "text-gray-600"), onClick: () => handleSelect(workspace.id), children: [renderIcon(workspace.icon), _jsx("span", { children: workspace.title })] }, `workspace-${workspace.id}`));
                    }) }) })] }));
}
//# sourceMappingURL=MobileWorkspacePopover.js.map