// Source registry. Every RadioSource adapter registers itself here.
// Popup/SW/offscreen never import individual adapters — they query
// the registry. To add a new network: write a new adapter file, then
// add one line to REGISTERED below.

import { somaFM } from './somafm.js';

/** @type {import('./base-source.js').RadioSource[]} */
const REGISTERED = [
  somaFM,
];

export function listSources() {
  return REGISTERED.map(s => ({ id: s.id, displayName: s.displayName }));
}

export function getSource(id) {
  return REGISTERED.find(s => s.id === id) || null;
}

/**
 * Aggregate stations across every registered source. Returns a flat
 * list. Each Station already carries its sourceId so the popup knows
 * which adapter to call back for play/now-playing.
 */
export async function listAllStations() {
  const results = await Promise.allSettled(REGISTERED.map(s => s.listStations()));
  /** @type {import('./base-source.js').Station[]} */
  const stations = [];
  results.forEach((r, i) => {
    if (r.status === 'fulfilled') stations.push(...r.value);
    else console.warn(`Source "${REGISTERED[i].id}" failed:`, r.reason);
  });
  return stations;
}
