//#region src/internal/json-safe.ts
const CIRCULAR = "[Circular]";
const ACCESSOR = "[Accessor]";
const UNSERIALIZABLE = "[Unserializable]";
function dataProperty(object, key) {
	let current = object;
	for (let depth = 0; current !== null && depth < 32; depth += 1) {
		const descriptor = Object.getOwnPropertyDescriptor(current, key);
		if (descriptor) return "value" in descriptor ? descriptor.value : void 0;
		current = Object.getPrototypeOf(current);
	}
}
function normalizeError(error, seen) {
	const name = dataProperty(error, "name");
	const message = dataProperty(error, "message");
	const stack = dataProperty(error, "stack");
	const cause = dataProperty(error, "cause");
	const output = {
		name: typeof name === "string" ? name : "Error",
		message: typeof message === "string" ? message : ""
	};
	if (typeof stack === "string") output.stack = stack;
	if (cause !== void 0) {
		const normalizedCause = toJsonValue(cause, seen);
		if (normalizedCause !== void 0) output.cause = normalizedCause;
	}
	return output;
}
function normalizeArray(value, seen) {
	const descriptors = Object.getOwnPropertyDescriptors(value);
	const lengthDescriptor = Object.getOwnPropertyDescriptor(value, "length");
	const lengthValue = lengthDescriptor && "value" in lengthDescriptor ? lengthDescriptor.value : void 0;
	const length = typeof lengthValue === "number" && Number.isSafeInteger(lengthValue) && lengthValue >= 0 ? lengthValue : 0;
	const output = Array.from({ length }, () => null);
	for (let index = 0; index < length; index += 1) {
		const descriptor = descriptors[String(index)];
		if (!descriptor) continue;
		if (!("value" in descriptor)) {
			output[index] = ACCESSOR;
			continue;
		}
		output[index] = toJsonValue(descriptor.value, seen) ?? null;
	}
	return output;
}
function normalizeObject(value, seen) {
	const descriptors = Object.getOwnPropertyDescriptors(value);
	const output = {};
	for (const [key, descriptor] of Object.entries(descriptors)) {
		if (!descriptor.enumerable) continue;
		if (!("value" in descriptor)) {
			output[key] = ACCESSOR;
			continue;
		}
		const safe = toJsonValue(descriptor.value, seen);
		if (safe !== void 0) output[key] = safe;
	}
	return output;
}
function toJsonValue(value, seen = /* @__PURE__ */ new WeakSet()) {
	if (value === null || typeof value === "string" || typeof value === "boolean") return value;
	if (typeof value === "number") return Number.isFinite(value) ? value : String(value);
	if (typeof value === "undefined") return void 0;
	if (typeof value === "bigint") return value.toString();
	if (typeof value === "symbol") return String(value);
	if (typeof value === "function") return "[Function]";
	if (typeof value !== "object") return String(value);
	if (seen.has(value)) return CIRCULAR;
	seen.add(value);
	try {
		if (value instanceof Error) return normalizeError(value, seen);
		if (Array.isArray(value)) return normalizeArray(value, seen);
		return normalizeObject(value, seen);
	} catch {
		return UNSERIALIZABLE;
	} finally {
		seen.delete(value);
	}
}
function toJsonObject(value) {
	const normalized = toJsonValue(value ?? {});
	return normalized && !Array.isArray(normalized) && typeof normalized === "object" ? normalized : {};
}
//#endregion
export { toJsonValue as n, toJsonObject as t };
