import { $ZodAsyncError, $ZodEncodeError, $constructor, config, globalConfig } from "./core.js";
import { aborted, allowsEval, cached, cleanRegex, clone, defineLazy, esc, escapeRegex, finalizeIssue, getEnumValues, isObject, isPlainObject, issue, optionalKeys, prefixIssues, propertyKeyTypes } from "./util.js";
import { safeParse, safeParseAsync } from "./parse.js";
import { _null, base64, base64url, boolean, cidrv4, cidrv6, cuid, cuid2, date, datetime, duration, e164, email, emoji, guid, ipv4, ipv6, ksuid, nanoid, number, string, time, ulid, uuid, xid } from "./regexes.js";
import { $ZodCheck, $ZodCheckNumberFormat, $ZodCheckStringFormat } from "./checks.js";
import { Doc } from "./doc.js";
import { version } from "./versions.js";
const $ZodType = /* @__PURE__ */ $constructor("$ZodType", (V, H) => {
	var U;
	V ??= {}, V._zod.def = H, V._zod.bag = V._zod.bag || {}, V._zod.version = version;
	let W = [...V._zod.def.checks ?? []];
	V._zod.traits.has("$ZodCheck") && W.unshift(V);
	for (let f of W) for (let H of f._zod.onattach) H(V);
	if (W.length === 0) (U = V._zod).deferred ?? (U.deferred = []), V._zod.deferred?.push(() => {
		V._zod.run = V._zod.parse;
	});
	else {
		let H = (V, H, U) => {
			let W = aborted(V), K;
			for (let q of H) {
				if (q._zod.def.when) {
					if (!q._zod.def.when(V)) continue;
				} else if (W) continue;
				let H = V.issues.length, J = q._zod.check(V);
				if (J instanceof Promise && U?.async === !1) throw new $ZodAsyncError();
				if (K || J instanceof Promise) K = (K ?? Promise.resolve()).then(async () => {
					await J, V.issues.length !== H && (W ||= aborted(V, H));
				});
				else {
					if (V.issues.length === H) continue;
					W ||= aborted(V, H);
				}
			}
			return K ? K.then(() => V) : V;
		}, U = (U, K, q) => {
			if (aborted(U)) return U.aborted = !0, U;
			let J = H(K, W, q);
			if (J instanceof Promise) {
				if (q.async === !1) throw new $ZodAsyncError();
				return J.then((f) => V._zod.parse(f, q));
			}
			return V._zod.parse(J, q);
		};
		V._zod.run = (G, K) => {
			if (K.skipChecks) return V._zod.parse(G, K);
			if (K.direction === "backward") {
				let f = V._zod.parse({
					value: G.value,
					issues: []
				}, {
					...K,
					skipChecks: !0
				});
				return f instanceof Promise ? f.then((f) => U(f, G, K)) : U(f, G, K);
			}
			let q = V._zod.parse(G, K);
			if (q instanceof Promise) {
				if (K.async === !1) throw new $ZodAsyncError();
				return q.then((f) => H(f, W, K));
			}
			return H(q, W, K);
		};
	}
	defineLazy(V, "~standard", () => ({
		validate: (f) => {
			try {
				let H = safeParse(V, f);
				return H.success ? { value: H.data } : { issues: H.error?.issues };
			} catch {
				return safeParseAsync(V, f).then((f) => f.success ? { value: f.data } : { issues: f.error?.issues });
			}
		},
		vendor: "zod",
		version: 1
	}));
}), $ZodString = /* @__PURE__ */ $constructor("$ZodString", (f, V) => {
	$ZodType.init(f, V), f._zod.pattern = [...f?._zod.bag?.patterns ?? []].pop() ?? string(f._zod.bag), f._zod.parse = (H, U) => {
		if (V.coerce) try {
			H.value = String(H.value);
		} catch {}
		return typeof H.value == "string" || H.issues.push({
			expected: "string",
			code: "invalid_type",
			input: H.value,
			inst: f
		}), H;
	};
}), $ZodStringFormat = /* @__PURE__ */ $constructor("$ZodStringFormat", (f, V) => {
	$ZodCheckStringFormat.init(f, V), $ZodString.init(f, V);
}), $ZodGUID = /* @__PURE__ */ $constructor("$ZodGUID", (f, V) => {
	V.pattern ??= guid, $ZodStringFormat.init(f, V);
}), $ZodUUID = /* @__PURE__ */ $constructor("$ZodUUID", (f, V) => {
	if (V.version) {
		let f = {
			v1: 1,
			v2: 2,
			v3: 3,
			v4: 4,
			v5: 5,
			v6: 6,
			v7: 7,
			v8: 8
		}[V.version];
		if (f === void 0) throw Error(`Invalid UUID version: "${V.version}"`);
		V.pattern ??= uuid(f);
	} else V.pattern ??= uuid();
	$ZodStringFormat.init(f, V);
}), $ZodEmail = /* @__PURE__ */ $constructor("$ZodEmail", (f, V) => {
	V.pattern ??= email, $ZodStringFormat.init(f, V);
}), $ZodURL = /* @__PURE__ */ $constructor("$ZodURL", (f, V) => {
	$ZodStringFormat.init(f, V), f._zod.check = (H) => {
		try {
			let U = H.value.trim(), W = new URL(U);
			V.hostname && (V.hostname.lastIndex = 0, V.hostname.test(W.hostname) || H.issues.push({
				code: "invalid_format",
				format: "url",
				note: "Invalid hostname",
				pattern: V.hostname.source,
				input: H.value,
				inst: f,
				continue: !V.abort
			})), V.protocol && (V.protocol.lastIndex = 0, V.protocol.test(W.protocol.endsWith(":") ? W.protocol.slice(0, -1) : W.protocol) || H.issues.push({
				code: "invalid_format",
				format: "url",
				note: "Invalid protocol",
				pattern: V.protocol.source,
				input: H.value,
				inst: f,
				continue: !V.abort
			})), V.normalize ? H.value = W.href : H.value = U;
			return;
		} catch {
			H.issues.push({
				code: "invalid_format",
				format: "url",
				input: H.value,
				inst: f,
				continue: !V.abort
			});
		}
	};
}), $ZodEmoji = /* @__PURE__ */ $constructor("$ZodEmoji", (f, V) => {
	V.pattern ??= emoji(), $ZodStringFormat.init(f, V);
}), $ZodNanoID = /* @__PURE__ */ $constructor("$ZodNanoID", (f, V) => {
	V.pattern ??= nanoid, $ZodStringFormat.init(f, V);
}), $ZodCUID = /* @__PURE__ */ $constructor("$ZodCUID", (f, V) => {
	V.pattern ??= cuid, $ZodStringFormat.init(f, V);
}), $ZodCUID2 = /* @__PURE__ */ $constructor("$ZodCUID2", (f, V) => {
	V.pattern ??= cuid2, $ZodStringFormat.init(f, V);
}), $ZodULID = /* @__PURE__ */ $constructor("$ZodULID", (f, V) => {
	V.pattern ??= ulid, $ZodStringFormat.init(f, V);
}), $ZodXID = /* @__PURE__ */ $constructor("$ZodXID", (f, V) => {
	V.pattern ??= xid, $ZodStringFormat.init(f, V);
}), $ZodKSUID = /* @__PURE__ */ $constructor("$ZodKSUID", (f, V) => {
	V.pattern ??= ksuid, $ZodStringFormat.init(f, V);
}), $ZodISODateTime = /* @__PURE__ */ $constructor("$ZodISODateTime", (f, V) => {
	V.pattern ??= datetime(V), $ZodStringFormat.init(f, V);
}), $ZodISODate = /* @__PURE__ */ $constructor("$ZodISODate", (f, V) => {
	V.pattern ??= date, $ZodStringFormat.init(f, V);
}), $ZodISOTime = /* @__PURE__ */ $constructor("$ZodISOTime", (f, V) => {
	V.pattern ??= time(V), $ZodStringFormat.init(f, V);
}), $ZodISODuration = /* @__PURE__ */ $constructor("$ZodISODuration", (f, V) => {
	V.pattern ??= duration, $ZodStringFormat.init(f, V);
}), $ZodIPv4 = /* @__PURE__ */ $constructor("$ZodIPv4", (f, V) => {
	V.pattern ??= ipv4, $ZodStringFormat.init(f, V), f._zod.bag.format = "ipv4";
}), $ZodIPv6 = /* @__PURE__ */ $constructor("$ZodIPv6", (f, V) => {
	V.pattern ??= ipv6, $ZodStringFormat.init(f, V), f._zod.bag.format = "ipv6", f._zod.check = (H) => {
		try {
			new URL(`http://[${H.value}]`);
		} catch {
			H.issues.push({
				code: "invalid_format",
				format: "ipv6",
				input: H.value,
				inst: f,
				continue: !V.abort
			});
		}
	};
}), $ZodCIDRv4 = /* @__PURE__ */ $constructor("$ZodCIDRv4", (f, V) => {
	V.pattern ??= cidrv4, $ZodStringFormat.init(f, V);
}), $ZodCIDRv6 = /* @__PURE__ */ $constructor("$ZodCIDRv6", (f, V) => {
	V.pattern ??= cidrv6, $ZodStringFormat.init(f, V), f._zod.check = (H) => {
		let U = H.value.split("/");
		try {
			if (U.length !== 2) throw Error();
			let [f, V] = U;
			if (!V) throw Error();
			let H = Number(V);
			if (`${H}` !== V || H < 0 || H > 128) throw Error();
			new URL(`http://[${f}]`);
		} catch {
			H.issues.push({
				code: "invalid_format",
				format: "cidrv6",
				input: H.value,
				inst: f,
				continue: !V.abort
			});
		}
	};
});
function isValidBase64(f) {
	if (f === "") return !0;
	if (f.length % 4 != 0) return !1;
	try {
		return atob(f), !0;
	} catch {
		return !1;
	}
}
const $ZodBase64 = /* @__PURE__ */ $constructor("$ZodBase64", (f, V) => {
	V.pattern ??= base64, $ZodStringFormat.init(f, V), f._zod.bag.contentEncoding = "base64", f._zod.check = (H) => {
		isValidBase64(H.value) || H.issues.push({
			code: "invalid_format",
			format: "base64",
			input: H.value,
			inst: f,
			continue: !V.abort
		});
	};
});
function isValidBase64URL(f) {
	if (!base64url.test(f)) return !1;
	let V = f.replace(/[-_]/g, (f) => f === "-" ? "+" : "/");
	return isValidBase64(V.padEnd(Math.ceil(V.length / 4) * 4, "="));
}
const $ZodBase64URL = /* @__PURE__ */ $constructor("$ZodBase64URL", (f, V) => {
	V.pattern ??= base64url, $ZodStringFormat.init(f, V), f._zod.bag.contentEncoding = "base64url", f._zod.check = (H) => {
		isValidBase64URL(H.value) || H.issues.push({
			code: "invalid_format",
			format: "base64url",
			input: H.value,
			inst: f,
			continue: !V.abort
		});
	};
}), $ZodE164 = /* @__PURE__ */ $constructor("$ZodE164", (f, V) => {
	V.pattern ??= e164, $ZodStringFormat.init(f, V);
});
function isValidJWT(f, V = null) {
	try {
		let H = f.split(".");
		if (H.length !== 3) return !1;
		let [U] = H;
		if (!U) return !1;
		let W = JSON.parse(atob(U));
		return !("typ" in W && W?.typ !== "JWT" || !W.alg || V && (!("alg" in W) || W.alg !== V));
	} catch {
		return !1;
	}
}
const $ZodJWT = /* @__PURE__ */ $constructor("$ZodJWT", (f, V) => {
	$ZodStringFormat.init(f, V), f._zod.check = (H) => {
		isValidJWT(H.value, V.alg) || H.issues.push({
			code: "invalid_format",
			format: "jwt",
			input: H.value,
			inst: f,
			continue: !V.abort
		});
	};
}), $ZodNumber = /* @__PURE__ */ $constructor("$ZodNumber", (f, V) => {
	$ZodType.init(f, V), f._zod.pattern = f._zod.bag.pattern ?? number, f._zod.parse = (H, U) => {
		if (V.coerce) try {
			H.value = Number(H.value);
		} catch {}
		let W = H.value;
		if (typeof W == "number" && !Number.isNaN(W) && Number.isFinite(W)) return H;
		let G = typeof W == "number" ? Number.isNaN(W) ? "NaN" : Number.isFinite(W) ? void 0 : "Infinity" : void 0;
		return H.issues.push({
			expected: "number",
			code: "invalid_type",
			input: W,
			inst: f,
			...G ? { received: G } : {}
		}), H;
	};
}), $ZodNumberFormat = /* @__PURE__ */ $constructor("$ZodNumberFormat", (f, V) => {
	$ZodCheckNumberFormat.init(f, V), $ZodNumber.init(f, V);
}), $ZodBoolean = /* @__PURE__ */ $constructor("$ZodBoolean", (f, V) => {
	$ZodType.init(f, V), f._zod.pattern = boolean, f._zod.parse = (H, U) => {
		if (V.coerce) try {
			H.value = !!H.value;
		} catch {}
		let W = H.value;
		return typeof W == "boolean" || H.issues.push({
			expected: "boolean",
			code: "invalid_type",
			input: W,
			inst: f
		}), H;
	};
}), $ZodNull = /* @__PURE__ */ $constructor("$ZodNull", (f, V) => {
	$ZodType.init(f, V), f._zod.pattern = _null, f._zod.values = new Set([null]), f._zod.parse = (V, H) => {
		let U = V.value;
		return U === null || V.issues.push({
			expected: "null",
			code: "invalid_type",
			input: U,
			inst: f
		}), V;
	};
}), $ZodUnknown = /* @__PURE__ */ $constructor("$ZodUnknown", (f, V) => {
	$ZodType.init(f, V), f._zod.parse = (f) => f;
}), $ZodNever = /* @__PURE__ */ $constructor("$ZodNever", (f, V) => {
	$ZodType.init(f, V), f._zod.parse = (V, H) => (V.issues.push({
		expected: "never",
		code: "invalid_type",
		input: V.value,
		inst: f
	}), V);
});
function handleArrayResult(f, V, H) {
	f.issues.length && V.issues.push(...prefixIssues(H, f.issues)), V.value[H] = f.value;
}
const $ZodArray = /* @__PURE__ */ $constructor("$ZodArray", (f, V) => {
	$ZodType.init(f, V), f._zod.parse = (H, U) => {
		let W = H.value;
		if (!Array.isArray(W)) return H.issues.push({
			expected: "array",
			code: "invalid_type",
			input: W,
			inst: f
		}), H;
		H.value = Array(W.length);
		let G = [];
		for (let f = 0; f < W.length; f++) {
			let K = W[f], q = V.element._zod.run({
				value: K,
				issues: []
			}, U);
			q instanceof Promise ? G.push(q.then((V) => handleArrayResult(V, H, f))) : handleArrayResult(q, H, f);
		}
		return G.length ? Promise.all(G).then(() => H) : H;
	};
});
function handlePropertyResult(f, V, H, U, W) {
	if (f.issues.length) {
		if (W && !(H in U)) return;
		V.issues.push(...prefixIssues(H, f.issues));
	}
	f.value === void 0 ? H in U && (V.value[H] = void 0) : V.value[H] = f.value;
}
function normalizeDef(f) {
	let V = Object.keys(f.shape);
	for (let H of V) if (!f.shape?.[H]?._zod?.traits?.has("$ZodType")) throw Error(`Invalid element at key "${H}": expected a Zod schema`);
	let H = optionalKeys(f.shape);
	return {
		...f,
		keys: V,
		keySet: new Set(V),
		numKeys: V.length,
		optionalKeys: new Set(H)
	};
}
function handleCatchall(f, V, H, U, W, G) {
	let K = [], q = W.keySet, J = W.catchall._zod, Y = J.def.type, X = J.optout === "optional";
	for (let W in V) {
		if (q.has(W)) continue;
		if (Y === "never") {
			K.push(W);
			continue;
		}
		let G = J.run({
			value: V[W],
			issues: []
		}, U);
		G instanceof Promise ? f.push(G.then((f) => handlePropertyResult(f, H, W, V, X))) : handlePropertyResult(G, H, W, V, X);
	}
	return K.length && H.issues.push({
		code: "unrecognized_keys",
		keys: K,
		input: V,
		inst: G
	}), f.length ? Promise.all(f).then(() => H) : H;
}
const $ZodObject = /* @__PURE__ */ $constructor("$ZodObject", (f, V) => {
	if ($ZodType.init(f, V), !Object.getOwnPropertyDescriptor(V, "shape")?.get) {
		let f = V.shape;
		Object.defineProperty(V, "shape", { get: () => {
			let H = { ...f };
			return Object.defineProperty(V, "shape", { value: H }), H;
		} });
	}
	let H = cached(() => normalizeDef(V));
	defineLazy(f._zod, "propValues", () => {
		let f = V.shape, H = {};
		for (let V in f) {
			let U = f[V]._zod;
			if (U.values) {
				H[V] ?? (H[V] = /* @__PURE__ */ new Set());
				for (let f of U.values) H[V].add(f);
			}
		}
		return H;
	});
	let U = isObject, W = V.catchall, G;
	f._zod.parse = (V, K) => {
		G ??= H.value;
		let q = V.value;
		if (!U(q)) return V.issues.push({
			expected: "object",
			code: "invalid_type",
			input: q,
			inst: f
		}), V;
		V.value = {};
		let J = [], Y = G.shape;
		for (let f of G.keys) {
			let H = Y[f], U = H._zod.optout === "optional", W = H._zod.run({
				value: q[f],
				issues: []
			}, K);
			W instanceof Promise ? J.push(W.then((H) => handlePropertyResult(H, V, f, q, U))) : handlePropertyResult(W, V, f, q, U);
		}
		return W ? handleCatchall(J, q, V, K, H.value, f) : J.length ? Promise.all(J).then(() => V) : V;
	};
}), $ZodObjectJIT = /* @__PURE__ */ $constructor("$ZodObjectJIT", (f, V) => {
	$ZodObject.init(f, V);
	let H = f._zod.parse, U = cached(() => normalizeDef(V)), G = (f) => {
		let V = new Doc([
			"shape",
			"payload",
			"ctx"
		]), H = U.value, W = (f) => {
			let V = esc(f);
			return `shape[${V}]._zod.run({ value: input[${V}], issues: [] }, ctx)`;
		};
		V.write("const input = payload.value;");
		let G = Object.create(null), K = 0;
		for (let f of H.keys) G[f] = `key_${K++}`;
		V.write("const newResult = {};");
		for (let U of H.keys) {
			let H = G[U], K = esc(U), q = f[U]?._zod?.optout === "optional";
			V.write(`const ${H} = ${W(U)};`), q ? V.write(`
        if (${H}.issues.length) {
          if (${K} in input) {
            payload.issues = payload.issues.concat(${H}.issues.map(iss => ({
              ...iss,
              path: iss.path ? [${K}, ...iss.path] : [${K}]
            })));
          }
        }
        
        if (${H}.value === undefined) {
          if (${K} in input) {
            newResult[${K}] = undefined;
          }
        } else {
          newResult[${K}] = ${H}.value;
        }
        
      `) : V.write(`
        if (${H}.issues.length) {
          payload.issues = payload.issues.concat(${H}.issues.map(iss => ({
            ...iss,
            path: iss.path ? [${K}, ...iss.path] : [${K}]
          })));
        }
        
        if (${H}.value === undefined) {
          if (${K} in input) {
            newResult[${K}] = undefined;
          }
        } else {
          newResult[${K}] = ${H}.value;
        }
        
      `);
		}
		V.write("payload.value = newResult;"), V.write("return payload;");
		let q = V.compile();
		return (V, H) => q(f, V, H);
	}, J, Y = isObject, X = !globalConfig.jitless, Z = X && allowsEval.value, Q = V.catchall, $;
	f._zod.parse = (W, K) => {
		$ ??= U.value;
		let q = W.value;
		return Y(q) ? X && Z && K?.async === !1 && K.jitless !== !0 ? (J ||= G(V.shape), W = J(W, K), Q ? handleCatchall([], q, W, K, $, f) : W) : H(W, K) : (W.issues.push({
			expected: "object",
			code: "invalid_type",
			input: q,
			inst: f
		}), W);
	};
});
function handleUnionResults(f, V, H, W) {
	for (let H of f) if (H.issues.length === 0) return V.value = H.value, V;
	let K = f.filter((f) => !aborted(f));
	return K.length === 1 ? (V.value = K[0].value, K[0]) : (V.issues.push({
		code: "invalid_union",
		input: V.value,
		inst: H,
		errors: f.map((f) => f.issues.map((f) => finalizeIssue(f, W, config())))
	}), V);
}
const $ZodUnion = /* @__PURE__ */ $constructor("$ZodUnion", (f, V) => {
	$ZodType.init(f, V), defineLazy(f._zod, "optin", () => V.options.some((f) => f._zod.optin === "optional") ? "optional" : void 0), defineLazy(f._zod, "optout", () => V.options.some((f) => f._zod.optout === "optional") ? "optional" : void 0), defineLazy(f._zod, "values", () => {
		if (V.options.every((f) => f._zod.values)) return new Set(V.options.flatMap((f) => Array.from(f._zod.values)));
	}), defineLazy(f._zod, "pattern", () => {
		if (V.options.every((f) => f._zod.pattern)) {
			let f = V.options.map((f) => f._zod.pattern);
			return /* @__PURE__ */ RegExp(`^(${f.map((f) => cleanRegex(f.source)).join("|")})$`);
		}
	});
	let H = V.options.length === 1, U = V.options[0]._zod.run;
	f._zod.parse = (W, G) => {
		if (H) return U(W, G);
		let K = !1, q = [];
		for (let f of V.options) {
			let V = f._zod.run({
				value: W.value,
				issues: []
			}, G);
			if (V instanceof Promise) q.push(V), K = !0;
			else {
				if (V.issues.length === 0) return V;
				q.push(V);
			}
		}
		return K ? Promise.all(q).then((V) => handleUnionResults(V, W, f, G)) : handleUnionResults(q, W, f, G);
	};
}), $ZodDiscriminatedUnion = /* @__PURE__ */ $constructor("$ZodDiscriminatedUnion", (f, V) => {
	V.inclusive = !1, $ZodUnion.init(f, V);
	let H = f._zod.parse;
	defineLazy(f._zod, "propValues", () => {
		let f = {};
		for (let H of V.options) {
			let U = H._zod.propValues;
			if (!U || Object.keys(U).length === 0) throw Error(`Invalid discriminated union option at index "${V.options.indexOf(H)}"`);
			for (let [V, H] of Object.entries(U)) {
				f[V] || (f[V] = /* @__PURE__ */ new Set());
				for (let U of H) f[V].add(U);
			}
		}
		return f;
	});
	let U = cached(() => {
		let f = V.options, H = /* @__PURE__ */ new Map();
		for (let U of f) {
			let f = U._zod.propValues?.[V.discriminator];
			if (!f || f.size === 0) throw Error(`Invalid discriminated union option at index "${V.options.indexOf(U)}"`);
			for (let V of f) {
				if (H.has(V)) throw Error(`Duplicate discriminator value "${String(V)}"`);
				H.set(V, U);
			}
		}
		return H;
	});
	f._zod.parse = (W, G) => {
		let K = W.value;
		if (!isObject(K)) return W.issues.push({
			code: "invalid_type",
			expected: "object",
			input: K,
			inst: f
		}), W;
		let q = U.value.get(K?.[V.discriminator]);
		return q ? q._zod.run(W, G) : V.unionFallback ? H(W, G) : (W.issues.push({
			code: "invalid_union",
			errors: [],
			note: "No matching discriminator",
			discriminator: V.discriminator,
			input: K,
			path: [V.discriminator],
			inst: f
		}), W);
	};
}), $ZodIntersection = /* @__PURE__ */ $constructor("$ZodIntersection", (f, V) => {
	$ZodType.init(f, V), f._zod.parse = (f, H) => {
		let U = f.value, W = V.left._zod.run({
			value: U,
			issues: []
		}, H), G = V.right._zod.run({
			value: U,
			issues: []
		}, H);
		return W instanceof Promise || G instanceof Promise ? Promise.all([W, G]).then(([V, H]) => handleIntersectionResults(f, V, H)) : handleIntersectionResults(f, W, G);
	};
});
function mergeValues(f, V) {
	if (f === V || f instanceof Date && V instanceof Date && +f == +V) return {
		valid: !0,
		data: f
	};
	if (isPlainObject(f) && isPlainObject(V)) {
		let H = Object.keys(V), U = Object.keys(f).filter((f) => H.indexOf(f) !== -1), W = {
			...f,
			...V
		};
		for (let H of U) {
			let U = mergeValues(f[H], V[H]);
			if (!U.valid) return {
				valid: !1,
				mergeErrorPath: [H, ...U.mergeErrorPath]
			};
			W[H] = U.data;
		}
		return {
			valid: !0,
			data: W
		};
	}
	if (Array.isArray(f) && Array.isArray(V)) {
		if (f.length !== V.length) return {
			valid: !1,
			mergeErrorPath: []
		};
		let H = [];
		for (let U = 0; U < f.length; U++) {
			let W = f[U], G = V[U], K = mergeValues(W, G);
			if (!K.valid) return {
				valid: !1,
				mergeErrorPath: [U, ...K.mergeErrorPath]
			};
			H.push(K.data);
		}
		return {
			valid: !0,
			data: H
		};
	}
	return {
		valid: !1,
		mergeErrorPath: []
	};
}
function handleIntersectionResults(f, V, H) {
	let U = /* @__PURE__ */ new Map(), W;
	for (let H of V.issues) if (H.code === "unrecognized_keys") {
		W ??= H;
		for (let f of H.keys) U.has(f) || U.set(f, {}), U.get(f).l = !0;
	} else f.issues.push(H);
	for (let V of H.issues) if (V.code === "unrecognized_keys") for (let f of V.keys) U.has(f) || U.set(f, {}), U.get(f).r = !0;
	else f.issues.push(V);
	let K = [...U].filter(([, f]) => f.l && f.r).map(([f]) => f);
	if (K.length && W && f.issues.push({
		...W,
		keys: K
	}), aborted(f)) return f;
	let q = mergeValues(V.value, H.value);
	if (!q.valid) throw Error(`Unmergable intersection. Error path: ${JSON.stringify(q.mergeErrorPath)}`);
	return f.value = q.data, f;
}
const $ZodRecord = /* @__PURE__ */ $constructor("$ZodRecord", (f, V) => {
	$ZodType.init(f, V), f._zod.parse = (H, W) => {
		let G = H.value;
		if (!isPlainObject(G)) return H.issues.push({
			expected: "record",
			code: "invalid_type",
			input: G,
			inst: f
		}), H;
		let K = [], q = V.keyType._zod.values;
		if (q) {
			H.value = {};
			let U = /* @__PURE__ */ new Set();
			for (let f of q) if (typeof f == "string" || typeof f == "number" || typeof f == "symbol") {
				U.add(typeof f == "number" ? f.toString() : f);
				let q = V.valueType._zod.run({
					value: G[f],
					issues: []
				}, W);
				q instanceof Promise ? K.push(q.then((V) => {
					V.issues.length && H.issues.push(...prefixIssues(f, V.issues)), H.value[f] = V.value;
				})) : (q.issues.length && H.issues.push(...prefixIssues(f, q.issues)), H.value[f] = q.value);
			}
			let J;
			for (let f in G) U.has(f) || (J ??= [], J.push(f));
			J && J.length > 0 && H.issues.push({
				code: "unrecognized_keys",
				input: G,
				inst: f,
				keys: J
			});
		} else {
			H.value = {};
			for (let q of Reflect.ownKeys(G)) {
				if (q === "__proto__") continue;
				let J = V.keyType._zod.run({
					value: q,
					issues: []
				}, W);
				if (J instanceof Promise) throw Error("Async schemas not supported in object keys currently");
				if (typeof q == "string" && number.test(q) && J.issues.length) {
					let f = V.keyType._zod.run({
						value: Number(q),
						issues: []
					}, W);
					if (f instanceof Promise) throw Error("Async schemas not supported in object keys currently");
					f.issues.length === 0 && (J = f);
				}
				if (J.issues.length) {
					V.mode === "loose" ? H.value[q] = G[q] : H.issues.push({
						code: "invalid_key",
						origin: "record",
						issues: J.issues.map((f) => finalizeIssue(f, W, config())),
						input: q,
						path: [q],
						inst: f
					});
					continue;
				}
				let Y = V.valueType._zod.run({
					value: G[q],
					issues: []
				}, W);
				Y instanceof Promise ? K.push(Y.then((f) => {
					f.issues.length && H.issues.push(...prefixIssues(q, f.issues)), H.value[J.value] = f.value;
				})) : (Y.issues.length && H.issues.push(...prefixIssues(q, Y.issues)), H.value[J.value] = Y.value);
			}
		}
		return K.length ? Promise.all(K).then(() => H) : H;
	};
}), $ZodEnum = /* @__PURE__ */ $constructor("$ZodEnum", (f, V) => {
	$ZodType.init(f, V);
	let H = getEnumValues(V.entries), U = new Set(H);
	f._zod.values = U, f._zod.pattern = /* @__PURE__ */ RegExp(`^(${H.filter((f) => propertyKeyTypes.has(typeof f)).map((f) => typeof f == "string" ? escapeRegex(f) : f.toString()).join("|")})$`), f._zod.parse = (V, W) => {
		let G = V.value;
		return U.has(G) || V.issues.push({
			code: "invalid_value",
			values: H,
			input: G,
			inst: f
		}), V;
	};
}), $ZodLiteral = /* @__PURE__ */ $constructor("$ZodLiteral", (f, V) => {
	if ($ZodType.init(f, V), V.values.length === 0) throw Error("Cannot create literal schema with no valid values");
	let H = new Set(V.values);
	f._zod.values = H, f._zod.pattern = /* @__PURE__ */ RegExp(`^(${V.values.map((f) => typeof f == "string" ? escapeRegex(f) : f ? escapeRegex(f.toString()) : String(f)).join("|")})$`), f._zod.parse = (U, W) => {
		let G = U.value;
		return H.has(G) || U.issues.push({
			code: "invalid_value",
			values: V.values,
			input: G,
			inst: f
		}), U;
	};
}), $ZodTransform = /* @__PURE__ */ $constructor("$ZodTransform", (H, U) => {
	$ZodType.init(H, U), H._zod.parse = (W, G) => {
		if (G.direction === "backward") throw new $ZodEncodeError(H.constructor.name);
		let K = U.transform(W.value, W);
		if (G.async) return (K instanceof Promise ? K : Promise.resolve(K)).then((f) => (W.value = f, W));
		if (K instanceof Promise) throw new $ZodAsyncError();
		return W.value = K, W;
	};
});
function handleOptionalResult(f, V) {
	return f.issues.length && V === void 0 ? {
		issues: [],
		value: void 0
	} : f;
}
const $ZodOptional = /* @__PURE__ */ $constructor("$ZodOptional", (f, V) => {
	$ZodType.init(f, V), f._zod.optin = "optional", f._zod.optout = "optional", defineLazy(f._zod, "values", () => V.innerType._zod.values ? new Set([...V.innerType._zod.values, void 0]) : void 0), defineLazy(f._zod, "pattern", () => {
		let f = V.innerType._zod.pattern;
		return f ? /* @__PURE__ */ RegExp(`^(${cleanRegex(f.source)})?$`) : void 0;
	}), f._zod.parse = (f, H) => {
		if (V.innerType._zod.optin === "optional") {
			let U = V.innerType._zod.run(f, H);
			return U instanceof Promise ? U.then((V) => handleOptionalResult(V, f.value)) : handleOptionalResult(U, f.value);
		}
		return f.value === void 0 ? f : V.innerType._zod.run(f, H);
	};
}), $ZodExactOptional = /* @__PURE__ */ $constructor("$ZodExactOptional", (f, V) => {
	$ZodOptional.init(f, V), defineLazy(f._zod, "values", () => V.innerType._zod.values), defineLazy(f._zod, "pattern", () => V.innerType._zod.pattern), f._zod.parse = (f, H) => V.innerType._zod.run(f, H);
}), $ZodNullable = /* @__PURE__ */ $constructor("$ZodNullable", (f, V) => {
	$ZodType.init(f, V), defineLazy(f._zod, "optin", () => V.innerType._zod.optin), defineLazy(f._zod, "optout", () => V.innerType._zod.optout), defineLazy(f._zod, "pattern", () => {
		let f = V.innerType._zod.pattern;
		return f ? /* @__PURE__ */ RegExp(`^(${cleanRegex(f.source)}|null)$`) : void 0;
	}), defineLazy(f._zod, "values", () => V.innerType._zod.values ? new Set([...V.innerType._zod.values, null]) : void 0), f._zod.parse = (f, H) => f.value === null ? f : V.innerType._zod.run(f, H);
}), $ZodDefault = /* @__PURE__ */ $constructor("$ZodDefault", (f, V) => {
	$ZodType.init(f, V), f._zod.optin = "optional", defineLazy(f._zod, "values", () => V.innerType._zod.values), f._zod.parse = (f, H) => {
		if (H.direction === "backward") return V.innerType._zod.run(f, H);
		if (f.value === void 0) return f.value = V.defaultValue, f;
		let U = V.innerType._zod.run(f, H);
		return U instanceof Promise ? U.then((f) => handleDefaultResult(f, V)) : handleDefaultResult(U, V);
	};
});
function handleDefaultResult(f, V) {
	return f.value === void 0 && (f.value = V.defaultValue), f;
}
const $ZodPrefault = /* @__PURE__ */ $constructor("$ZodPrefault", (f, V) => {
	$ZodType.init(f, V), f._zod.optin = "optional", defineLazy(f._zod, "values", () => V.innerType._zod.values), f._zod.parse = (f, H) => (H.direction === "backward" || f.value === void 0 && (f.value = V.defaultValue), V.innerType._zod.run(f, H));
}), $ZodNonOptional = /* @__PURE__ */ $constructor("$ZodNonOptional", (f, V) => {
	$ZodType.init(f, V), defineLazy(f._zod, "values", () => {
		let f = V.innerType._zod.values;
		return f ? new Set([...f].filter((f) => f !== void 0)) : void 0;
	}), f._zod.parse = (H, U) => {
		let W = V.innerType._zod.run(H, U);
		return W instanceof Promise ? W.then((V) => handleNonOptionalResult(V, f)) : handleNonOptionalResult(W, f);
	};
});
function handleNonOptionalResult(f, V) {
	return !f.issues.length && f.value === void 0 && f.issues.push({
		code: "invalid_type",
		expected: "nonoptional",
		input: f.value,
		inst: V
	}), f;
}
const $ZodCatch = /* @__PURE__ */ $constructor("$ZodCatch", (f, V) => {
	$ZodType.init(f, V), defineLazy(f._zod, "optin", () => V.innerType._zod.optin), defineLazy(f._zod, "optout", () => V.innerType._zod.optout), defineLazy(f._zod, "values", () => V.innerType._zod.values), f._zod.parse = (f, H) => {
		if (H.direction === "backward") return V.innerType._zod.run(f, H);
		let W = V.innerType._zod.run(f, H);
		return W instanceof Promise ? W.then((W) => (f.value = W.value, W.issues.length && (f.value = V.catchValue({
			...f,
			error: { issues: W.issues.map((f) => finalizeIssue(f, H, config())) },
			input: f.value
		}), f.issues = []), f)) : (f.value = W.value, W.issues.length && (f.value = V.catchValue({
			...f,
			error: { issues: W.issues.map((f) => finalizeIssue(f, H, config())) },
			input: f.value
		}), f.issues = []), f);
	};
}), $ZodPipe = /* @__PURE__ */ $constructor("$ZodPipe", (f, V) => {
	$ZodType.init(f, V), defineLazy(f._zod, "values", () => V.in._zod.values), defineLazy(f._zod, "optin", () => V.in._zod.optin), defineLazy(f._zod, "optout", () => V.out._zod.optout), defineLazy(f._zod, "propValues", () => V.in._zod.propValues), f._zod.parse = (f, H) => {
		if (H.direction === "backward") {
			let U = V.out._zod.run(f, H);
			return U instanceof Promise ? U.then((f) => handlePipeResult(f, V.in, H)) : handlePipeResult(U, V.in, H);
		}
		let U = V.in._zod.run(f, H);
		return U instanceof Promise ? U.then((f) => handlePipeResult(f, V.out, H)) : handlePipeResult(U, V.out, H);
	};
});
function handlePipeResult(f, V, H) {
	return f.issues.length ? (f.aborted = !0, f) : V._zod.run({
		value: f.value,
		issues: f.issues
	}, H);
}
const $ZodReadonly = /* @__PURE__ */ $constructor("$ZodReadonly", (f, V) => {
	$ZodType.init(f, V), defineLazy(f._zod, "propValues", () => V.innerType._zod.propValues), defineLazy(f._zod, "values", () => V.innerType._zod.values), defineLazy(f._zod, "optin", () => V.innerType?._zod?.optin), defineLazy(f._zod, "optout", () => V.innerType?._zod?.optout), f._zod.parse = (f, H) => {
		if (H.direction === "backward") return V.innerType._zod.run(f, H);
		let U = V.innerType._zod.run(f, H);
		return U instanceof Promise ? U.then(handleReadonlyResult) : handleReadonlyResult(U);
	};
});
function handleReadonlyResult(f) {
	return f.value = Object.freeze(f.value), f;
}
const $ZodLazy = /* @__PURE__ */ $constructor("$ZodLazy", (f, V) => {
	$ZodType.init(f, V), defineLazy(f._zod, "innerType", () => V.getter()), defineLazy(f._zod, "pattern", () => f._zod.innerType?._zod?.pattern), defineLazy(f._zod, "propValues", () => f._zod.innerType?._zod?.propValues), defineLazy(f._zod, "optin", () => f._zod.innerType?._zod?.optin ?? void 0), defineLazy(f._zod, "optout", () => f._zod.innerType?._zod?.optout ?? void 0), f._zod.parse = (V, H) => f._zod.innerType._zod.run(V, H);
}), $ZodCustom = /* @__PURE__ */ $constructor("$ZodCustom", (f, V) => {
	$ZodCheck.init(f, V), $ZodType.init(f, V), f._zod.parse = (f, V) => f, f._zod.check = (H) => {
		let U = H.value, W = V.fn(U);
		if (W instanceof Promise) return W.then((V) => handleRefineResult(V, H, U, f));
		handleRefineResult(W, H, U, f);
	};
});
function handleRefineResult(f, V, H, U) {
	if (!f) {
		let f = {
			code: "custom",
			input: H,
			inst: U,
			path: [...U._zod.def.path ?? []],
			continue: !U._zod.def.abort
		};
		U._zod.def.params && (f.params = U._zod.def.params), V.issues.push(issue(f));
	}
}
export { $ZodArray, $ZodBase64, $ZodBase64URL, $ZodBoolean, $ZodCIDRv4, $ZodCIDRv6, $ZodCUID, $ZodCUID2, $ZodCatch, $ZodCustom, $ZodDefault, $ZodDiscriminatedUnion, $ZodE164, $ZodEmail, $ZodEmoji, $ZodEnum, $ZodExactOptional, $ZodGUID, $ZodIPv4, $ZodIPv6, $ZodISODate, $ZodISODateTime, $ZodISODuration, $ZodISOTime, $ZodIntersection, $ZodJWT, $ZodKSUID, $ZodLazy, $ZodLiteral, $ZodNanoID, $ZodNever, $ZodNonOptional, $ZodNull, $ZodNullable, $ZodNumber, $ZodNumberFormat, $ZodObjectJIT, $ZodOptional, $ZodPipe, $ZodPrefault, $ZodReadonly, $ZodRecord, $ZodString, $ZodStringFormat, $ZodTransform, $ZodType, $ZodULID, $ZodURL, $ZodUUID, $ZodUnion, $ZodUnknown, $ZodXID };
