/**
 *
 * 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

export interface StringOpDef {
  array: (s: string, callParams: CallParams<"s">) => string[];
}

export function registerStringOp(service: StringOpDef): void;
export function registerStringOp(serviceId: string, service: StringOpDef): void;
export function registerStringOp(peer: FluencePeer, service: StringOpDef): void;
export function registerStringOp(
  peer: FluencePeer,
  serviceId: string,
  service: StringOpDef
): void;
export function registerStringOp(...args: any) {
  let peer: FluencePeer;
  let serviceId: any;
  let service: any;
  if (FluencePeer.isInstance(args[0])) {
    peer = args[0];
  } else {
    peer = FluencePeer.default;
  }

  if (typeof args[0] === "string") {
    serviceId = args[0];
  } else if (typeof args[1] === "string") {
    serviceId = args[1];
  } else {
    serviceId = "op";
  }

  if (!FluencePeer.isInstance(args[0]) && typeof args[0] === "object") {
    service = args[0];
  } else if (typeof args[1] === "object") {
    service = args[1];
  } else {
    service = args[2];
  }

  peer.internals.callServiceHandler.use((req, resp, next) => {
    if (req.serviceId !== serviceId) {
      next();
      return;
    }

    if (req.fnName === "array") {
      const callParams = {
        ...req.particleContext,
        tetraplets: {
          s: req.tetraplets[0],
        },
      };
      resp.retCode = ResultCodes.success;
      resp.result = service.array(req.args[0], callParams);
    }

    next();
  });
}

// Functions

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

  let request: RequestFlow;
  const promise = new Promise<string>((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" "module_hash") [] module_hash)
      )
      (call %init_peer_id% ("op" "concat_strings") ["hash:" module_hash] prefixed_hash)
     )
     (call %init_peer_id% ("op" "array") [prefixed_hash] dependencies)
    )
    (call %init_peer_id% ("dist" "make_blueprint") ["process_files" dependencies] blueprint)
   )
   (call %init_peer_id% ("dist" "add_blueprint") [blueprint] blueprint_id)
  )
  (xor
   (call %init_peer_id% ("callbackSrv" "response") [blueprint_id])
   (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
  )
 )
 (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
)

                 `
      )
      .configHandler((h) => {
        h.on("getDataSrv", "-relay-", () => {
          return peer.connectionInfo.connectedRelay;
        });
        h.on("getDataSrv", "module_hash", () => {
          return module_hash;
        });
        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 add_blueprint");
      });
    if (config && config.ttl) {
      r.withTTL(config.ttl);
    }
    request = r.build();
  });
  peer.internals.initiateFlow(request!);
  return promise;
}

export function put_file_size(
  relay: string,
  cid: string,
  ipfs: string,
  service_id: string,
  logSize: (arg0: number, callParams: CallParams<"arg0">) => void,
  error: (
    arg0: string,
    arg1: string,
    callParams: CallParams<"arg0" | "arg1">
  ) => void,
  config?: { ttl?: number }
): Promise<{ error: string; hash: string; success: boolean } | null>;
export function put_file_size(
  peer: FluencePeer,
  relay: string,
  cid: string,
  ipfs: string,
  service_id: string,
  logSize: (arg0: number, callParams: CallParams<"arg0">) => void,
  error: (
    arg0: string,
    arg1: string,
    callParams: CallParams<"arg0" | "arg1">
  ) => void,
  config?: { ttl?: number }
): Promise<{ error: string; hash: string; success: boolean } | null>;
export function put_file_size(...args: any) {
  let peer: FluencePeer;
  let relay: any;
  let cid: any;
  let ipfs: any;
  let service_id: any;
  let logSize: any;
  let error: any;
  let config: any;
  if (FluencePeer.isInstance(args[0])) {
    peer = args[0];
    relay = args[1];
    cid = args[2];
    ipfs = args[3];
    service_id = args[4];
    logSize = args[5];
    error = args[6];
    config = args[7];
  } else {
    peer = FluencePeer.default;
    relay = args[0];
    cid = args[1];
    ipfs = args[2];
    service_id = args[3];
    logSize = args[4];
    error = args[5];
    config = args[6];
  }

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

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

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

        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 put_file_size");
      });
    if (config && config.ttl) {
      r.withTTL(config.ttl);
    }
    request = r.build();
  });
  peer.internals.initiateFlow(request!);
  return promise;
}

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

  let request: RequestFlow;
  const promise = new Promise<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" "relay") [] relay)
      )
      (call %init_peer_id% ("getDataSrv" "service_id") [] service_id)
     )
     (call -relay- ("op" "noop") [])
    )
    (xor
     (call relay ("srv" "remove") [service_id])
     (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") [true])
   (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", "relay", () => {
          return relay;
        });
        h.on("getDataSrv", "service_id", () => {
          return service_id;
        });
        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 remove_service");
      });
    if (config && config.ttl) {
      r.withTTL(config.ttl);
    }
    request = r.build();
  });
  peer.internals.initiateFlow(request!);
  return promise;
}

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

  let request: RequestFlow;
  const promise = new Promise<string>((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" "name") [] name)
     )
     (call %init_peer_id% ("getDataSrv" "path") [] path)
    )
    (call %init_peer_id% ("dist" "default_module_config") [name] config)
   )
   (call %init_peer_id% ("dist" "add_module_from_vault") [path config] module_hash)
  )
  (xor
   (call %init_peer_id% ("callbackSrv" "response") [module_hash])
   (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
  )
 )
 (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
)

                 `
      )
      .configHandler((h) => {
        h.on("getDataSrv", "-relay-", () => {
          return peer.connectionInfo.connectedRelay;
        });
        h.on("getDataSrv", "name", () => {
          return name;
        });
        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 add_module");
      });
    if (config && config.ttl) {
      r.withTTL(config.ttl);
    }
    request = r.build();
  });
  peer.internals.initiateFlow(request!);
  return promise;
}

export function deploy_service(
  relay: string,
  cid: string,
  ipfs: string,
  error: (
    arg0: string,
    arg1: string,
    callParams: CallParams<"arg0" | "arg1">
  ) => void,
  config?: { ttl?: number }
): Promise<string | null>;
export function deploy_service(
  peer: FluencePeer,
  relay: string,
  cid: string,
  ipfs: string,
  error: (
    arg0: string,
    arg1: string,
    callParams: CallParams<"arg0" | "arg1">
  ) => void,
  config?: { ttl?: number }
): Promise<string | null>;
export function deploy_service(...args: any) {
  let peer: FluencePeer;
  let relay: any;
  let cid: any;
  let ipfs: any;
  let error: any;
  let config: any;
  if (FluencePeer.isInstance(args[0])) {
    peer = args[0];
    relay = args[1];
    cid = args[2];
    ipfs = args[3];
    error = args[4];
    config = args[5];
  } else {
    peer = FluencePeer.default;
    relay = args[0];
    cid = args[1];
    ipfs = 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" "relay") [] relay)
       )
       (call %init_peer_id% ("getDataSrv" "cid") [] cid)
      )
      (call %init_peer_id% ("getDataSrv" "ipfs") [] ipfs)
     )
     (call -relay- ("op" "noop") [])
    )
    (xor
     (seq
      (call relay ("aqua-ipfs" "get_from") [cid ipfs] get_result)
      (xor
       (match get_result.$.success! true
        (xor
         (seq
          (seq
           (seq
            (seq
             (seq
              (seq
               (call relay ("dist" "default_module_config") ["process_files"] config)
               (call relay ("dist" "add_module_from_vault") [get_result.$.path! config] module_hash)
              )
              (call relay ("op" "concat_strings") ["hash:" module_hash] prefixed_hash)
             )
             (call relay ("op" "array") [prefixed_hash] dependencies)
            )
            (call relay ("dist" "make_blueprint") ["process_files" dependencies] blueprint)
           )
           (call relay ("dist" "add_blueprint") [blueprint] blueprint_id)
          )
          (call relay ("srv" "create") [blueprint_id] $service_id)
         )
         (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.get_from failed" get_result.$.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])
     )
    )
   )
   (call -relay- ("op" "noop") [])
  )
  (xor
   (call %init_peer_id% ("callbackSrv" "response") [$service_id])
   (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 4])
  )
 )
 (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 5])
)

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

        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 deploy_service");
      });
    if (config && config.ttl) {
      r.withTTL(config.ttl);
    }
    request = r.build();
  });
  peer.internals.initiateFlow(request!);
  return promise;
}
