// THIS FILE IS AUTO GENERATED, DON"T EDIT IT DIRECTLY.
import {
  async_once,

  PrepareCall,
  NanoEventHelper,
  JSON2GrpcMessage,
  DefaultDialHelper
} from '@nanoapp/proxy'

let permissions = [
];

import { grpc } from "@improbable-eng/grpc-web";
const wsTransport = grpc.WebsocketTransport()

import { progInfos as proxyApps } from '@nanoapp/proxy/dist/proginfo.js';

import linux_amd64_9a287731682cc49f1905b4dd0059fd71 from 'file-loader?name=[md5:hash:hex]&&outputPath=nanoapps/linux/amd64/NAPIs/!../../napi.bin/linux/amd64.nanoapp';

import linux_arm64_a6f596039944b780a529260dc69cf112 from 'file-loader?name=[md5:hash:hex]&&outputPath=nanoapps/linux/arm64/NAPIs/!../../napi.bin/linux/arm64.nanoapp';

let __nprogs__ = {

    "linux/amd64": {
      path: linux_amd64_9a287731682cc49f1905b4dd0059fd71,
      hash: "9a287731682cc49f1905b4dd0059fd71",
      size: "3702120",
    },

    "linux/arm64": {
      path: linux_arm64_a6f596039944b780a529260dc69cf112,
      hash: "a6f596039944b780a529260dc69cf112",
      size: "9568256",
    },

}

import { FsManagerClient as _FsManagerClient } from "./fm_pb_service.js";

export class FsManager {
  constructor(dialHelper=DefaultDialHelper) {
    this.__dial_helper = dialHelper
    if (typeof this.__dial_helper.set_nproxy_programs === "function") {
       this.__dial_helper.set_nproxy_programs(proxyApps)
    }
    
    
    
    
    
    
    
  }
  async __dial__() {
    return await this.__dial_helper.dial({
      name: "nanoapp.nstd",
      type: "",


      programs: __nprogs__,

      appclient: _FsManagerClient,
      permissions,
    });
  }
  async GetTree(req, callback, scontrol) {
    req = JSON2GrpcMessage(req, GetTreeRequest) 
    let prepare = new PrepareCall(scontrol);
    let thisApp = await this.__dial__();
    if (prepare.shouldReturn()) {
      return
    }
    return new Promise(function(resolve, reject) {
      let ret = thisApp.getTree(req, thisApp.options.metadata)
        .on("data", (msg) => { callback(msg) })
        .on("end", (status) => {
           if (status.code != 0) {
             reject(status)
           } else {
             resolve(status)
           }
        });
      prepare.switchToApp(ret, GetTreeRequest)
    })
  }
  async Mkdir(req, scontrol) {
    req = JSON2GrpcMessage(req, MkdirRequest) 
    let prepare = new PrepareCall(scontrol);
    let thisApp = await this.__dial__();
    if (prepare.shouldReturn()) {
      return
    }
    return new Promise(function(resolve, reject) {
      let ret = thisApp.mkdir(req, thisApp.options.metadata, (err, msg) => {
        if (err) { reject(err) } else { resolve(msg) }
      })
      prepare.switchToApp(ret, MkdirRequest)
    })
  }
  async RemoveFile(req, scontrol) {
    req = JSON2GrpcMessage(req, RemoveRequest) 
    let prepare = new PrepareCall(scontrol);
    let thisApp = await this.__dial__();
    if (prepare.shouldReturn()) {
      return
    }
    return new Promise(function(resolve, reject) {
      let ret = thisApp.removeFile(req, thisApp.options.metadata, (err, msg) => {
        if (err) { reject(err) } else { resolve(msg) }
      })
      prepare.switchToApp(ret, RemoveRequest)
    })
  }
  async PasteFile(req, callback, scontrol) {
    req = JSON2GrpcMessage(req, PasteRequest) 
    let prepare = new PrepareCall(scontrol);
    prepare.write(req) //必须在最开始就发送第一个req，否则可能导致乱序。
    let thisApp = await this.__dial__();
    if (prepare.shouldReturn()) {
      return
    }
    return new Promise(function(resolve, reject) {
      let oldTransport = thisApp.options.transport
      thisApp.options.transport = wsTransport;
      let ret = thisApp.pasteFile(thisApp.options.metadata)
        .on("data", (msg) => { callback(msg) })
        .on("end", (status) => {
           if (status.code != 0) {
             reject(status)
           } else {
             resolve(status)
           }
        });
      thisApp.options.transport = oldTransport;
      prepare.switchToApp(ret, PasteRequest)
    })
  }
  async Rename(req, scontrol) {
    req = JSON2GrpcMessage(req, RenameRequest) 
    let prepare = new PrepareCall(scontrol);
    let thisApp = await this.__dial__();
    if (prepare.shouldReturn()) {
      return
    }
    return new Promise(function(resolve, reject) {
      let ret = thisApp.rename(req, thisApp.options.metadata, (err, msg) => {
        if (err) { reject(err) } else { resolve(msg) }
      })
      prepare.switchToApp(ret, RenameRequest)
    })
  }
  async GetFile(req, callback, scontrol) {
    req = JSON2GrpcMessage(req, FileName) 
    let prepare = new PrepareCall(scontrol);
    let thisApp = await this.__dial__();
    if (prepare.shouldReturn()) {
      return
    }
    return new Promise(function(resolve, reject) {
      let ret = thisApp.getFile(req, thisApp.options.metadata)
        .on("data", (msg) => { callback(msg) })
        .on("end", (status) => {
           if (status.code != 0) {
             reject(status)
           } else {
             resolve(status)
           }
        });
      prepare.switchToApp(ret, FileName)
    })
  }
  async SaveFile(req, scontrol) {
    req = JSON2GrpcMessage(req, SaveFileRequest) 
    let prepare = new PrepareCall(scontrol);
    let thisApp = await this.__dial__();
    if (prepare.shouldReturn()) {
      return
    }
    return new Promise(function(resolve, reject) {
      let ret = thisApp.saveFile(req, thisApp.options.metadata, (err, msg) => {
        if (err) { reject(err) } else { resolve(msg) }
      })
      prepare.switchToApp(ret, SaveFileRequest)
    })
  }
}

// 导入所有的Message Type
import { GetTreeRequest, GetTreeResponse, MkdirRequest, MkdirResponse, RemoveRequest, RemoveResponse, PasteRequest, PasteResponse, RenameRequest, RenameResponse, FileData, FileName, SaveFileRequest, SaveFileResponse } from "./fm_pb.js";
export { GetTreeRequest, GetTreeResponse, MkdirRequest, MkdirResponse, RemoveRequest, RemoveResponse, PasteRequest, PasteResponse, RenameRequest, RenameResponse, FileData, FileName, SaveFileRequest, SaveFileResponse }
