import type { AgentNodeRow } from "@/types/api/admin-agent";

export type FlatAgentOption = {
  id: number;
  label: string;
  depth: number;
};

export function flattenAgentTree(nodes: readonly AgentNodeRow[], depth = 0): FlatAgentOption[] {
  const out: FlatAgentOption[] = [];
  for (const node of nodes) {
    const prefix = depth > 0 ? `${"—".repeat(depth)} ` : "";
    out.push({
      id: node.id,
      depth,
      label: `${prefix}${node.name} (${node.code})`,
    });
    if (node.children?.length) {
      out.push(...flattenAgentTree(node.children, depth + 1));
    }
  }
  return out;
}
