/**
 * Shared serialization utility for the state inspector.
 *
 * Converts Maps → plain objects, Sets → arrays, and replaces circular
 * references with a sentinel string so any state snapshot is JSON-safe.
 */
/**
 * Serialize a value to a JSON-safe representation.
 * Maps → plain objects, Sets → arrays, circular refs → '[Circular]'.
 *
 * @param value - Value to serialize.
 * @param seen - WeakSet tracking seen objects to detect cycles.
 * @returns A JSON-safe representation.
 */
export function serializeSafe(value, seen = new WeakSet()) {
    if (value === null || typeof value !== 'object')
        return value;
    if (seen.has(value))
        return '[Circular]';
    seen.add(value);
    if (value instanceof Map) {
        const result = {};
        for (const [k, v] of value.entries()) {
            result[String(k)] = serializeSafe(v, seen);
        }
        return result;
    }
    if (value instanceof Set) {
        return [...value].map((v) => serializeSafe(v, seen));
    }
    if (Array.isArray(value)) {
        return value.map((v) => serializeSafe(v, seen));
    }
    const result = {};
    for (const [k, v] of Object.entries(value)) {
        result[k] = serializeSafe(v, seen);
    }
    return result;
}
