import { $constructor as f } from "./core.js";
import { jsonStringifyReplacer as p } from "./util.js";
const c = (e, n) => {
  e.name = "$ZodError", Object.defineProperty(e, "_zod", {
    value: e._zod,
    enumerable: !1
  }), Object.defineProperty(e, "issues", {
    value: n,
    enumerable: !1
  }), e.message = JSON.stringify(n, p, 2), Object.defineProperty(e, "toString", {
    value: () => e.message,
    enumerable: !1
  });
}, g = f("$ZodError", c), E = f("$ZodError", c, { Parent: Error });
function _(e, n = (o) => o.message) {
  const o = {}, i = [];
  for (const s of e.issues)
    s.path.length > 0 ? (o[s.path[0]] = o[s.path[0]] || [], o[s.path[0]].push(n(s))) : i.push(n(s));
  return { formErrors: i, fieldErrors: o };
}
function b(e, n) {
  const o = n || function(u) {
    return u.message;
  }, i = { _errors: [] }, s = (u) => {
    for (const r of u.issues)
      if (r.code === "invalid_union" && r.errors.length)
        r.errors.map((t) => s({ issues: t }));
      else if (r.code === "invalid_key")
        s({ issues: r.issues });
      else if (r.code === "invalid_element")
        s({ issues: r.issues });
      else if (r.path.length === 0)
        i._errors.push(o(r));
      else {
        let t = i, a = 0;
        for (; a < r.path.length; ) {
          const l = r.path[a];
          a === r.path.length - 1 ? (t[l] = t[l] || { _errors: [] }, t[l]._errors.push(o(r))) : t[l] = t[l] || { _errors: [] }, t = t[l], a++;
        }
      }
  };
  return s(e), i;
}
export {
  g as $ZodError,
  E as $ZodRealError,
  _ as flattenError,
  b as formatError
};
//# sourceMappingURL=errors.js.map
