"use client";
import { jsxs as d, jsx as i, Fragment as N } from "react/jsx-runtime";
import { useState as n } from "react";
import { cn as x } from "../../../../utils/cn/cn.es.js";
import { isValidEmail as S } from "../../../../utils/isValidEmail.es.js";
import e from "../../../../pageComponents/Footer/components/NewsLetter/newsLetter.module.css";
import { Typography as u } from "../../../../components/Typography/Typography.es.js";
import { TextInput as T } from "../../../../components/TextInput/TextInput.es.js";
const k = "https://hook.us2.make.com/t87e9ij5ebvcoqeushd45ihubsghratq", O = ({ onSubmit: l }) => {
  const [a, m] = n(""), [f, g] = n(""), [h, t] = n(!1), [y, s] = n(""), [b, p] = n(!1), [r, o] = n(null), v = (c) => {
    m(c.target.value), h && (t(!1), s("")), p(c.target.value.length > 0);
  }, w = async () => {
    if (!a) {
      t(!0), s("Please enter your email address");
      return;
    }
    if (!S(a)) {
      t(!0), s("Please enter a valid email address");
      return;
    }
    t(!1), s(""), o("sending");
    try {
      (await fetch(k, {
        method: "POST",
        headers: {
          "Content-Type": "application/json"
        },
        body: JSON.stringify({ email: a })
      })).ok ? (o("success"), g(a), m(""), t(!1), s(""), p(!1), l == null || l(a)) : (o("error"), t(!0), s("Subscription failed. Please try again."));
    } catch {
      o("error"), t(!0), s("Subscription failed. Please try again.");
    }
  };
  return /* @__PURE__ */ d("div", { className: x(e.root, { [e.subscribed]: r === "success" }), children: [
    /* @__PURE__ */ d("div", { className: e.container, children: [
      /* @__PURE__ */ i(u, { defaultVariant: "headingLg", mobileVariant: "headingSm", as: "h2", className: e.title, children: r === "success" ? "Thanks for subscribing!" : "Read the latest research reviews" }),
      /* @__PURE__ */ i(u, { defaultVariant: "bodySm", mobileVariant: "bodyXs", as: "span", className: e.text, children: r === "success" ? /* @__PURE__ */ d(N, { children: [
        "You will now begin receiving our monthly newsletter at",
        " ",
        /* @__PURE__ */ i("b", { className: e.email, children: f }),
        ". It's a great way to keep up to date on the latest developments in longevity science."
      ] }) : "Request The Longevity Blueprint to receive our weekly newsletter containing synopses of the latest clinical research." })
    ] }),
    r === "sending" && /* @__PURE__ */ i("div", { className: e.container, children: /* @__PURE__ */ i(u, { defaultVariant: "bodySm", mobileVariant: "bodyXs", as: "span", className: e.text, children: "Subscribing, please wait..." }) }),
    r !== "success" && r !== "sending" && /* @__PURE__ */ i(
      T,
      {
        placeholder: "Your email",
        variant: "primary-invert",
        value: a,
        handleChange: v,
        onButtonClick: w,
        showButton: !0,
        error: h,
        errorText: y,
        active: b
      }
    )
  ] });
};
export {
  O as NewsLetter
};
