async function login(username,password) {
    var wispcounter = 0
  while (!currentWisp){
if (checkWisp(wisps[wispcounter])){
  currentWisp = wisps[wispcounter]
  console.log(currentWisp)
} else {
  wispcounter += 1
}
  }
    client = new EpoxyTransport.default({
    wisp: currentWisp
});
await client.init();

  try {
    const response = await epoxyFetch(website + "/api/collections/users/auth-with-password", {
      method: "POST", 
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify({
        "identity": username,
        "password": password
      })
    });
    if (!response.ok) {
      throw new Error(`HTTP error! Status: ${response.status}`);
    }

    const data = await response.json();
    console.log("Success:", data);
    UserAuth = data["token"]
    UserID = (data["record"])["id"]
    return(data)
  } catch (error) {
    alert("Error:", error);
  }
}

async function PostData(collection,body,params="") {
  try {
    var formData
    formData = new FormData();
    for (i in body){
      formData.append(i, body[i])
    }
    const response = await epoxyFetch(website + "/api/collections/" + collection + "/records" + params, {
      method: "POST",
      headers: {
        "Authorization": `Bearer ${UserAuth}`
      },
      body: formData
    });

    if (!response.ok) {
      throw new Error(`HTTP error! Status: ${response.status}`);
    }

    const data = await response.json();
    console.log("Success:", data);
    return(data)
  } catch (error) {
    console.error("Error sending data:", error);
  }
}

async function UpdateData(id,collection,body,params="") {
  try {
    var formData
    formData = new FormData();
    for (i in body){
      formData.append(i, body[i])
    }
    const response = await epoxyFetch(website + "/api/collections/" + collection + "/records/"  + id + params, {
      method: "PATCH",
      headers: {
        "Authorization": `Bearer ${UserAuth}`
      },
      body: formData
    });

    if (!response.ok) {
      throw new Error(`HTTP error! Status: ${response.status}`);
    }

    const data = await response.json();
    console.log("Success:", data);
    return(data)
  } catch (error) {
    console.error("Error sending data:", error);
  }
}
async function GetData(collection,id="",params="") {
  try {
    var slash
    if (id == ""){
      slash = ""
    } else {
      slash = "/"
    }
    const response = await epoxyFetch(website + "/api/collections/" + collection + "/records" + slash + id + params, {
      method: "GET",
      headers: {
        "Content-Type": "application/json",
        "Authorization": `Bearer ${UserAuth}`
      }
    });

    if (!response.ok) {
      throw new Error(`HTTP error! Status: ${response.status}`);
    }

    const data = await response.json();
    console.log("Success:", data);
    return(data)
  } catch (error) {
    console.error("Error sending data:", error);
  }
}
async function DeleteData(collection,id) {
  try {
    const response = await epoxyFetch(website + "/api/collections/" + collection + "/records/" + id, {
      method: "DELETE",
      headers: {
        "Content-Type": "application/json",
        "Authorization": `Bearer ${UserAuth}`
      }
    });
   if (response.status === 204) {
    console.log("Deleted Data!")
   }


} catch (error) {
        console.error(error);
    }
}
async function epoxyFetch(url, options = {}) {
    const method = options.method || "GET";
    const body = options.body;

    const headers = options.headers
        ? Object.entries(options.headers)
        : [];

    const result = await client.request(
        new URL(url),
        method,
        body,
        headers,
        options.signal
    );

    return new Response(result.body, {
        status: result.status,
        statusText: result.statusText,
        headers: result.headers
    });
}