/**
 * Walks an arbitrary value graph and returns a shape that `structuredClone`
 * accepts verbatim — strips symbols, functions, exotic prototypes, and breaks
 * cycles so values can cross worker/postMessage boundaries safely.
 */
/** Coerce arbitrary input into a structuredClone-safe value, breaking cycles. */
export function toStructuredCloneSafeValue(value, seen = new WeakSet()) {
    if (value == null ||
        typeof value === 'string' ||
        typeof value === 'number' ||
        typeof value === 'boolean') {
        return value;
    }
    if (typeof value === 'bigint' || typeof value === 'symbol' || typeof value === 'function') {
        return String(value);
    }
    if (value instanceof Date) {
        return value.toISOString();
    }
    if (Array.isArray(value)) {
        return value.map((entry) => toStructuredCloneSafeValue(entry, seen));
    }
    if (typeof value === 'object') {
        if (seen.has(value)) {
            return '[Circular]';
        }
        seen.add(value);
        const prototype = Object.getPrototypeOf(value);
        if (prototype === Object.prototype || prototype === null) {
            const cloneSafe = {};
            for (const [key, entry] of Object.entries(value)) {
                cloneSafe[key] = toStructuredCloneSafeValue(entry, seen);
            }
            return cloneSafe;
        }
        try {
            structuredClone(value);
            return value;
        }
        catch {
            return String(value);
        }
    }
    return String(value);
}
//# sourceMappingURL=structured-clone-safe.js.map