var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
  function adopt(value) {
    return value instanceof P ? value : new P(function (resolve) {
      resolve(value);
    });
  }

  return new (P || (P = Promise))(function (resolve, reject) {
    function fulfilled(value) {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    }

    function rejected(value) {
      try {
        step(generator["throw"](value));
      } catch (e) {
        reject(e);
      }
    }

    function step(result) {
      result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
    }

    step((generator = generator.apply(thisArg, _arguments || [])).next());
  });
};

import { http } from "../../utils/http.js";
const chequebookEndpoint = 'chequebook';
/**
 * Get the address of the chequebook contract used
 *
 * @param ky Ky debug instance
 */

export function getChequebookAddress(ky) {
  return __awaiter(this, void 0, void 0, function* () {
    const response = yield http(ky, {
      path: chequebookEndpoint + '/address',
      responseType: 'json'
    });
    return response.data;
  });
}
/**
 * Get the balance of the chequebook
 *
 * @param ky Ky debug instance
 */

export function getChequebookBalance(ky) {
  return __awaiter(this, void 0, void 0, function* () {
    const response = yield http(ky, {
      path: chequebookEndpoint + '/balance',
      responseType: 'json'
    });
    return response.data;
  });
}
/**
 * Get last cashout action for the peer
 *
 * @param ky Ky debug instance
 * @param peer  Swarm address of peer
 */

export function getLastCashoutAction(ky, peer) {
  return __awaiter(this, void 0, void 0, function* () {
    const response = yield http(ky, {
      path: chequebookEndpoint + `/cashout/${peer}`,
      responseType: 'json'
    });
    return response.data;
  });
}
/**
 * Cashout the last cheque for the peer
 *
 * @param ky Ky debug instance
 * @param peer  Swarm address of peer
 * @param options
 */

export function cashoutLastCheque(ky, peer, options) {
  return __awaiter(this, void 0, void 0, function* () {
    const headers = {};

    if (options === null || options === void 0 ? void 0 : options.gasPrice) {
      headers['gas-price'] = options.gasPrice.toString();
    }

    if (options === null || options === void 0 ? void 0 : options.gasLimit) {
      headers['gas-limit'] = options.gasLimit.toString();
    }

    const response = yield http(ky, {
      method: 'post',
      path: chequebookEndpoint + `/cashout/${peer}`,
      responseType: 'json',
      headers
    });
    return response.data.transactionHash;
  });
}
/**
 * Get last cheques for the peer
 *
 * @param ky Ky debug instance
 * @param peer  Swarm address of peer
 */

export function getLastChequesForPeer(ky, peer) {
  return __awaiter(this, void 0, void 0, function* () {
    const response = yield http(ky, {
      path: chequebookEndpoint + `/cheque/${peer}`,
      responseType: 'json'
    });
    return response.data;
  });
}
/**
 * Get last cheques for all peers
 *
 * @param ky Ky debug instance
 */

export function getLastCheques(ky) {
  return __awaiter(this, void 0, void 0, function* () {
    const response = yield http(ky, {
      path: chequebookEndpoint + '/cheque',
      responseType: 'json'
    });
    return response.data;
  });
}
/**
 * Deposit tokens from overlay address into chequebook
 *
 * @param ky Ky debug instance
 * @param amount   Amount of tokens to deposit
 * @param gasPrice Gas Price in WEI for the transaction call
 * @return string  Hash of the transaction
 */

export function depositTokens(ky, amount, gasPrice) {
  return __awaiter(this, void 0, void 0, function* () {
    const headers = {};

    if (gasPrice) {
      headers['gas-price'] = gasPrice.toString();
    }

    const response = yield http(ky, {
      method: 'post',
      path: chequebookEndpoint + '/deposit',
      responseType: 'json',
      searchParams: {
        amount: amount.toString(10)
      },
      headers
    });
    return response.data.transactionHash;
  });
}
/**
 * Withdraw tokens from the chequebook to the overlay address
 *
 * @param ky Ky debug instance
 * @param amount   Amount of tokens to withdraw
 * @param gasPrice Gas Price in WEI for the transaction call
 * @return string  Hash of the transaction
 */

export function withdrawTokens(ky, amount, gasPrice) {
  return __awaiter(this, void 0, void 0, function* () {
    const headers = {};

    if (gasPrice) {
      headers['gas-price'] = gasPrice.toString();
    }

    const response = yield http(ky, {
      method: 'post',
      path: chequebookEndpoint + '/withdraw',
      responseType: 'json',
      searchParams: {
        amount: amount.toString(10)
      },
      headers
    });
    return response.data.transactionHash;
  });
}