'use client';
import { jsx as o, jsxs as M } from "react/jsx-runtime";
import i from "react";
import { Input as se, Chip as te, Button as K, Popover as ie, PopoverTrigger as le, PopoverContent as re, Listbox as ae, ListboxItem as ne } from "@heroui/react";
import { AnimatePresence as me, motion as ce } from "framer-motion";
import { useConfig as ue } from "../../hooks/use-config.js";
import { useFormField as de } from "./form-wrapper.js";
import { FieldError as fe } from "./field-error.js";
const he = [
  "gmail.com",
  "yahoo.com",
  "hotmail.com",
  "outlook.com",
  "icloud.com",
  "aol.com",
  "live.com",
  "msn.com",
  "me.com",
  "mail.com",
  "protonmail.com",
  "yandex.com",
  "zoho.com",
  "fastmail.com"
], Q = {
  "gmial.com": "gmail.com",
  "gmai.com": "gmail.com",
  "gmil.com": "gmail.com",
  "yahho.com": "yahoo.com",
  "yaho.com": "yahoo.com",
  "hotmial.com": "hotmail.com",
  "hotmil.com": "hotmail.com",
  "outlok.com": "outlook.com",
  "outloo.com": "outlook.com"
};
function pe(c, k = {}) {
  const {
    validateFormat: x = !0,
    validateDomain: g = !1,
    allowedDomains: l = [],
    blockedDomains: v = []
  } = k, n = [], u = [];
  let s = null, r = null;
  x && (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(c) || n.push("Please enter a valid email address"));
  const d = c.split("@");
  return d.length === 2 && (r = d[0], s = d[1].toLowerCase(), s && Q[s] && u.push(`Did you mean ${r}@${Q[s]}?`), g && s && (l.length > 0 && !l.includes(s) && n.push(`Email must be from one of these domains: ${l.join(", ")}`), v.includes(s) && n.push("This email domain is not allowed"), /^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(s) || n.push("Invalid email domain")), r && s && s.length > 0 && !s.includes(".") && he.filter(
    (f) => f.toLowerCase().startsWith(s.toLowerCase())
  ).slice(0, 3).forEach((f) => {
    u.push(`${r}@${f}`);
  })), {
    isValid: n.length === 0,
    errors: n,
    suggestions: u,
    domain: s,
    username: r
  };
}
function ge({
  name: c = "email",
  label: k = "Email",
  placeholder: x = "Enter your email address",
  value: g = "",
  onChange: l,
  onBlur: v,
  onFocus: n,
  required: u = !1,
  disabled: s = !1,
  showSuggestions: r = !0,
  validateFormat: d = !0,
  validateDomain: y = !1,
  allowedDomains: f = [],
  blockedDomains: L = [],
  size: $ = "md",
  radius: U = "md",
  variant: j = "bordered",
  className: F = "",
  autoFocus: B = !1,
  autoComplete: R = "email",
  error: h,
  description: T,
  showVerificationStatus: N = !1,
  isVerified: D = !1,
  onRequestVerification: w,
  startContent: W,
  endContent: C
}) {
  const { components: P, organizationSettings: A } = ue(), a = de(c), X = i.useMemo(() => P.Input ?? se, [P.Input]), S = P.EmailField;
  if (S)
    return /* @__PURE__ */ o(S, { name: c, label: k, placeholder: x, value: g, onChange: l, onBlur: v, onFocus: n, required: u, disabled: s, showSuggestions: r, validateFormat: d, validateDomain: y, allowedDomains: f, blockedDomains: L, size: $, variant: j, className: F, autoFocus: B, autoComplete: R, error: h, description: T, showVerificationStatus: N, isVerified: D, onRequestVerification: w, startContent: W, endContent: C });
  const [Y, V] = i.useState(g), [b, O] = i.useState(!1), [Z, p] = i.useState(!1), m = l ? g : Y, I = i.useMemo(() => {
    const e = A?.emailRestrictions?.allowedDomains;
    return e && e.length > 0 ? e : f;
  }, [f, A]), _ = i.useMemo(() => {
    const e = A?.emailRestrictions?.blockedDomains;
    return e && e.length > 0 ? [...L, ...e] : L;
  }, [L, A]), t = i.useMemo(() => m ? pe(m, {
    validateFormat: d,
    validateDomain: y,
    allowedDomains: I,
    blockedDomains: _
  }) : null, [m, d, y, I, _]), z = i.useMemo(() => {
    const e = [];
    return h && (Array.isArray(h) ? e.push(...h) : e.push(h)), a.error && (Array.isArray(a.error) ? e.push(...a.error) : e.push(a.error)), t && !t.isValid && e.push(...t.errors), e.length > 0 ? e : null;
  }, [h, a.error, t]), E = i.useCallback((e) => {
    l ? l(e) : V(e), a.clearError && a.clearError(), t?.suggestions.length && b && r ? p(!0) : p(!1);
  }, [l, a, t, b, r]), q = i.useCallback(() => {
    if (O(!1), p(!1), m) {
      const e = m.trim().toLowerCase();
      e !== m && (l ? l(e) : V(e));
    }
    a.setTouched && a.setTouched(!0), v?.();
  }, [a, v, m, l]), ee = i.useCallback(() => {
    O(!0), n?.(), r && t?.suggestions.length && p(!0);
  }, [n]), G = i.useCallback((e) => {
    E(e), p(!1);
  }, [E]), oe = i.useMemo(() => !N || C ? null : D ? /* @__PURE__ */ o(
    te,
    {
      size: "sm",
      color: "success",
      variant: "flat",
      startContent: /* @__PURE__ */ o("svg", { className: "w-3 h-3", fill: "none", stroke: "currentColor", viewBox: "0 0 24 24", children: /* @__PURE__ */ o("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: 2, d: "M5 13l4 4L19 7" }) }),
      children: "Verified"
    }
  ) : m && t?.isValid && w ? /* @__PURE__ */ o(
    K,
    {
      size: "sm",
      variant: "flat",
      color: "primary",
      onPress: w,
      children: "Verify"
    }
  ) : null, [N, C, D, m, t, w]), H = /* @__PURE__ */ o(
    X,
    {
      name: c,
      label: k,
      placeholder: x,
      value: m,
      onChange: (e) => E(e.target.value),
      onBlur: q,
      onFocus: ee,
      type: "email",
      isRequired: u,
      required: u,
      isDisabled: s,
      disabled: s,
      size: $,
      radius: U,
      variant: j,
      autoFocus: B,
      autoComplete: R,
      description: T,
      isInvalid: !!z,
      errorMessage: "",
      startContent: W || /* @__PURE__ */ o("svg", { className: "w-4 h-4 text-default-400", fill: "none", stroke: "currentColor", viewBox: "0 0 24 24", children: /* @__PURE__ */ o("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: 2, d: "M16 12a4 4 0 10-8 0 4 4 0 008 0zm0 0v1.5a2.5 2.5 0 005 0V12a9 9 0 10-9 9m4.5-1.206a8.959 8.959 0 01-4.5 1.207" }) }),
      endContent: C || oe
    }
  );
  return /* @__PURE__ */ M("div", { className: `space-y-2 ${F}`, children: [
    r && t?.suggestions.length ? /* @__PURE__ */ M(
      ie,
      {
        isOpen: Z,
        onOpenChange: p,
        placement: "bottom-start",
        showArrow: !0,
        children: [
          /* @__PURE__ */ o(le, { children: H }),
          /* @__PURE__ */ o(re, { className: "p-1", children: /* @__PURE__ */ o(
            ae,
            {
              "aria-label": "Email suggestions",
              onAction: (e) => G(e),
              children: t.suggestions.map((e, J) => /* @__PURE__ */ o(
                ne,
                {
                  value: e,
                  startContent: /* @__PURE__ */ o("svg", { className: "w-4 h-4 text-primary", fill: "none", stroke: "currentColor", viewBox: "0 0 24 24", children: /* @__PURE__ */ o("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: 2, d: "M13 10V3L4 14h7v7l9-11h-7z" }) }),
                  children: e
                },
                e
              ))
            }
          ) })
        ]
      }
    ) : H,
    z && /* @__PURE__ */ o(fe, { error: z, fieldName: c }),
    I.length > 0 && /* @__PURE__ */ M("div", { className: "text-xs text-default-500", children: [
      /* @__PURE__ */ o("span", { className: "font-medium", children: "Allowed domains:" }),
      " ",
      I.join(", ")
    ] }),
    /* @__PURE__ */ o(me, { children: !Z && t?.suggestions.length && b && /* @__PURE__ */ M(
      ce.div,
      {
        initial: { opacity: 0, y: -10 },
        animate: { opacity: 1, y: 0 },
        exit: { opacity: 0, y: -10 },
        className: "space-y-1",
        children: [
          /* @__PURE__ */ o("div", { className: "text-xs text-default-600 font-medium", children: "Did you mean:" }),
          /* @__PURE__ */ o("div", { className: "flex flex-wrap gap-1", children: t.suggestions.slice(0, 3).map((e, J) => /* @__PURE__ */ o(
            K,
            {
              size: "sm",
              variant: "flat",
              color: "primary",
              onPress: () => G(e),
              className: "text-xs h-6",
              children: e
            },
            J
          )) })
        ]
      }
    ) })
  ] });
}
var Ie = ge;
export {
  ge as EmailField,
  Ie as default
};
//# sourceMappingURL=email-field.js.map
