/**
 * Snippet → dataset reverse lookup (docs/architecture/07 §4).
 *
 * The forward link is a snippet's `datasetRefs` (recomputed from its published
 * spec). The reverse — "which snippets use this dataset" — is **derived** by a
 * scan, never stored, so it can't drift. This module is that scan's single pure
 * implementation: it takes plain snippet records and a name and returns plain
 * data, so the same logic serves both the reactive UI (the Dataset Manager passes
 * its live `snippets` selection) and any non-reactive caller (which passes a
 * store snapshot inline). One definition, per arch/07 §6 ("'who references this'
 * has exactly one implementation").
 *
 * Matching is **case-insensitive**, mirroring the naming policy (`core/naming`):
 * a reference to `"sales"` and a dataset named `"Sales"` are the same link.
 */

/** The minimal snippet shape the reverse lookup needs (its forward refs). */
interface HasDatasetRefs {
  datasetRefs: readonly string[];
}

/** Snippets whose `datasetRefs` include `name` (case-insensitive). */
export function snippetsReferencingDataset<T extends HasDatasetRefs>(
  snippets: readonly T[],
  name: string,
): T[] {
  const lower = name.toLowerCase();
  return snippets.filter((s) => s.datasetRefs.some((ref) => ref.toLowerCase() === lower));
}

/**
 * Usage counts for every referenced dataset name, keyed by **lower-cased** name —
 * one pass over the library for the Dataset Manager's per-row badges (cheaper than
 * an O(snippets) `snippetsReferencingDataset` scan per dataset). Look up a dataset
 * with `counts.get(name.toLowerCase()) ?? 0`.
 */
export function datasetUsageCounts(snippets: readonly HasDatasetRefs[]): Map<string, number> {
  const counts = new Map<string, number>();
  for (const s of snippets) {
    for (const ref of s.datasetRefs) {
      const key = ref.toLowerCase();
      counts.set(key, (counts.get(key) ?? 0) + 1);
    }
  }
  return counts;
}
