import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useEffect, useMemo, useState } from "react";
import { Bot, RefreshCw, RotateCcw } from "lucide-react";
import { Button } from "../../../components/ui/button";
import { AgentTerminal } from "../../ai/AgentTerminal";
import { getAiProfilesErrorMessage, loadAiProfiles, } from "../../services/aiService";
export function ProjectTemplateAgentPanel({ templateName, agentId, loading, resetting, error, onStartOver, headerStart, }) {
    const [profiles, setProfiles] = useState([]);
    const [profilesError, setProfilesError] = useState(null);
    useEffect(() => {
        let cancelled = false;
        setProfilesError(null);
        loadAiProfiles()
            .then((items) => {
            if (!cancelled) {
                setProfiles(items || []);
            }
        })
            .catch((loadError) => {
            if (!cancelled) {
                setProfiles([]);
                setProfilesError(getAiProfilesErrorMessage(loadError));
            }
        });
        return () => {
            cancelled = true;
        };
    }, []);
    const agentStub = useMemo(() => {
        if (!agentId)
            return null;
        return {
            id: agentId,
            name: "",
            userId: "",
            updatedDate: "",
            status: "idle",
        };
    }, [agentId]);
    const headerTitle = templateName?.trim()
        ? `${templateName} Assistant`
        : "Template Assistant";
    return (_jsxs("div", { className: "flex h-full min-h-0 flex-col bg-white", "data-testid": "project-template-agent-panel", children: [_jsxs("div", { className: "flex items-center justify-between gap-2 border-b px-4 py-3", children: [_jsxs("div", { className: "flex min-w-0 flex-1 items-center gap-2", children: [headerStart ? (_jsx("div", { className: "shrink-0", children: headerStart })) : null, _jsx(Bot, { className: "h-5 w-5 shrink-0 text-muted-foreground" }), _jsx("div", { className: "min-w-0 truncate text-sm font-semibold", children: headerTitle })] }), _jsx("div", { className: "flex shrink-0 items-center gap-2", children: _jsxs(Button, { variant: "outline", size: "sm", onClick: onStartOver, disabled: !templateName || loading || resetting, title: "Create a fresh assistant conversation for this template", "data-testid": "project-template-agent-reset-button", children: [resetting ? (_jsx(RefreshCw, { className: "h-4 w-4 animate-spin", strokeWidth: 1.5 })) : (_jsx(RotateCcw, { className: "h-4 w-4", strokeWidth: 1.5 })), "Start Over"] }) })] }), profilesError ? (_jsx("div", { className: "border-b border-amber-200 bg-amber-50 px-4 py-2 text-xs text-amber-700", children: profilesError })) : null, error ? (_jsx("div", { className: "border-b border-red-200 bg-red-50 px-4 py-2 text-xs text-red-700", children: error })) : null, _jsx("div", { className: "min-h-0 flex-1", children: loading ? (_jsxs("div", { className: "flex h-full flex-col items-center justify-center gap-3 p-6 text-center", children: [_jsx(RefreshCw, { className: "h-8 w-8 animate-spin text-muted-foreground/60" }), _jsx("div", { className: "text-sm text-muted-foreground", children: "Loading template assistant..." })] })) : !templateName ? (_jsxs("div", { className: "flex h-full flex-col items-center justify-center gap-3 p-6 text-center", children: [_jsx(Bot, { className: "h-10 w-10 text-muted-foreground/40" }), _jsxs("div", { className: "grid gap-1", children: [_jsx("div", { className: "text-sm font-medium text-muted-foreground", children: "No template selected" }), _jsx("div", { className: "text-xs text-muted-foreground", children: "Select a project template to open its dedicated assistant." })] })] })) : !agentStub ? (_jsxs("div", { className: "flex h-full flex-col items-center justify-center gap-3 p-6 text-center", children: [_jsx(Bot, { className: "h-10 w-10 text-muted-foreground/40" }), _jsxs("div", { className: "grid gap-1", children: [_jsx("div", { className: "text-sm font-medium text-muted-foreground", children: "Assistant unavailable" }), _jsx("div", { className: "text-xs text-muted-foreground", children: "Try selecting the template again or use Start Over to create a fresh session." })] })] })) : (_jsx(AgentTerminal, { agentStub: agentStub, profiles: profiles, isActive: true, compact: true, hideGreeting: true, displayMode: "full", showSummaryInput: false, className: "h-full" })) })] }));
}
export default ProjectTemplateAgentPanel;
//# sourceMappingURL=ProjectTemplateAgentPanel.js.map