import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useEffect, useMemo, useState } from "react";
import { Bot, CheckCircle2, Play, UserPlus } from "lucide-react";
import { Button } from "../../components/ui/button";
import { AgentTerminal } from "../../editor/ai/AgentTerminal";
import { isAgentClosed as isClosedAgentStatus } from "../../editor/services/agentStatus";
import { LicenseFeatures, useFeature } from "../../licensing";
import { getAiProfilesErrorMessage, loadAiProfiles, } from "../../editor/services/aiService";
function isAgentClosed(status) {
    return isClosedAgentStatus(status);
}
export function TaskAgentPanel(props) {
    const { agentId, terminalKey, mode, label, onAssignAgent, onRunAssignedAgent, hasAssignedAgentProfile = false, assignedAgentProfileName, canAssignAgent = true, assignAgentDisabledReason, canRunAssignedAgent = true, runAssignedAgentDisabledReason, onStartPlanning, assignedAgentStatus, onReopenTask, } = props;
    const canUseAi = useFeature(LicenseFeatures.AI);
    const [profiles, setProfiles] = useState([]);
    const [profilesError, setProfilesError] = useState(null);
    useEffect(() => {
        if (!canUseAi) {
            setProfiles([]);
            setProfilesError(null);
            return;
        }
        let cancelled = false;
        setProfilesError(null);
        loadAiProfiles()
            .then((p) => {
            if (!cancelled)
                setProfiles(p);
        })
            .catch((error) => {
            if (cancelled)
                return;
            setProfiles([]);
            setProfilesError(getAiProfilesErrorMessage(error));
        });
        return () => {
            cancelled = true;
        };
    }, [canUseAi]);
    const agentStub = useMemo(() => {
        if (!agentId)
            return null;
        return {
            id: agentId,
            name: "",
            userId: "",
            updatedDate: "",
            status: "running",
        };
    }, [agentId]);
    const canPerformPrimaryAction = hasAssignedAgentProfile
        ? canRunAssignedAgent
        : canAssignAgent;
    const primaryActionDisabledReason = hasAssignedAgentProfile
        ? runAssignedAgentDisabledReason
        : assignAgentDisabledReason;
    const agentClosed = isAgentClosed(assignedAgentStatus);
    if (!canUseAi) {
        return (_jsxs("div", { className: "flex h-full flex-col border-l bg-white", "data-testid": "taskboard-agent-panel", children: [_jsxs("div", { className: "flex items-center gap-2 border-b px-4 py-3", children: [_jsx(Bot, { className: "text-muted-foreground h-5 w-5" }), _jsx("div", { className: "text-sm font-semibold", children: label || "Agent" })] }), _jsxs("div", { className: "flex flex-1 flex-col items-center justify-center gap-3 p-6 text-center", children: [_jsx(Bot, { className: "text-muted-foreground/40 h-10 w-10" }), _jsxs("div", { className: "grid gap-1", children: [_jsx("div", { className: "text-muted-foreground text-sm font-medium", children: "AI features are unavailable" }), _jsx("div", { className: "text-muted-foreground text-xs", children: "This license does not include task agents or planning." })] })] })] }));
    }
    // ── empty states ──
    if (mode === "no-task-selected") {
        return (_jsxs("div", { className: "flex h-full flex-col border-l bg-white", "data-testid": "taskboard-agent-panel", children: [_jsxs("div", { className: "flex items-center gap-2 border-b px-4 py-3", children: [_jsx(Bot, { className: "text-muted-foreground h-5 w-5" }), _jsx("div", { className: "text-sm font-semibold", children: label || "Agent" })] }), _jsxs("div", { className: "flex flex-1 flex-col items-center justify-center gap-3 p-6 text-center", children: [_jsx(Bot, { className: "text-muted-foreground/40 h-10 w-10" }), _jsxs("div", { className: "grid gap-1", children: [_jsx("div", { className: "text-muted-foreground text-sm font-medium", children: "No task selected" }), _jsx("div", { className: "text-muted-foreground text-xs", children: "Click on a task to see its agent here." })] })] })] }));
    }
    if (!agentStub) {
        // mode === "no-agent"
        return (_jsxs("div", { className: "flex h-full flex-col border-l bg-white", "data-testid": "taskboard-agent-panel", children: [_jsxs("div", { className: "flex items-center gap-2 border-b px-4 py-3", children: [_jsx(Bot, { className: "text-muted-foreground h-5 w-5" }), _jsx("div", { className: "text-sm font-semibold", children: label || "Agent" })] }), _jsxs("div", { className: "flex flex-1 flex-col items-center justify-center gap-4 p-6 text-center", children: [_jsx(Bot, { className: "text-muted-foreground/40 h-10 w-10" }), _jsxs("div", { className: "grid gap-1", children: [_jsxs("div", { className: "flex items-center justify-center gap-2", children: [_jsx("div", { className: "text-muted-foreground text-sm font-medium", children: hasAssignedAgentProfile
                                                ? "No agent running"
                                                : "No agent assigned" }), (onAssignAgent || onRunAssignedAgent) && (_jsxs(Button, { variant: "outline", size: "sm", onClick: hasAssignedAgentProfile ? onRunAssignedAgent : onAssignAgent, disabled: !canPerformPrimaryAction, title: !canPerformPrimaryAction
                                                ? primaryActionDisabledReason
                                                : undefined, children: [hasAssignedAgentProfile ? (_jsx(Play, { className: "mr-2 h-4 w-4" })) : (_jsx(UserPlus, { className: "mr-2 h-4 w-4" })), hasAssignedAgentProfile
                                                    ? "Run agent"
                                                    : "Assign and run agent"] }))] }), _jsx("div", { className: "text-muted-foreground text-xs", children: hasAssignedAgentProfile
                                        ? `This task already has an assigned agent profile (${assignedAgentProfileName || "Unknown profile"}).`
                                        : "This task does not have an agent assigned yet." }), profilesError && (_jsx("div", { className: "text-destructive text-xs", children: profilesError })), !canPerformPrimaryAction && primaryActionDisabledReason && (_jsx("div", { className: "text-feedback-orange text-xs", children: primaryActionDisabledReason }))] }), onStartPlanning && (_jsxs(Button, { variant: "default", size: "sm", onClick: onStartPlanning, children: [_jsx(Play, { className: "mr-1 h-4 w-4" }), "Start Planning"] }))] })] }));
    }
    // ── agent terminal ──
    return (_jsxs("div", { className: "flex h-full min-h-0 flex-col border-l bg-white", "data-testid": "taskboard-agent-panel", children: [_jsx("div", { className: "flex items-center justify-between gap-2 border-b px-4 py-3", children: _jsxs("div", { className: "flex items-center gap-2", children: [_jsx(Bot, { className: "text-muted-foreground h-5 w-5" }), _jsx("div", { className: "text-sm font-semibold", children: label || "Agent" })] }) }), profilesError ? (_jsx("div", { className: "border-destructive/20 bg-destructive/5 text-destructive border-b px-4 py-2 text-xs", children: profilesError })) : null, _jsx("div", { className: "min-h-0 flex-1", children: _jsx(AgentTerminal, { agentStub: agentStub, profiles: profiles, isActive: true, compact: true, hideGreeting: true, displayMode: "full", showSummaryInput: false, hideBottomControls: agentClosed, className: "h-full" }, terminalKey ?? agentId ?? "task-agent-terminal") }), agentClosed ? (_jsxs("div", { className: "border-feedback-green bg-feedback-green-light border-t px-4 py-3", children: [_jsxs("div", { className: "text-feedback-green flex items-center gap-2 text-sm font-medium", children: [_jsx(CheckCircle2, { className: "h-4 w-4 shrink-0" }), _jsx("span", { children: "This task's agent has finished." })] }), _jsx("p", { className: "text-feedback-green mt-1 text-xs", children: "Reopen this task to continue working with its agent." }), onReopenTask && (_jsx(Button, { variant: "outline", size: "sm", className: "border-feedback-green text-feedback-green hover:bg-feedback-green-light mt-2", onClick: onReopenTask, children: "Reopen" }))] })) : null] }));
}
//# sourceMappingURL=TaskAgentPanel.js.map