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

// package: nde.filesystem
// file: fm.proto

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

var FsManager = (function () {
  function FsManager() {}
  FsManager.serviceName = "nde.filesystem.FsManager";
  return FsManager;
}());

FsManager.GetTree = {
  methodName: "GetTree",
  service: FsManager,
  requestStream: false,
  responseStream: true,
  requestType: fm_pb.GetTreeRequest,
  responseType: fm_pb.GetTreeResponse
};

FsManager.Mkdir = {
  methodName: "Mkdir",
  service: FsManager,
  requestStream: false,
  responseStream: false,
  requestType: fm_pb.MkdirRequest,
  responseType: fm_pb.MkdirResponse
};

FsManager.RemoveFile = {
  methodName: "RemoveFile",
  service: FsManager,
  requestStream: false,
  responseStream: false,
  requestType: fm_pb.RemoveRequest,
  responseType: fm_pb.RemoveResponse
};

FsManager.PasteFile = {
  methodName: "PasteFile",
  service: FsManager,
  requestStream: true,
  responseStream: true,
  requestType: fm_pb.PasteRequest,
  responseType: fm_pb.PasteResponse
};

FsManager.Rename = {
  methodName: "Rename",
  service: FsManager,
  requestStream: false,
  responseStream: false,
  requestType: fm_pb.RenameRequest,
  responseType: fm_pb.RenameResponse
};

FsManager.GetFile = {
  methodName: "GetFile",
  service: FsManager,
  requestStream: false,
  responseStream: true,
  requestType: fm_pb.FileName,
  responseType: fm_pb.FileData
};

FsManager.SaveFile = {
  methodName: "SaveFile",
  service: FsManager,
  requestStream: false,
  responseStream: false,
  requestType: fm_pb.SaveFileRequest,
  responseType: fm_pb.SaveFileResponse
};

exports.FsManager = FsManager;

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

FsManagerClient.prototype.getTree = function getTree(requestMessage, metadata) {
  var listeners = {
    data: [],
    end: [],
    status: []
  };
  var client = grpc.invoke(FsManager.GetTree, {
    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();
    }
  };
};

FsManagerClient.prototype.mkdir = function mkdir(requestMessage, metadata, callback) {
  if (arguments.length === 2) {
    callback = arguments[1];
  }
  var client = grpc.unary(FsManager.Mkdir, {
    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();
    }
  };
};

FsManagerClient.prototype.removeFile = function removeFile(requestMessage, metadata, callback) {
  if (arguments.length === 2) {
    callback = arguments[1];
  }
  var client = grpc.unary(FsManager.RemoveFile, {
    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();
    }
  };
};

FsManagerClient.prototype.pasteFile = function pasteFile(metadata) {
  var listeners = {
    data: [],
    end: [],
    status: []
  };
  var client = grpc.client(FsManager.PasteFile, {
    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();
    }
  };
};

FsManagerClient.prototype.rename = function rename(requestMessage, metadata, callback) {
  if (arguments.length === 2) {
    callback = arguments[1];
  }
  var client = grpc.unary(FsManager.Rename, {
    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();
    }
  };
};

FsManagerClient.prototype.getFile = function getFile(requestMessage, metadata) {
  var listeners = {
    data: [],
    end: [],
    status: []
  };
  var client = grpc.invoke(FsManager.GetFile, {
    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();
    }
  };
};

FsManagerClient.prototype.saveFile = function saveFile(requestMessage, metadata, callback) {
  if (arguments.length === 2) {
    callback = arguments[1];
  }
  var client = grpc.unary(FsManager.SaveFile, {
    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.FsManagerClient = FsManagerClient;

