import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React from "react";
import { getAgentTriggerSubscriptions, isAgentReadOnly, } from "../editor/services/agentService";
import { isAgentClosed, isRunningAgentStatus, } from "../editor/services/agentStatus";
import { getAgentStatusConfig } from "../editor/ai/AgentStatusBadge";
import { cn } from "../lib/utils";
import { sanitizeSvg } from "../lib/sanitize";
import { Users, AlertCircle, Clock, DollarSign, Coins, MessageSquare, HelpCircle, Bell, } from "lucide-react";
import { DeleteIcon } from "../components/ui/delete-icon";
import { CloseButton } from "../components/ui/close-button";
import { SimpleIconButton } from "../editor/ui/SimpleIconButton";
import { SecretAgentIcon } from "../editor/ui/Icons";
import { Spinner } from "../editor/ui/Spinner";
/**
 * Returns visual treatment for genuinely actionable states only.
 * Plain "completed" / "idle" agents are intentionally NOT highlighted as needing
 * attention — the status dot already conveys their state and the row should stay quiet.
 */
function getAttentionInfo(agent) {
    switch (agent.status) {
        case "waitingForApproval":
            return {
                icon: _jsx(Clock, { className: "size-3", strokeWidth: 2 }),
                label: "Needs Approval",
                chipClass: "bg-feedback-orange-light text-feedback-orange",
                barClass: "bg-feedback-orange",
                rowTintClass: "bg-feedback-orange-light/30 hover:bg-feedback-orange-light/60",
                priority: 1,
            };
        case "waitingForInput":
            return {
                icon: _jsx(HelpCircle, { className: "size-3", strokeWidth: 2 }),
                label: "Input Required",
                chipClass: "bg-feedback-orange-light text-feedback-orange",
                barClass: "bg-feedback-orange",
                rowTintClass: "bg-feedback-orange-light/30 hover:bg-feedback-orange-light/60",
                priority: 0,
            };
        case "costLimitReached":
            return {
                icon: _jsx(DollarSign, { className: "size-3", strokeWidth: 2 }),
                label: "Budget Exceeded",
                chipClass: "bg-component-shared-drop-zone text-component-shared",
                barClass: "bg-component-shared",
                rowTintClass: "bg-component-shared-drop-zone/30 hover:bg-component-shared-drop-zone/60",
                priority: 2,
            };
        case "error":
            return {
                icon: _jsx(AlertCircle, { className: "size-3", strokeWidth: 2 }),
                label: "Error",
                chipClass: "bg-feedback-red-light text-feedback-red",
                barClass: "bg-feedback-red",
                rowTintClass: "bg-feedback-red-light/30 hover:bg-feedback-red-light/60",
                priority: 3,
            };
        default:
            return null;
    }
}
/**
 * Priority order used by the inbox to sort agents.
 * Lower number = shown higher in the list.
 */
