/**
 * NodePorts Component
 *
 * Renders expandable port bars on nodes for connection points.
 * Orchestrates port grouping by side and delegates rendering
 * to PortBar (bar layout) and PortHandle (individual ports).
 *
 * @since 1.0.0
 * @refactored 1.4.0 — Extracted PortBar and PortHandle into separate files
 */

'use client';

import React from 'react';
import type { PortDefinition, PortSide } from '../core/port-types';
import { calculatePortPosition, getNodePorts } from '../core/port-types';
import { PortBar } from './PortBar';

// Re-export sub-components for barrel consumers
export { PortBar } from './PortBar';
export type { PortBarProps } from './PortBar';
export { PortHandle } from './PortHandle';
export type { PortHandleProps } from './PortHandle';

// --- Types ---

export interface NodePortsProps {
	/** Node ID */
	nodeId: string;
	/** Node position and dimensions */
	nodeX: number;
	nodeY: number;
	nodeWidth: number;
	nodeHeight: number;
	/** Port definitions (uses default if not provided) */
	ports?: PortDefinition[];
	/** Filter to show only specific sides */
	sides?: PortSide[];
	/** Callback when port is clicked to start a connection */
	onStartConnection?: (nodeId: string, portId: string, port: PortDefinition) => void;
	/** Callback when port is hovered */
	onPortHover?: (nodeId: string, portId: string | null, port: PortDefinition | null) => void;
	/** Whether this node is being hovered during edge drag */
	isDragTarget?: boolean;
	/** Source port info during drag (for compatibility checking) */
	dragSourcePort?: PortDefinition | null;
}

// --- Component ---

/**
 * Renders all port bars for a node.
 */
export function NodePorts({
	nodeId,
	nodeX,
	nodeY,
	nodeWidth,
	nodeHeight,
	ports: portsProp,
	sides,
	onStartConnection,
	onPortHover,
	isDragTarget = false,
	dragSourcePort,
}: NodePortsProps) {
	const ports = getNodePorts(portsProp);

	// Group ports by side
	const portsBySide: Record<PortSide, PortDefinition[]> = { top: [], right: [], bottom: [], left: [] };
	for (const port of ports) {
		portsBySide[port.side].push(port);
	}

	const sidesToRender = sides || (['left', 'right', 'top', 'bottom'] as PortSide[]);

	return (
		<>
			{sidesToRender.map((side) => {
				const sidePorts = portsBySide[side];
				if (sidePorts.length === 0) return null;

				return (
					<PortBar
						key={side}
						nodeId={nodeId}
						side={side}
						ports={sidePorts}
						nodeWidth={nodeWidth}
						nodeHeight={nodeHeight}
						onPortClick={(portId, port) => onStartConnection?.(nodeId, portId, port)}
						onPortHover={(portId, port) => onPortHover?.(nodeId, portId, port)}
						isDragTarget={isDragTarget}
						dragSourcePort={dragSourcePort}
					/>
				);
			})}
		</>
	);
}

// --- Port Position Helpers ---

/**
 * Get the world-space position of a specific port on a node.
 */
export function getPortWorldPosition(
	nodeX: number,
	nodeY: number,
	nodeWidth: number,
	nodeHeight: number,
	port: PortDefinition
) {
	return calculatePortPosition(nodeX, nodeY, nodeWidth, nodeHeight, port);
}
