export type Orient = "h" | "v";

export type SurfaceState = "work" | "wait" | "done" | "error" | "idle";

export type LayoutNode =
  | { leaf: { surface_id: string } }
  | { split: { orient: Orient; ratios: number[]; children: LayoutNode[] } };

export interface SurfaceView {
  spec: {
    command: string;
    args: string[];
    cwd: string;
    agent_label: string | null;
    cols: number;
    rows: number;
    autostart: boolean;
  };
  running: boolean;
  state: SurfaceState;
}

export interface Group {
  id: string;
  name: string;
  color: string;
  order: number;
}

export interface WorkspaceView {
  id: string;
  path: string;
  name: string;
  group_id: string | null;
  order: number;
  unread: boolean;
  pinned: boolean;
  layout: LayoutNode | null;
  zoomed: string | null;
  surfaces: Record<string, SurfaceView>;
}

export function leafIds(node: LayoutNode | null): string[] {
  if (!node) return [];
  if ("leaf" in node) return [node.leaf.surface_id];
  return node.split.children.flatMap(leafIds);
}
