export type JobRef = { id?: string; name: string }; type DraftLookup = { name: string; rkey?: string; migration?: { status: string; jobId?: string } }; export const createJobMatcher = ( repos: readonly T[], drafts: Record ): ((job: JobRef) => T | undefined) => { const byJobId = new Map(); const byDraft = new Map(); const byName = new Map(); const claim = (map: Map, key: string | undefined, repo: T) => { if (key !== undefined && !map.has(key)) map.set(key, repo); }; for (const repo of repos) { const draft = drafts[repo.id]; const m = draft?.migration; if (m?.status === "success") claim(byJobId, m.jobId, repo); if (draft) { claim(byDraft, draft.name.toLowerCase(), repo); claim(byDraft, draft.rkey?.toLowerCase(), repo); } claim(byName, repo.name.toLowerCase(), repo); } return (job) => { const jobName = job.name.toLowerCase(); return ( (job.id ? byJobId.get(job.id) : undefined) ?? byDraft.get(jobName) ?? byName.get(jobName) ); }; };