export const IS_NODE =
    typeof process !== "undefined" &&
    process.versions?.node != null

async function bridgeSend(name, args) {
    // Example browser implementation: send function call to server
    const res = await global.Socket.send({
        name: name,
        args: args
    })

    return res
}

/**
 * Wraps an object of functions so that:
 * - Node calls the real function
 * - Browser calls bridgeSend
 */
export function createBridge(funcs) {
    return new Proxy(funcs, {
        get(target, prop) {
            const orig = target[prop]

            return function (...args) {
                if (IS_NODE) {
                    return orig(...args)
                } else {
                    return bridgeSend(prop, args)
                }
            }
        }
    })
}
