import { useMemo as I, useContext as j, useState as V, useRef as S, useEffect as O, createContext as F } from "react";
import { c as P } from "./useFocusVisible-_CKiSck5.es.js";
import { b as Q } from "./mergeProps-B-cqsBhL.es.js";
import { $ as h } from "./useEffectEvent-B4Fd4l1o.es.js";
const x = {
  badInput: !1,
  customError: !1,
  patternMismatch: !1,
  rangeOverflow: !1,
  rangeUnderflow: !1,
  stepMismatch: !1,
  tooLong: !1,
  tooShort: !1,
  typeMismatch: !1,
  valueMissing: !1,
  valid: !0
}, B = {
  ...x,
  customError: !0,
  valid: !1
}, m = {
  isInvalid: !1,
  validationDetails: x,
  validationErrors: []
}, k = /* @__PURE__ */ F({}), C = "__formValidationState" + /* @__PURE__ */ Date.now();
function _(i) {
  if (i[C]) {
    let { realtimeValidation: e, displayValidation: a, updateValidation: r, resetValidation: s, commitValidation: l } = i[C];
    return {
      realtimeValidation: e,
      displayValidation: a,
      updateValidation: r,
      resetValidation: s,
      commitValidation: l
    };
  }
  return q(i);
}
function q(i) {
  let { isInvalid: e, validationState: a, name: r, value: s, builtinValidation: l, validate: v, validationBehavior: d = "aria" } = i;
  a && (e || (e = a === "invalid"));
  let c = e !== void 0 ? {
    isInvalid: e,
    validationErrors: [],
    validationDetails: B
  } : null, n = I(() => {
    if (!v || s == null) return null;
    let o = N(v, s);
    return A(o);
  }, [
    v,
    s
  ]);
  l?.validationDetails.valid && (l = void 0);
  let t = j(k), u = I(() => r ? Array.isArray(r) ? r.flatMap((o) => w(t[o])) : w(t[r]) : [], [
    t,
    r
  ]), [f, R] = V(t), [D, b] = V(!1);
  t !== f && (R(t), b(!1));
  let p = I(() => A(D ? [] : u), [
    D,
    u
  ]), L = S(m), [E, g] = V(m), $ = S(m), T = () => {
    if (!U) return;
    y(!1);
    let o = n || l || L.current;
    M(o, $.current) || ($.current = o, g(o));
  }, [U, y] = V(!1);
  return O(T), {
    realtimeValidation: c || p || n || l || m,
    displayValidation: d === "native" ? c || p || E : c || p || n || l || E,
    updateValidation(o) {
      d === "aria" && !M(E, o) ? g(o) : L.current = o;
    },
    resetValidation() {
      let o = m;
      M(o, $.current) || ($.current = o, g(o)), d === "native" && y(!1), b(!0);
    },
    commitValidation() {
      d === "native" && y(!0), b(!0);
    }
  };
}
function w(i) {
  return i ? Array.isArray(i) ? i : [
    i
  ] : [];
}
function N(i, e) {
  if (typeof i == "function") {
    let a = i(e);
    if (a && typeof a != "boolean") return w(a);
  }
  return [];
}
function A(i) {
  return i.length ? {
    isInvalid: !0,
    validationErrors: i,
    validationDetails: B
  } : null;
}
function M(i, e) {
  return i === e ? !0 : !!i && !!e && i.isInvalid === e.isInvalid && i.validationErrors.length === e.validationErrors.length && i.validationErrors.every((a, r) => a === e.validationErrors[r]) && Object.entries(i.validationDetails).every(([a, r]) => e.validationDetails[a] === r);
}
function ee(...i) {
  let e = /* @__PURE__ */ new Set(), a = !1, r = {
    ...x
  };
  for (let v of i) {
    var s, l;
    for (let d of v.validationErrors) e.add(d);
    a || (a = v.isInvalid);
    for (let d in r) (s = r)[l = d] || (s[l] = v.validationDetails[d]);
  }
  return r.valid = !a, {
    isInvalid: a,
    validationErrors: [
      ...e
    ],
    validationDetails: r
  };
}
function ae(i, e, a) {
  let { validationBehavior: r, focus: s } = i;
  Q(() => {
    if (r === "native" && a?.current && !a.current.disabled) {
      let n = e.realtimeValidation.isInvalid ? e.realtimeValidation.validationErrors.join(" ") || "Invalid value." : "";
      a.current.setCustomValidity(n), a.current.hasAttribute("title") || (a.current.title = ""), e.realtimeValidation.isInvalid || e.updateValidation(z(a.current));
    }
  });
  let l = S(!1), v = h(() => {
    l.current || e.resetValidation();
  }), d = h((n) => {
    var t;
    e.displayValidation.isInvalid || e.commitValidation();
    let u = a == null || (t = a.current) === null || t === void 0 ? void 0 : t.form;
    if (!n.defaultPrevented && a && u && G(u) === a.current) {
      var f;
      s ? s() : (f = a.current) === null || f === void 0 || f.focus(), P("keyboard");
    }
    n.preventDefault();
  }), c = h(() => {
    e.commitValidation();
  });
  O(() => {
    let n = a?.current;
    if (!n) return;
    let t = n.form, u = t?.reset;
    return t && (t.reset = () => {
      l.current = !window.event || window.event.type === "message" && window.event.target instanceof MessagePort, u?.call(t), l.current = !1;
    }), n.addEventListener("invalid", d), n.addEventListener("change", c), t?.addEventListener("reset", v), () => {
      n.removeEventListener("invalid", d), n.removeEventListener("change", c), t?.removeEventListener("reset", v), t && (t.reset = u);
    };
  }, [
    a,
    r
  ]);
}
function Y(i) {
  let e = i.validity;
  return {
    badInput: e.badInput,
    customError: e.customError,
    patternMismatch: e.patternMismatch,
    rangeOverflow: e.rangeOverflow,
    rangeUnderflow: e.rangeUnderflow,
    stepMismatch: e.stepMismatch,
    tooLong: e.tooLong,
    tooShort: e.tooShort,
    typeMismatch: e.typeMismatch,
    valueMissing: e.valueMissing,
    valid: e.valid
  };
}
function z(i) {
  return {
    isInvalid: !i.validity.valid,
    validationDetails: Y(i),
    validationErrors: i.validationMessage ? [
      i.validationMessage
    ] : []
  };
}
function G(i) {
  for (let e = 0; e < i.elements.length; e++) {
    let a = i.elements[e];
    if (!a.validity.valid) return a;
  }
  return null;
}
export {
  _ as $,
  ae as a,
  C as b,
  m as c,
  ee as d,
  x as e
};
//# sourceMappingURL=useFormValidation-Cx9rG8-b.es.js.map
