import { o as ne } from "./rolldown-runtime.js";
import { i as ve, r as ge, t as pe } from "./vendor/react.js";
import { p as ye } from "./safeReact.js";
import { l as Z, r as be, s as Re, t as O, u as xe } from "./useRenderElement.js";
import { n as L, t as _ } from "./useIsoLayoutEffect.js";
import { t as A } from "./utils.js";
import { a as Fe, n as Ce, t as Me } from "./useTransitionStatus.js";
import { n as Ve } from "./useTimeout.js";
import { t as q } from "./useBaseUiId.js";
import { r as he } from "./shadowDom.js";
import { t as Ie } from "./owner.js";
import { a as ee, c as z, i as De, n as Se, r as k, s as te } from "./useRegisterFieldControl.js";
import { t as G } from "./FormContext.js";
import { t as Pe } from "./FieldControl.js";
var M = pe(), s = /* @__PURE__ */ ne(ve()), oe = /* @__PURE__ */ s.createContext({
  legendId: void 0,
  setLegendId: () => {
  },
  disabled: void 0
});
function le(t = !1) {
  const e = s.useContext(oe);
  if (!e && !t) throw new Error(Re(86));
  return e;
}
var Ee = function(e) {
  const o = q(), b = e.controlId === void 0 ? o : e.controlId, [p, R] = s.useState(b), [c, v] = s.useState(e.labelId), [u, r] = s.useState([]), l = xe(() => /* @__PURE__ */ new Map()), { messageIds: d } = k(), a = L((x, i) => {
    const n = l.current;
    if (i === void 0) {
      n.delete(x);
      return;
    }
    n.set(x, i), R((g) => {
      if (n.size === 0) return;
      let I;
      for (const S of n.values()) {
        if (g !== void 0 && S === g) return g;
        I === void 0 && (I = S);
      }
      return I;
    });
  }), V = s.useCallback((x) => Z({ "aria-describedby": d.concat(u).join(" ") || void 0 }, x), [d, u]), D = s.useMemo(() => ({
    controlId: p,
    registerControlId: a,
    labelId: c,
    setLabelId: v,
    messageIds: u,
    setMessageIds: r,
    getDescriptionProps: V
  }), [
    p,
    a,
    c,
    v,
    u,
    r,
    V
  ]);
  return /* @__PURE__ */ (0, M.jsx)(Se.Provider, {
    value: D,
    children: e.children
  });
}, je = /* @__PURE__ */ ne(ge());
function Le(t, e) {
  const o = q(t);
  return _(() => (e(o), () => {
    e(void 0);
  }), [o, e]), o;
}
function Ne(t = {}) {
  const { id: e, fallbackControlId: o, native: b = !1, setLabelId: p, focusControl: R } = t, { controlId: c, setLabelId: v } = k(), u = Le(e, L((a) => {
    v(a), p?.(a);
  })), r = c ?? o;
  function l(a) {
    if (R) {
      R(a, r);
      return;
    }
    if (!r) return;
    const V = Ie(a.currentTarget).getElementById(r);
    ye(V) && we(V);
  }
  function d(a) {
    he(a.nativeEvent)?.closest("button,input,select,textarea") || (!a.defaultPrevented && a.detail > 1 && a.preventDefault(), !b && l(a));
  }
  return b ? {
    id: u,
    htmlFor: r ?? void 0,
    onMouseDown: d
  } : {
    id: u,
    onClick: d,
    onPointerDown(a) {
      a.preventDefault();
    }
  };
}
function we(t) {
  t.focus({ focusVisible: !0 });
}
function Y(t, e) {
  return {
    ...t,
    state: {
      ...t.state,
      valid: !e && t.state.valid
    }
  };
}
var W = Object.keys(te);
function Te(t) {
  if (!t || t.valid || !t.valueMissing) return !1;
  let e = !1;
  for (const o of W)
    o !== "valid" && (o === "valueMissing" && (e = t[o]), t[o] && (e = !1));
  return e;
}
function Ae(t) {
  const { formRef: e, clearErrors: o } = G(), { setValidityData: b, validate: p, validityData: R, validationDebounceTime: c, invalid: v, markedDirtyRef: u, state: r, name: l, shouldValidateOnChange: d } = t, { controlId: a, getDescriptionProps: V } = k(), D = Ve(), x = s.useRef(null), i = L(async (I, S = !1) => {
    const f = x.current;
    if (!f) return;
    if (S) {
      if (r.valid !== !1) return;
      const C = f.validity;
      if (!C.valueMissing) {
        const m = {
          value: I,
          state: {
            ...te,
            valid: !0
          },
          error: "",
          errors: [],
          initialValue: R.initialValue
        };
        if (f.setCustomValidity(""), a) {
          const y = e.current.fields.get(a);
          y && e.current.fields.set(a, {
            ...y,
            ...Y(m, !1)
          });
        }
        b(m);
        return;
      }
      const F = W.reduce((m, y) => (m[y] = C[y], m), {});
      if (!F.valid && !Te(F)) return;
    }
    function $(C) {
      const F = W.reduce((y, B) => (y[B] = C.validity[B], y), {});
      let m = !1;
      for (const y of W)
        if (y !== "valid") {
          if (y === "valueMissing" && F[y]) m = !0;
          else if (F[y]) return F;
        }
      return m && !u.current && (F.valid = !0, F.valueMissing = !1), F;
    }
    D.clear();
    let h = null, N = [];
    const j = $(f);
    let E;
    const w = d();
    if (f.validationMessage && !w)
      E = f.validationMessage, N = [f.validationMessage];
    else {
      const C = p(I, Array.from(e.current.fields.values()).reduce((F, m) => (m.name && (F[m.name] = m.getValue()), F), {}));
      typeof C == "object" && C !== null && "then" in C ? h = await C : h = C, h !== null ? (j.valid = !1, j.customError = !0, Array.isArray(h) ? (N = h, f.setCustomValidity(h.join(`
`))) : h && (N = [h], f.setCustomValidity(h))) : w && (f.setCustomValidity(""), j.customError = !1, f.validationMessage ? (E = f.validationMessage, N = [f.validationMessage]) : f.validity.valid && !j.valid && (j.valid = !0));
    }
    const P = {
      value: I,
      state: j,
      error: E ?? (Array.isArray(h) ? h[0] : h ?? ""),
      errors: N,
      initialValue: R.initialValue
    };
    if (a) {
      const C = e.current.fields.get(a);
      C && e.current.fields.set(a, {
        ...C,
        ...Y(P, v)
      });
    }
    b(P);
  }), n = s.useCallback((I = {}) => Z(V, r.valid === !1 ? { "aria-invalid": !0 } : be, I), [V, r.valid]), g = s.useCallback((I = {}) => Z({ onChange(S) {
    if (S.nativeEvent.defaultPrevented) return;
    if (o(l), !d()) {
      i(S.currentTarget.value, !0);
      return;
    }
    const f = S.currentTarget;
    if (f.value === "") {
      i(f.value);
      return;
    }
    D.clear(), c ? D.start(c, () => {
      i(f.value);
    }) : i(f.value);
  } }, n(I)), [
    n,
    o,
    l,
    D,
    i,
    c,
    d
  ]);
  return s.useMemo(() => ({
    getValidationProps: n,
    getInputValidationProps: g,
    inputRef: x,
    commit: i
  }), [
    n,
    g,
    i
  ]);
}
function _e(t) {
  const { commit: e, invalid: o, markedDirtyRef: b, name: p, setValidityData: R, validityData: c } = t, { formRef: v } = G(), u = s.useRef(null), r = s.useRef(null), l = s.useRef(null), d = L(() => {
    const i = r.current;
    if (i)
      return i.getValue ? i.getValue() : i.value;
  }), a = L((i = !0) => {
    const n = r.current;
    if (!n) return;
    let g = n.value;
    g === void 0 && (g = d()), b.current = !0, i ? je.flushSync(() => e(g)) : e(g);
  });
  function V() {
    const i = r.current;
    !i || !i.id || v.current.fields.set(i.id, {
      getValue: d,
      name: p,
      controlRef: i.controlRef ?? l,
      validityData: Y(c, o),
      validate: a
    });
  }
  function D(i = r.current?.id) {
    i && v.current.fields.delete(i);
  }
  function x() {
    const i = r.current;
    if (!i) return;
    let n = i.value;
    n === void 0 && (n = d()), c.initialValue === null && n !== null && R((g) => ({
      ...g,
      initialValue: n
    }));
  }
  return _(() => {
    const i = r.current;
    !i || !i.id || v.current.fields.set(i.id, {
      getValue: d,
      name: p,
      controlRef: i.controlRef ?? l,
      validityData: Y(c, o),
      validate: a
    });
  }, [
    v,
    d,
    o,
    p,
    a,
    c
  ]), _(() => {
    const i = v.current.fields;
    return () => {
      const n = r.current?.id;
      n && i.delete(n);
    };
  }, [v]), L((i, n) => {
    if (!n) {
      u.current === i && (u.current = null, D(), r.current = null);
      return;
    }
    const g = r.current?.id;
    u.current = i, r.current = n, g && g !== n.id && D(g), x(), V();
  });
}
var Oe = /* @__PURE__ */ s.forwardRef(function(e, o) {
  const { errors: b, validationMode: p, submitAttemptedRef: R } = G(), { render: c, className: v, validate: u, validationDebounceTime: r = 0, validationMode: l = p, name: d, disabled: a = !1, invalid: V, dirty: D, touched: x, actionsRef: i, style: n, ...g } = e, { disabled: I } = le(), S = L(u || (() => null)), f = I || a, [$, h] = s.useState(!1), [N, j] = s.useState(!1), [E, w] = s.useState(!1), [P, C] = s.useState(!1), F = D ?? N, m = x ?? $, y = s.useRef(!1), B = L((H) => {
    D === void 0 && (H && (y.current = !0), j(H));
  }), re = L((H) => {
    x === void 0 && h(H);
  }), Q = L(() => l === "onChange" || l === "onSubmit" && R.current), ce = !!d && Object.hasOwn(b, d) && b[d] !== void 0, K = V === !0 || ce, [T, X] = s.useState({
    state: te,
    error: "",
    errors: [],
    value: null,
    initialValue: null
  }), ie = !K && T.state.valid, J = s.useMemo(() => ({
    disabled: f,
    touched: m,
    dirty: F,
    valid: ie,
    filled: E,
    focused: P
  }), [
    f,
    m,
    F,
    ie,
    E,
    P
  ]), U = Ae({
    setValidityData: X,
    validate: S,
    validityData: T,
    validationDebounceTime: r,
    invalid: K,
    markedDirtyRef: y,
    state: J,
    name: d,
    shouldValidateOnChange: Q
  }), se = s.useCallback(() => {
    y.current = !0, U.commit(T.value);
  }, [U, T]), ae = _e({
    commit: U.commit,
    invalid: K,
    markedDirtyRef: y,
    name: d,
    setValidityData: X,
    validityData: T
  });
  s.useImperativeHandle(i, () => ({ validate: se }), [se]);
  const fe = s.useMemo(() => ({
    invalid: K,
    name: d,
    validityData: T,
    setValidityData: X,
    disabled: f,
    touched: m,
    setTouched: re,
    dirty: F,
    setDirty: B,
    filled: E,
    setFilled: w,
    focused: P,
    setFocused: C,
    validate: S,
    validationMode: l,
    validationDebounceTime: r,
    shouldValidateOnChange: Q,
    state: J,
    markedDirtyRef: y,
    registerFieldControl: ae,
    validation: U
  }), [
    K,
    d,
    T,
    f,
    m,
    re,
    F,
    B,
    E,
    w,
    P,
    C,
    S,
    l,
    r,
    Q,
    J,
    ae,
    U
  ]), me = O("div", e, {
    ref: o,
    state: J,
    props: g,
    stateAttributesMapping: z
  });
  return /* @__PURE__ */ (0, M.jsx)(De.Provider, {
    value: fe,
    children: me
  });
}), ke = /* @__PURE__ */ s.forwardRef(function(e, o) {
  return /* @__PURE__ */ (0, M.jsx)(Ee, { children: /* @__PURE__ */ (0, M.jsx)(Oe, {
    ...e,
    ref: o
  }) });
}), $e = /* @__PURE__ */ s.forwardRef(function(e, o) {
  const { render: b, className: p, style: R, id: c, nativeLabel: v = !0, ...u } = e, r = ee(!1), { labelId: l } = k(), d = s.useRef(null), a = Ne({
    id: l ?? c,
    native: v
  });
  return O("label", e, {
    ref: [o, d],
    state: r.state,
    props: [a, u],
    stateAttributesMapping: z
  });
}), Be = {
  ...z,
  ...Fe
}, Ke = /* @__PURE__ */ s.forwardRef(function(e, o) {
  const { render: b, id: p, className: R, match: c, style: v, ...u } = e, r = q(p), { validityData: l, state: d, name: a } = ee(!1), { setMessageIds: V } = k(), { errors: D } = G(), x = a ? D[a] : null, i = typeof c == "string";
  let n = !1;
  c === !0 ? n = !0 : i ? n = !!l.state[c] : n = !!x || l.state.valid === !1;
  const { mounted: g, transitionStatus: I, setMounted: S } = Me(n);
  _(() => {
    if (!(!n || !r))
      return V((m) => m.concat(r)), () => {
        V((m) => m.filter((y) => y !== r));
      };
  }, [
    n,
    r,
    V
  ]);
  const f = s.useRef(null), [$, h] = s.useState(null), [N, j] = s.useState(null), E = l.errors.length > 1 ? /* @__PURE__ */ (0, M.jsx)("ul", { children: l.errors.map((m) => /* @__PURE__ */ (0, M.jsx)("li", { children: m }, m)) }) : l.error, w = i ? E : x || E;
  let P = l.error;
  x != null ? P = Array.isArray(x) ? JSON.stringify(x) : x : l.errors.length > 1 && (P = JSON.stringify(l.errors)), n && P !== N && (j(P), h(w)), Ce({
    open: n,
    ref: f,
    onComplete() {
      n || S(!1);
    }
  });
  const C = {
    ...d,
    transitionStatus: I
  }, F = O("div", e, {
    ref: [o, f],
    state: C,
    props: [{
      id: r,
      children: n ? w : $
    }, u],
    stateAttributesMapping: Be,
    enabled: g
  });
  return g ? F : null;
}), Ue = /* @__PURE__ */ s.forwardRef(function(e, o) {
  const { render: b, id: p, className: R, style: c, ...v } = e, u = q(p), r = ee(!1), { setMessageIds: l } = k();
  return _(() => {
    if (u)
      return l((d) => d.concat(u)), () => {
        l((d) => d.filter((a) => a !== u));
      };
  }, [u, l]), O("p", e, {
    ref: o,
    state: r.state,
    props: [{ id: u }, v],
    stateAttributesMapping: z
  });
});
var qe = /* @__PURE__ */ s.forwardRef(function(e, o) {
  const { render: b, className: p, style: R, disabled: c = !1, ...v } = e, [u, r] = s.useState(void 0), l = O("fieldset", e, {
    ref: o,
    state: { disabled: c },
    props: [{ "aria-labelledby": u }, v]
  }), d = s.useMemo(() => ({
    legendId: u,
    setLegendId: r,
    disabled: c
  }), [
    u,
    r,
    c
  ]);
  return /* @__PURE__ */ (0, M.jsx)(oe.Provider, {
    value: d,
    children: l
  });
}), Je = /* @__PURE__ */ s.forwardRef(function(e, o) {
  const { render: b, className: p, style: R, id: c, ...v } = e, { disabled: u, setLegendId: r } = le(), l = q(c);
  return _(() => (r(l), () => {
    r(void 0);
  }), [r, l]), O("div", e, {
    state: { disabled: u ?? !1 },
    ref: o,
    props: [{ id: l }, v]
  });
});
function He({ className: t, ...e }) {
  return /* @__PURE__ */ (0, M.jsx)(ke, {
    "data-slot": "field-root",
    className: A("flex w-full flex-col gap-1.5", t),
    ...e
  });
}
function We({ className: t, ...e }) {
  return /* @__PURE__ */ (0, M.jsx)($e, {
    "data-slot": "field-label",
    className: A("text-sm font-medium leading-5 text-secondary-foreground", "data-[disabled]:opacity-50", t),
    ...e
  });
}
function Ye({ className: t, ...e }) {
  return /* @__PURE__ */ (0, M.jsx)(Pe, {
    "data-slot": "field-control",
    className: A(t),
    ...e
  });
}
function ze({ className: t, ...e }) {
  return /* @__PURE__ */ (0, M.jsx)(Ue, {
    "data-slot": "field-description",
    className: A("text-muted-foreground text-sm leading-5", t),
    ...e
  });
}
function Ge({ className: t, ...e }) {
  return /* @__PURE__ */ (0, M.jsx)(Ke, {
    "data-slot": "field-error",
    className: A("text-destructive text-sm font-normal", t),
    ...e
  });
}
function de({ className: t, ...e }) {
  return /* @__PURE__ */ (0, M.jsx)(qe, {
    "data-slot": "fieldset-root",
    className: A("flex flex-col gap-6 border-0 p-0 m-0", "has-[>[data-slot=checkbox-group]]:gap-3", "has-[>[data-slot=radio-group]]:gap-3", t),
    ...e
  });
}
function ue({ className: t, ...e }) {
  return /* @__PURE__ */ (0, M.jsx)(Je, {
    "data-slot": "fieldset-legend",
    className: A("mb-3 text-base font-medium leading-6", t),
    ...e
  });
}
He.displayName = "FieldRoot";
We.displayName = "FieldLabel";
Ye.displayName = "FieldControl";
ze.displayName = "FieldDescription";
Ge.displayName = "FieldError";
de.displayName = "FieldsetRoot";
ue.displayName = "FieldsetLegend";
function Qe({ legend: t, description: e, children: o, className: b, disabled: p }) {
  const R = s.useId();
  return /* @__PURE__ */ (0, M.jsxs)(de, {
    className: b,
    disabled: p,
    "aria-describedby": e ? R : void 0,
    children: [
      t && /* @__PURE__ */ (0, M.jsx)(ue, { children: t }),
      e && /* @__PURE__ */ (0, M.jsx)("p", {
        id: R,
        className: "text-muted-foreground -mt-2 mb-4 text-sm",
        children: e
      }),
      o
    ]
  });
}
Qe.displayName = "Fieldset";
export {
  Qe as t
};

//# sourceMappingURL=Fieldset.js.map