const API_BASE = "https://data.education.gouv.fr/api/explore/v2.1/catalog/datasets/fr-esr-parcoursup/records"



export async function recupereFormations(motCle, limit = 20) 
{
  const url = `${API_BASE}?where=search("${motCle}")&limit=${limit}`
  const response = await fetch(url)
  const data = await response.json()
  return data.results
}



export async function recupereFormation(id) 
{
  const url = `${API_BASE}?where=cod_aff_form="${id}"&limit=1`
  const response = await fetch(url)
  const data = await response.json()
  return data.results[0]
}



export async function recupereEvolutionFormation(id) 
{
  const années = ["2020", "2021", "2022", "2023", "2024", "2025"]
  
  const promesses = années.map(annee => 
    fetch(`${API_BASE}?where=cod_aff_form="${id}" AND session="${annee}"&limit=1`)
      .then(r => r.json())
      .then(data => data.results[0])
  )
  
  const resultats = await Promise.all(promesses)
  return resultats.filter(r => r !== null && r !== undefined)
}