"use client";
import { useEffect, useRef, useState } from "react";
import { buttonVariants } from "@/components/ui/button";
import { edgeClock } from "@/lib/report/clipEditorModel.mjs";
// Two of the clip editor's edits on the CUT rather than the clip (UI notes
// 2026-10-30, items 2 and 11):
//
// Flash-forward this span… a tight copy of the selected seconds put
// earlier (after an entry, or into a section), its
// attribution and locks copied, as a repeat of
// this clip -- or moved out of it (POST
// /api/report/clip-editor { op: "flash-forward" },
// manifest.mjs flashForward; `umtool timeline
// flash-forward`)
// Add to trailer the selection (else the cut) as a trailer beat,
// in the cut's order (POST /api/report/trailer-beats,
// trailerBeats.mjs; `umtool trailer-beats
add`)
type Section = { index: number; words: string; clips: string[] };
type Entry = { id: string; type: string };
export function FlashForwardForm({
project,
clipId,
span,
busy,
onCancel,
onGo,
}: {
project: string;
clipId: string;
span: { from: number; to: number };
busy: boolean;
onCancel: () => void;
onGo: (body: Record) => void;
}) {
const [entries, setEntries] = useState([]);
const [sections, setSections] = useState([]);
const [where, setWhere] = useState<"after" | "section">("after");
const [after, setAfter] = useState("");
const [section, setSection] = useState("");
const [move, setMove] = useState(false);
// A choice made before the sections came back is the person's: the default never overrides it.
const touched = useRef(false);
useEffect(() => {
let live = true;
void fetch(`/api/report/timeline?project=${encodeURIComponent(project)}`, { cache: "no-store" })
.then((r) => r.json())
.then((j: { timeline?: Entry[]; sections?: Section[] }) => {
if (!live) return;
const tl = Array.isArray(j.timeline) ? j.timeline : [];
setEntries(tl);
const secs = Array.isArray(j.sections) ? j.sections : [];
setSections(secs);
// A flash-forward goes EARLIER: by default, the start of the section this clip is in.
const mine = secs.find((s) => s.clips.includes(clipId));
if (mine && secs.length > 1 && !touched.current) {
setWhere("section");
setSection(String(mine.index));
}
})
.catch(() => {});
return () => {
live = false;
};
}, [project, clipId]);
const input = "rounded border border-[var(--color-line)] bg-[var(--color-ink)] px-1.5 py-0.5 text-[11px] text-[var(--color-text)] outline-none focus:border-[var(--color-sel)]";
const mineAt = entries.findIndex((e) => e.id === clipId);
const go = () =>
onGo({
op: "flash-forward",
clip: clipId,
from: span.from,
to: span.to,
move,
...(where === "section" && section !== "" ? { section: Number(section), position: "start" } : { after: after || null }),
});
return (