/**
 *
 * 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 ProcessFilesDef {
  file_size: (
    file_path: string,
    callParams: CallParams<"file_path">
  ) => { error: string; size: number; success: boolean };
  write_file_size: (
    size: number,
    callParams: CallParams<"size">
  ) => { error: string; path: string; success: boolean };
}

export function registerProcessFiles(
  serviceId: string,
  service: ProcessFilesDef
): void;
export function registerProcessFiles(
  peer: FluencePeer,
  serviceId: string,
  service: ProcessFilesDef
): void;
export function registerProcessFiles(...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];
  }

  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 === "file_size") {
      const callParams = {
        ...req.particleContext,
        tetraplets: {
          file_path: req.tetraplets[0],
        },
      };
      resp.retCode = ResultCodes.success;
      resp.result = service.file_size(req.args[0], callParams);
    }

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

    next();
  });
}

// Functions
