import { $ZodAsyncError, config } from "./core.js";
import { captureStackTrace, finalizeIssue } from "./util.js";
import { $ZodError, $ZodRealError } from "./errors.js";
const _parse = (p) => (m, h, g, _) => {
	let v = g ? Object.assign(g, { async: !1 }) : { async: !1 }, y = m._zod.run({
		value: h,
		issues: []
	}, v);
	if (y instanceof Promise) throw new $ZodAsyncError();
	if (y.issues.length) {
		let e = new (_?.Err ?? p)(y.issues.map((e) => finalizeIssue(e, v, config())));
		throw captureStackTrace(e, _?.callee), e;
	}
	return y.value;
}, _parseAsync = (e) => async (p, m, h, g) => {
	let _ = h ? Object.assign(h, { async: !0 }) : { async: !0 }, v = p._zod.run({
		value: m,
		issues: []
	}, _);
	if (v instanceof Promise && (v = await v), v.issues.length) {
		let p = new (g?.Err ?? e)(v.issues.map((e) => finalizeIssue(e, _, config())));
		throw captureStackTrace(p, g?.callee), p;
	}
	return v.value;
}, _safeParse = (d) => (m, h, g) => {
	let _ = g ? {
		...g,
		async: !1
	} : { async: !1 }, v = m._zod.run({
		value: h,
		issues: []
	}, _);
	if (v instanceof Promise) throw new $ZodAsyncError();
	return v.issues.length ? {
		success: !1,
		error: new (d ?? $ZodError)(v.issues.map((e) => finalizeIssue(e, _, config())))
	} : {
		success: !0,
		data: v.value
	};
}, safeParse = /* @__PURE__ */ _safeParse($ZodRealError), _safeParseAsync = (e) => async (d, p, m) => {
	let h = m ? Object.assign(m, { async: !0 }) : { async: !0 }, g = d._zod.run({
		value: p,
		issues: []
	}, h);
	return g instanceof Promise && (g = await g), g.issues.length ? {
		success: !1,
		error: new e(g.issues.map((e) => finalizeIssue(e, h, config())))
	} : {
		success: !0,
		data: g.value
	};
}, safeParseAsync = /* @__PURE__ */ _safeParseAsync($ZodRealError), _encode = (e) => (u, d, f) => {
	let p = f ? Object.assign(f, { direction: "backward" }) : { direction: "backward" };
	return _parse(e)(u, d, p);
}, _decode = (e) => (u, d, f) => _parse(e)(u, d, f), _encodeAsync = (e) => async (u, d, f) => {
	let p = f ? Object.assign(f, { direction: "backward" }) : { direction: "backward" };
	return _parseAsync(e)(u, d, p);
}, _decodeAsync = (e) => async (u, d, f) => _parseAsync(e)(u, d, f), _safeEncode = (e) => (u, d, f) => {
	let p = f ? Object.assign(f, { direction: "backward" }) : { direction: "backward" };
	return _safeParse(e)(u, d, p);
}, _safeDecode = (e) => (u, d, f) => _safeParse(e)(u, d, f), _safeEncodeAsync = (e) => async (u, d, f) => {
	let p = f ? Object.assign(f, { direction: "backward" }) : { direction: "backward" };
	return _safeParseAsync(e)(u, d, p);
}, _safeDecodeAsync = (e) => async (u, d, f) => _safeParseAsync(e)(u, d, f);
export { _decode, _decodeAsync, _encode, _encodeAsync, _parse, _parseAsync, _safeDecode, _safeDecodeAsync, _safeEncode, _safeEncodeAsync, _safeParse, _safeParseAsync, safeParse, safeParseAsync };
