import { memo } from 'react';
import NodeShell from '../components/NodeShell.jsx';
import { NodeDraftInput } from '../components/NodeDraftField.jsx';
import { useWorkflow } from '../context/WorkflowContext.jsx';
import { getNodeAccent } from '../lib/nodeRegistry.js';

function WaitFlowNode({ id, data, selected, type }) {
  const { getNodeHandles, patchNodeData } = useWorkflow();
  const handles = getNodeHandles(type, data);
  const runtime = data.runtime || {};

  return (
    <NodeShell
      nodeId={id}
      title={data.title}
      accent={getNodeAccent(type)}
      selected={selected}
      status={runtime.status}
      inputs={handles.inputs}
      outputs={handles.outputs}
    >
      <div className="field-stack">
        <label className="field-row">
          <span>Wait, ms</span>
          <NodeDraftInput
            className="nodrag node-input"
            type="number"
            min="0"
            step="100"
            value={data.ms || 1000}
            onCommit={(value) => patchNodeData(id, { ms: Math.max(0, Number(value) || 0) })}
          />
        </label>

        <div className="node-note">
          {runtime.waitedMs !== undefined
            ? `Ожидание завершено: ${runtime.waitedMs} ms.`
            : 'Задерживает проход сценария на заданное число миллисекунд.'}
        </div>

        {runtime.error ? <div className="node-error">{runtime.error}</div> : null}
      </div>
    </NodeShell>
  );
}

export default memo(WaitFlowNode);
