'use client';
import { jsx as e, jsxs as d, Fragment as me } from "react/jsx-runtime";
import o from "react";
import { Input as he, Button as T, Progress as G, Chip as fe } from "@heroui/react";
import { AnimatePresence as v, motion as Z } from "framer-motion";
import { useConfig as pe } from "../../hooks/use-config.js";
import { useFormField as ge } from "./form-wrapper.js";
import { FieldError as we } from "./field-error.js";
import { EyeSlashIcon as be, EyeIcon as xe, ExclamationTriangleIcon as ye, CheckIcon as Ne } from "@heroicons/react/24/outline";
import { generatePasswordSuggestions as ve, getPasswordStrength as ke } from "../auth/sign-up/index.js";
const C = {
  minLength: 8,
  maxLength: 128,
  requireUppercase: !0,
  requireLowercase: !0,
  requireNumbers: !0,
  requireSpecialChars: !0,
  preventCommon: !0,
  preventUserInfo: !1
}, Se = [
  "password",
  "123456",
  "123456789",
  "qwerty",
  "abc123",
  "password123",
  "admin",
  "letmein",
  "welcome",
  "monkey",
  "1234567890",
  "iloveyou"
];
function Pe(s, r, m) {
  const c = [];
  let a = 0;
  const i = [];
  if (r.minLength) {
    const n = s.length >= r.minLength;
    c.push({
      id: "length",
      label: `At least ${r.minLength} characters`,
      met: n,
      required: !0
    }), n ? a += 1 : i.push(`Password must be at least ${r.minLength} characters long`);
  }
  if (r.requireUppercase) {
    const n = /[A-Z]/.test(s);
    c.push({
      id: "uppercase",
      label: "One uppercase letter",
      met: n,
      required: !0
    }), n ? a += 1 : i.push("Password must contain at least one uppercase letter");
  }
  if (r.requireLowercase) {
    const n = /[a-z]/.test(s);
    c.push({
      id: "lowercase",
      label: "One lowercase letter",
      met: n,
      required: !0
    }), n ? a += 1 : i.push("Password must contain at least one lowercase letter");
  }
  if (r.requireNumbers) {
    const n = /\d/.test(s);
    c.push({
      id: "number",
      label: "One number",
      met: n,
      required: !0
    }), n ? a += 1 : i.push("Password must contain at least one number");
  }
  if (r.requireSpecialChars) {
    const n = /[!@#$%^&*(),.?":{}|<>_\-+=\[\]\\;'\/~`]/.test(s);
    c.push({
      id: "special",
      label: "One special character",
      met: n,
      required: !0
    }), n ? a += 1 : i.push("Password must contain at least one special character");
  }
  if (r.preventCommon) {
    const n = Se.includes(s.toLowerCase());
    c.push({
      id: "common",
      label: "Not a common password",
      met: !n,
      required: !1
    }), n && (a = Math.max(0, a - 2), i.push("This is a commonly used password"));
  }
  s.length >= 12 && (a += 1), s.length >= 16 && (a += 1), /[A-Z].*[A-Z]/.test(s) && (a += 0.5), /\d.*\d/.test(s) && (a += 0.5), /[!@#$%^&*()].*[!@#$%^&*()]/.test(s) && (a += 0.5), a = Math.min(4, Math.floor(a * 0.8));
  let l, h, f;
  switch (a) {
    case 0:
    case 1:
      l = "Very Weak", h = "danger", f = 20;
      break;
    case 2:
      l = "Weak", h = "warning", f = 40;
      break;
    case 3:
      l = "Good", h = "primary", f = 70;
      break;
    case 4:
      l = "Strong", h = "success", f = 100;
      break;
    default:
      l = "Very Weak", h = "danger", f = 0;
  }
  return {
    score: a,
    label: l,
    color: h,
    percentage: f,
    feedback: i,
    requirements: c
  };
}
function qe({
  name: s = "password",
  label: r = "Password",
  placeholder: m = "Enter your password",
  value: c = "",
  onChange: a,
  onBlur: i,
  onFocus: l,
  required: h = !1,
  disabled: f = !1,
  showStrength: n = !0,
  showRequirements: L = !1,
  showSuggestions: D = !1,
  enableGenerate: H = !1,
  allowToggle: I = !0,
  rules: w = C,
  size: M = "md",
  radius: J = "md",
  variant: F = "bordered",
  className: $ = "",
  autoFocus: A = !1,
  autoComplete: O = "current-password",
  isConfirmation: y = !1,
  originalPassword: N,
  error: b,
  description: R,
  startContent: K,
  advanceRequirements: z
}) {
  const { components: k, organizationSettings: U } = pe(), u = ge(s), Q = k.Input ?? he, X = k.Button ?? T, V = k.PasswordField;
  if (V)
    return /* @__PURE__ */ e(V, { name: s, label: r, placeholder: m, value: c, onChange: a, onBlur: i, onFocus: l, required: h, disabled: f, showStrength: n, showRequirements: L, allowToggle: I, rules: w, size: M, variant: F, className: $, autoFocus: A, autoComplete: O, isConfirmation: y, originalPassword: N, error: b, description: R });
  const [Y, ee] = o.useState(c), [S, te] = o.useState(!1), [j, W] = o.useState(!1), [B, se] = o.useState([]), p = a ? c : Y, E = o.useMemo(() => {
    const t = U?.passwordPolicy;
    return t ? {
      ...C,
      ...w,
      minLength: t.minLength || w.minLength,
      requireUppercase: t.requireUppercase ?? w.requireUppercase,
      requireLowercase: t.requireLowercase ?? w.requireLowercase,
      requireNumbers: t.requireNumbers ?? w.requireNumbers,
      requireSpecialChars: t.requireSpecialChars ?? w.requireSpecialChars
    } : { ...C, ...w };
  }, [w, U]), g = o.useMemo(() => p ? Pe(p, E) : null, [p, E]), P = o.useMemo(() => !y || !N || !p ? null : N !== p ? "Passwords do not match" : null, [y, N, p]), q = o.useMemo(() => {
    const t = [];
    return b && (Array.isArray(b) ? t.push(...b) : t.push(b)), u.error && (Array.isArray(u.error) ? t.push(...u.error) : t.push(u.error)), P && t.push(P), t.length > 0 ? t : null;
  }, [b, u.error, P]), re = o.useCallback((t) => {
    a ? a(t) : ee(t), u.clearError && u.clearError();
  }, [a, u]), ae = o.useCallback(() => {
    W(!1), u.setTouched && u.setTouched(!0), i?.();
  }, [u, i]), ne = o.useCallback(() => {
    W(!0), l?.();
  }, [l]), ie = o.useCallback(() => {
    te((t) => !t);
  }, []), ce = L && (j || p), oe = D && (j || p), le = n && p && !y, ue = o.useCallback(() => {
    const t = ve();
    se(t);
  }, []);
  return /* @__PURE__ */ d("div", { className: `space-y-2 ${$}`, children: [
    /* @__PURE__ */ e(
      Q,
      {
        name: s,
        label: r,
        placeholder: m,
        value: p,
        onBlur: ae,
        onFocus: ne,
        type: S ? "text" : "password",
        isRequired: h,
        isDisabled: f,
        onChange: (t) => re(typeof t == "string" ? t : t.target.value),
        required: h,
        disabled: f,
        size: M,
        radius: J,
        variant: F,
        autoFocus: A,
        autoComplete: O,
        description: R,
        isInvalid: !!q,
        errorMessage: "",
        startContent: K,
        endContent: I && /* @__PURE__ */ e(
          X,
          {
            isIconOnly: !0,
            variant: "light",
            size: "sm",
            onPress: ie,
            className: "text-default-400 hover:text-default-600",
            "aria-label": S ? "Hide password" : "Show password",
            children: S ? /* @__PURE__ */ e(be, { className: "w-4 h-4 text-default-400" }) : /* @__PURE__ */ e(xe, { className: "w-4 h-4 text-default-400" })
          }
        )
      }
    ),
    q && /* @__PURE__ */ e(we, { error: q, fieldName: s }),
    /* @__PURE__ */ e(v, { children: le && g && /* @__PURE__ */ d(
      Z.div,
      {
        initial: { opacity: 0, height: 0 },
        animate: { opacity: 1, height: "auto" },
        exit: { opacity: 0, height: 0 },
        className: "space-y-2",
        children: [
          /* @__PURE__ */ d("div", { className: "flex items-center justify-between text-sm", children: [
            /* @__PURE__ */ e("span", { className: "text-default-600", children: "Password strength:" }),
            /* @__PURE__ */ e("span", { className: `font-medium text-${g.color}`, children: g.label })
          ] }),
          /* @__PURE__ */ e(
            G,
            {
              value: g.percentage,
              color: g.color,
              size: "sm",
              className: "w-full"
            }
          ),
          g.feedback.length > 0 && /* @__PURE__ */ e("div", { className: "text-xs text-default-500", children: g.feedback[0] })
        ]
      }
    ) }),
    /* @__PURE__ */ e(v, { children: ce && g && /* @__PURE__ */ e(
      Z.div,
      {
        initial: { opacity: 0, height: 0 },
        animate: { opacity: 1, height: "auto" },
        exit: { opacity: 0, height: 0 },
        className: "space-y-2",
        children: z ? /* @__PURE__ */ e(
          Ce,
          {
            password: c,
            requirements: z
          }
        ) : /* @__PURE__ */ d(me, { children: [
          /* @__PURE__ */ e("div", { className: "text-sm font-medium text-default-700", children: "Password Requirements:" }),
          /* @__PURE__ */ e("div", { className: "space-y-1", children: g.requirements.map((t) => /* @__PURE__ */ d("div", { className: "flex items-center gap-2 text-xs", children: [
            /* @__PURE__ */ e("div", { className: `
                    w-4 h-4 rounded-full flex items-center justify-center
                    ${t.met ? "bg-success-100 text-success-600 dark:bg-success-900/30 dark:text-success-400" : "bg-default-200 text-default-400 dark:bg-default-800"}
                  `, children: t.met ? /* @__PURE__ */ e(
              "svg",
              {
                className: "w-3 h-3",
                fill: "none",
                stroke: "currentColor",
                viewBox: "0 0 24 24",
                children: /* @__PURE__ */ e(
                  "path",
                  {
                    strokeLinecap: "round",
                    strokeLinejoin: "round",
                    strokeWidth: 2,
                    d: "M5 13l4 4L19 7"
                  }
                )
              }
            ) : /* @__PURE__ */ e("div", { className: "w-1.5 h-1.5 rounded-full bg-current" }) }),
            /* @__PURE__ */ e(
              "span",
              {
                className: t.met ? "text-success-600 dark:text-success-400" : "text-default-500",
                children: t.label
              }
            )
          ] }, t.id)) })
        ] })
      }
    ) }),
    /* @__PURE__ */ e(v, { children: oe && g && /* @__PURE__ */ e(
      "div",
      {
        children: B.length > 0 && /* @__PURE__ */ d("div", { className: "space-y-2", children: [
          /* @__PURE__ */ e("div", { className: "text-xs text-default-500", children: "Suggested passwords:" }),
          /* @__PURE__ */ e("div", { className: "space-y-1", children: B.map((t, de) => /* @__PURE__ */ e(
            "button",
            {
              type: "button",
              className: "text-xs font-mono text-primary-600 hover:text-primary-800 hover:underline block",
              children: t
            },
            de
          )) })
        ] })
      }
    ) }),
    /* @__PURE__ */ e(v, { children: H && /* @__PURE__ */ e(
      T,
      {
        type: "button",
        variant: "light",
        size: "sm",
        onPress: ue,
        children: "Generate secure password"
      }
    ) })
  ] });
}
const _ = o.memo(qe);
function Ue({
  originalPassword: s,
  ...r
}) {
  return /* @__PURE__ */ e(
    _,
    {
      ...r,
      name: r.name || "passwordConfirmation",
      label: r.label || "Confirm Password",
      placeholder: r.placeholder || "Confirm your password",
      autoComplete: "new-password",
      isConfirmation: !0,
      originalPassword: s,
      showStrength: !1,
      showRequirements: !1
    }
  );
}
function Ce({
  password: s,
  requirements: r
}) {
  const m = ke(s), c = (i) => {
    switch (i) {
      case "weak":
        return "danger";
      case "fair":
        return "warning";
      case "good":
        return "primary";
      case "strong":
        return "success";
      default:
        return "default";
    }
  }, a = (i) => {
    switch (i) {
      case "weak":
        return "Weak";
      case "fair":
        return "Fair";
      case "good":
        return "Good";
      case "strong":
        return "Strong";
      default:
        return "";
    }
  };
  return s ? /* @__PURE__ */ d("div", { className: "space-y-2", children: [
    /* @__PURE__ */ d("div", { className: "flex items-center gap-2", children: [
      /* @__PURE__ */ e(
        G,
        {
          value: m.score / 6 * 100,
          color: c(m.strength),
          size: "sm",
          className: "flex-1"
        }
      ),
      /* @__PURE__ */ e(
        fe,
        {
          size: "sm",
          color: c(m.strength),
          variant: "flat",
          children: a(m.strength)
        }
      )
    ] }),
    m.feedback.length > 0 && /* @__PURE__ */ e("div", { className: "text-xs space-y-1", children: m.feedback.map((i, l) => /* @__PURE__ */ d("div", { className: "flex items-center gap-1 text-default-500", children: [
      /* @__PURE__ */ e(ye, { className: "w-3 h-3" }),
      /* @__PURE__ */ e("span", { children: i })
    ] }, l)) }),
    r && /* @__PURE__ */ d("div", { className: "text-xs space-y-1", children: [
      r.minLength && /* @__PURE__ */ e(
        x,
        {
          met: s.length >= r.minLength,
          text: `At least ${r.minLength} characters`
        }
      ),
      r.requireUppercase && /* @__PURE__ */ e(
        x,
        {
          met: /[A-Z]/.test(s),
          text: "One uppercase letter"
        }
      ),
      r.requireLowercase && /* @__PURE__ */ e(
        x,
        {
          met: /[a-z]/.test(s),
          text: "One lowercase letter"
        }
      ),
      r.requireNumbers && /* @__PURE__ */ e(
        x,
        {
          met: /\d/.test(s),
          text: "One number"
        }
      ),
      r.requireSymbols && /* @__PURE__ */ e(
        x,
        {
          met: /[!@#$%^&*(),.?":{}|<>]/.test(s),
          text: "One symbol"
        }
      )
    ] })
  ] }) : null;
}
function x({ met: s, text: r }) {
  return /* @__PURE__ */ d("div", { className: `flex items-center gap-1 ${s ? "text-success-600" : "text-default-400"}`, children: [
    /* @__PURE__ */ e(Ne, { className: `w-3 h-3 ${s ? "opacity-100" : "opacity-30"}` }),
    /* @__PURE__ */ e("span", { children: r })
  ] });
}
var Ve = _;
export {
  Ue as PasswordConfirmationField,
  _ as PasswordField,
  qe as PasswordFieldComponent,
  Ve as default
};
//# sourceMappingURL=password-field.js.map
