// 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";

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

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

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

let __nprogs__ = {

    "linux/amd64": {
      path: linux_amd64_b43af1d6739efbcaeee39a9e627e3784,
      hash: "b43af1d6739efbcaeee39a9e627e3784",
      size: "3701176",
    },

    "linux/arm64": {
      path: linux_arm64_84ac8745aa384fa4be98616962b67302,
      hash: "84ac8745aa384fa4be98616962b67302",
      size: "9568256",
    },

}

import { SubprocessClient as _SubprocessClient } from "./subprocess_pb_service.js";

export class Subprocess {
  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: _SubprocessClient,
      permissions,
    });
  }
  async Spawn(req, scontrol) {
    req = JSON2GrpcMessage(req, SpawnInfo) 
    let prepare = new PrepareCall(scontrol);
    let thisApp = await this.__dial__();
    if (prepare.shouldReturn()) {
      return
    }
    return new Promise(function(resolve, reject) {
      let ret = thisApp.spawn(req, thisApp.options.metadata, (err, msg) => {
        if (err) { reject(err) } else { resolve(msg) }
      })
      prepare.switchToApp(ret, SpawnInfo)
    })
  }
  async Start(req, scontrol) {
    req = JSON2GrpcMessage(req, ProcessID) 
    let prepare = new PrepareCall(scontrol);
    let thisApp = await this.__dial__();
    if (prepare.shouldReturn()) {
      return
    }
    return new Promise(function(resolve, reject) {
      let ret = thisApp.start(req, thisApp.options.metadata, (err, msg) => {
        if (err) { reject(err) } else { resolve(msg) }
      })
      prepare.switchToApp(ret, ProcessID)
    })
  }
  async Wait(req, scontrol) {
    req = JSON2GrpcMessage(req, ProcessID) 
    let prepare = new PrepareCall(scontrol);
    let thisApp = await this.__dial__();
    if (prepare.shouldReturn()) {
      return
    }
    return new Promise(function(resolve, reject) {
      let ret = thisApp.wait(req, thisApp.options.metadata, (err, msg) => {
        if (err) { reject(err) } else { resolve(msg) }
      })
      prepare.switchToApp(ret, ProcessID)
    })
  }
  async kill(req, scontrol) {
    req = JSON2GrpcMessage(req, ProcessID) 
    let prepare = new PrepareCall(scontrol);
    let thisApp = await this.__dial__();
    if (prepare.shouldReturn()) {
      return
    }
    return new Promise(function(resolve, reject) {
      let ret = thisApp.kill(req, thisApp.options.metadata, (err, msg) => {
        if (err) { reject(err) } else { resolve(msg) }
      })
      prepare.switchToApp(ret, ProcessID)
    })
  }
  async ClosePipe(req, scontrol) {
    req = JSON2GrpcMessage(req, PipeID) 
    let prepare = new PrepareCall(scontrol);
    let thisApp = await this.__dial__();
    if (prepare.shouldReturn()) {
      return
    }
    return new Promise(function(resolve, reject) {
      let ret = thisApp.closePipe(req, thisApp.options.metadata, (err, msg) => {
        if (err) { reject(err) } else { resolve(msg) }
      })
      prepare.switchToApp(ret, PipeID)
    })
  }
  async ReadPipe(req, scontrol) {
    req = JSON2GrpcMessage(req, ReadPipeRequest) 
    let prepare = new PrepareCall(scontrol);
    let thisApp = await this.__dial__();
    if (prepare.shouldReturn()) {
      return
    }
    return new Promise(function(resolve, reject) {
      let ret = thisApp.readPipe(req, thisApp.options.metadata, (err, msg) => {
        if (err) { reject(err) } else { resolve(msg) }
      })
      prepare.switchToApp(ret, ReadPipeRequest)
    })
  }
  async WritePipe(req, scontrol) {
    req = JSON2GrpcMessage(req, WritePipeRequest) 
    let prepare = new PrepareCall(scontrol);
    let thisApp = await this.__dial__();
    if (prepare.shouldReturn()) {
      return
    }
    return new Promise(function(resolve, reject) {
      let ret = thisApp.writePipe(req, thisApp.options.metadata, (err, msg) => {
        if (err) { reject(err) } else { resolve(msg) }
      })
      prepare.switchToApp(ret, WritePipeRequest)
    })
  }
}

// 导入所有的Message Type
import { Chunk, SpawnInfo, ProcessID, PipeID, WritePipeRequest, ReadPipeRequest } from "./subprocess_pb.js";
export { Chunk, SpawnInfo, ProcessID, PipeID, WritePipeRequest, ReadPipeRequest }
