import { useRef as S, useState as x, useCallback as s } from "react";
import { getPath as R } from "../../paths/get-path.mjs";
import { setPath as W } from "../../paths/set-path.mjs";
function A({
  initialValues: d,
  onValuesChange: a,
  mode: h
}) {
  const p = S(!1), [V, v] = x(d || {}), e = S(V), l = S(V), r = s(
    ({
      values: t,
      subscribers: u,
      updateState: i = !0,
      mergeWithPreviousValues: f = !0
    }) => {
      const c = e.current, o = t instanceof Function ? t(e.current) : t, n = f ? { ...c, ...o } : o;
      e.current = n, i && v(n), a == null || a(n, c), u == null || u.filter(Boolean).forEach((k) => k({ updatedValues: n, previousValues: c }));
    },
    [a]
  ), F = s(
    (t) => {
      var f;
      const u = R(t.path, e.current), i = t.value instanceof Function ? t.value(u) : t.value;
      if (u !== i) {
        const c = e.current, o = W(t.path, i, e.current);
        r({ values: o, updateState: t.updateState }), (f = t.subscribers) == null || f.filter(Boolean).forEach(
          (n) => n({ path: t.path, updatedValues: o, previousValues: c })
        );
      }
    },
    [r]
  ), m = s((t) => {
    l.current = t;
  }, []), P = s(
    (t, u) => {
      p.current || (p.current = !0, r({ values: t, updateState: h === "controlled" }), m(t), u());
    },
    [r]
  ), z = s(() => {
    r({
      values: l.current,
      updateState: !0,
      mergeWithPreviousValues: !1
    });
  }, [r]), B = s(() => e.current, []), E = s(() => l.current, []);
  return {
    initialized: p,
    stateValues: V,
    refValues: e,
    valuesSnapshot: l,
    setValues: r,
    setFieldValue: F,
    resetValues: z,
    setValuesSnapshot: m,
    initialize: P,
    getValues: B,
    getValuesSnapshot: E
  };
}
export {
  A as useFormValues
};
//# sourceMappingURL=use-form-values.mjs.map
