import type { EntryOut } from '../../types/api'; export type EntryNode = EntryOut & { key: string; children?: EntryNode[] }; /** Дерево строк ОСВ по parent_entry_id; порядок — как пришёл (по row_number). */ export function buildEntryTree(entries: EntryOut[]): EntryNode[] { const nodes = new Map(); for (const entry of entries) nodes.set(entry.id, { ...entry, key: entry.id }); const roots: EntryNode[] = []; for (const entry of entries) { const node = nodes.get(entry.id) as EntryNode; const parent = entry.parent_entry_id ? nodes.get(entry.parent_entry_id) : undefined; if (parent) { parent.children = [...(parent.children ?? []), node]; } else { roots.push(node); } } return roots; }