// package: nanoapp.std.socket
// file: socket.proto

import * as socket_pb from "./socket_pb";
import {grpc} from "@improbable-eng/grpc-web";

type NSocketListen = {
  readonly methodName: string;
  readonly service: typeof NSocket;
  readonly requestStream: false;
  readonly responseStream: true;
  readonly requestType: typeof socket_pb.SocketAddress;
  readonly responseType: typeof socket_pb.ConnID;
};

type NSocketReadWriteCloseConn = {
  readonly methodName: string;
  readonly service: typeof NSocket;
  readonly requestStream: true;
  readonly responseStream: true;
  readonly requestType: typeof socket_pb.ConnData;
  readonly responseType: typeof socket_pb.ConnData;
};

type NSocketDial = {
  readonly methodName: string;
  readonly service: typeof NSocket;
  readonly requestStream: false;
  readonly responseStream: true;
  readonly requestType: typeof socket_pb.SocketAddress;
  readonly responseType: typeof socket_pb.ConnID;
};

type NSocketConnect = {
  readonly methodName: string;
  readonly service: typeof NSocket;
  readonly requestStream: true;
  readonly responseStream: true;
  readonly requestType: typeof socket_pb.ConnData;
  readonly responseType: typeof socket_pb.ConnData;
};

export class NSocket {
  static readonly serviceName: string;
  static readonly Listen: NSocketListen;
  static readonly ReadWriteCloseConn: NSocketReadWriteCloseConn;
  static readonly Dial: NSocketDial;
  static readonly Connect: NSocketConnect;
}

export type ServiceError = { message: string, code: number; metadata: grpc.Metadata }
export type Status = { details: string, code: number; metadata: grpc.Metadata }

interface UnaryResponse {
  cancel(): void;
}
interface ResponseStream<T> {
  cancel(): void;
  on(type: 'data', handler: (message: T) => void): ResponseStream<T>;
  on(type: 'end', handler: (status?: Status) => void): ResponseStream<T>;
  on(type: 'status', handler: (status: Status) => void): ResponseStream<T>;
}
interface RequestStream<T> {
  write(message: T): RequestStream<T>;
  end(): void;
  cancel(): void;
  on(type: 'end', handler: (status?: Status) => void): RequestStream<T>;
  on(type: 'status', handler: (status: Status) => void): RequestStream<T>;
}
interface BidirectionalStream<ReqT, ResT> {
  write(message: ReqT): BidirectionalStream<ReqT, ResT>;
  end(): void;
  cancel(): void;
  on(type: 'data', handler: (message: ResT) => void): BidirectionalStream<ReqT, ResT>;
  on(type: 'end', handler: (status?: Status) => void): BidirectionalStream<ReqT, ResT>;
  on(type: 'status', handler: (status: Status) => void): BidirectionalStream<ReqT, ResT>;
}

export class NSocketClient {
  readonly serviceHost: string;

  constructor(serviceHost: string, options?: grpc.RpcOptions);
  listen(requestMessage: socket_pb.SocketAddress, metadata?: grpc.Metadata): ResponseStream<socket_pb.ConnID>;
  readWriteCloseConn(metadata?: grpc.Metadata): BidirectionalStream<socket_pb.ConnData, socket_pb.ConnData>;
  dial(requestMessage: socket_pb.SocketAddress, metadata?: grpc.Metadata): ResponseStream<socket_pb.ConnID>;
  connect(metadata?: grpc.Metadata): BidirectionalStream<socket_pb.ConnData, socket_pb.ConnData>;
}

