function showToast(message, type = "success") {
  if (type === "success") {
    Toastify({
      text: message,
      style: {
        borderRadius: "8px",
        background: "linear-gradient(135deg, #388e3c, #66bb6a)",
      },
    }).showToast();
  } else if (type === "warning") {
    Toastify({
      text: message,
      style: {
        borderRadius: "8px",
        background: "linear-gradient(135deg, #fbc02d, #ffb74d)",
        color: "black",
      },
    }).showToast();
  } else if (type === "error") {
    Toastify({
      text: message,
      style: {
        borderRadius: "8px",
        background: "linear-gradient(135deg, #d32f2f, #ff6659)",
      },
    }).showToast();
  }
}

$("#notify-me-button").on("click", async function () {
  const emailAddress = $("#user-email-address-for-notifications").val();

  if (!emailAddress) {
    return showToast("Please enter email address", "warning");
  }

  if (!/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(emailAddress)) {
    return showToast("Invalid email address", "warning");
  }

  const baseURL = new URL(location.href).origin;

  $("#notify-me-button").prop("disabled", true);
  $("#notify-me-button").html("Please wait");

  $.ajax({
    url: `${baseURL}/server/requests`,
    type: "POST",
    data: { email_notification_store: "yes", emailAddress },
    success: function (response) {
      const JSONResponse = JSON.parse(response);

      showToast(
        JSONResponse.message,
        JSONResponse.success ? "success" : "error"
      );
    },
    error: function (error) {
      showToast(error.message, "error");
    },
    complete: function () {
      $("#user-email-address-for-notifications").val("");
      $("#notify-me-button").prop("disabled", false);
      $("#notify-me-button").html(
        '<i class="mdi mdi-bell-ring-outline me-2"></i>Notify Me'
      );
    },
  });
});