export function getInboxAttentionPriority(agent) {
    const info = getAttentionInfo(agent);
    if (info)
        return info.priority;
    if (agent.status === "completed")
        return 5;
    return 4;
}
export function AgentCard({ agent, onClick, onResume, onClose, onDelete, onDismiss, formatDateTime, selected = false, }) {
    const [loadedTriggerCount, setLoadedTriggerCount] = React.useState(agent.triggerSubscriptionCount ?? null);
    React.useEffect(() => {
        if (agent.triggerSubscriptionCount !== undefined) {
            setLoadedTriggerCount(agent.triggerSubscriptionCount);
            return;
        }
        let cancelled = false;
        void getAgentTriggerSubscriptions(agent.id)
            .then((subscriptions) => {
            if (cancelled)
                return;
            setLoadedTriggerCount(subscriptions.filter((subscription) => subscription.isActive).length);
        })
            .catch(() => {
            if (cancelled)
                return;
            setLoadedTriggerCount(0);
        });
        return () => {
            cancelled = true;
        };
    }, [agent.id, agent.triggerSubscriptionCount]);
    const statusConfig = getAgentStatusConfig(agent);
    const isClosed = isAgentClosed(agent.status);
    const isRunning = isRunningAgentStatus(agent.status);
    const attention = getAttentionInfo(agent);
    const handleCardClick = (event) => {
        if (isClosed && onResume) {
            onResume(agent, event);
        }
        else {
            onClick(agent, event);
        }
    };
    const lastActivity = agent.lastMessageDate
        ? formatDateTime(agent.lastMessageDate)
        : formatDateTime(agent.updatedDate);
    return (_jsxs("div", { className: cn("group relative flex cursor-pointer items-start gap-3 px-3 py-2.5 transition-colors", attention ? attention.rowTintClass : "hover:bg-neutral-grey-5/80", selected && !attention && "bg-neutral-grey-5"), onClick: handleCardClick, "data-testid": "agent-list-row", "data-agent-id": agent.id, title: `${agent.name}\nID: ${agent.id}\nAlt-click to open in a new pane`, children: [(selected || attention) && (_jsx("div", { className: cn("absolute top-2 bottom-2 left-0 w-0.5 rounded-r", selected ? "bg-neutral-grey-100" : attention?.barClass) })), _jsx("div", { className: "text-neutral-grey-50 mt-0.5 flex h-6 w-6 shrink-0 items-center justify-center", children: agent.profileSvgIcon ? (_jsx("div", { className: "h-6 w-6 [&>svg]:h-full [&>svg]:w-full", dangerouslySetInnerHTML: {
                        __html: sanitizeSvg(agent.profileSvgIcon),
                    } })) : (_jsx(SecretAgentIcon, { size: 22, strokeWidth: 1.5, className: "text-neutral-grey-50" })) }), _jsxs("div", { className: "min-w-0 flex-1", children: [_jsxs("div", { className: "flex items-center gap-2", children: [_jsx("h4", { className: "text-neutral-grey-100 truncate text-[13px] font-medium", children: agent.name }), !isRunning && (_jsx("span", { className: cn("h-1.5 w-1.5 shrink-0 rounded-full", statusConfig.color, statusConfig.shouldPulse && "animate-pulse"), title: statusConfig.label })), attention && (_jsxs("span", { className: cn("rounded-badge badge-pad-sm text-2xs inline-flex shrink-0 items-center gap-1 font-semibold", attention.chipClass), children: [attention.icon, attention.label] })), agent.isShared && (_jsx("span", { className: "rounded-badge bg-feedback-blue-light badge-pad-sm text-2xs text-feedback-blue shrink-0 font-bold", children: "Shared" }))] }), agent.description && (_jsx("p", { className: "text-neutral-grey-50 mt-0.5 truncate text-[12px]", children: agent.description })), _jsxs("div", { className: "text-neutral-grey-50 mt-1 flex flex-wrap items-center gap-x-3 gap-y-0.5 text-[10px]", children: [agent.nextRunAt && (_jsxs("span", { className: "text-feedback-orange flex items-center gap-0.5", children: [_jsx(Clock, { className: "size-2.5", strokeWidth: 2 }), "Wake: ", formatDateTime(agent.nextRunAt)] })), _jsxs("span", { className: "flex items-center gap-0.5", children: [_jsx(Clock, { className: "size-2.5", strokeWidth: 2 }), lastActivity] }), agent.isShared && agent.ownerName && (_jsxs("span", { className: "flex items-center gap-0.5", children: [_jsx(Users, { className: "size-2.5", strokeWidth: 2 }), "By ", agent.ownerName] })), agent.messageCount !== undefined && agent.messageCount > 0 && (_jsxs("span", { className: "flex items-center gap-0.5", children: [_jsx(MessageSquare, { className: "size-2.5", strokeWidth: 2 }), agent.messageCount, " msg"] })), loadedTriggerCount !== null && loadedTriggerCount > 0 && (_jsxs("span", { className: "flex items-center gap-0.5", children: [_jsx(Bell, { className: "size-2.5", strokeWidth: 2 }), loadedTriggerCount, " trigger", loadedTriggerCount !== 1 ? "s" : ""] })), agent.totalCost !== undefined && agent.totalCost > 0 && (_jsxs("span", { className: "flex items-center gap-0.5", children: [_jsx(Coins, { className: "size-2.5", strokeWidth: 2 }), "$", agent.totalCost.toFixed(2)] }))] })] }), _jsxs("div", { className: "flex shrink-0 items-center gap-1 self-center opacity-0 transition-opacity group-hover:opacity-100", children: [!isClosed && !onDismiss && !isAgentReadOnly(agent) && (_jsx(CloseButton, { onClick: (e) => {
                            e.stopPropagation();
                            if (isAgentReadOnly(agent))
                                return;
                            onClose?.(agent.id, agent.name);
                        }, label: "Close Agent" })), onDismiss ? (_jsx(CloseButton, { onClick: (e) => {
                            e.stopPropagation();
                            onDismiss(agent.id, agent.name);
                        }, label: "Dismiss" })) : (!isAgentReadOnly(agent) && (_jsx(SimpleIconButton, { onClick: (e) => {
                            if (isAgentReadOnly(agent))
                                return;
                            onDelete?.(agent.id, agent.name, e);
                        }, icon: _jsx(DeleteIcon, { size: "md" }), label: "Delete Agent", className: "text-neutral-grey-50 hover:bg-neutral-grey-5 hover:text-neutral-grey-50 h-7 w-7 rounded-md" })))] }), isRunning && (_jsx("span", { title: statusConfig.label, className: "text-neutral-grey-100 flex shrink-0 items-center self-center px-1", children: _jsx(Spinner, { size: "sm" }) }))] }, agent.id));
}
//# sourceMappingURL=AgentCard.js.map