import { get as l, set as u, appendErrors as y, useForm as g } from "react-hook-form";
const d = (o, n, e) => {
  if (o && "reportValidity" in o) {
    const r = l(e, n);
    o.setCustomValidity(r && r.message || ""), o.reportValidity();
  }
}, h = (o, n) => {
  for (const e in n.fields) {
    const r = n.fields[e];
    r && r.ref && "reportValidity" in r.ref ? d(r.ref, e, o) : r && r.refs && r.refs.forEach((i) => d(i, e, o));
  }
}, E = (o, n) => {
  n.shouldUseNativeValidation && h(o, n);
  const e = {};
  for (const r in o) {
    const i = l(n.fields, r), t = Object.assign(o[r] || {}, { ref: i && i.ref });
    if (V(n.names || Object.keys(o), r)) {
      const s = Object.assign({}, l(e, r));
      u(s, "root", t), u(e, r, s);
    } else u(e, r, t);
  }
  return e;
}, V = (o, n) => {
  const e = m(n);
  return o.some((r) => m(r).match(`^${e}\\.\\d+`));
};
function m(o) {
  return o.replace(/\]|\[/g, "");
}
function j(o, n) {
  for (var e = {}; o.length; ) {
    var r = o[0], i = r.code, t = r.message, s = r.path.join(".");
    if (!e[s]) if ("unionErrors" in r) {
      var a = r.unionErrors[0].errors[0];
      e[s] = { message: a.message, type: a.code };
    } else e[s] = { message: t, type: i };
    if ("unionErrors" in r && r.unionErrors.forEach(function(v) {
      return v.errors.forEach(function(p) {
        return o.push(p);
      });
    }), n) {
      var c = e[s].types, f = c && c[r.code];
      e[s] = y(s, n, e, i, f ? [].concat(f, r.message) : r.message);
    }
    o.shift();
  }
  return e;
}
function b(o, n, e) {
  return e === void 0 && (e = {}), function(r, i, t) {
    try {
      return Promise.resolve((function(s, a) {
        try {
          var c = Promise.resolve(o[e.mode === "sync" ? "parse" : "parseAsync"](r, n)).then(function(f) {
            return t.shouldUseNativeValidation && h({}, t), { errors: {}, values: e.raw ? Object.assign({}, r) : f };
          });
        } catch (f) {
          return a(f);
        }
        return c && c.then ? c.then(void 0, a) : c;
      })(0, function(s) {
        if ((function(a) {
          return Array.isArray(a == null ? void 0 : a.errors);
        })(s)) return { values: {}, errors: E(j(s.errors, !t.shouldUseNativeValidation && t.criteriaMode === "all"), t) };
        throw s;
      }));
    } catch (s) {
      return Promise.reject(s);
    }
  };
}
function A({ schema: o, defaultValues: n, ...e }) {
  return g({
    ...e,
    defaultValues: n,
    resolver: b(o)
  });
}
export {
  A as default
};
//# sourceMappingURL=useZodForm.js.map
