"use client";
import * as h from "react-dom";
import { u as m } from "./useIsoLayoutEffect-DpJo20by.js";
import { u as C } from "./useStableCallback-CudqoENs.js";
import * as s from "react";
import { N as t, f as D, E as x } from "./useRenderElement-DNB2vtF1.js";
let T = /* @__PURE__ */ (function(e) {
  return e.disabled = "data-disabled", e.valid = "data-valid", e.invalid = "data-invalid", e.touched = "data-touched", e.dirty = "data-dirty", e.filled = "data-filled", e.focused = "data-focused", e;
})({});
const I = {
  badInput: !1,
  customError: !1,
  patternMismatch: !1,
  rangeOverflow: !1,
  rangeUnderflow: !1,
  stepMismatch: !1,
  tooLong: !1,
  tooShort: !1,
  typeMismatch: !1,
  valid: null,
  valueMissing: !1
}, d = {
  valid: null,
  touched: !1,
  dirty: !1,
  filled: !1,
  focused: !1
}, N = {
  disabled: !1,
  ...d
}, w = {
  valid(e) {
    return e === null ? null : e ? {
      [T.valid]: ""
    } : {
      [T.invalid]: ""
    };
  }
}, b = /* @__PURE__ */ s.createContext({
  invalid: void 0,
  name: void 0,
  validityData: {
    state: I,
    errors: [],
    error: "",
    value: "",
    initialValue: null
  },
  setValidityData: t,
  disabled: void 0,
  touched: d.touched,
  setTouched: t,
  dirty: d.dirty,
  setDirty: t,
  filled: d.filled,
  setFilled: t,
  focused: d.focused,
  setFocused: t,
  validate: () => null,
  validationMode: "onSubmit",
  validationDebounceTime: 0,
  shouldValidateOnChange: () => !1,
  state: N,
  markedDirtyRef: {
    current: !1
  },
  validation: {
    getValidationProps: (e = x) => e,
    getInputValidationProps: (e = x) => e,
    inputRef: {
      current: null
    },
    commit: async () => {
    }
  }
});
process.env.NODE_ENV !== "production" && (b.displayName = "FieldRootContext");
function _(e = !0) {
  const a = s.useContext(b);
  if (a.setValidityData === t && !e)
    throw new Error(process.env.NODE_ENV !== "production" ? "Base UI: FieldRootContext is missing. Field parts must be placed within <Field.Root>." : D(28));
  return a;
}
function F(e, a) {
  return {
    ...e,
    state: {
      ...e.state,
      valid: !a && e.state.valid
    }
  };
}
const g = /* @__PURE__ */ s.createContext({
  formRef: {
    current: {
      fields: /* @__PURE__ */ new Map()
    }
  },
  errors: {},
  clearErrors: t,
  validationMode: "onSubmit",
  submitAttemptedRef: {
    current: !1
  }
});
process.env.NODE_ENV !== "production" && (g.displayName = "FormContext");
function L() {
  return s.useContext(g);
}
function U(e) {
  const {
    enabled: a = !0,
    value: r,
    id: n,
    name: p,
    controlRef: v,
    commit: c
  } = e, {
    formRef: u
  } = L(), {
    invalid: E,
    markedDirtyRef: y,
    validityData: f,
    setValidityData: V
  } = _(), o = C(e.getValue);
  m(() => {
    if (!a)
      return;
    let i = r;
    i === void 0 && (i = o()), f.initialValue === null && i !== null && V((l) => ({
      ...l,
      initialValue: i
    }));
  }, [a, V, r, f.initialValue, o]), m(() => {
    !a || !n || u.current.fields.set(n, {
      getValue: o,
      name: p,
      controlRef: v,
      validityData: F(f, E),
      validate(i = !0) {
        let l = r;
        l === void 0 && (l = o()), y.current = !0, i ? h.flushSync(() => c(l)) : c(l);
      }
    });
  }, [c, v, a, u, o, n, E, y, p, f, r]), m(() => {
    const i = u.current.fields;
    return () => {
      n && i.delete(n);
    };
  }, [u, n]);
}
const R = /* @__PURE__ */ s.createContext({
  controlId: void 0,
  registerControlId: t,
  labelId: void 0,
  setLabelId: t,
  messageIds: [],
  setMessageIds: t,
  getDescriptionProps: (e) => e
});
process.env.NODE_ENV !== "production" && (R.displayName = "LabelableContext");
function P() {
  return s.useContext(R);
}
export {
  _ as a,
  U as b,
  L as c,
  w as f,
  P as u
};
