import { JSONParseError as A, InvalidArgumentError as E, TypeValidationError as g } from "../../../../../@ai-sdk_provider@2.0.0/node_modules/@ai-sdk/provider/dist/index.js";
import { EventSourceParserStream as z } from "../../../../../eventsource-parser@3.0.6/node_modules/eventsource-parser/dist/stream.js";
import { ZodFirstPartyTypeKind as s } from "../../../../../zod@4.1.5/node_modules/zod/v3/types.js";
import { toJSONSchema as L } from "../../../../../zod@4.1.5/node_modules/zod/v4/core/to-json-schema.js";
import { safeParseAsync as F } from "../../../../../zod@4.1.5/node_modules/zod/v4/classic/parse.js";
var I = ({
  prefix: e,
  size: t = 16,
  alphabet: r = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
  separator: a = "-"
} = {}) => {
  const n = () => {
    const i = r.length, o = new Array(t);
    for (let u = 0; u < t; u++)
      o[u] = r[Math.random() * i | 0];
    return o.join("");
  };
  if (e == null)
    return n;
  if (r.includes(a))
    throw new E({
      argument: "separator",
      message: `The separator "${a}" must not be part of the alphabet "${r}".`
    });
  return () => `${e}${a}${n()}`;
}, qe = I(), M = /"__proto__"\s*:/, R = /"constructor"\s*:/;
function C(e) {
  const t = JSON.parse(e);
  return t === null || typeof t != "object" || M.test(e) === !1 && R.test(e) === !1 ? t : V(t);
}
function V(e) {
  let t = [e];
  for (; t.length; ) {
    const r = t;
    t = [];
    for (const a of r) {
      if (Object.prototype.hasOwnProperty.call(a, "__proto__"))
        throw new SyntaxError("Object contains forbidden prototype property");
      if (Object.prototype.hasOwnProperty.call(a, "constructor") && Object.prototype.hasOwnProperty.call(a.constructor, "prototype"))
        throw new SyntaxError("Object contains forbidden prototype property");
      for (const n in a) {
        const i = a[n];
        i && typeof i == "object" && t.push(i);
      }
    }
  }
  return e;
}
function U(e) {
  const { stackTraceLimit: t } = Error;
  Error.stackTraceLimit = 0;
  try {
    return C(e);
  } finally {
    Error.stackTraceLimit = t;
  }
}
var v = Symbol.for("vercel.ai.validator");
function J(e) {
  return { [v]: !0, validate: e };
}
function B(e) {
  return typeof e == "object" && e !== null && v in e && e[v] === !0 && "validate" in e;
}
function q(e) {
  return B(e) ? e : K(e);
}
function K(e) {
  return J(async (t) => {
    const r = await e["~standard"].validate(t);
    return r.issues == null ? { success: !0, value: r.value } : {
      success: !1,
      error: new g({
        value: t,
        cause: r.issues
      })
    };
  });
}
async function Ke({
  value: e,
  schema: t
}) {
  const r = await j({ value: e, schema: t });
  if (!r.success)
    throw g.wrap({ value: e, cause: r.error });
  return r.value;
}
async function j({
  value: e,
  schema: t
}) {
  const r = q(t);
  try {
    if (r.validate == null)
      return { success: !0, value: e, rawValue: e };
    const a = await r.validate(e);
    return a.success ? { success: !0, value: a.value, rawValue: e } : {
      success: !1,
      error: g.wrap({ value: e, cause: a.error }),
      rawValue: e
    };
  } catch (a) {
    return {
      success: !1,
      error: g.wrap({ value: e, cause: a }),
      rawValue: e
    };
  }
}
async function G({
  text: e,
  schema: t
}) {
  try {
    const r = U(e);
    return t == null ? { success: !0, value: r, rawValue: r } : await j({ value: r, schema: t });
  } catch (r) {
    return {
      success: !1,
      error: A.isInstance(r) ? r : new A({ text: e, cause: r }),
      rawValue: void 0
    };
  }
}
function Ge({
  stream: e,
  schema: t
}) {
  return e.pipeThrough(new TextDecoderStream()).pipeThrough(new z()).pipeThrough(
    new TransformStream({
      async transform({ data: r }, a) {
        r !== "[DONE]" && a.enqueue(await G({ text: r, schema: t }));
      }
    })
  );
}
async function He(e) {
  return typeof e == "function" && (e = e()), Promise.resolve(e);
}
var H = (e, t) => {
  let r = 0;
  for (; r < e.length && r < t.length && e[r] === t[r]; r++)
    ;
  return [(e.length - r).toString(), ...t.slice(r)].join("/");
}, W = Symbol(
  "Let zodToJsonSchema decide on which parser to use"
), O = {
  name: void 0,
  $refStrategy: "root",
  basePath: ["#"],
  effectStrategy: "input",
  pipeStrategy: "all",
  dateStrategy: "format:date-time",
  mapStrategy: "entries",
  removeAdditionalStrategy: "passthrough",
  allowedAdditionalProperties: !0,
  rejectedAdditionalProperties: !1,
  definitionPath: "definitions",
  strictUnions: !1,
  definitions: {},
  errorMessages: !1,
  patternStrategy: "escape",
  applyRegexFlags: !1,
  emailStrategy: "format:email",
  base64Strategy: "contentEncoding:base64",
  nameStrategy: "ref"
}, Q = (e) => typeof e == "string" ? {
  ...O,
  name: e
} : {
  ...O,
  ...e
};
function m() {
  return {};
}
function X(e, t) {
  var r, a, n;
  const i = {
    type: "array"
  };
  return (r = e.type) != null && r._def && ((n = (a = e.type) == null ? void 0 : a._def) == null ? void 0 : n.typeName) !== s.ZodAny && (i.items = l(e.type._def, {
    ...t,
    currentPath: [...t.currentPath, "items"]
  })), e.minLength && (i.minItems = e.minLength.value), e.maxLength && (i.maxItems = e.maxLength.value), e.exactLength && (i.minItems = e.exactLength.value, i.maxItems = e.exactLength.value), i;
}
function Y(e) {
  const t = {
    type: "integer",
    format: "int64"
  };
  if (!e.checks)
    return t;
  for (const r of e.checks)
    switch (r.kind) {
      case "min":
        r.inclusive ? t.minimum = r.value : t.exclusiveMinimum = r.value;
        break;
      case "max":
        r.inclusive ? t.maximum = r.value : t.exclusiveMaximum = r.value;
        break;
      case "multipleOf":
        t.multipleOf = r.value;
        break;
    }
  return t;
}
function ee() {
  return { type: "boolean" };
}
function T(e, t) {
  return l(e.type._def, t);
}
var te = (e, t) => l(e.innerType._def, t);
function $(e, t, r) {
  const a = r ?? t.dateStrategy;
  if (Array.isArray(a))
    return {
      anyOf: a.map((n, i) => $(e, t, n))
    };
  switch (a) {
    case "string":
    case "format:date-time":
      return {
        type: "string",
        format: "date-time"
      };
    case "format:date":
      return {
        type: "string",
        format: "date"
      };
    case "integer":
      return re(e);
  }
}
var re = (e) => {
  const t = {
    type: "integer",
    format: "unix-time"
  };
  for (const r of e.checks)
    switch (r.kind) {
      case "min":
        t.minimum = r.value;
        break;
      case "max":
        t.maximum = r.value;
        break;
    }
  return t;
};
function ae(e, t) {
  return {
    ...l(e.innerType._def, t),
    default: e.defaultValue()
  };
}
function ne(e, t) {
  return t.effectStrategy === "input" ? l(e.schema._def, t) : m();
}
function ie(e) {
  return {
    type: "string",
    enum: Array.from(e.values)
  };
}
var oe = (e) => "type" in e && e.type === "string" ? !1 : "allOf" in e;
function se(e, t) {
  const r = [
    l(e.left._def, {
      ...t,
      currentPath: [...t.currentPath, "allOf", "0"]
    }),
    l(e.right._def, {
      ...t,
      currentPath: [...t.currentPath, "allOf", "1"]
    })
  ].filter((n) => !!n), a = [];
  return r.forEach((n) => {
    if (oe(n))
      a.push(...n.allOf);
    else {
      let i = n;
      if ("additionalProperties" in n && n.additionalProperties === !1) {
        const { additionalProperties: o, ...u } = n;
        i = u;
      }
      a.push(i);
    }
  }), a.length ? { allOf: a } : void 0;
}
function ue(e) {
  const t = typeof e.value;
  return t !== "bigint" && t !== "number" && t !== "boolean" && t !== "string" ? {
    type: Array.isArray(e.value) ? "array" : "object"
  } : {
    type: t === "bigint" ? "integer" : t,
    const: e.value
  };
}
var b = void 0, f = {
  /**
   * `c` was changed to `[cC]` to replicate /i flag
   */
  cuid: /^[cC][^\s-]{8,}$/,
  cuid2: /^[0-9a-z]+$/,
  ulid: /^[0-9A-HJKMNP-TV-Z]{26}$/,
  /**
   * `a-z` was added to replicate /i flag
   */
  email: /^(?!\.)(?!.*\.\.)([a-zA-Z0-9_'+\-\.]*)[a-zA-Z0-9_+-]@([a-zA-Z0-9][a-zA-Z0-9\-]*\.)+[a-zA-Z]{2,}$/,
  /**
   * Constructed a valid Unicode RegExp
   *
   * Lazily instantiate since this type of regex isn't supported
   * in all envs (e.g. React Native).
   *
   * See:
   * https://github.com/colinhacks/zod/issues/2433
   * Fix in Zod:
   * https://github.com/colinhacks/zod/commit/9340fd51e48576a75adc919bff65dbc4a5d4c99b
   */
  emoji: () => (b === void 0 && (b = RegExp(
    "^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$",
    "u"
  )), b),
  /**
   * Unused
   */
  uuid: /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/,
  /**
   * Unused
   */
  ipv4: /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/,
  ipv4Cidr: /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/(3[0-2]|[12]?[0-9])$/,
  /**
   * Unused
   */
  ipv6: /^(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))$/,
  ipv6Cidr: /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/,
  base64: /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/,
  base64url: /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/,
  nanoid: /^[a-zA-Z0-9_-]{21}$/,
  jwt: /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/
};
function N(e, t) {
  const r = {
    type: "string"
  };
  if (e.checks)
    for (const a of e.checks)
      switch (a.kind) {
        case "min":
          r.minLength = typeof r.minLength == "number" ? Math.max(r.minLength, a.value) : a.value;
          break;
        case "max":
          r.maxLength = typeof r.maxLength == "number" ? Math.min(r.maxLength, a.value) : a.value;
          break;
        case "email":
          switch (t.emailStrategy) {
            case "format:email":
              y(r, "email", a.message, t);
              break;
            case "format:idn-email":
              y(r, "idn-email", a.message, t);
              break;
            case "pattern:zod":
              d(r, f.email, a.message, t);
              break;
          }
          break;
        case "url":
          y(r, "uri", a.message, t);
          break;
        case "uuid":
          y(r, "uuid", a.message, t);
          break;
        case "regex":
          d(r, a.regex, a.message, t);
          break;
        case "cuid":
          d(r, f.cuid, a.message, t);
          break;
        case "cuid2":
          d(r, f.cuid2, a.message, t);
          break;
        case "startsWith":
          d(
            r,
            RegExp(`^${P(a.value, t)}`),
            a.message,
            t
          );
          break;
        case "endsWith":
          d(
            r,
            RegExp(`${P(a.value, t)}$`),
            a.message,
            t
          );
          break;
        case "datetime":
          y(r, "date-time", a.message, t);
          break;
        case "date":
          y(r, "date", a.message, t);
          break;
        case "time":
          y(r, "time", a.message, t);
          break;
        case "duration":
          y(r, "duration", a.message, t);
          break;
        case "length":
          r.minLength = typeof r.minLength == "number" ? Math.max(r.minLength, a.value) : a.value, r.maxLength = typeof r.maxLength == "number" ? Math.min(r.maxLength, a.value) : a.value;
          break;
        case "includes": {
          d(
            r,
            RegExp(P(a.value, t)),
            a.message,
            t
          );
          break;
        }
        case "ip": {
          a.version !== "v6" && y(r, "ipv4", a.message, t), a.version !== "v4" && y(r, "ipv6", a.message, t);
          break;
        }
        case "base64url":
          d(r, f.base64url, a.message, t);
          break;
        case "jwt":
          d(r, f.jwt, a.message, t);
          break;
        case "cidr": {
          a.version !== "v6" && d(r, f.ipv4Cidr, a.message, t), a.version !== "v4" && d(r, f.ipv6Cidr, a.message, t);
          break;
        }
        case "emoji":
          d(r, f.emoji(), a.message, t);
          break;
        case "ulid": {
          d(r, f.ulid, a.message, t);
          break;
        }
        case "base64": {
          switch (t.base64Strategy) {
            case "format:binary": {
              y(r, "binary", a.message, t);
              break;
            }
            case "contentEncoding:base64": {
              r.contentEncoding = "base64";
              break;
            }
            case "pattern:zod": {
              d(r, f.base64, a.message, t);
              break;
            }
          }
          break;
        }
        case "nanoid":
          d(r, f.nanoid, a.message, t);
      }
  return r;
}
function P(e, t) {
  return t.patternStrategy === "escape" ? le(e) : e;
}
var ce = new Set(
  "ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz0123456789"
);
function le(e) {
  let t = "";
  for (let r = 0; r < e.length; r++)
    ce.has(e[r]) || (t += "\\"), t += e[r];
  return t;
}
function y(e, t, r, a) {
  var n;
  e.format || (n = e.anyOf) != null && n.some((i) => i.format) ? (e.anyOf || (e.anyOf = []), e.format && (e.anyOf.push({
    format: e.format
  }), delete e.format), e.anyOf.push({
    format: t,
    ...r && a.errorMessages && { errorMessage: { format: r } }
  })) : e.format = t;
}
function d(e, t, r, a) {
  var n;
  e.pattern || (n = e.allOf) != null && n.some((i) => i.pattern) ? (e.allOf || (e.allOf = []), e.pattern && (e.allOf.push({
    pattern: e.pattern
  }), delete e.pattern), e.allOf.push({
    pattern: w(t, a),
    ...r && a.errorMessages && { errorMessage: { pattern: r } }
  })) : e.pattern = w(t, a);
}
function w(e, t) {
  var r;
  if (!t.applyRegexFlags || !e.flags)
    return e.source;
  const a = {
    i: e.flags.includes("i"),
    // Case-insensitive
    m: e.flags.includes("m"),
    // `^` and `$` matches adjacent to newline characters
    s: e.flags.includes("s")
    // `.` matches newlines
  }, n = a.i ? e.source.toLowerCase() : e.source;
  let i = "", o = !1, u = !1, p = !1;
  for (let c = 0; c < n.length; c++) {
    if (o) {
      i += n[c], o = !1;
      continue;
    }
    if (a.i) {
      if (u) {
        if (n[c].match(/[a-z]/)) {
          p ? (i += n[c], i += `${n[c - 2]}-${n[c]}`.toUpperCase(), p = !1) : n[c + 1] === "-" && ((r = n[c + 2]) != null && r.match(/[a-z]/)) ? (i += n[c], p = !0) : i += `${n[c]}${n[c].toUpperCase()}`;
          continue;
        }
      } else if (n[c].match(/[a-z]/)) {
        i += `[${n[c]}${n[c].toUpperCase()}]`;
        continue;
      }
    }
    if (a.m) {
      if (n[c] === "^") {
        i += `(^|(?<=[\r
]))`;
        continue;
      } else if (n[c] === "$") {
        i += `($|(?=[\r
]))`;
        continue;
      }
    }
    if (a.s && n[c] === ".") {
      i += u ? `${n[c]}\r
` : `[${n[c]}\r
]`;
      continue;
    }
    i += n[c], n[c] === "\\" ? o = !0 : u && n[c] === "]" ? u = !1 : !u && n[c] === "[" && (u = !0);
  }
  try {
    new RegExp(i);
  } catch {
    return console.warn(
      `Could not convert regex pattern at ${t.currentPath.join(
        "/"
      )} to a flag-independent form! Falling back to the flag-ignorant source`
    ), e.source;
  }
  return i;
}
function D(e, t) {
  var r, a, n, i, o, u;
  const p = {
    type: "object",
    additionalProperties: (r = l(e.valueType._def, {
      ...t,
      currentPath: [...t.currentPath, "additionalProperties"]
    })) != null ? r : t.allowedAdditionalProperties
  };
  if (((a = e.keyType) == null ? void 0 : a._def.typeName) === s.ZodString && ((n = e.keyType._def.checks) != null && n.length)) {
    const { type: c, ...h } = N(e.keyType._def, t);
    return {
      ...p,
      propertyNames: h
    };
  } else {
    if (((i = e.keyType) == null ? void 0 : i._def.typeName) === s.ZodEnum)
      return {
        ...p,
        propertyNames: {
          enum: e.keyType._def.values
        }
      };
    if (((o = e.keyType) == null ? void 0 : o._def.typeName) === s.ZodBranded && e.keyType._def.type._def.typeName === s.ZodString && ((u = e.keyType._def.type._def.checks) != null && u.length)) {
      const { type: c, ...h } = T(
        e.keyType._def,
        t
      );
      return {
        ...p,
        propertyNames: h
      };
    }
  }
  return p;
}
function pe(e, t) {
  if (t.mapStrategy === "record")
    return D(e, t);
  const r = l(e.keyType._def, {
    ...t,
    currentPath: [...t.currentPath, "items", "items", "0"]
  }) || m(), a = l(e.valueType._def, {
    ...t,
    currentPath: [...t.currentPath, "items", "items", "1"]
  }) || m();
  return {
    type: "array",
    maxItems: 125,
    items: {
      type: "array",
      items: [r, a],
      minItems: 2,
      maxItems: 2
    }
  };
}
function de(e) {
  const t = e.values, a = Object.keys(e.values).filter((i) => typeof t[t[i]] != "number").map((i) => t[i]), n = Array.from(
    new Set(a.map((i) => typeof i))
  );
  return {
    type: n.length === 1 ? n[0] === "string" ? "string" : "number" : ["string", "number"],
    enum: a
  };
}
function me() {
  return { not: m() };
}
function fe() {
  return {
    type: "null"
  };
}
var _ = {
  ZodString: "string",
  ZodNumber: "number",
  ZodBigInt: "integer",
  ZodBoolean: "boolean",
  ZodNull: "null"
};
function ye(e, t) {
  const r = e.options instanceof Map ? Array.from(e.options.values()) : e.options;
  if (r.every(
    (a) => a._def.typeName in _ && (!a._def.checks || !a._def.checks.length)
  )) {
    const a = r.reduce((n, i) => {
      const o = _[i._def.typeName];
      return o && !n.includes(o) ? [...n, o] : n;
    }, []);
    return {
      type: a.length > 1 ? a : a[0]
    };
  } else if (r.every((a) => a._def.typeName === "ZodLiteral" && !a.description)) {
    const a = r.reduce(
      (n, i) => {
        const o = typeof i._def.value;
        switch (o) {
          case "string":
          case "number":
          case "boolean":
            return [...n, o];
          case "bigint":
            return [...n, "integer"];
          case "object":
            if (i._def.value === null)
              return [...n, "null"];
          case "symbol":
          case "undefined":
          case "function":
          default:
            return n;
        }
      },
      []
    );
    if (a.length === r.length) {
      const n = a.filter((i, o, u) => u.indexOf(i) === o);
      return {
        type: n.length > 1 ? n : n[0],
        enum: r.reduce(
          (i, o) => i.includes(o._def.value) ? i : [...i, o._def.value],
          []
        )
      };
    }
  } else if (r.every((a) => a._def.typeName === "ZodEnum"))
    return {
      type: "string",
      enum: r.reduce(
        (a, n) => [
          ...a,
          ...n._def.values.filter((i) => !a.includes(i))
        ],
        []
      )
    };
  return he(e, t);
}
var he = (e, t) => {
  const r = (e.options instanceof Map ? Array.from(e.options.values()) : e.options).map(
    (a, n) => l(a._def, {
      ...t,
      currentPath: [...t.currentPath, "anyOf", `${n}`]
    })
  ).filter(
    (a) => !!a && (!t.strictUnions || typeof a == "object" && Object.keys(a).length > 0)
  );
  return r.length ? { anyOf: r } : void 0;
};
function ge(e, t) {
  if (["ZodString", "ZodNumber", "ZodBigInt", "ZodBoolean", "ZodNull"].includes(
    e.innerType._def.typeName
  ) && (!e.innerType._def.checks || !e.innerType._def.checks.length))
    return {
      type: [
        _[e.innerType._def.typeName],
        "null"
      ]
    };
  const r = l(e.innerType._def, {
    ...t,
    currentPath: [...t.currentPath, "anyOf", "0"]
  });
  return r && { anyOf: [r, { type: "null" }] };
}
function ve(e) {
  const t = {
    type: "number"
  };
  if (!e.checks)
    return t;
  for (const r of e.checks)
    switch (r.kind) {
      case "int":
        t.type = "integer";
        break;
      case "min":
        r.inclusive ? t.minimum = r.value : t.exclusiveMinimum = r.value;
        break;
      case "max":
        r.inclusive ? t.maximum = r.value : t.exclusiveMaximum = r.value;
        break;
      case "multipleOf":
        t.multipleOf = r.value;
        break;
    }
  return t;
}
function be(e, t) {
  const r = {
    type: "object",
    properties: {}
  }, a = [], n = e.shape();
  for (const o in n) {
    let u = n[o];
    if (u === void 0 || u._def === void 0)
      continue;
    const p = _e(u), c = l(u._def, {
      ...t,
      currentPath: [...t.currentPath, "properties", o],
      propertyPath: [...t.currentPath, "properties", o]
    });
    c !== void 0 && (r.properties[o] = c, p || a.push(o));
  }
  a.length && (r.required = a);
  const i = Pe(e, t);
  return i !== void 0 && (r.additionalProperties = i), r;
}
function Pe(e, t) {
  if (e.catchall._def.typeName !== "ZodNever")
    return l(e.catchall._def, {
      ...t,
      currentPath: [...t.currentPath, "additionalProperties"]
    });
  switch (e.unknownKeys) {
    case "passthrough":
      return t.allowedAdditionalProperties;
    case "strict":
      return t.rejectedAdditionalProperties;
    case "strip":
      return t.removeAdditionalStrategy === "strict" ? t.allowedAdditionalProperties : t.rejectedAdditionalProperties;
  }
}
function _e(e) {
  try {
    return e.isOptional();
  } catch {
    return !0;
  }
}
var ke = (e, t) => {
  var r;
  if (t.currentPath.toString() === ((r = t.propertyPath) == null ? void 0 : r.toString()))
    return l(e.innerType._def, t);
  const a = l(e.innerType._def, {
    ...t,
    currentPath: [...t.currentPath, "anyOf", "1"]
  });
  return a ? { anyOf: [{ not: m() }, a] } : m();
}, Se = (e, t) => {
  if (t.pipeStrategy === "input")
    return l(e.in._def, t);
  if (t.pipeStrategy === "output")
    return l(e.out._def, t);
  const r = l(e.in._def, {
    ...t,
    currentPath: [...t.currentPath, "allOf", "0"]
  }), a = l(e.out._def, {
    ...t,
    currentPath: [...t.currentPath, "allOf", r ? "1" : "0"]
  });
  return {
    allOf: [r, a].filter((n) => n !== void 0)
  };
};
function Ze(e, t) {
  return l(e.type._def, t);
}
function Ae(e, t) {
  const a = {
    type: "array",
    uniqueItems: !0,
    items: l(e.valueType._def, {
      ...t,
      currentPath: [...t.currentPath, "items"]
    })
  };
  return e.minSize && (a.minItems = e.minSize.value), e.maxSize && (a.maxItems = e.maxSize.value), a;
}
function Oe(e, t) {
  return e.rest ? {
    type: "array",
    minItems: e.items.length,
    items: e.items.map(
      (r, a) => l(r._def, {
        ...t,
        currentPath: [...t.currentPath, "items", `${a}`]
      })
    ).reduce(
      (r, a) => a === void 0 ? r : [...r, a],
      []
    ),
    additionalItems: l(e.rest._def, {
      ...t,
      currentPath: [...t.currentPath, "additionalItems"]
    })
  } : {
    type: "array",
    minItems: e.items.length,
    maxItems: e.items.length,
    items: e.items.map(
      (r, a) => l(r._def, {
        ...t,
        currentPath: [...t.currentPath, "items", `${a}`]
      })
    ).reduce(
      (r, a) => a === void 0 ? r : [...r, a],
      []
    )
  };
}
function we() {
  return {
    not: m()
  };
}
function je() {
  return m();
}
var Te = (e, t) => l(e.innerType._def, t), $e = (e, t, r) => {
  switch (t) {
    case s.ZodString:
      return N(e, r);
    case s.ZodNumber:
      return ve(e);
    case s.ZodObject:
      return be(e, r);
    case s.ZodBigInt:
      return Y(e);
    case s.ZodBoolean:
      return ee();
    case s.ZodDate:
      return $(e, r);
    case s.ZodUndefined:
      return we();
    case s.ZodNull:
      return fe();
    case s.ZodArray:
      return X(e, r);
    case s.ZodUnion:
    case s.ZodDiscriminatedUnion:
      return ye(e, r);
    case s.ZodIntersection:
      return se(e, r);
    case s.ZodTuple:
      return Oe(e, r);
    case s.ZodRecord:
      return D(e, r);
    case s.ZodLiteral:
      return ue(e);
    case s.ZodEnum:
      return ie(e);
    case s.ZodNativeEnum:
      return de(e);
    case s.ZodNullable:
      return ge(e, r);
    case s.ZodOptional:
      return ke(e, r);
    case s.ZodMap:
      return pe(e, r);
    case s.ZodSet:
      return Ae(e, r);
    case s.ZodLazy:
      return () => e.getter()._def;
    case s.ZodPromise:
      return Ze(e, r);
    case s.ZodNaN:
    case s.ZodNever:
      return me();
    case s.ZodEffects:
      return ne(e, r);
    case s.ZodAny:
      return m();
    case s.ZodUnknown:
      return je();
    case s.ZodDefault:
      return ae(e, r);
    case s.ZodBranded:
      return T(e, r);
    case s.ZodReadonly:
      return Te(e, r);
    case s.ZodCatch:
      return te(e, r);
    case s.ZodPipeline:
      return Se(e, r);
    case s.ZodFunction:
    case s.ZodVoid:
    case s.ZodSymbol:
      return;
    default:
      return /* @__PURE__ */ ((a) => {
      })();
  }
};
function l(e, t, r = !1) {
  var a;
  const n = t.seen.get(e);
  if (t.override) {
    const p = (a = t.override) == null ? void 0 : a.call(
      t,
      e,
      t,
      n,
      r
    );
    if (p !== W)
      return p;
  }
  if (n && !r) {
    const p = Ne(n, t);
    if (p !== void 0)
      return p;
  }
  const i = { def: e, path: t.currentPath, jsonSchema: void 0 };
  t.seen.set(e, i);
  const o = $e(e, e.typeName, t), u = typeof o == "function" ? l(o(), t) : o;
  if (u && De(e, t, u), t.postProcess) {
    const p = t.postProcess(u, e, t);
    return i.jsonSchema = u, p;
  }
  return i.jsonSchema = u, u;
}
var Ne = (e, t) => {
  switch (t.$refStrategy) {
    case "root":
      return { $ref: e.path.join("/") };
    case "relative":
      return { $ref: H(t.currentPath, e.path) };
    case "none":
    case "seen":
      return e.path.length < t.currentPath.length && e.path.every((r, a) => t.currentPath[a] === r) ? (console.warn(
        `Recursive reference detected at ${t.currentPath.join(
          "/"
        )}! Defaulting to any`
      ), m()) : t.$refStrategy === "seen" ? m() : void 0;
  }
}, De = (e, t, r) => (e.description && (r.description = e.description), r), xe = (e) => {
  const t = Q(e), r = t.name !== void 0 ? [...t.basePath, t.definitionPath, t.name] : t.basePath;
  return {
    ...t,
    currentPath: r,
    propertyPath: void 0,
    seen: new Map(
      Object.entries(t.definitions).map(([a, n]) => [
        n._def,
        {
          def: n._def,
          path: [...t.basePath, t.definitionPath, a],
          // Resolution of references will be forced even though seen, so it's ok that the schema is undefined here for now.
          jsonSchema: void 0
        }
      ])
    )
  };
}, Ee = (e, t) => {
  var r;
  const a = xe(t);
  let n = typeof t == "object" && t.definitions ? Object.entries(t.definitions).reduce(
    (c, [h, x]) => {
      var Z;
      return {
        ...c,
        [h]: (Z = l(
          x._def,
          {
            ...a,
            currentPath: [...a.basePath, a.definitionPath, h]
          },
          !0
        )) != null ? Z : m()
      };
    },
    {}
  ) : void 0;
  const i = typeof t == "string" ? t : t?.nameStrategy === "title" ? void 0 : t?.name, o = (r = l(
    e._def,
    i === void 0 ? a : {
      ...a,
      currentPath: [...a.basePath, a.definitionPath, i]
    },
    !1
  )) != null ? r : m(), u = typeof t == "object" && t.name !== void 0 && t.nameStrategy === "title" ? t.name : void 0;
  u !== void 0 && (o.title = u);
  const p = i === void 0 ? n ? {
    ...o,
    [a.definitionPath]: n
  } : o : {
    $ref: [
      ...a.$refStrategy === "relative" ? [] : a.basePath,
      a.definitionPath,
      i
    ].join("/"),
    [a.definitionPath]: {
      ...n,
      [i]: o
    }
  };
  return p.$schema = "http://json-schema.org/draft-07/schema#", p;
}, ze = Ee;
function Le(e, t) {
  var r;
  const a = (r = void 0) != null ? r : !1;
  return S(
    ze(e, {
      $refStrategy: a ? "root" : "none"
    }),
    {
      validate: async (n) => {
        const i = await e.safeParseAsync(n);
        return i.success ? { success: !0, value: i.data } : { success: !1, error: i.error };
      }
    }
  );
}
function Fe(e, t) {
  var r;
  const a = (r = void 0) != null ? r : !1, n = L(e, {
    target: "draft-7",
    io: "output",
    reused: a ? "ref" : "inline"
  });
  return S(n, {
    validate: async (i) => {
      const o = await F(e, i);
      return o.success ? { success: !0, value: o.data } : { success: !1, error: o.error };
    }
  });
}
function Ie(e) {
  return "_zod" in e;
}
function Me(e, t) {
  return Ie(e) ? Fe(e) : Le(e);
}
var k = Symbol.for("vercel.ai.schema");
function S(e, {
  validate: t
} = {}) {
  return {
    [k]: !0,
    _type: void 0,
    // should never be used directly
    [v]: !0,
    jsonSchema: e,
    validate: t
  };
}
function Re(e) {
  return typeof e == "object" && e !== null && k in e && e[k] === !0 && "jsonSchema" in e && "validate" in e;
}
function We(e) {
  return e == null ? S({
    properties: {},
    additionalProperties: !1
  }) : Re(e) ? e : Me(e);
}
export {
  z as EventSourceParserStream,
  We as asSchema,
  q as asValidator,
  I as createIdGenerator,
  qe as generateId,
  B as isValidator,
  S as jsonSchema,
  Ge as parseJsonEventStream,
  He as resolve,
  G as safeParseJSON,
  j as safeValidateTypes,
  K as standardSchemaValidator,
  Ke as validateTypes,
  J as validator,
  v as validatorSymbol,
  Me as zodSchema
};
//# sourceMappingURL=index.js.map
