"use client";
import { jsxs as h, jsx as p } from "react/jsx-runtime";
import { useState as n } from "react";
import { isValidEmail as O } from "../../../../utils/isValidEmail.es.js";
import { EAnalyticsEventType as H } from "../../../../types/analytics/analytics.es.js";
import v from "../../../../modules/HSModal/components/Newsletter/newsletter.module.css";
import { TextInput as J } from "../../../../components/TextInput/TextInput.es.js";
import { Button as g } from "../../../../components/Button/Button.es.js";
const R = ({
  buttonText: S,
  colorScheme: u,
  onCloseCallback: s,
  cancelButtonText: T,
  tags: m,
  onClickCallback: d,
  placeholder: x,
  trackingEvent: l,
  trackingLocation: E,
  webhookUrl: P
}) => {
  const [r, c] = n(""), [f, e] = n(!1), [w, t] = n(""), [N, y] = n(!1), [j, a] = n(null), V = (i) => {
    c(i.target.value), f && (e(!1), t("")), y(i.target.value.length > 0);
  }, z = async () => {
    var i;
    if (!r) {
      e(!0), t("Please enter your email address");
      return;
    }
    if (!O(r)) {
      e(!0), t("Please enter a valid email address");
      return;
    }
    e(!1), t(""), a("sending");
    try {
      const o = m == null ? void 0 : m.split(",").map((I) => ({
        name: I,
        status: "active"
      }));
      (await fetch(P, {
        method: "POST",
        headers: {
          "Content-Type": "application/json"
        },
        body: JSON.stringify({
          email: r,
          tags: o
        })
      })).ok ? (a("success"), c(""), e(!1), t(""), y(!1), s == null || s()) : (a("error"), e(!0), t("Subscription failed. Please try again."));
    } catch {
      a("error"), e(!0), t("Subscription failed. Please try again.");
    }
    if (d == null || d(), l) {
      const o = {
        event: l,
        location: E || "",
        type: H.interaction,
        payload: {
          email: r,
          component: "HSModal",
          element: "Newsletter",
          pageTitle: document.title
        }
      };
      (i = window.eventBus) == null || i.emit("analytics", o);
    }
  }, A = u === "light" ? "primary" : "primary-invert", B = u === "light" ? "primary" : "primary-invert";
  return /* @__PURE__ */ h("div", { className: v.interface, children: [
    /* @__PURE__ */ p(
      J,
      {
        placeholder: x || "Email",
        variant: A,
        value: r,
        handleChange: V,
        error: f,
        errorText: w,
        active: N
      }
    ),
    /* @__PURE__ */ h("div", { className: v.buttonContainer, children: [
      /* @__PURE__ */ p(g, { size: "md", variant: B, onClick: z, children: j === "sending" ? "submitting…" : S }),
      /* @__PURE__ */ p(g, { size: "md", variant: "ghost", onClick: s, children: T })
    ] })
  ] });
};
export {
  R as Newsletter
};
