export const toBase64 = async (file) => {
  const d = new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = (error) => reject(error);
  });
  return await d;
};

export const fixBase64 = (base64) => {
  return base64.split(",").slice(1)[0];
};

export const urlToBase64 = async (url) => {
  try {
    const response = await fetch(url);
    if (!response.ok) {
      throw new Error("Failed to fetch image: " + response.status);
    }
    const blob = await response.blob();
    const reader = new FileReader();
    reader.readAsDataURL(blob);
    return new Promise((resolve) => {
      reader.onloadend = () => resolve(reader.result);
    });
  } catch (error) {
    console.error("Error converting URL to base64:", error);
    throw error; // Rethrow the error to propagate it further
  }
};
