/**
 * NodeWrapper - Shared wrapper component for all node types.
 *
 * Provides the common chrome shared by CustomNode, StartNode, GatewayNode,
 * and SubprocessNode: className construction, delete button, QuickAddButton,
 * and Annotation rendering.
 */
import React from 'react';
import { FiTrash2 } from 'react-icons/fi';
import classNames from 'classnames';
import { t } from '../../utils/translation';
import QuickAddButton from '../QuickAddButton';
import Annotation from '../Annotation';
import type { BaseNodeData } from '../../types/settings';

interface NodeWrapperProps {
  data: BaseNodeData;
  selected: boolean;
  /** The node-specific CSS class (e.g., 'action-node', 'start-node') */
  nodeClass: string;
  children: React.ReactNode;
}

const NodeWrapper: React.FC<NodeWrapperProps> = ({
  data,
  selected,
  nodeClass,
  children,
}) => {
  const handleDelete = (e: React.MouseEvent) => {
    e.stopPropagation();
    if (data.onDelete) {
      data.onDelete();
    }
  };

  return (
    <div className={classNames('custom-node', nodeClass, {
      selected: selected && !data.locked,
      'node-locked': data.locked,
      'node-has-annotation': data.annotation,
    })}>
      {children}

      {data.annotation && (
        <Annotation
          annotation={data.annotation}
          isVisible={data?.isAnnotationVisible}
          onToggle={data?.onToggleAnnotation}
        />
      )}

      {!data.isLocked && !data.locked && (
        <button
          className="node-delete-btn"
          onClick={handleDelete}
          title={t('Delete node')}
          aria-label={t('Delete node')}
        >
          <FiTrash2 />
        </button>
      )}

      {data.onQuickAdd && !data.isLocked && !data.locked && (
        <QuickAddButton onAddNode={data.onQuickAdd} />
      )}
    </div>
  );
};

export default NodeWrapper;
