// videoDirId — which `data/
/` a RECORD's id lives in (release 23 F1, the
// Twitch join, option (b), ruled 2026-10-10).
//
// A Twitch VOD's directory is its canonical id, the URL's `/videos/`
// (`extractVideoId`), while its record carries yt-dlp's native id `v`. The
// published `v` URLs stay; the join between the two normalizes on the
// DIRECTORY side, here, once. Records and published ids are never rewritten.
//
// - A record id that is not `v` passes through untouched, with no
// filesystem read: every other platform, and every record but a Twitch
// VOD's.
// - A `v` id maps to `` only for a TWITCH record (the record's
// platform, else its channel's: config.json `platform`, else the host of
// its `url`). A `v`-prefixed id of any other platform (a Rumble slug, a
// YouTube id that happens to read `v` + ten digits) is never guessed at.
// - Between the two directories the one that EXISTS wins, `` first (it is
// the one the download made, with the metadata); with neither, ``, the
// canonical name, so a writer (fetch-window) creates the right one.
//
// Plain JS with JSDoc types, for the reason detectPlatform.mjs is: umtool's
// report-to-video scripts run under bare `node` and read these directories too
// (evidenceClip.mjs, cues.mjs). `lib/videoId.ts` re-exports it; TS callers
// import it from there. PURE (client-safe, as videoId.ts is): the disk half —
// which directory exists — is ./videoDir-server.mjs.
import { detectPlatform } from "./detectPlatform.mjs";
const TWITCH_RECORD_ID_RE = /^v(\d+)$/;
const TWITCH_DIR_ID_RE = /^\d+$/;
/**
* A Twitch record id's directory id: `v` → ``; anything else → null.
* Pure: says what the id would map to, not that the record is Twitch's.
* @param {string} recordId
* @returns {string | null}
*/
export function twitchVodDirId(recordId) {
const m = TWITCH_RECORD_ID_RE.exec(recordId);
return m ? m[1] : null;
}
/**
* The reverse: a Twitch VOD directory `` → the record id `v` its record
* carries; anything else → null.
* @param {string} dirId
* @returns {string | null}
*/
export function twitchVodRecordId(dirId) {
return TWITCH_DIR_ID_RE.test(dirId) ? `v${dirId}` : null;
}
/**
* The id the ARCHIVE serves a manifest entry's recording under — what a link
* to its page (`?v=/`) must carry. An explicit `siteVideo` wins
* (a Rumble clip: its local dir is the URL slug, the archive keys the embed
* id). Else `video`, except that a TWITCH VOD's directory id `` is served
* as its record id `v` (twitchVodRecordId) — keyed on the PLATFORM, never
* on the id being all digits: an X status id is digits too, and is served as
* it is.
* @param {string | { video?: unknown, siteVideo?: unknown } | null | undefined} entry
* an entry (`{video, siteVideo?}`) or a bare id
* @param {string | null | undefined} platform the recording's (or its channel's) platform
* @returns {string}
*/
export function siteVideoId(entry, platform) {
const e = typeof entry === "string" ? { video: entry } : (entry ?? {});
if (typeof e.siteVideo === "string" && e.siteVideo) return e.siteVideo;
const id = e.video == null ? "" : String(e.video);
return platform === "twitch" ? (twitchVodRecordId(id) ?? id) : id;
}
/**
* The platform a channel's config names: `platform`, else its `url`'s host
* (ytdlp/channelArgs.ts channelPlatform's rule). Null for none.
* @param {{ platform?: unknown, url?: unknown } | null | undefined} config
* @returns {string | null}
*/
export function channelConfigPlatform(config) {
if (!config || typeof config !== "object") return null;
if (typeof config.platform === "string" && config.platform) return config.platform;
return typeof config.url === "string" ? detectPlatform(config.url) : null;
}
/**
* The directory id for a record, given its platform and which directories
* exist. Synchronous over an `exists` callback, so a caller with its own
* listing (or a test) decides without touching the disk.
* @param {string} recordId
* @param {{ platform?: string | null, exists: (dirId: string) => boolean }} opts
* @returns {string}
*/
export function pickVideoDirId(recordId, { platform, exists }) {
const n = twitchVodDirId(recordId);
if (n === null || platform !== "twitch") return recordId;
if (exists(n)) return n;
if (exists(recordId)) return recordId;
return n;
}