import path from "node:path";
import { cuesInWindow, readManifest } from "@/lib/projects/report.mjs";
import { reelPlayView } from "@/lib/report/reelPlay.mjs";
import { sourceAt, type FileMap } from "@/lib/report/fileMedia.mjs";
import type { ProjectRef } from "@/lib/project-types";
import type { ReelView } from "@/components/projects/reel/reelTypes";
// THE REEL'S WALK, as data (lane/reel-bench): what `/projects/
/trailer/walk`
// renders (components/projects/reel/ReelBench.tsx) for one beat -- the reel's
// view (lib/report/reelPlay.mjs reelPlayView: each beat mapped onto the take's
// rendered master by the trailer's own plan), and for the beat on screen: the
// master as the bench's media (its project-relative path, its mtime, the
// build record's map onto the clip's source, the source seconds its segment
// plays), and the clip's words over those seconds.
export type ReelBenchCue = { start: number; end: number; text: string; endsSentence: boolean };
export type ReelBenchData = {
view: ReelView;
beat: number;
/** The clip the beat plays, from the take's manifest (or the project's). Null for a master beat. */
clip: { id: string; video: string; channel: string | null; quote: string | null; note: string | null; title: string | null } | null;
cues: ReelBenchCue[];
/** The master as the bench plays it; null when there is none or the beat does not map. */
media: { path: string; v: number | null; map: FileMap; span: { from: number; to: number } } | null;
/** The clips of the take's cut, in order: what an orphaned beat can be re-pointed to. */
clips: string[];
};
const r3 = (n: number) => Number(n.toFixed(3));
export async function reelBenchData(project: ProjectRef, { reel, take, beat }: { reel: string; take: string | null; beat: number }): Promise {
const view = (await reelPlayView(project, { reel, take })) as ReelView;
const row = (view.rows ?? []).find((r) => r.n === beat) ?? null;
const out: ReelBenchData = { view, beat, clip: null, cues: [], media: null, clips: [] };
if (!row || !view.take?.master || view.noMaster) return out;
const takeManifest = await readManifest(path.dirname(path.dirname(view.take.master))).catch(() => null);
out.clips = ((takeManifest?.timeline ?? []) as Record[]).filter((e) => e?.type === "clip" && typeof e.id === "string").map((e) => String(e.id));
const rel = path.relative(project.dir, view.take.master).split(path.sep).join("/");
if (rel.startsWith("..") || path.isAbsolute(rel)) return out;
const v = view.take.masterMtimeMs ?? null;
if (row.kind === "raw" && row.limits) {
// A master beat: master seconds are its seconds (the identity map), the whole master its room.
out.media = { path: rel, v, map: { segStart: 0, recStart: 0, gaps: [] }, span: { from: 0, to: row.limits.end } };
return out;
}
if (!row.map || !row.limits || !row.id) return out;
// The source seconds its segment plays: the record's start, to the master second the segment stops being this clip.
const map: FileMap = { segStart: row.map.segStart, recStart: row.map.recStart, gaps: row.map.gaps ?? [] };
const endSrc = row.map.gaps?.length ? sourceAt(map, row.limits.end - 0.002) + 0.002 : sourceAt(map, row.limits.end);
const span = { from: r3(row.limits.source?.start ?? map.recStart), to: r3(Math.min(row.limits.source?.end ?? endSrc, endSrc)) };
out.media = { path: rel, v, map, span };
// The clip, from the take's manifest (what the master was built from), else the project's.
const projectManifest = await readManifest(project.dir).catch(() => null);
const find = (m: Record | null) => ((m?.timeline ?? []) as Record[]).find((e) => e?.id === row.id && e?.type === "clip") ?? null;
const manifest = find(takeManifest) ? takeManifest : find(projectManifest) ? projectManifest : null;
const e = find(manifest);
if (e) {
out.clip = {
id: String(e.id),
video: String(e.video ?? ""),
channel: typeof e.channel === "string" ? e.channel : null,
quote: typeof e.quote === "string" ? e.quote : null,
note: typeof e.note === "string" ? e.note : null,
title: typeof e.title === "string" ? e.title : null,
};
const got = await cuesInWindow(project.dir, row.id, span.from - 30, span.to + 30, { manifest }).catch(() => null);
out.cues = ((got?.cues ?? []) as ReelBenchCue[]).map((c) => ({ start: c.start, end: c.end, text: c.text, endsSentence: !!c.endsSentence }));
}
return out;
}