/**
 *
 * This file is auto-generated. Do not edit manually: changes may be erased.
 * Generated by Aqua compiler: https://github.com/fluencelabs/aqua/.
 * If you find any bugs, please write an issue on GitHub: https://github.com/fluencelabs/aqua/issues
 * Aqua version: 0.3.0-224
 *
 */
import { FluencePeer } from "@fluencelabs/fluence";
import {
  ResultCodes,
  RequestFlow,
  RequestFlowBuilder,
  CallParams,
} from "@fluencelabs/fluence/dist/internal/compilerSupport/v1";

// Services

// Functions

export function set_timeout(
  node: string,
  timeout_sec: number,
  config?: { ttl?: number }
): Promise<void>;
export function set_timeout(
  peer: FluencePeer,
  node: string,
  timeout_sec: number,
  config?: { ttl?: number }
): Promise<void>;
export function set_timeout(...args: any) {
  let peer: FluencePeer;
  let node: any;
  let timeout_sec: any;
  let config: any;
  if (FluencePeer.isInstance(args[0])) {
    peer = args[0];
    node = args[1];
    timeout_sec = args[2];
    config = args[3];
  } else {
    peer = FluencePeer.default;
    node = args[0];
    timeout_sec = args[1];
    config = args[2];
  }

  let request: RequestFlow;
  const promise = new Promise<void>((resolve, reject) => {
    const r = new RequestFlowBuilder()
      .disableInjections()
      .withRawScript(
        `
     (xor
 (seq
  (seq
   (seq
    (seq
     (call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
     (call %init_peer_id% ("getDataSrv" "node") [] node)
    )
    (call %init_peer_id% ("getDataSrv" "timeout_sec") [] timeout_sec)
   )
   (call -relay- ("op" "noop") [])
  )
  (xor
   (call node ("aqua-ipfs" "set_timeout") [timeout_sec])
   (seq
    (call -relay- ("op" "noop") [])
    (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
   )
  )
 )
 (seq
  (call -relay- ("op" "noop") [])
  (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
 )
)

                 `
      )
      .configHandler((h) => {
        h.on("getDataSrv", "-relay-", () => {
          return peer.connectionInfo.connectedRelay;
        });
        h.on("getDataSrv", "node", () => {
          return node;
        });
        h.on("getDataSrv", "timeout_sec", () => {
          return timeout_sec;
        });
        h.onEvent("callbackSrv", "response", (args) => {});

        h.onEvent("errorHandlingSrv", "error", (args) => {
          const [err] = args;
          reject(err);
        });
      })
      .handleScriptError(reject)
      .handleTimeout(() => {
        reject("Request timed out for set_timeout");
      });
    if (config && config.ttl) {
      r.withTTL(config.ttl);
    }
    request = r.build();
  });
  peer.internals.initiateFlow(request!);
  return Promise.race([promise, Promise.resolve()]);
}

