export type SidebarDocumentOpenMode = "main" | "sidebar";

export type SidebarDocumentOpenTarget =
  | { kind: "same-window"; path: string }
  | { kind: "new-window"; url: string };

export function buildSidebarDocumentOpenTarget(
  documentId: string,
  mode: SidebarDocumentOpenMode,
  origin?: string | null,
): SidebarDocumentOpenTarget {
  const path = `/documents/${documentId}`;
  if (mode === "main") {
    return { kind: "same-window", path };
  }

  const base = origin ? `${origin}${path}` : path;
  return { kind: "new-window", url: `${base}?preview=sidebar` };
}

export function buildSidebarMindmapOpenTarget(
  documentId: string,
  mindmapId: string,
  mode: SidebarDocumentOpenMode,
  origin?: string | null,
): SidebarDocumentOpenTarget {
  const path = `/mindmap/${documentId}/${mindmapId}`;
  if (mode === "main") {
    return { kind: "same-window", path };
  }

  const url = origin ? `${origin}${path}` : path;
  return { kind: "new-window", url };
}
