import { openDocument, resolveDocPath } from '../document-manager';
import { computeLayoutTree, getDocChildren, type LayoutEntry } from '../utils/node-operations';

export interface SnapshotLayoutParams {
  filePath?: string;
  parentId?: string;
  maxDepth?: number;
  pageId?: string;
}

export async function handleSnapshotLayout(
  params: SnapshotLayoutParams,
): Promise<{ layout: LayoutEntry[] }> {
  const filePath = resolveDocPath(params.filePath);
  const doc = await openDocument(filePath);

  const maxDepth = params.maxDepth ?? 1;

  const docChildren = getDocChildren(doc, params.pageId);
  let nodes = docChildren;
  if (params.parentId) {
    const findNode = (list: typeof nodes, id: string): (typeof nodes)[0] | undefined => {
      for (const n of list) {
        if (n.id === id) return n;
        if ('children' in n && n.children) {
          const found = findNode(n.children, id);
          if (found) return found;
        }
      }
      return undefined;
    };
    const parent = findNode(docChildren, params.parentId);
    if (!parent) {
      throw new Error(`Node not found: ${params.parentId}`);
    }
    nodes = 'children' in parent && parent.children ? parent.children : [];
  }

  const layout = computeLayoutTree(nodes, docChildren, maxDepth);
  return { layout };
}
