"""Demo sources — a curated list of sampling classics (demos.json).

Fuel for demoing the Foundry, precomputing a fixed corpus, and iterating on
loop/chop quality (#19 autotune). The data is authored in demos.json (so it's
editable without touching code); this module just loads + resolves it.
"""
import json
from pathlib import Path

_PATH = Path(__file__).resolve().parent.parent / "demos.json"


def load() -> list[dict]:
    """All demo entries (order preserved)."""
    return json.loads(_PATH.read_text())["demos"]


def by_slug(slug: str) -> dict | None:
    return next((d for d in load() if d["slug"] == slug), None)


def source_of(slug: str) -> str:
    """The yt-dlp input (verified URL or `ytsearch1:` query) for a demo slug."""
    d = by_slug(slug)
    if not d:
        raise KeyError(f"no demo {slug!r} (try: {', '.join(x['slug'] for x in load())})")
    return d["source"]