export function get_and_cache(
  node: string,
  cid: string,
  from: string,
  error: (
    arg0: string,
    arg1: string,
    callParams: CallParams<"arg0" | "arg1">
  ) => void,
  config?: { ttl?: number }
): Promise<string | null>;
export function get_and_cache(
  peer: FluencePeer,
  node: string,
  cid: string,
  from: string,
  error: (
    arg0: string,
    arg1: string,
    callParams: CallParams<"arg0" | "arg1">
  ) => void,
  config?: { ttl?: number }
): Promise<string | null>;
export function get_and_cache(...args: any) {
  let peer: FluencePeer;
  let node: any;
  let cid: any;
  let from: any;
  let error: any;
  let config: any;
  if (FluencePeer.isInstance(args[0])) {
    peer = args[0];
    node = args[1];
    cid = args[2];
    from = args[3];
    error = args[4];
    config = args[5];
  } else {
    peer = FluencePeer.default;
    node = args[0];
    cid = args[1];
    from = args[2];
    error = args[3];
    config = args[4];
  }

  let request: RequestFlow;
  const promise = new Promise<string | null>((resolve, reject) => {
    const r = new RequestFlowBuilder()
      .disableInjections()
      .withRawScript(
        `
     (xor
 (seq
  (seq
   (seq
    (seq
     (seq
      (seq
       (seq
        (call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
        (call %init_peer_id% ("getDataSrv" "node") [] node)
       )
       (call %init_peer_id% ("getDataSrv" "cid") [] cid)
      )
      (call %init_peer_id% ("getDataSrv" "from") [] from)
     )
     (call -relay- ("op" "noop") [])
    )
    (xor
     (seq
      (call node ("aqua-ipfs" "get_from") [cid from] get)
      (xor
       (match get.$.success! true
        (xor
         (seq
          (call node ("aqua-ipfs" "put") [get.$.path!] put)
          (xor
           (match put.$.success! true
            (xor
             (ap put.$.hash! $localCid)
             (seq
              (call -relay- ("op" "noop") [])
              (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
             )
            )
           )
           (par
            (seq
             (call -relay- ("op" "noop") [])
             (xor
              (call %init_peer_id% ("callbackSrv" "error") ["Ipfs.put failed" put.$.error!])
              (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
             )
            )
            (null)
           )
          )
         )
         (seq
          (call -relay- ("op" "noop") [])
          (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
         )
        )
       )
       (par
        (seq
         (call -relay- ("op" "noop") [])
         (xor
          (call %init_peer_id% ("callbackSrv" "error") ["Ipfs.get failed" get.$.error!])
          (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 4])
         )
        )
        (null)
       )
      )
     )
     (seq
      (call -relay- ("op" "noop") [])
      (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 5])
     )
    )
   )
   (call -relay- ("op" "noop") [])
  )
  (xor
   (call %init_peer_id% ("callbackSrv" "response") [$localCid])
   (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 6])
  )
 )
 (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 7])
)

                 `
      )
      .configHandler((h) => {
        h.on("getDataSrv", "-relay-", () => {
          return peer.connectionInfo.connectedRelay;
        });
        h.on("getDataSrv", "node", () => {
          return node;
        });
        h.on("getDataSrv", "cid", () => {
          return cid;
        });
        h.on("getDataSrv", "from", () => {
          return from;
        });

        h.use((req, resp, next) => {
          if (req.serviceId === "callbackSrv" && req.fnName === "error") {
            const callParams = {
              ...req.particleContext,
              tetraplets: {
                arg0: req.tetraplets[0],
                arg1: req.tetraplets[1],
              },
            };
            resp.retCode = ResultCodes.success;
            error(req.args[0], req.args[1], callParams);
            resp.result = {};
          }
          next();
        });

        h.onEvent("callbackSrv", "response", (args) => {
          let [opt] = args;
          if (Array.isArray(opt)) {
            if (opt.length === 0) {
              resolve(null);
            }
            opt = opt[0];
          }
          return resolve(opt);
        });

        h.onEvent("errorHandlingSrv", "error", (args) => {
          const [err] = args;
          reject(err);
        });
      })
      .handleScriptError(reject)
      .handleTimeout(() => {
        reject("Request timed out for get_and_cache");
      });
    if (config && config.ttl) {
      r.withTTL(config.ttl);
    }
    request = r.build();
  });
  peer.internals.initiateFlow(request!);
  return promise;
}

export function get_from(
  node: string,
  cid: string,
  from: string,
  config?: { ttl?: number }
): Promise<{ error: string; path: string; success: boolean }>;
export function get_from(
  peer: FluencePeer,
  node: string,
  cid: string,
  from: string,
  config?: { ttl?: number }
): Promise<{ error: string; path: string; success: boolean }>;
export function get_from(...args: any) {
  let peer: FluencePeer;
  let node: any;
  let cid: any;
  let from: any;
  let config: any;
  if (FluencePeer.isInstance(args[0])) {
    peer = args[0];
    node = args[1];
    cid = args[2];
    from = args[3];
    config = args[4];
  } else {
    peer = FluencePeer.default;
    node = args[0];
    cid = args[1];
    from = args[2];
    config = args[3];
  }

  let request: RequestFlow;
  const promise = new Promise<{
    error: string;
    path: string;
    success: boolean;
  }>((resolve, reject) => {
    const r = new RequestFlowBuilder()
      .disableInjections()
      .withRawScript(
        `
     (xor
 (seq
  (seq
   (seq
    (seq
     (seq
      (seq
       (seq
        (call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
        (call %init_peer_id% ("getDataSrv" "node") [] node)
       )
       (call %init_peer_id% ("getDataSrv" "cid") [] cid)
      )
      (call %init_peer_id% ("getDataSrv" "from") [] from)
     )
     (call -relay- ("op" "noop") [])
    )
    (xor
     (call node ("aqua-ipfs" "get_from") [cid from] result)
     (seq
      (call -relay- ("op" "noop") [])
      (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
     )
    )
   )
   (call -relay- ("op" "noop") [])
  )
  (xor
   (call %init_peer_id% ("callbackSrv" "response") [result])
   (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
  )
 )
 (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
)

                 `
      )
      .configHandler((h) => {
        h.on("getDataSrv", "-relay-", () => {
          return peer.connectionInfo.connectedRelay;
        });
        h.on("getDataSrv", "node", () => {
          return node;
        });
        h.on("getDataSrv", "cid", () => {
          return cid;
        });
        h.on("getDataSrv", "from", () => {
          return from;
        });
        h.onEvent("callbackSrv", "response", (args) => {
          const [res] = args;
          resolve(res);
        });

        h.onEvent("errorHandlingSrv", "error", (args) => {
          const [err] = args;
          reject(err);
        });
      })
      .handleScriptError(reject)
      .handleTimeout(() => {
        reject("Request timed out for get_from");
      });
    if (config && config.ttl) {
      r.withTTL(config.ttl);
    }
    request = r.build();
  });
  peer.internals.initiateFlow(request!);
  return promise;
}

