function idAndDate(str){
  let data = str.split("T");
  let date = data[0].split("-");
  return {
    date: date[2] +'/'+ date[1],
    id: date[2] + date[1]
  }
}

function titleAndContent(str){
  let data = str.split(" :");
  let title = data[0];
  let contentData = data[1].split("<br>").filter(e => e);
  let content = "";
  for(let line of contentData){
      content += line + '\n';
  }
  return {
      title: title,
      content: content
    }
  }

export async function loadMsgData() {
  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();
  //console.log("POUETS :" , pouets);
  const filtered = pouets.filter(p => p.account.username === 'drags_nerds_live' || p.reblog?.account.username === 'drags_nerds_live');
  //console.log("FILTERED :" , filtered);

  let msgContent = [];

  for (let pouet of filtered){
    let selectedPouet;
    if (pouet.reblog){
      selectedPouet = pouet.reblog;
    } else {
      selectedPouet = pouet;
    }
    let textInfos = titleAndContent(selectedPouet.text);
    let dateInfos = idAndDate(selectedPouet.created_at);
    let entry = {
      title: textInfos.title,
      date: dateInfos.date,
      dateInfo: selectedPouet.created_at,
      content: textInfos.content,
      like: selectedPouet.favourites_count,
      isLiked: false,
      wasRead: false,
      isSelected: false
    }
    msgContent.push(entry);
  }

  return msgContent;
}
