import { RpcMethod } from "..";
import { ProxyContext, ProxyContextMutator } from "../core/ProxyContext";
import { ProxyMethodConfig } from "../types";

const DefaultConfig: ProxyMethodConfig = {
};

export function ProxyMethod(config?: ProxyMethodConfig): MethodDecorator {

    const props = config ?? DefaultConfig;

    return (target: Function | Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<any>) => {

        if (typeof target === 'function') {
            throw new RpcMethod(propertyKey.toString(), 'ProxyMethod must be applied to instance members only.');
        }

        const ctx = ProxyContext.getContext(props.contextDomain) as ProxyContextMutator;

        return ctx.defineMember(
            props,
            target,
            propertyKey,
            descriptor
        );
    }
}