/* eslint-disable no-undef, no-unused-vars, no-redeclare */

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

var socket_pb = require("./socket_pb");
var grpc = require("@improbable-eng/grpc-web").grpc;

var NSocket = (function () {
  function NSocket() {}
  NSocket.serviceName = "nanoapp.std.socket.NSocket";
  return NSocket;
}());

NSocket.Listen = {
  methodName: "Listen",
  service: NSocket,
  requestStream: false,
  responseStream: true,
  requestType: socket_pb.SocketAddress,
  responseType: socket_pb.ConnID
};

NSocket.ReadWriteCloseConn = {
  methodName: "ReadWriteCloseConn",
  service: NSocket,
  requestStream: true,
  responseStream: true,
  requestType: socket_pb.ConnData,
  responseType: socket_pb.ConnData
};

NSocket.Dial = {
  methodName: "Dial",
  service: NSocket,
  requestStream: false,
  responseStream: true,
  requestType: socket_pb.SocketAddress,
  responseType: socket_pb.ConnID
};

NSocket.Connect = {
  methodName: "Connect",
  service: NSocket,
  requestStream: true,
  responseStream: true,
  requestType: socket_pb.ConnData,
  responseType: socket_pb.ConnData
};

exports.NSocket = NSocket;

function NSocketClient(serviceHost, options) {
  this.serviceHost = serviceHost;
  this.options = options || {};
}

NSocketClient.prototype.listen = function listen(requestMessage, metadata) {
  var listeners = {
    data: [],
    end: [],
    status: []
  };
  var client = grpc.invoke(NSocket.Listen, {
    request: requestMessage,
    host: this.serviceHost,
    metadata: metadata,
    transport: this.options.transport,
    debug: this.options.debug,
    onMessage: function (responseMessage) {
      listeners.data.forEach(function (handler) {
        handler(responseMessage);
      });
    },
    onEnd: function (status, statusMessage, trailers) {
      listeners.status.forEach(function (handler) {
        handler({ code: status, details: statusMessage, metadata: trailers });
      });
      listeners.end.forEach(function (handler) {
        handler({ code: status, details: statusMessage, metadata: trailers });
      });
      listeners = null;
    }
  });
  return {
    on: function (type, handler) {
      listeners[type].push(handler);
      return this;
    },
    cancel: function () {
      listeners = null;
      client.close();
    }
  };
};

NSocketClient.prototype.readWriteCloseConn = function readWriteCloseConn(metadata) {
  var listeners = {
    data: [],
    end: [],
    status: []
  };
  var client = grpc.client(NSocket.ReadWriteCloseConn, {
    host: this.serviceHost,
    metadata: metadata,
    transport: this.options.transport
  });
  client.onEnd(function (status, statusMessage, trailers) {
    listeners.status.forEach(function (handler) {
      handler({ code: status, details: statusMessage, metadata: trailers });
    });
    listeners.end.forEach(function (handler) {
      handler({ code: status, details: statusMessage, metadata: trailers });
    });
    listeners = null;
  });
  client.onMessage(function (message) {
    listeners.data.forEach(function (handler) {
      handler(message);
    })
  });
  client.start(metadata);
  return {
    on: function (type, handler) {
      listeners[type].push(handler);
      return this;
    },
    write: function (requestMessage) {
      client.send(requestMessage);
      return this;
    },
    end: function () {
      client.finishSend();
    },
    cancel: function () {
      listeners = null;
      client.close();
    }
  };
};

NSocketClient.prototype.dial = function dial(requestMessage, metadata) {
  var listeners = {
    data: [],
    end: [],
    status: []
  };
  var client = grpc.invoke(NSocket.Dial, {
    request: requestMessage,
    host: this.serviceHost,
    metadata: metadata,
    transport: this.options.transport,
    debug: this.options.debug,
    onMessage: function (responseMessage) {
      listeners.data.forEach(function (handler) {
        handler(responseMessage);
      });
    },
    onEnd: function (status, statusMessage, trailers) {
      listeners.status.forEach(function (handler) {
        handler({ code: status, details: statusMessage, metadata: trailers });
      });
      listeners.end.forEach(function (handler) {
        handler({ code: status, details: statusMessage, metadata: trailers });
      });
      listeners = null;
    }
  });
  return {
    on: function (type, handler) {
      listeners[type].push(handler);
      return this;
    },
    cancel: function () {
      listeners = null;
      client.close();
    }
  };
};

NSocketClient.prototype.connect = function connect(metadata) {
  var listeners = {
    data: [],
    end: [],
    status: []
  };
  var client = grpc.client(NSocket.Connect, {
    host: this.serviceHost,
    metadata: metadata,
    transport: this.options.transport
  });
  client.onEnd(function (status, statusMessage, trailers) {
    listeners.status.forEach(function (handler) {
      handler({ code: status, details: statusMessage, metadata: trailers });
    });
    listeners.end.forEach(function (handler) {
      handler({ code: status, details: statusMessage, metadata: trailers });
    });
    listeners = null;
  });
  client.onMessage(function (message) {
    listeners.data.forEach(function (handler) {
      handler(message);
    })
  });
  client.start(metadata);
  return {
    on: function (type, handler) {
      listeners[type].push(handler);
      return this;
    },
    write: function (requestMessage) {
      client.send(requestMessage);
      return this;
    },
    end: function () {
      client.finishSend();
    },
    cancel: function () {
      listeners = null;
      client.close();
    }
  };
};

exports.NSocketClient = NSocketClient;

