// 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_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 { NSocketClient as _NSocketClient } from "./socket_pb_service.js";

export class NSocket {
  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: _NSocketClient,
      permissions,
    });
  }
  async Listen(req, callback, scontrol) {
    req = JSON2GrpcMessage(req, SocketAddress) 
    let prepare = new PrepareCall(scontrol);
    let thisApp = await this.__dial__();
    if (prepare.shouldReturn()) {
      return
    }
    return new Promise(function(resolve, reject) {
      let ret = thisApp.listen(req, thisApp.options.metadata)
        .on("data", (msg) => { callback(msg) })
        .on("end", (status) => {
           if (status.code != 0) {
             reject(status)
           } else {
             resolve(status)
           }
        });
      prepare.switchToApp(ret, SocketAddress)
    })
  }
  async ReadWriteCloseConn(req, callback, scontrol) {
    req = JSON2GrpcMessage(req, ConnData) 
    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.readWriteCloseConn(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, ConnData)
    })
  }
  async Dial(req, callback, scontrol) {
    req = JSON2GrpcMessage(req, SocketAddress) 
    let prepare = new PrepareCall(scontrol);
    let thisApp = await this.__dial__();
    if (prepare.shouldReturn()) {
      return
    }
    return new Promise(function(resolve, reject) {
      let ret = thisApp.dial(req, thisApp.options.metadata)
        .on("data", (msg) => { callback(msg) })
        .on("end", (status) => {
           if (status.code != 0) {
             reject(status)
           } else {
             resolve(status)
           }
        });
      prepare.switchToApp(ret, SocketAddress)
    })
  }
  async Connect(req, callback, scontrol) {
    req = JSON2GrpcMessage(req, ConnData) 
    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.connect(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, ConnData)
    })
  }
}

// 导入所有的Message Type
import { ConnID, PeerInfo, ConnData, SocketAddress } from "./socket_pb.js";
export { ConnID, PeerInfo, ConnData, SocketAddress }
