// Bundled YouTube URL → track lookup (data/ytmap.json), grown by
// EjePlayerCodes/extension/grow_per_track.py from textually-confirmed uploads.
//
// This is the "instant recognition" store: open a known YouTube video and the
// overlay / Detect resolve it straight to its library track — no audio capture,
// no title guessing. Complements src/confirms.js (the user's own confirmations):
// confirms (personal) take precedence, then this bundled map, then title match.
// Keyed by the same normalized urlKey as confirms.js (yt:<videoId>).
import { dataURL } from "./util.js";
import { urlKey } from "./confirms.js";

let _map = null;
let _loading = null;

export async function ensure() {
  if (_map) return _map;
  if (!_loading) {
    _loading = fetch(dataURL("ytmap.json"))
      .then(r => r.json())
      .catch(() => ({}))
      .then(m => (_map = m || {}));
  }
  return _loading;
}

// → groupId for a known URL, or null.
export async function lookup(url) {
  if (!url) return null;
  const m = await ensure();
  return m[urlKey(url)] || null;
}

export async function count() {
  return Object.keys(await ensure()).length;
}
