import { $constructor } from "./core.js";
import { NUMBER_FORMAT_RANGES, escapeRegex, floatSafeRemainder, getLengthableOrigin, nullish } from "./util.js";
import { integer, lowercase, uppercase } from "./regexes.js";
const $ZodCheck = /* @__PURE__ */ $constructor("$ZodCheck", (t, y) => {
	var b;
	t._zod ??= {}, t._zod.def = y, (b = t._zod).onattach ?? (b.onattach = []);
});
var numericOriginMap = {
	number: "number",
	bigint: "bigint",
	object: "date"
};
const $ZodCheckLessThan = /* @__PURE__ */ $constructor("$ZodCheckLessThan", (t, y) => {
	$ZodCheck.init(t, y);
	let b = numericOriginMap[typeof y.value];
	t._zod.onattach.push((t) => {
		let b = t._zod.bag, x = (y.inclusive ? b.maximum : b.exclusiveMaximum) ?? Infinity;
		y.value < x && (y.inclusive ? b.maximum = y.value : b.exclusiveMaximum = y.value);
	}), t._zod.check = (x) => {
		(y.inclusive ? x.value <= y.value : x.value < y.value) || x.issues.push({
			origin: b,
			code: "too_big",
			maximum: typeof y.value == "object" ? y.value.getTime() : y.value,
			input: x.value,
			inclusive: y.inclusive,
			inst: t,
			continue: !y.abort
		});
	};
}), $ZodCheckGreaterThan = /* @__PURE__ */ $constructor("$ZodCheckGreaterThan", (t, y) => {
	$ZodCheck.init(t, y);
	let b = numericOriginMap[typeof y.value];
	t._zod.onattach.push((t) => {
		let b = t._zod.bag, x = (y.inclusive ? b.minimum : b.exclusiveMinimum) ?? -Infinity;
		y.value > x && (y.inclusive ? b.minimum = y.value : b.exclusiveMinimum = y.value);
	}), t._zod.check = (x) => {
		(y.inclusive ? x.value >= y.value : x.value > y.value) || x.issues.push({
			origin: b,
			code: "too_small",
			minimum: typeof y.value == "object" ? y.value.getTime() : y.value,
			input: x.value,
			inclusive: y.inclusive,
			inst: t,
			continue: !y.abort
		});
	};
}), $ZodCheckMultipleOf = /* @__PURE__ */ $constructor("$ZodCheckMultipleOf", (t, y) => {
	$ZodCheck.init(t, y), t._zod.onattach.push((t) => {
		var b;
		(b = t._zod.bag).multipleOf ?? (b.multipleOf = y.value);
	}), t._zod.check = (b) => {
		if (typeof b.value != typeof y.value) throw Error("Cannot mix number and bigint in multiple_of check.");
		(typeof b.value == "bigint" ? b.value % y.value === BigInt(0) : floatSafeRemainder(b.value, y.value) === 0) || b.issues.push({
			origin: typeof b.value,
			code: "not_multiple_of",
			divisor: y.value,
			input: b.value,
			inst: t,
			continue: !y.abort
		});
	};
}), $ZodCheckNumberFormat = /* @__PURE__ */ $constructor("$ZodCheckNumberFormat", (t, b) => {
	$ZodCheck.init(t, b), b.format = b.format || "float64";
	let x = b.format?.includes("int"), S = x ? "int" : "number", [C, T] = NUMBER_FORMAT_RANGES[b.format];
	t._zod.onattach.push((t) => {
		let y = t._zod.bag;
		y.format = b.format, y.minimum = C, y.maximum = T, x && (y.pattern = integer);
	}), t._zod.check = (y) => {
		let w = y.value;
		if (x) {
			if (!Number.isInteger(w)) {
				y.issues.push({
					expected: S,
					format: b.format,
					code: "invalid_type",
					continue: !1,
					input: w,
					inst: t
				});
				return;
			}
			if (!Number.isSafeInteger(w)) {
				w > 0 ? y.issues.push({
					input: w,
					code: "too_big",
					maximum: 2 ** 53 - 1,
					note: "Integers must be within the safe integer range.",
					inst: t,
					origin: S,
					inclusive: !0,
					continue: !b.abort
				}) : y.issues.push({
					input: w,
					code: "too_small",
					minimum: -(2 ** 53 - 1),
					note: "Integers must be within the safe integer range.",
					inst: t,
					origin: S,
					inclusive: !0,
					continue: !b.abort
				});
				return;
			}
		}
		w < C && y.issues.push({
			origin: "number",
			input: w,
			code: "too_small",
			minimum: C,
			inclusive: !0,
			inst: t,
			continue: !b.abort
		}), w > T && y.issues.push({
			origin: "number",
			input: w,
			code: "too_big",
			maximum: T,
			inclusive: !0,
			inst: t,
			continue: !b.abort
		});
	};
}), $ZodCheckMaxLength = /* @__PURE__ */ $constructor("$ZodCheckMaxLength", (t, y) => {
	var b;
	$ZodCheck.init(t, y), (b = t._zod.def).when ?? (b.when = (t) => {
		let y = t.value;
		return !nullish(y) && y.length !== void 0;
	}), t._zod.onattach.push((t) => {
		let b = t._zod.bag.maximum ?? Infinity;
		y.maximum < b && (t._zod.bag.maximum = y.maximum);
	}), t._zod.check = (b) => {
		let x = b.value;
		if (x.length <= y.maximum) return;
		let C = getLengthableOrigin(x);
		b.issues.push({
			origin: C,
			code: "too_big",
			maximum: y.maximum,
			inclusive: !0,
			input: x,
			inst: t,
			continue: !y.abort
		});
	};
}), $ZodCheckMinLength = /* @__PURE__ */ $constructor("$ZodCheckMinLength", (t, y) => {
	var b;
	$ZodCheck.init(t, y), (b = t._zod.def).when ?? (b.when = (t) => {
		let y = t.value;
		return !nullish(y) && y.length !== void 0;
	}), t._zod.onattach.push((t) => {
		let b = t._zod.bag.minimum ?? -Infinity;
		y.minimum > b && (t._zod.bag.minimum = y.minimum);
	}), t._zod.check = (b) => {
		let x = b.value;
		if (x.length >= y.minimum) return;
		let C = getLengthableOrigin(x);
		b.issues.push({
			origin: C,
			code: "too_small",
			minimum: y.minimum,
			inclusive: !0,
			input: x,
			inst: t,
			continue: !y.abort
		});
	};
}), $ZodCheckLengthEquals = /* @__PURE__ */ $constructor("$ZodCheckLengthEquals", (t, y) => {
	var b;
	$ZodCheck.init(t, y), (b = t._zod.def).when ?? (b.when = (t) => {
		let y = t.value;
		return !nullish(y) && y.length !== void 0;
	}), t._zod.onattach.push((t) => {
		let b = t._zod.bag;
		b.minimum = y.length, b.maximum = y.length, b.length = y.length;
	}), t._zod.check = (b) => {
		let x = b.value, C = x.length;
		if (C === y.length) return;
		let w = getLengthableOrigin(x), T = C > y.length;
		b.issues.push({
			origin: w,
			...T ? {
				code: "too_big",
				maximum: y.length
			} : {
				code: "too_small",
				minimum: y.length
			},
			inclusive: !0,
			exact: !0,
			input: b.value,
			inst: t,
			continue: !y.abort
		});
	};
}), $ZodCheckStringFormat = /* @__PURE__ */ $constructor("$ZodCheckStringFormat", (t, y) => {
	var b, x;
	$ZodCheck.init(t, y), t._zod.onattach.push((t) => {
		let b = t._zod.bag;
		b.format = y.format, y.pattern && (b.patterns ??= /* @__PURE__ */ new Set(), b.patterns.add(y.pattern));
	}), y.pattern ? (b = t._zod).check ?? (b.check = (b) => {
		y.pattern.lastIndex = 0, !y.pattern.test(b.value) && b.issues.push({
			origin: "string",
			code: "invalid_format",
			format: y.format,
			input: b.value,
			...y.pattern ? { pattern: y.pattern.toString() } : {},
			inst: t,
			continue: !y.abort
		});
	}) : (x = t._zod).check ?? (x.check = () => {});
}), $ZodCheckRegex = /* @__PURE__ */ $constructor("$ZodCheckRegex", (t, y) => {
	$ZodCheckStringFormat.init(t, y), t._zod.check = (b) => {
		y.pattern.lastIndex = 0, !y.pattern.test(b.value) && b.issues.push({
			origin: "string",
			code: "invalid_format",
			format: "regex",
			input: b.value,
			pattern: y.pattern.toString(),
			inst: t,
			continue: !y.abort
		});
	};
}), $ZodCheckLowerCase = /* @__PURE__ */ $constructor("$ZodCheckLowerCase", (t, y) => {
	y.pattern ??= lowercase, $ZodCheckStringFormat.init(t, y);
}), $ZodCheckUpperCase = /* @__PURE__ */ $constructor("$ZodCheckUpperCase", (t, y) => {
	y.pattern ??= uppercase, $ZodCheckStringFormat.init(t, y);
}), $ZodCheckIncludes = /* @__PURE__ */ $constructor("$ZodCheckIncludes", (t, y) => {
	$ZodCheck.init(t, y);
	let x = escapeRegex(y.includes), S = new RegExp(typeof y.position == "number" ? `^.{${y.position}}${x}` : x);
	y.pattern = S, t._zod.onattach.push((t) => {
		let y = t._zod.bag;
		y.patterns ??= /* @__PURE__ */ new Set(), y.patterns.add(S);
	}), t._zod.check = (b) => {
		b.value.includes(y.includes, y.position) || b.issues.push({
			origin: "string",
			code: "invalid_format",
			format: "includes",
			includes: y.includes,
			input: b.value,
			inst: t,
			continue: !y.abort
		});
	};
}), $ZodCheckStartsWith = /* @__PURE__ */ $constructor("$ZodCheckStartsWith", (t, y) => {
	$ZodCheck.init(t, y);
	let x = /* @__PURE__ */ RegExp(`^${escapeRegex(y.prefix)}.*`);
	y.pattern ??= x, t._zod.onattach.push((t) => {
		let y = t._zod.bag;
		y.patterns ??= /* @__PURE__ */ new Set(), y.patterns.add(x);
	}), t._zod.check = (b) => {
		b.value.startsWith(y.prefix) || b.issues.push({
			origin: "string",
			code: "invalid_format",
			format: "starts_with",
			prefix: y.prefix,
			input: b.value,
			inst: t,
			continue: !y.abort
		});
	};
}), $ZodCheckEndsWith = /* @__PURE__ */ $constructor("$ZodCheckEndsWith", (t, y) => {
	$ZodCheck.init(t, y);
	let x = /* @__PURE__ */ RegExp(`.*${escapeRegex(y.suffix)}$`);
	y.pattern ??= x, t._zod.onattach.push((t) => {
		let y = t._zod.bag;
		y.patterns ??= /* @__PURE__ */ new Set(), y.patterns.add(x);
	}), t._zod.check = (b) => {
		b.value.endsWith(y.suffix) || b.issues.push({
			origin: "string",
			code: "invalid_format",
			format: "ends_with",
			suffix: y.suffix,
			input: b.value,
			inst: t,
			continue: !y.abort
		});
	};
}), $ZodCheckOverwrite = /* @__PURE__ */ $constructor("$ZodCheckOverwrite", (t, y) => {
	$ZodCheck.init(t, y), t._zod.check = (t) => {
		t.value = y.tx(t.value);
	};
});
export { $ZodCheck, $ZodCheckEndsWith, $ZodCheckGreaterThan, $ZodCheckIncludes, $ZodCheckLengthEquals, $ZodCheckLessThan, $ZodCheckLowerCase, $ZodCheckMaxLength, $ZodCheckMinLength, $ZodCheckMultipleOf, $ZodCheckNumberFormat, $ZodCheckOverwrite, $ZodCheckRegex, $ZodCheckStartsWith, $ZodCheckStringFormat, $ZodCheckUpperCase };