export function get_local_api_multiaddr(
  node: string,
  config?: { ttl?: number }
): Promise<{ error: string; multiaddr: string; success: boolean }>;
export function get_local_api_multiaddr(
  peer: FluencePeer,
  node: string,
  config?: { ttl?: number }
): Promise<{ error: string; multiaddr: string; success: boolean }>;
export function get_local_api_multiaddr(...args: any) {
  let peer: FluencePeer;
  let node: any;
  let config: any;
  if (FluencePeer.isInstance(args[0])) {
    peer = args[0];
    node = args[1];
    config = args[2];
  } else {
    peer = FluencePeer.default;
    node = args[0];
    config = args[1];
  }

  let request: RequestFlow;
  const promise = new Promise<{
    error: string;
    multiaddr: string;
    success: boolean;
  }>((resolve, reject) => {
    const r = new RequestFlowBuilder()
      .disableInjections()
      .withRawScript(
        `
     (xor
 (seq
  (seq
   (seq
    (seq
     (seq
      (call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
      (call %init_peer_id% ("getDataSrv" "node") [] node)
     )
     (call -relay- ("op" "noop") [])
    )
    (xor
     (call node ("aqua-ipfs" "get_local_api_multiaddr") [] result)
     (seq
      (call -relay- ("op" "noop") [])
      (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
     )
    )
   )
   (call -relay- ("op" "noop") [])
  )
  (xor
   (call %init_peer_id% ("callbackSrv" "response") [result])
   (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
  )
 )
 (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
)

                 `
      )
      .configHandler((h) => {
        h.on("getDataSrv", "-relay-", () => {
          return peer.connectionInfo.connectedRelay;
        });
        h.on("getDataSrv", "node", () => {
          return node;
        });
        h.onEvent("callbackSrv", "response", (args) => {
          const [res] = args;
          resolve(res);
        });

        h.onEvent("errorHandlingSrv", "error", (args) => {
          const [err] = args;
          reject(err);
        });
      })
      .handleScriptError(reject)
      .handleTimeout(() => {
        reject("Request timed out for get_local_api_multiaddr");
      });
    if (config && config.ttl) {
      r.withTTL(config.ttl);
    }
    request = r.build();
  });
  peer.internals.initiateFlow(request!);
  return promise;
}

export function get_external_swarm_multiaddr(
  node: string,
  config?: { ttl?: number }
): Promise<{ error: string; multiaddr: string; success: boolean }>;
export function get_external_swarm_multiaddr(
  peer: FluencePeer,
  node: string,
  config?: { ttl?: number }
): Promise<{ error: string; multiaddr: string; success: boolean }>;
export function get_external_swarm_multiaddr(...args: any) {
  let peer: FluencePeer;
  let node: any;
  let config: any;
  if (FluencePeer.isInstance(args[0])) {
    peer = args[0];
    node = args[1];
    config = args[2];
  } else {
    peer = FluencePeer.default;
    node = args[0];
    config = args[1];
  }

  let request: RequestFlow;
  const promise = new Promise<{
    error: string;
    multiaddr: string;
    success: boolean;
  }>((resolve, reject) => {
    const r = new RequestFlowBuilder()
      .disableInjections()
      .withRawScript(
        `
     (xor
 (seq
  (seq
   (seq
    (seq
     (seq
      (call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
      (call %init_peer_id% ("getDataSrv" "node") [] node)
     )
     (call -relay- ("op" "noop") [])
    )
    (xor
     (call node ("aqua-ipfs" "get_external_swarm_multiaddr") [] result)
     (seq
      (call -relay- ("op" "noop") [])
      (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
     )
    )
   )
   (call -relay- ("op" "noop") [])
  )
  (xor
   (call %init_peer_id% ("callbackSrv" "response") [result])
   (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
  )
 )
 (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
)

                 `
      )
      .configHandler((h) => {
        h.on("getDataSrv", "-relay-", () => {
          return peer.connectionInfo.connectedRelay;
        });
        h.on("getDataSrv", "node", () => {
          return node;
        });
        h.onEvent("callbackSrv", "response", (args) => {
          const [res] = args;
          resolve(res);
        });

        h.onEvent("errorHandlingSrv", "error", (args) => {
          const [err] = args;
          reject(err);
        });
      })
      .handleScriptError(reject)
      .handleTimeout(() => {
        reject("Request timed out for get_external_swarm_multiaddr");
      });
    if (config && config.ttl) {
      r.withTTL(config.ttl);
    }
    request = r.build();
  });
  peer.internals.initiateFlow(request!);
  return promise;
}

