declare const _kConnectionID: unique symbol;

/** RPC 连接 ID */
export type ConnectionID = string & { tag: typeof _kConnectionID };
/** 认证信息 */
export type RpcMetadata = Record<string, unknown>;

/** RPC 错误 */
export interface RpcErrorLike {
    /** 名称 */
    name: string;
    /** 消息 */
    message: string;
    /** 调用 */
    stack?: string;
    /** 引发错误的其他错误 */
    errors?: RpcErrorLike[];
}

/** RPC 调用的通用属性 */
interface RpcPayloadBase {
    /** 调用类型 */
    type: string;
    /** 序列号 */
    seq: number;
}

/** RPC 认证调用 */
export interface RpcAuthPayload extends RpcPayloadBase {
    /** @inheritdoc */
    type: 'auth';
    /** 连接 ID */
    id: ConnectionID;
    /** 协议版本 */
    version: number;
    /** 认证信息 */
    metadata: RpcMetadata;
    /** 异常 */
    error?: RpcErrorLike;
}

/** RPC 方法调用 */
export interface RpcCallPayload extends RpcPayloadBase {
    /** @inheritdoc */
    type: 'call';
    /** 调用的方法 */
    method: string;
    /** 参数 */
    args: unknown[];
}

/** RPC 方法返回 */
export interface RpcReturnPayload extends RpcPayloadBase {
    /** @inheritdoc */
    type: 'return';
    /** 返回值 */
    result?: unknown;
    /** 异常 */
    error?: RpcErrorLike;
}

/** RPC 通知 */
export interface RpcNotifyPayload extends RpcPayloadBase {
    /** @inheritdoc */
    type: 'notify';
    /** 调用的方法 */
    method: string;
    /** 参数 */
    args: unknown[];
}

/** RPC 订阅 */
export interface RpcSubscribePayload extends RpcPayloadBase {
    /** @inheritdoc */
    type: 'subscribe';
    /** 调用的方法 */
    method: string;
    /** 参数 */
    args: unknown[];
}

/** RPC 发布 */
export interface RpcPublishPayload extends RpcPayloadBase {
    /** @inheritdoc */
    type: 'publish';
    /** 发布的值 */
    next?: unknown;
    /** 异常结束 */
    error?: RpcErrorLike;
    /** 正常结束 */
    complete?: true;
}

/** RPC 取消订阅 */
export interface RpcUnsubscribePayload extends RpcPayloadBase {
    /** @inheritdoc */
    type: 'unsubscribe';
}

/** RPC 错误 */
export interface RpcErrorPayload extends RpcPayloadBase {
    /** @inheritdoc */
    type: 'error';
    /** 异常 */
    error: RpcErrorLike;
}

/** RPC 调用 */
export type RpcPayload =
    | RpcCallPayload
    | RpcReturnPayload
    | RpcNotifyPayload
    | RpcPublishPayload
    | RpcSubscribePayload
    | RpcUnsubscribePayload
    | RpcAuthPayload
    | RpcErrorPayload;
