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

// package: lnks.nstd
// file: subprocess.proto

var subprocess_pb = require("./subprocess_pb");
var google_protobuf_empty_pb = require("google-protobuf/google/protobuf/empty_pb");
var grpc = require("@improbable-eng/grpc-web").grpc;

var Subprocess = (function () {
  function Subprocess() {}
  Subprocess.serviceName = "lnks.nstd.Subprocess";
  return Subprocess;
}());

Subprocess.Spawn = {
  methodName: "Spawn",
  service: Subprocess,
  requestStream: false,
  responseStream: false,
  requestType: subprocess_pb.SpawnInfo,
  responseType: subprocess_pb.ProcessID
};

Subprocess.Start = {
  methodName: "Start",
  service: Subprocess,
  requestStream: false,
  responseStream: false,
  requestType: subprocess_pb.ProcessID,
  responseType: google_protobuf_empty_pb.Empty
};

Subprocess.Wait = {
  methodName: "Wait",
  service: Subprocess,
  requestStream: false,
  responseStream: false,
  requestType: subprocess_pb.ProcessID,
  responseType: google_protobuf_empty_pb.Empty
};

Subprocess.kill = {
  methodName: "kill",
  service: Subprocess,
  requestStream: false,
  responseStream: false,
  requestType: subprocess_pb.ProcessID,
  responseType: google_protobuf_empty_pb.Empty
};

Subprocess.ClosePipe = {
  methodName: "ClosePipe",
  service: Subprocess,
  requestStream: false,
  responseStream: false,
  requestType: subprocess_pb.PipeID,
  responseType: google_protobuf_empty_pb.Empty
};

Subprocess.ReadPipe = {
  methodName: "ReadPipe",
  service: Subprocess,
  requestStream: false,
  responseStream: false,
  requestType: subprocess_pb.ReadPipeRequest,
  responseType: subprocess_pb.Chunk
};

Subprocess.WritePipe = {
  methodName: "WritePipe",
  service: Subprocess,
  requestStream: false,
  responseStream: false,
  requestType: subprocess_pb.WritePipeRequest,
  responseType: google_protobuf_empty_pb.Empty
};

exports.Subprocess = Subprocess;

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

SubprocessClient.prototype.spawn = function spawn(requestMessage, metadata, callback) {
  if (arguments.length === 2) {
    callback = arguments[1];
  }
  var client = grpc.unary(Subprocess.Spawn, {
    request: requestMessage,
    host: this.serviceHost,
    metadata: metadata,
    transport: this.options.transport,
    debug: this.options.debug,
    onEnd: function (response) {
      if (callback) {
        if (response.status !== grpc.Code.OK) {
          var err = new Error(response.statusMessage);
          err.code = response.status;
          err.metadata = response.trailers;
          callback(err, null);
        } else {
          callback(null, response.message);
        }
      }
    }
  });
  return {
    cancel: function () {
      callback = null;
      client.close();
    }
  };
};

SubprocessClient.prototype.start = function start(requestMessage, metadata, callback) {
  if (arguments.length === 2) {
    callback = arguments[1];
  }
  var client = grpc.unary(Subprocess.Start, {
    request: requestMessage,
    host: this.serviceHost,
    metadata: metadata,
    transport: this.options.transport,
    debug: this.options.debug,
    onEnd: function (response) {
      if (callback) {
        if (response.status !== grpc.Code.OK) {
          var err = new Error(response.statusMessage);
          err.code = response.status;
          err.metadata = response.trailers;
          callback(err, null);
        } else {
          callback(null, response.message);
        }
      }
    }
  });
  return {
    cancel: function () {
      callback = null;
      client.close();
    }
  };
};

SubprocessClient.prototype.wait = function wait(requestMessage, metadata, callback) {
  if (arguments.length === 2) {
    callback = arguments[1];
  }
  var client = grpc.unary(Subprocess.Wait, {
    request: requestMessage,
    host: this.serviceHost,
    metadata: metadata,
    transport: this.options.transport,
    debug: this.options.debug,
    onEnd: function (response) {
      if (callback) {
        if (response.status !== grpc.Code.OK) {
          var err = new Error(response.statusMessage);
          err.code = response.status;
          err.metadata = response.trailers;
          callback(err, null);
        } else {
          callback(null, response.message);
        }
      }
    }
  });
  return {
    cancel: function () {
      callback = null;
      client.close();
    }
  };
};

SubprocessClient.prototype.kill = function kill(requestMessage, metadata, callback) {
  if (arguments.length === 2) {
    callback = arguments[1];
  }
  var client = grpc.unary(Subprocess.kill, {
    request: requestMessage,
    host: this.serviceHost,
    metadata: metadata,
    transport: this.options.transport,
    debug: this.options.debug,
    onEnd: function (response) {
      if (callback) {
        if (response.status !== grpc.Code.OK) {
          var err = new Error(response.statusMessage);
          err.code = response.status;
          err.metadata = response.trailers;
          callback(err, null);
        } else {
          callback(null, response.message);
        }
      }
    }
  });
  return {
    cancel: function () {
      callback = null;
      client.close();
    }
  };
};

SubprocessClient.prototype.closePipe = function closePipe(requestMessage, metadata, callback) {
  if (arguments.length === 2) {
    callback = arguments[1];
  }
  var client = grpc.unary(Subprocess.ClosePipe, {
    request: requestMessage,
    host: this.serviceHost,
    metadata: metadata,
    transport: this.options.transport,
    debug: this.options.debug,
    onEnd: function (response) {
      if (callback) {
        if (response.status !== grpc.Code.OK) {
          var err = new Error(response.statusMessage);
          err.code = response.status;
          err.metadata = response.trailers;
          callback(err, null);
        } else {
          callback(null, response.message);
        }
      }
    }
  });
  return {
    cancel: function () {
      callback = null;
      client.close();
    }
  };
};

SubprocessClient.prototype.readPipe = function readPipe(requestMessage, metadata, callback) {
  if (arguments.length === 2) {
    callback = arguments[1];
  }
  var client = grpc.unary(Subprocess.ReadPipe, {
    request: requestMessage,
    host: this.serviceHost,
    metadata: metadata,
    transport: this.options.transport,
    debug: this.options.debug,
    onEnd: function (response) {
      if (callback) {
        if (response.status !== grpc.Code.OK) {
          var err = new Error(response.statusMessage);
          err.code = response.status;
          err.metadata = response.trailers;
          callback(err, null);
        } else {
          callback(null, response.message);
        }
      }
    }
  });
  return {
    cancel: function () {
      callback = null;
      client.close();
    }
  };
};

SubprocessClient.prototype.writePipe = function writePipe(requestMessage, metadata, callback) {
  if (arguments.length === 2) {
    callback = arguments[1];
  }
  var client = grpc.unary(Subprocess.WritePipe, {
    request: requestMessage,
    host: this.serviceHost,
    metadata: metadata,
    transport: this.options.transport,
    debug: this.options.debug,
    onEnd: function (response) {
      if (callback) {
        if (response.status !== grpc.Code.OK) {
          var err = new Error(response.statusMessage);
          err.code = response.status;
          err.metadata = response.trailers;
          callback(err, null);
        } else {
          callback(null, response.message);
        }
      }
    }
  });
  return {
    cancel: function () {
      callback = null;
      client.close();
    }
  };
};

exports.SubprocessClient = SubprocessClient;

