import { isProxy, toRaw } from "vue";

/**
 * Converts reactive proxies or objects into a deep, plain clone.
 */
export const unwrapAndClone = <T extends object>(obj: T): T => {
    const raw = isProxy(obj) ? toRaw(obj) : obj;

    // Use structuredClone if available (handles Dates, Maps, Sets, circulars)
    if (typeof globalThis.structuredClone === "function") {
        return globalThis.structuredClone(raw);
    }

    // Fallback for older environments (loses Dates, Sets, Maps)
    return JSON.parse(JSON.stringify(raw));
};