import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import React, { useState, useCallback, useRef } from "react";
import { Target, FileText, ListTodo, GitBranch, Plus, Upload, Image as ImageIcon, FilePlus2, } from "lucide-react";
import { Popover, PopoverContent, PopoverTrigger, } from "../../../../components/ui/popover";
import { Tooltip, TooltipContent, TooltipTrigger, } from "../../../../components/ui/tooltip";
import { CountBadge } from "../../../../components/ui/badge";
import { AgentDocumentList, } from "./AgentDocumentList";
import { SpawnedAgentsPanel } from "./SpawnedAgentsPanel";
import { TodoListPanel } from "./TodoListPanel";
import { computeAgentTodos } from "../../agentTodoExtraction";
import { useEditContext } from "../../../client/editContext";
import { cn } from "../../../../lib/utils";
import { hasViewedContent, mergeViewedContext } from "../agentContextMerge";
import { AgentContextItemPicker } from "./AgentContextItemPicker";
const FILE_UPLOAD_ACCEPT = ".txt,.md,.pdf,.docx,.xlsx,.pptx,.csv,.json,.xml,.cs,.js,.ts,.tsx,.jsx,.py,.java,.cpp,.c,.h,.hpp,.cshtml,.razor,.vb,.php,.rb,.go,.rs,.swift,.kt,.scala,.sql,.sh,.ps1,.bat,.yaml,.yml,.png,.jpg,.jpeg,.gif,.webp";
function TrayIcon({ id, title, tooltip, icon: Icon, count, open, onOpenChange, renderBody, tone = "default", }) {
    const isLive = tone === "live";
    const [actionsEl, setActionsEl] = useState(null);
    const badgeText = count !== undefined && count > 0
        ? count > 99
            ? "99+"
            : String(count)
        : null;
    return (_jsxs(Popover, { open: open, onOpenChange: onOpenChange, children: [_jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { asChild: true, children: _jsx(PopoverTrigger, { asChild: true, children: _jsxs("button", { type: "button", "aria-label": title, className: cn("relative inline-flex h-6 w-6 items-center justify-center rounded transition-colors", isLive
                                    ? open
                                        ? "bg-feedback-green-light text-feedback-green"
                                        : "text-feedback-green hover:bg-neutral-grey-10 hover:text-feedback-green"
                                    : open
                                        ? "bg-neutral-grey-10 text-neutral-grey-100"
                                        : "text-neutral-grey-100 hover:bg-neutral-grey-10"), "data-testid": `tray-icon-${id}`, "data-live": isLive ? "true" : undefined, children: [_jsx(Icon, { className: "h-4 w-4", strokeWidth: 1.75 }), badgeText !== null && (_jsx(CountBadge, { className: "absolute -top-0.5 -right-0.5 min-w-[10px]", "aria-hidden": "true", children: badgeText }))] }) }) }), !open && (_jsx(TooltipContent, { side: "top", sideOffset: 6, children: tooltip ?? title }))] }), _jsxs(PopoverContent, { align: "start", side: "top", sideOffset: 6, className: "w-[420px] max-w-[90vw] p-0", children: [_jsxs("div", { className: "border-border-default flex items-center justify-between gap-2 border-b px-3 py-2", children: [_jsxs("div", { className: "flex items-center gap-1.5", children: [_jsx(Icon, { className: "text-neutral-grey-50 h-3.5 w-3.5", strokeWidth: 1.5 }), _jsx("span", { className: "text-neutral-grey-100 text-xs font-medium", children: title })] }), _jsx("div", { ref: setActionsEl, className: "flex items-center gap-1" })] }), renderBody(actionsEl)] })] }));
}
export function AgentPromptTrayPopovers({ contextChrome, agent, activeProfile, documentListRef, hasTodoContent, hasSpawnedAgents, messages, agentMetadata, setAgentMetadata, setAgent, viewedContext, }) {
    const [openPanel, setOpenPanel] = useState(null);
    const [documentCount, setDocumentCount] = useState(0);
    const [addMenuOpen, setAddMenuOpen] = useState(false);
    const [addMenuView, setAddMenuView] = useState("menu");
    const uploadInputRef = useRef(null);
    const editContext = useEditContext();
    const canSelectMedia = !!editContext?.selectMedia;
    const handleDocumentCount = useCallback((n) => {
        setDocumentCount(n);
    }, []);
    const makeHandler = (id) => (open) => {
        setOpenPanel(open ? id : null);
    };
    const includeEditorContext = activeProfile?.includeEditorContext === true;
    const includeLiveEditorContext = activeProfile?.includeLiveEditorContext === true;
    const hasLiveViewedContext = includeLiveEditorContext && hasViewedContent(viewedContext);
    const ctx = hasLiveViewedContext
        ? mergeViewedContext(agentMetadata, viewedContext)
        : agentMetadata;
    const contextCount = (includeEditorContext ? (ctx?.items?.length ?? 0) : 0) +
        (includeEditorContext ? (ctx?.components?.length ?? 0) : 0) +
        (includeEditorContext && ctx?.field ? 1 : 0) +
        (ctx?.comment ? 1 : 0);
    const spawnedCount = Array.isArray(agentMetadata?.childAgents)
        ? agentMetadata.childAgents.length
        : 0;
    const todos = React.useMemo(() => computeAgentTodos(messages, agentMetadata), [messages, agentMetadata]);
    const todosCompleted = todos.filter((t) => t.status === "completed").length;
    const showDocumentsIcon = false;
    // The add menu is visible when either profile document actions or explicit
    // item context selection are available.
    const showUploadActions = !!agent?.id && !!activeProfile?.enableDocumentUpload && !!documentListRef;
    const showAddItemsToContext = !!agent?.id && activeProfile?.includeEditorContext === true;
    const showAddButton = showUploadActions || showAddItemsToContext;
    const showContextIcon = !!agent;
    const showTodosIcon = hasTodoContent;
    const showSpawnedIcon = hasSpawnedAgents;
    const renderContextNode = (actionsContainer) => React.isValidElement(contextChrome)
        ? React.cloneElement(contextChrome, {
            headerless: true,
            headerActionsContainer: actionsContainer,
        })
        : contextChrome;
    return (_jsxs("div", { className: "flex items-center gap-1", children: [showAddButton && (_jsxs(_Fragment, { children: [showUploadActions && (_jsx("input", { ref: uploadInputRef, type: "file", className: "hidden", onChange: (event) => {
                            void documentListRef.current?.uploadFiles(event.target.files ?? []);
                            event.target.value = "";
                        }, accept: FILE_UPLOAD_ACCEPT })), _jsxs(Popover, { open: addMenuOpen, onOpenChange: (open) => {
                            setAddMenuOpen(open);
                            if (!open)
                                setAddMenuView("menu");
                        }, children: [_jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { asChild: true, children: _jsx(PopoverTrigger, { asChild: true, children: _jsx("button", { type: "button", "aria-label": "Add", className: cn("inline-flex h-6 w-6 items-center justify-center rounded transition-colors", addMenuOpen
                                                    ? "bg-neutral-grey-10 text-neutral-grey-100"
                                                    : "text-neutral-grey-100 hover:bg-neutral-grey-10"), "data-testid": "tray-add-button", children: _jsx(Plus, { className: "h-4 w-4", strokeWidth: 1.75 }) }) }) }), !addMenuOpen && (_jsx(TooltipContent, { side: "top", sideOffset: 6, children: "Add" }))] }), _jsx(PopoverContent, { align: "start", side: "top", sideOffset: 6, className: cn(addMenuView === "items"
                                    ? "w-[460px] max-w-[90vw] p-0"
                                    : "w-52 p-1"), children: addMenuView === "items" ? (_jsx(AgentContextItemPicker, { agent: agent, agentMetadata: agentMetadata, setAgentMetadata: setAgentMetadata, setAgent: setAgent, onBack: () => setAddMenuView("menu"), onDone: () => {
                                        setAddMenuOpen(false);
                                        setAddMenuView("menu");
                                    } })) : (_jsxs(_Fragment, { children: [showAddItemsToContext && (_jsxs("button", { type: "button", className: "text-neutral-grey-100 hover:bg-neutral-grey-10 flex w-full items-center gap-2 rounded px-2 py-1.5 text-left text-xs transition-colors", "data-testid": "tray-add-items-to-context", onClick: () => setAddMenuView("items"), children: [_jsx(FilePlus2, { className: "text-neutral-grey-50 h-3.5 w-3.5", strokeWidth: 1.5 }), "Add items to context"] })), showUploadActions && (_jsxs("button", { type: "button", className: "text-neutral-grey-100 hover:bg-neutral-grey-10 flex w-full items-center gap-2 rounded px-2 py-1.5 text-left text-xs transition-colors", "data-testid": "tray-add-upload-file", onClick: () => {
                                                setAddMenuOpen(false);
                                                uploadInputRef.current?.click();
                                            }, children: [_jsx(Upload, { className: "text-neutral-grey-50 h-3.5 w-3.5", strokeWidth: 1.5 }), "Upload file"] })), showUploadActions && canSelectMedia && (_jsxs("button", { type: "button", className: "text-neutral-grey-100 hover:bg-neutral-grey-10 flex w-full items-center gap-2 rounded px-2 py-1.5 text-left text-xs transition-colors", "data-testid": "tray-add-select-media", onClick: () => {
                                                setAddMenuOpen(false);
                                                void documentListRef.current?.browseMedia();
                                            }, children: [_jsx(ImageIcon, { className: "text-neutral-grey-50 h-3.5 w-3.5", strokeWidth: 1.5 }), "Select media"] }))] })) })] })] })), showContextIcon && (_jsx(TrayIcon, { id: "context", title: "Context", tooltip: contextCount > 0
                    ? `Context - ${contextCount} item${contextCount === 1 ? "" : "s"}`
                    : "Context", icon: Target, count: contextCount, open: openPanel === "context", onOpenChange: makeHandler("context"), tone: hasLiveViewedContext ? "live" : "default", renderBody: (actionsEl) => (_jsx("div", { className: "max-h-[60vh] overflow-y-auto", children: renderContextNode(actionsEl) })) })), showDocumentsIcon && (_jsx(TrayIcon, { id: "documents", title: "Documents", tooltip: documentCount > 0
                    ? `Documents - ${documentCount} uploaded`
                    : "Documents", icon: FileText, count: documentCount, open: openPanel === "documents", onOpenChange: makeHandler("documents"), renderBody: (actionsEl) => (_jsx("div", { className: "max-h-[60vh] overflow-y-auto", children: _jsx(AgentDocumentList, { agentId: agent.id, maxFileSizeMB: activeProfile.maxDocumentSizeMB ?? 10, enabled: activeProfile.enableDocumentUpload ?? false, profileId: activeProfile.id, headerless: true, headerActionsContainer: actionsEl, onCountChange: handleDocumentCount, onRequestClose: () => setOpenPanel(null) }, `popover-${agent.id}-${agent.updatedDate || ""}-${activeProfile.id}`) })) })), showTodosIcon && (_jsx(TrayIcon, { id: "todos", title: "Todos", tooltip: todos.length > 0
                    ? `Todos - ${todosCompleted}/${todos.length} completed`
                    : "Todos", icon: ListTodo, count: todos.length, open: openPanel === "todos", onOpenChange: makeHandler("todos"), renderBody: () => (_jsx("div", { className: "max-h-[60vh] overflow-y-auto", children: _jsx(TodoListPanel, { messages: messages, agentMetadata: agentMetadata, headerless: true }) })) })), showSpawnedIcon && (_jsx(TrayIcon, { id: "spawned", title: "Spawned Agents", tooltip: spawnedCount > 0
                    ? `Spawned agents - ${spawnedCount}`
                    : "Spawned agents", icon: GitBranch, count: spawnedCount, open: openPanel === "spawned", onOpenChange: makeHandler("spawned"), renderBody: () => (_jsx("div", { className: "max-h-[60vh] overflow-y-auto", children: _jsx(SpawnedAgentsPanel, { agentMetadata: agentMetadata, headerless: true }) })) }))] }));
}
//# sourceMappingURL=AgentPromptTrayPopovers.js.map