export function put(
  node: string,
  path: string,
  config?: { ttl?: number }
): Promise<{ error: string; hash: string; success: boolean }>;
export function put(
  peer: FluencePeer,
  node: string,
  path: string,
  config?: { ttl?: number }
): Promise<{ error: string; hash: string; success: boolean }>;
export function put(...args: any) {
  let peer: FluencePeer;
  let node: any;
  let path: any;
  let config: any;
  if (FluencePeer.isInstance(args[0])) {
    peer = args[0];
    node = args[1];
    path = args[2];
    config = args[3];
  } else {
    peer = FluencePeer.default;
    node = args[0];
    path = args[1];
    config = args[2];
  }

  let request: RequestFlow;
  const promise = new Promise<{
    error: string;
    hash: string;
    success: boolean;
  }>((resolve, reject) => {
    const r = new RequestFlowBuilder()
      .disableInjections()
      .withRawScript(
        `
     (xor
 (seq
  (seq
   (seq
    (seq
     (seq
      (seq
       (call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
       (call %init_peer_id% ("getDataSrv" "node") [] node)
      )
      (call %init_peer_id% ("getDataSrv" "path") [] path)
     )
     (call -relay- ("op" "noop") [])
    )
    (xor
     (call node ("aqua-ipfs" "put") [path] result)
     (seq
      (call -relay- ("op" "noop") [])
      (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
     )
    )
   )
   (call -relay- ("op" "noop") [])
  )
  (xor
   (call %init_peer_id% ("callbackSrv" "response") [result])
   (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
  )
 )
 (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
)

                 `
      )
      .configHandler((h) => {
        h.on("getDataSrv", "-relay-", () => {
          return peer.connectionInfo.connectedRelay;
        });
        h.on("getDataSrv", "node", () => {
          return node;
        });
        h.on("getDataSrv", "path", () => {
          return path;
        });
        h.onEvent("callbackSrv", "response", (args) => {
          const [res] = args;
          resolve(res);
        });

        h.onEvent("errorHandlingSrv", "error", (args) => {
          const [err] = args;
          reject(err);
        });
      })
      .handleScriptError(reject)
      .handleTimeout(() => {
        reject("Request timed out for put");
      });
    if (config && config.ttl) {
      r.withTTL(config.ttl);
    }
    request = r.build();
  });
  peer.internals.initiateFlow(request!);
  return promise;
}

export function get_external_api_multiaddr(
  node: string,
  config?: { ttl?: number }
): Promise<{ error: string; multiaddr: string; success: boolean }>;
export function get_external_api_multiaddr(
  peer: FluencePeer,
  node: string,
  config?: { ttl?: number }
): Promise<{ error: string; multiaddr: string; success: boolean }>;
export function get_external_api_multiaddr(...args: any) {
  let peer: FluencePeer;
  let node: any;
  let config: any;
  if (FluencePeer.isInstance(args[0])) {
    peer = args[0];
    node = args[1];
    config = args[2];
  } else {
    peer = FluencePeer.default;
    node = args[0];
    config = args[1];
  }

  let request: RequestFlow;
  const promise = new Promise<{
    error: string;
    multiaddr: string;
    success: boolean;
  }>((resolve, reject) => {
    const r = new RequestFlowBuilder()
      .disableInjections()
      .withRawScript(
        `
     (xor
 (seq
  (seq
   (seq
    (seq
     (seq
      (call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
      (call %init_peer_id% ("getDataSrv" "node") [] node)
     )
     (call -relay- ("op" "noop") [])
    )
    (xor
     (call node ("aqua-ipfs" "get_external_api_multiaddr") [] result)
     (seq
      (call -relay- ("op" "noop") [])
      (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
     )
    )
   )
   (call -relay- ("op" "noop") [])
  )
  (xor
   (call %init_peer_id% ("callbackSrv" "response") [result])
   (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
  )
 )
 (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
)

                 `
      )
      .configHandler((h) => {
        h.on("getDataSrv", "-relay-", () => {
          return peer.connectionInfo.connectedRelay;
        });
        h.on("getDataSrv", "node", () => {
          return node;
        });
        h.onEvent("callbackSrv", "response", (args) => {
          const [res] = args;
          resolve(res);
        });

        h.onEvent("errorHandlingSrv", "error", (args) => {
          const [err] = args;
          reject(err);
        });
      })
      .handleScriptError(reject)
      .handleTimeout(() => {
        reject("Request timed out for get_external_api_multiaddr");
      });
    if (config && config.ttl) {
      r.withTTL(config.ttl);
    }
    request = r.build();
  });
  peer.internals.initiateFlow(request!);
  return promise;
}
