/**
 *
 * 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-222
 *
 */
import { FluencePeer } from "@fluencelabs/fluence";
import {
  ResultCodes,
  RequestFlow,
  RequestFlowBuilder,
  CallParams,
} from "@fluencelabs/fluence/dist/internal/compilerSupport/v1";

// Services

// Functions

export function demoCalculation(config?: { ttl?: number }): Promise<number>;
export function demoCalculation(
  peer: FluencePeer,
  config?: { ttl?: number }
): Promise<number>;
export function demoCalculation(...args) {
  let peer: FluencePeer;

  let config;
  if (FluencePeer.isInstance(args[0])) {
    peer = args[0];
    config = args[1];
  } else {
    peer = FluencePeer.default;
    config = args[0];
  }

  let request: RequestFlow;
  const promise = new Promise<number>((resolve, reject) => {
    const r = new RequestFlowBuilder()
      .disableInjections()
      .withRawScript(
        `
     (xor
 (seq
  (seq
   (seq
    (seq
     (seq
      (seq
       (call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
       (call -relay- ("op" "noop") [])
      )
      (call "12D3KooWSD5PToNiLQwKDXsu8JSysCwUt8BVUJEqCHcDe7P5h45e" ("op" "noop") [])
     )
     (xor
      (seq
       (seq
        (seq
         (seq
          (call "12D3KooWLBkw4Tz8bRoSriy5WEpHyWfU11jEK3b5yCa7FBRDRWH3" ("calc" "add") [10])
          (call "12D3KooWLBkw4Tz8bRoSriy5WEpHyWfU11jEK3b5yCa7FBRDRWH3" ("calc" "multiply") [5])
         )
         (call "12D3KooWLBkw4Tz8bRoSriy5WEpHyWfU11jEK3b5yCa7FBRDRWH3" ("calc" "subtract") [8])
        )
        (call "12D3KooWLBkw4Tz8bRoSriy5WEpHyWfU11jEK3b5yCa7FBRDRWH3" ("calc" "divide") [6])
       )
       (call "12D3KooWLBkw4Tz8bRoSriy5WEpHyWfU11jEK3b5yCa7FBRDRWH3" ("calc" "getResult") [] res)
      )
      (seq
       (seq
        (seq
         (call "12D3KooWSD5PToNiLQwKDXsu8JSysCwUt8BVUJEqCHcDe7P5h45e" ("op" "noop") [])
         (call -relay- ("op" "noop") [])
        )
        (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
       )
       (call -relay- ("op" "noop") [])
      )
     )
    )
    (call "12D3KooWSD5PToNiLQwKDXsu8JSysCwUt8BVUJEqCHcDe7P5h45e" ("op" "noop") [])
   )
   (call -relay- ("op" "noop") [])
  )
  (xor
   (call %init_peer_id% ("callbackSrv" "response") [res])
   (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.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 demoCalculation");
      });
    if (config && config.ttl) {
      r.withTTL(config.ttl);
    }
    request = r.build();
  });
  peer.internals.initiateFlow(request!);
  return promise;
}
