import type { SourceOption } from "@/lib/research/types"; import type { EditorChannel } from "./generation"; // THE CREATE FORM'S SOURCES (demo epic D1 item 2), pure so it is tested // without an editor: the editor's own corpus (handle "") first, then each // published archive (`remote:`) the source picker offers. The first // archive is preselected when the editor holds no channel of its own — a // fresh clone with an archive connected sweeps that archive by default. A // remote-only channel (held only to fetch an archive's clips) is not a local // corpus. An editor that did not answer (null) preselects nothing: the form // says it is down. export type CreateSource = { handle: string; label: string; detail?: string }; export function createSourcesOf(sources: SourceOption[], editorChannels: EditorChannel[] | null): { sources: CreateSource[]; preselect: string } { const local: CreateSource = { handle: "", label: "Local corpus", detail: "the editor's channels" }; const remote = sources.filter((s) => s.kind === "remote").map((s) => ({ handle: s.handle, label: s.label, ...(s.detail ? { detail: s.detail } : {}) })); const ownChannels = editorChannels?.filter((c) => c.kind !== "remote-only").length ?? null; const preselect = ownChannels === 0 && remote.length > 0 ? remote[0].handle : ""; return { sources: [local, ...remote], preselect }; }