// In a real setup, this data would be loaded from Sharadar/Compustat.
// For testing, we allow an injected dataset.

let globalUniverseData = []; // Array of { date, symbols: [ 'AAPL', 'MSFT', ... ] }

export function loadUniverse(data) {
  globalUniverseData = [...data].sort((a, b) => new Date(a.date) - new Date(b.date));
}

export function getUniverseAsOf(dateStr) {
  // Find the most recent universe membership <= dateStr
  const targetDate = new Date(dateStr);
  let latest = null;
  for (const record of globalUniverseData) {
    if (new Date(record.date) <= targetDate) {
      latest = record;
    } else {
      break;
    }
  }
  return latest ? latest.symbols : [];
}
