// Episode scripts are filed by season + episode number ("2-1"), because each book of the Bible // is its own podcast season and episode numbers can restart in every season. A script with no // season keeps its plain episode number as its key. export function scriptKey(season, episodeNumber) { const ep = String(episodeNumber ?? '').trim() const s = String(season ?? '').trim() return /^\d+$/.test(s) ? `${s}-${ep}` : ep } // The episode number a stored script belongs to (new entries record it; legacy ones are keyed by it). export function scriptEpisodeNumber(key, entry) { if (entry && typeof entry.episodeNumber === 'string' && entry.episodeNumber) return entry.episodeNumber const parts = String(key).split('-') return parts[parts.length - 1] } // One-time move of legacy scripts ({ "7": { season: "1" } }) to season-episode keys ({ "1-7": ... }). // Scripts without a season stay under their plain episode number. Never overwrites an existing key. export function migrateScriptKeys(scripts) { const next = {} let changed = false for (const [key, entry] of Object.entries(scripts ?? {})) { if (!entry || typeof entry !== 'object') continue const episodeNumber = scriptEpisodeNumber(key, entry) const wanted = scriptKey(entry.season, episodeNumber) const targetKey = wanted !== key && !(wanted in scripts) && !(wanted in next) ? wanted : key const withEpisode = entry.episodeNumber === episodeNumber ? entry : { ...entry, episodeNumber } if (targetKey !== key || withEpisode !== entry) changed = true next[targetKey] = withEpisode } return { scripts: next, changed } }