import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { cn } from "../../lib/utils";
import { Loader2, Calendar, CheckCircle2, GitBranch } from "lucide-react";
import { getDisplayableAssignee } from "../assigneeDisplay";
import { Badge } from "../../components/ui/badge";
import { format } from "date-fns";
import { useResolvedUser } from "../../editor/services/userResolution";
import { getTaskExecutionDisplayFromTask } from "../taskExecutionStatus";
import { TaskAssigneePicker } from "./TaskAssigneePicker";
import { TaskAssigneeBadge, taskAssigneePickerButtonClassName, } from "./TaskAssigneeBadge";
import { getTaskPriorityBadgeClass, getTaskPriorityLabel, } from "../taskPriority";
const defaultBorderColor = "border-neutral-grey-10";
const hoverBorderColor = "hover:border-neutral-grey-50";
const selectedColor = "border-highlight-100";
export function TaskCard(props) {
    const { task, selected, onClick, dependencyTaskIds = [], isBlocked = false, showDoneCheckmark = false, projectId, canEditAssignee = false, onAssigneeChange, } = props;
    const executionDisplay = getTaskExecutionDisplayFromTask({
        taskStatus: task.status,
        executionState: task.executionState,
        isBlocked,
    });
    const showExecutionBadge = !!executionDisplay &&
        executionDisplay.label !== "Not started" &&
        executionDisplay.label !== "Idle";
    const priorityLabel = getTaskPriorityLabel(task.priority);
    const priorityBadgeColor = getTaskPriorityBadgeClass(task.priority);
    const displayTaskId = task.taskKey || task.taskId.slice(0, 8).toUpperCase();
    const isPlanTask = String(task.taskType ?? "").toLowerCase() === "plan" ||
        task.title === "Project Plan";
    const isDecisionPointTask = String(task.taskType ?? "").toLowerCase() === "decisionpoint";
    const assigneeValue = getDisplayableAssignee(task, { isPlanTask });
    const shouldResolveUserAssignee = task.assigneeType === "user" &&
        !!task.assigneeId &&
        assigneeValue !== "Unassigned";
    const resolvedAssignee = useResolvedUser(shouldResolveUserAssignee ? task.assigneeId : null, { fullName: shouldResolveUserAssignee ? assigneeValue : null });
    const userAssigneeLabel = shouldResolveUserAssignee
        ? resolvedAssignee.displayName
        : assigneeValue;
    const canEditInlineAssignee = !!projectId && !!onAssigneeChange && canEditAssignee && !isPlanTask;
    const showTopMeta = showExecutionBadge ||
        !!priorityLabel ||
        isDecisionPointTask ||
        (showDoneCheckmark && task.status === "Done");
    const priorityBadge = priorityLabel ? (_jsx(Badge, { variant: "outline", size: "sm", className: cn("font-medium", priorityBadgeColor), children: priorityLabel })) : null;
    const handleKeyDown = (event) => {
        if (event.key !== "Enter" && event.key !== " ")
            return;
        event.preventDefault();
        event.currentTarget.click();
    };
    return (_jsx("div", { role: "button", tabIndex: 0, onClick: onClick, onKeyDown: handleKeyDown, className: cn("group relative w-full overflow-hidden rounded-[12px] border bg-white p-[12px] text-left transition-all", defaultBorderColor, hoverBorderColor, selected && selectedColor), children: _jsxs("div", { className: "flex flex-col gap-2", children: [showTopMeta && (_jsxs("div", { className: "flex min-h-5 items-center justify-between gap-2", children: [_jsx("div", { className: "flex min-w-0 items-center", children: showExecutionBadge && executionDisplay ? (_jsxs(Badge, { variant: executionDisplay.variant, size: "sm", className: cn("max-w-full font-medium", executionDisplay.className), title: executionDisplay.needsAttention
                                    ? "Assigned agent requires attention"
                                    : undefined, children: [executionDisplay.showSpinner && (_jsx(Loader2, { className: "mr-1 h-2.5 w-2.5 animate-spin" })), _jsx("span", { className: "truncate", children: executionDisplay.label })] })) : (priorityBadge) }), _jsxs("div", { className: "flex shrink-0 items-center gap-1", children: [showExecutionBadge ? priorityBadge : null, isDecisionPointTask && (_jsx(GitBranch, { className: "text-feedback-blue h-4 w-4" })), showDoneCheckmark && task.status === "Done" && (_jsx(CheckCircle2, { className: "text-feedback-green h-4 w-4" }))] })] })), _jsx("div", { className: "text-neutral-grey-100 line-clamp-2 text-sm leading-snug font-medium", children: task.title }), dependencyTaskIds.length > 0 && (_jsxs("div", { className: "text-neutral-grey-50 line-clamp-1 text-[10px] font-medium", children: ["Depends on: ", dependencyTaskIds.join(", ")] })), _jsxs("div", { className: "mt-0.5 flex flex-wrap items-center gap-2", children: [isDecisionPointTask && (_jsx(Badge, { variant: "outline", size: "sm", className: "text-2xs border-feedback-blue bg-feedback-blue-light text-feedback-blue", children: "Decision Point" })), canEditInlineAssignee ? (_jsx("div", { className: "min-w-0 self-start", onClick: (event) => event.stopPropagation(), onMouseDown: (event) => event.stopPropagation(), children: _jsx(TaskAssigneePicker, { projectId: projectId, assigneeType: task.assigneeType ?? null, value: task.assigneeId ?? null, displayValue: task.assigneeDisplayName ?? task.assigneeId ?? null, onChange: onAssigneeChange, placeholder: "Assign user or agent", buttonClassName: taskAssigneePickerButtonClassName, buttonTestId: `taskboard-task-assignee-${task.taskId}`, showChevron: false, showAssigneeIcon: true }) })) : (_jsx(TaskAssigneeBadge, { assigneeType: task.assigneeType ?? null, label: userAssigneeLabel, labelClassName: "max-w-[160px]" })), task.dueDate && (_jsxs("div", { className: "text-neutral-grey-50 badge-pad-sm bg-neutral-grey-5 flex items-center gap-1 rounded-md text-xs font-medium", children: [_jsx(Calendar, { className: "h-3 w-3" }), _jsx("span", { children: format(new Date(task.dueDate), "MMM d") })] }))] }), _jsx("div", { className: "text-neutral-grey-50 text-[10px] font-medium tracking-widest", children: displayTaskId })] }) }));
}
//# sourceMappingURL=TaskCard.js.map