import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useEffect, useRef, useState } from "react";
import { ArrowRight, CheckCircle2, FilePenLine, FileText, Loader2, TerminalSquare, } from "lucide-react";
import { MarkdownDisplay } from "../../components/MarkdownDisplay";
import { Button } from "../../components/ui/button";
import { CloseButton } from "../../components/ui/close-button";
import { Textarea } from "../../components/ui/textarea";
import { getDisplayableAssignee } from "../assigneeDisplay";
import { normalizeTaskStatus } from "../taskStatus";
import { TaskAttachmentsSection } from "./TaskAttachmentsSection";
import { TaskReviewActions } from "./TaskReviewActions";
import { compactMarkdownComponents, WizardTaskStatusBadge, } from "./WizardCommunicationShared";
import { TaskboardPersistentLogPanel } from "./TaskboardPersistentLogPanel";
import { TaskAssigneeBadge } from "./TaskAssigneeBadge";
const sectionHeadingClass = "text-xs font-medium tracking-wider text-neutral-grey-100 ";
export function WizardTaskDetailsPanel(props) {
    const { task, agentId, executionDisplay, canEdit, isPlanTask, isEditingDescription, localDescriptionMarkdown, isSavingDescription, isEditingPlan, localPlanMarkdown, isSavingPlan, shouldCollapseApprovalCommunication, openChatButton, canShowAgentChatTab = false, isAgentChatTabActive = false, agentChatContent, onDescriptionChange, onDescriptionSave, onDescriptionCancel, onPlanEdit, onPlanChange, onPlanSave, onPlanCancel, onTaskChanged, onAttachmentsChanged, onTaskApproved, showApprovedState, hasNextReviewTask, onReviewNext, onShowAgentChat, onShowTaskDetails, onCloseTask, } = props;
    const resultsContentRef = useRef(null);
    const [isResultsExpanded, setIsResultsExpanded] = useState(false);
    const [resultsCanExpand, setResultsCanExpand] = useState(false);
    useEffect(() => {
        setIsResultsExpanded(false);
    }, [task?.taskId, task?.resultData]);
    useEffect(() => {
        if (!task || isEditingPlan || isAgentChatTabActive) {
            setResultsCanExpand(false);
            return;
        }
        const content = resultsContentRef.current;
        if (!content) {
            setResultsCanExpand(false);
            return;
        }
        setResultsCanExpand(content.scrollHeight > 400);
    }, [isAgentChatTabActive, isEditingPlan, task?.resultData, task?.taskId]);
    if (!task) {
        return null;
    }
    const hasReviewActions = Boolean(onTaskChanged && onTaskApproved !== undefined);
    const normalizedTaskStatus = normalizeTaskStatus(task.status, task.executionState);
    const isReviewTask = normalizedTaskStatus === "Review";
    const isTaskApproved = showApprovedState === true && normalizedTaskStatus === "Done";
    const shouldShowFooter = Boolean(onTaskChanged && (isReviewTask || isTaskApproved));
    const taskStatusLabel = task.status === "InProgress"
        ? "In progress"
        : task.status === "Waiting"
            ? "Waiting"
            : (task.status ?? "-");
    const shouldShowExecutionDisplay = Boolean(executionDisplay &&
        executionDisplay.label.trim().toLowerCase() !==
            taskStatusLabel.trim().toLowerCase());
    const taskTabClassName = isAgentChatTabActive
        ? "border-transparent text-neutral-grey-50 hover:text-neutral-grey-100"
        : "border-black text-neutral-grey-100";
    const chatTabClassName = isAgentChatTabActive
        ? "border-black text-neutral-grey-100"
        : "border-transparent text-neutral-grey-50 hover:text-neutral-grey-100";
    return (_jsx("div", { className: "flex min-h-0 flex-1 flex-col overflow-hidden px-5 pt-4", children: _jsxs("div", { className: "flex min-h-0 flex-1 flex-col overflow-hidden", children: [_jsxs("div", { className: "flex shrink-0 items-start justify-between gap-3", children: [_jsx("div", { className: "min-w-0 flex-1", children: _jsx("h2", { className: "text-neutral-grey-100 mt-1 mb-2 text-lg font-medium", children: task.title }) }), _jsxs("div", { className: "flex shrink-0 items-center gap-2", children: [_jsx(TaskboardPersistentLogPanel, { scope: "task", projectId: task.projectId, taskId: task.taskId, title: "Execution Log", emptyMessage: "No task-scoped log entries yet.", refreshKey: `${task.taskId}:${task.executionUpdatedAt ?? task.updatedAt ?? ""}:${task.executionState}:${task.status}`, testId: "taskboard-wizard-task-log-panel" }), onCloseTask ? (_jsx(CloseButton, { label: "Close current task", "data-testid": "taskboard-wizard-close-task", onClick: onCloseTask })) : null] })] }), _jsxs("div", { className: "mt-1 flex shrink-0 flex-wrap gap-2 text-xs", children: [_jsx(WizardTaskStatusBadge, { status: task.status, testId: "taskboard-wizard-communication-task-status" }), shouldShowExecutionDisplay && executionDisplay ? (_jsx("span", { className: `rounded-badge badge-pad text-2xs border font-medium ${executionDisplay.className ||
                                (executionDisplay.needsAttention
                                    ? "border-feedback-orange bg-feedback-orange-light text-feedback-orange"
                                    : "border-border-default bg-neutral-grey-5 text-neutral-grey-100")}`, children: executionDisplay.label })) : null, (() => {
                            const assignee = getDisplayableAssignee(task, { isPlanTask });
                            return assignee !== "Unassigned" ? (_jsx(TaskAssigneeBadge, { assigneeType: task.assigneeType ?? null, label: assignee, className: "max-w-none", iconClassName: "text-neutral-grey-100", labelClassName: "max-w-none overflow-visible text-clip" })) : null;
                        })()] }), canShowAgentChatTab ? (_jsxs("div", { className: "border-border-default mt-3 flex shrink-0 items-center gap-1 border-b", role: "tablist", "aria-label": "Task workspace", children: [_jsxs("button", { type: "button", role: "tab", "aria-selected": !isAgentChatTabActive, className: `flex h-9 items-center gap-1.5 border-b-[3px] px-2.5 text-xs font-medium transition-colors ${taskTabClassName}`, "data-testid": "taskboard-wizard-task-tab", onClick: onShowTaskDetails, children: [_jsx(FileText, { strokeWidth: 1, className: "h-3.5 w-3.5" }), "Task"] }), _jsxs("button", { type: "button", role: "tab", "aria-selected": isAgentChatTabActive, className: `flex h-9 items-center gap-1.5 border-b-[3px] px-2.5 text-xs font-medium transition-colors ${chatTabClassName}`, "data-testid": "taskboard-wizard-agent-chat-tab", onClick: onShowAgentChat, children: [_jsx(TerminalSquare, { strokeWidth: 1, className: "h-3.5 w-3.5" }), "Agent chat"] })] })) : null, _jsx("div", { className: `min-h-0 flex-1 ${isAgentChatTabActive && agentChatContent
                        ? "overflow-hidden"
                        : `overflow-y-auto pr-2 ${hasReviewActions ? "pb-6" : "pb-4"}`}`, children: isAgentChatTabActive && agentChatContent ? (_jsx("div", { className: "h-full min-h-0", children: agentChatContent })) : (_jsxs("div", { className: "space-y-3", children: [_jsxs("div", { className: canShowAgentChatTab ? "pt-5" : "pt-2", children: [_jsx("div", { className: "flex items-center justify-between gap-2", children: _jsx("div", { className: sectionHeadingClass, children: "Description" }) }), isEditingDescription ? (_jsxs("div", { className: "mt-1 space-y-2", children: [_jsx(Textarea, { value: localDescriptionMarkdown, onChange: (event) => onDescriptionChange(event.target.value), rows: 8, disabled: isSavingDescription, className: "min-h-[180px] font-mono text-sm" }), _jsxs("div", { className: "flex items-center gap-2", children: [_jsxs(Button, { type: "button", size: "sm", onClick: onDescriptionSave, disabled: isSavingDescription, children: [isSavingDescription ? (_jsx(Loader2, { strokeWidth: 1, className: "h-3.5 w-3.5 animate-spin" })) : null, "Save description"] }), _jsx(Button, { type: "button", variant: "outline", size: "sm", onClick: onDescriptionCancel, disabled: isSavingDescription, children: "Cancel" })] })] })) : task.description?.trim() ? (_jsx(MarkdownDisplay, { source: task.description, className: "text-neutral-grey-100 mt-1 text-xs", components: compactMarkdownComponents })) : (_jsx("p", { className: "text-neutral-grey-100 mt-1 text-xs", children: "No task description yet." }))] }), task.resultData?.trim() || (isPlanTask && canEdit) ? (_jsxs("div", { className: "border-border-default/60 border-t pt-4", children: [_jsxs("div", { className: "flex items-center justify-between gap-2", children: [_jsx("div", { className: sectionHeadingClass, children: "Results" }), isPlanTask && canEdit && !isEditingPlan ? (_jsxs(Button, { type: "button", variant: "outline", size: "sm", className: "h-7 px-2 text-[11px]", onClick: onPlanEdit, children: [_jsx(FilePenLine, { strokeWidth: 1, className: "h-3.5 w-3.5" }), "Edit plan"] })) : null] }), isPlanTask && isEditingPlan ? (_jsxs("div", { className: "mt-1 space-y-2", children: [_jsx(Textarea, { value: localPlanMarkdown, onChange: (event) => onPlanChange(event.target.value), rows: 12, disabled: isSavingPlan, className: "min-h-[220px] font-mono text-sm" }), _jsxs("div", { className: "flex items-center gap-2", children: [_jsxs(Button, { type: "button", size: "sm", onClick: onPlanSave, disabled: isSavingPlan, children: [isSavingPlan ? (_jsx(Loader2, { strokeWidth: 1, className: "h-3.5 w-3.5 animate-spin" })) : null, "Save plan"] }), _jsx(Button, { type: "button", variant: "outline", size: "sm", onClick: onPlanCancel, disabled: isSavingPlan, children: "Cancel" })] })] })) : task.resultData?.trim() ? (_jsxs("div", { className: "mt-1", children: [_jsxs("div", { ref: resultsContentRef, className: `relative ${isResultsExpanded
                                                    ? ""
                                                    : "max-h-[400px] overflow-hidden"}`, children: [_jsx(MarkdownDisplay, { source: task.resultData, className: "text-neutral-grey-100 text-xs", components: compactMarkdownComponents }), !isResultsExpanded && resultsCanExpand ? (_jsx("div", { className: "pointer-events-none absolute right-0 bottom-0 left-0 h-12 bg-gradient-to-t from-white to-transparent" })) : null] }), resultsCanExpand ? (_jsx(Button, { type: "button", variant: "ghost", size: "sm", className: "text-neutral-grey-50 hover:text-neutral-grey-100 mt-2 h-7 px-2 text-xs", onClick: () => setIsResultsExpanded((value) => !value), children: isResultsExpanded ? "Show less" : "Show more" })) : null] })) : (_jsx("p", { className: "text-neutral-grey-100 mt-1 text-xs", children: "No plan has been written yet." }))] })) : null, _jsx(TaskAttachmentsSection, { task: task, canEdit: canEdit, onTaskChanged: onTaskChanged, onAttachmentsChanged: onAttachmentsChanged, className: "pt-4" })] })) }), shouldShowFooter ? (_jsx("div", { className: "border-border-default shrink-0 border-t bg-white px-1 py-3", children: _jsxs("div", { className: "flex flex-wrap items-center justify-between gap-2", children: [isTaskApproved ? (_jsxs("div", { className: "text-feedback-green flex min-w-0 items-center gap-2 text-xs font-medium", children: [_jsx(CheckCircle2, { strokeWidth: 1, className: "h-4 w-4 shrink-0" }), _jsx("span", { className: "truncate", children: "Approved" })] })) : (_jsx("div", {})), _jsx("div", { className: "flex flex-wrap items-center justify-end gap-2", children: isTaskApproved ? (hasNextReviewTask ? (_jsxs(Button, { type: "button", size: "sm", onClick: onReviewNext, className: "h-8 px-3 text-xs", children: ["Review next", _jsx(ArrowRight, { strokeWidth: 1, className: "ml-1.5 h-3.5 w-3.5" })] })) : (_jsx("span", { className: "text-neutral-grey-50 text-xs", children: "No more review tasks" }))) : (_jsxs(_Fragment, { children: [shouldCollapseApprovalCommunication
                                            ? openChatButton
                                            : null, _jsx(TaskReviewActions, { task: task, agentId: agentId, canEdit: canEdit, onTaskChanged: onTaskChanged, onTaskApproved: onTaskApproved, approveLast: true, className: "flex flex-wrap items-center gap-2" })] })) })] }) })) : null] }) }));
}
//# sourceMappingURL=WizardTaskDetailsPanel.js.map