var scheduleDataCache = null;

export function loadWellVisitScheduleData() {
  if (scheduleDataCache) return Promise.resolve(scheduleDataCache);

  return fetch('/data/well-visit/schedule.json', { credentials: 'same-origin' })
    .then(function(response) {
      if (!response.ok) throw new Error('Failed to load well-visit schedule data');
      return response.json();
    })
    .then(function(data) {
      scheduleDataCache = data;
      return scheduleDataCache;
    });
}

export function applyWellVisitScheduleGlobals(data, target) {
  var scope = target || globalThis;
  scope.VISIT_AGES = data.visitAges;
  scope.WELL_VISIT_CODES = data.wellVisitCodes;
  scope.VACCINES = data.vaccines;
  scope.PERIODICITY = data.periodicity;
  scope.CATCH_UP_SCHEDULE = data.catchUpSchedule;
  scope.ANTICIPATORY_GUIDANCE = data.anticipatoryGuidance;
  scope.AAP_FOOTNOTES = data.aapFootnotes;
  scope.DYSLIPIDEMIA_SCHEDULE = data.dyslipidemiaSchedule;
  scope.NEWBORN_SCREENING = data.newbornScreening;
  scope.GROWTH_REFERENCE = data.growthReference;
  scope.REFLEXES_REFERENCE = data.reflexesReference;
  scope.BMI_CLASSIFICATION = data.bmiClassification;
  return scope;
}
