"use client";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { memo } from "react";
import { CATEGORY_COLORS } from "./canvas-constants";
import { getVisualSpec } from "./task-type-visual-registry";
import { NodeShell, NodeContent, NodeHandles, NodeActions, ExecutionBadge, BranchBadge, DiffBadge } from "./node-shell";
import { useWorkflowGraphMode } from "./WorkflowGraphModeContext";
const NESTED_TASK_KINDS = new Set(["fork", "for_each", "try_catch"]);
/**
 * Unified workflow node component for the React Flow canvas.
 *
 * Composes single-responsibility sub-components and adapts rendering
 * based on the current graph mode (design / overview / execution):
 * - `NodeShell` — visual shape boundary (CSS or SVG based on visual class)
 * - `NodeContent` — task name + kind badge
 * - `NodeHandles` — connection ports driven by the visual registry
 * - `NodeActions` — design-mode only interaction affordances
 * - `ExecutionBadge` — execution-mode only status badge
 */
export const WorkflowNode = memo(function WorkflowNode({ id, data, selected, }) {
    const mode = useWorkflowGraphMode();
    const visualSpec = getVisualSpec(data.isSentinel ? id : data.kindString);
    const categoryColor = CATEGORY_COLORS[data.category];
    const errorCount = data.errorCount ?? 0;
    const isNested = NESTED_TASK_KINDS.has(data.kindString);
    const executionState = data.executionState;
    const diffState = data.diffState;
    return (_jsxs("div", { "data-visual-class": data.visualClass, "data-task-kind": data.kindString, "data-execution-status": executionState?.status, "data-diff-status": diffState?.status, "aria-label": buildAriaLabel(data, errorCount, executionState?.status), role: "button", className: "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--stgm-ring,#3b82f6)] focus-visible:ring-offset-1 rounded-sm", children: [_jsx(NodeShell, { visualClass: visualSpec.visualClass, width: visualSpec.defaultWidth, height: visualSpec.defaultHeight, captionHeight: visualSpec.captionHeight, categoryColor: categoryColor, selected: selected, errorCount: mode === "design" ? errorCount : 0, executionStatus: executionState?.status, diffStatus: mode === "diff" ? diffState?.status : undefined, children: _jsx(NodeContent, { visualClass: visualSpec.visualClass, taskName: data.taskName, displayName: data.displayName, categoryColor: categoryColor, captionHeight: visualSpec.captionHeight, isNested: isNested }) }), mode === "design" && errorCount > 0 && (_jsx("span", { className: "absolute -right-1.5 -top-1.5 z-20 flex h-4 min-w-4 items-center justify-center rounded-full bg-[var(--stgm-destructive,#ef4444)] px-1 text-[9px] font-bold leading-none text-[var(--stgm-destructive-foreground,#fff)]", title: `${errorCount} validation ${errorCount === 1 ? "error" : "errors"}`, children: errorCount })), mode === "execution" && executionState && (_jsx(ExecutionBadge, { status: executionState.status, attemptNumber: executionState.attemptNumber, forkProgress: data.forkProgress ?? undefined, approvalToolName: data.approvalToolName, agentActivity: data.agentActivity ?? undefined })), mode === "diff" && diffState && (_jsx(DiffBadge, { status: diffState.status, changedFieldCount: diffState.changedFields?.length })), _jsx(NodeHandles, { portPattern: visualSpec.portPattern, data: data }), mode === "design" && isNested && (_jsx(BranchBadge, { kindString: data.kindString, config: data.config })), mode === "design" && !data.isSentinel && (_jsx(NodeActions, { nodeId: id, taskName: data.taskName, kindString: data.kindString }))] }));
});
function buildAriaLabel(data, errorCount, executionStatus) {
    const base = `${data.displayName} node ${data.taskName}, ${data.ariaShapeLabel} shape`;
    const parts = [base];
    const config = data.config;
    if (data.kindString === "switch_case" && Array.isArray(config.cases)) {
        const caseNames = config.cases
            .filter((c) => c && typeof c.name === "string")
            .map((c) => c.name);
        if (caseNames.length > 0) {
            parts.push(`${caseNames.length} branches: ${caseNames.join(", ")}`);
        }
    }
    else if (data.kindString === "fork" && Array.isArray(config.branches)) {
        const branchNames = config.branches
            .filter((b) => b && typeof b.name === "string")
            .map((b) => b.name);
        if (branchNames.length > 0) {
            const joinPolicy = config.compete === true ? "race mode" : "wait for all";
            parts.push(`${branchNames.length} branches: ${branchNames.join(", ")}. Join policy: ${joinPolicy}`);
        }
    }
    else if (data.kindString === "try_catch" && config.catch && typeof config.catch === "object") {
        const catchAs = config.catch.as;
        parts.push(`catch handler: ${catchAs || "error"}`);
    }
    else if (data.kindString === "for_each") {
        const par = config.max_parallelism || 0;
        parts.push(par > 0 ? `concurrency: ${par}` : "sequential");
    }
    if (executionStatus && executionStatus !== "not_reached") {
        parts.push(executionStatus.replace(/_/g, " "));
    }
    if (errorCount > 0) {
        parts.push(`${errorCount} ${errorCount === 1 ? "error" : "errors"}`);
    }
    return parts.join(", ");
}
//# sourceMappingURL=WorkflowNode.js.map