import { API_BASE } from "../../config/api.config";
import type { FormData } from "../../config/interfaces.config";
import Cookies from "js-cookie";

export const submitFormData = async (
  data: FormData,
  username: string | null,
) => {
  console.warn("submitFormData is fetching!");

  const draw = Cookies.get("selectedDraw");

  if (!draw) {
    throw new Error("You need to set an prize draw!");
  }

  // await new Promise((resolve) => setTimeout(resolve, 3000)); // Wait 3 seconds

  const response = await fetch(
    `${API_BASE}/default/new-entry?username=${username}&draw=${draw}`,
    {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(data),
    },
  );

  if (!response.ok) {
    const error = await response.text();
    throw new Error(error || "Form submission failed");
  }

  return;
};
