function inlineContent(str){
  let data = str.split("<br>");
  let content = "";
  for (let line of data){
    content += line + '\n';
  }
  return content
}

function creditsContent(str){
  let data = str.split("<br>")
  let credits = data[0].split(" - ");
  return {
    track: credits[0],
    artist: credits[1]
  }
}

export async function loadAudioData() {
  const audioRes = await fetch('./DATA/audioData.json');
  const aData = await audioRes.json();
  const res = await fetch("https://pouet.drags-nerds.net/api/v1/timelines/public?local=true&limit=40");

  if(!res.ok) throw new Error(`Server responded with ${res.status} ${res.statusText}`);

  const pouets = await res.json();
  const filtered = pouets.filter(p => aData.some(user => p.account.display_name === user));
  //console.log(filtered);
  const files = {};
  for (const user of aData) {
    files[user] = [];
  }

  let audioFiles = [];

  for (const pouet of filtered){
    //ignorer les PJ autres que audio
    if (pouet.media_attachments?.length > 0 &&
        !pouet.media_attachments[0].type.includes('audio')) {
      continue;
    }

    if(pouet.media_attachments?.length > 0){
        let credits = creditsContent(pouet.content);
      let entry = {
        track: credits.track,
        artist: credits.artist,
        src: pouet.media_attachments[0].url
      }
      audioFiles.push(entry);
    }
  }

  //console.log(audioFiles);
  return audioFiles
}
