"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
var _exportNames = {
  DapiConfigurations: true,
  DapiConnection: true,
  DapiPair: true,
  DapiLineAddress: true,
  DapiBeneficiary: true,
  DapiWireBeneficiary: true,
  DapiAccount: true,
  DapiCardBalance: true,
  DapiCard: true
};
exports.default = exports.DapiWireBeneficiary = exports.DapiPair = exports.DapiLineAddress = exports.DapiConnection = exports.DapiConfigurations = exports.DapiCardBalance = exports.DapiCard = exports.DapiBeneficiary = exports.DapiAccount = void 0;

var _nativeInterface = _interopRequireDefault(require("./internal/nativeInterface"));

var _types = require("./internal/types");

Object.keys(_types).forEach(function (key) {
  if (key === "default" || key === "__esModule") return;
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
  if (key in exports && exports[key] === _types[key]) return;
  Object.defineProperty(exports, key, {
    enumerable: true,
    get: function () {
      return _types[key];
    }
  });
});

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

class DapiConfigurations {
  constructor(countries) {
    let environment = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _types.DapiEnvironment.production;

    _defineProperty(this, "environment", void 0);

    _defineProperty(this, "countries", void 0);

    _defineProperty(this, "endpoints", void 0);

    _defineProperty(this, "endPointExtraQueryItems", void 0);

    _defineProperty(this, "endPointExtraHeaderFields", void 0);

    _defineProperty(this, "endPointExtraBody", void 0);

    _defineProperty(this, "showLogos", void 0);

    _defineProperty(this, "showCloseButton", void 0);

    _defineProperty(this, "showAddButton", void 0);

    _defineProperty(this, "showTransferSuccessfulResult", void 0);

    _defineProperty(this, "showTransferErrorResult", void 0);

    _defineProperty(this, "showExperimentalBanks", void 0);

    _defineProperty(this, "postSuccessfulConnectionLoadingText", void 0);

    this.environment = environment;
    this.countries = countries;
  }

}

exports.DapiConfigurations = DapiConfigurations;

class TransferResponse {
  constructor(amount, operationID, account) {
    _defineProperty(this, "account", void 0);

    _defineProperty(this, "amount", void 0);

    _defineProperty(this, "operationID", void 0);

    this.account = account;
    this.amount = amount;
    this.operationID = operationID;
  }

}

class DapiConnection {
  get clientUserID() {
    return this._clientUserID;
  }

  get userID() {
    return this._userID;
  }

  get bankID() {
    return this._bankID;
  }

  get swiftCode() {
    return this._swiftCode;
  }

  get country() {
    return this._country;
  }

  get bankShortName() {
    return this._bankShortName;
  }

  get bankFullName() {
    return this._bankFullName;
  }

  get accounts() {
    return this._accounts;
  }

  get cards() {
    return this._cards;
  }

  get fullLogo() {
    return this._fullLogo;
  }

  get halfLogo() {
    return this._halfLogo;
  }

  get miniLogo() {
    return this._miniLogo;
  }

  static async create(jsonConnectionDetails) {
    var promise = new Promise(async (resolve, reject) => {
      const isStarted = await Dapi.instance.isStarted();

      if (!isStarted) {
        reject("Dapi SDK is not started yet. It's not permitted to call create() (or any other method) on Dapi SDK unless started.");
        return;
      }

      _nativeInterface.default.createConnection(jsonConnectionDetails).then(newJSONConnection => {
        Dapi.instance.getConnections().then(connections => {
          let isResolved = false;

          for (let index = 0; index < connections.length; index++) {
            const element = connections[index];

            if (element.userID === newJSONConnection.userID) {
              isResolved = true;
              resolve(element);
              break;
            }
          }

          if (!isResolved) {
            reject('matchingConnection is undefined');
          }
        }).catch(getConnectionsError => {
          reject(getConnectionsError);
        });
      }).catch(error => {
        reject(error);
      });
    });
    return promise;
  }

  constructor(clientUserID, userID, bankID, swiftCode, country, bankShortName, bankFullName, accounts, cards, fullLogo, halfLogo, miniLogo) {
    _defineProperty(this, "_clientUserID", void 0);

    _defineProperty(this, "_userID", void 0);

    _defineProperty(this, "_bankID", void 0);

    _defineProperty(this, "_swiftCode", void 0);

    _defineProperty(this, "_country", void 0);

    _defineProperty(this, "_bankShortName", void 0);

    _defineProperty(this, "_bankFullName", void 0);

    _defineProperty(this, "_accounts", void 0);

    _defineProperty(this, "_cards", void 0);

    _defineProperty(this, "_fullLogo", void 0);

    _defineProperty(this, "_halfLogo", void 0);

    _defineProperty(this, "_miniLogo", void 0);

    this._clientUserID = clientUserID;
    this._userID = userID;
    this._bankID = bankID;
    this._swiftCode = swiftCode;
    this._country = country;
    this._bankShortName = bankShortName;
    this._bankFullName = bankFullName;
    this._accounts = accounts;
    this._cards = cards;
    this._fullLogo = fullLogo;
    this._halfLogo = halfLogo;
    this._miniLogo = miniLogo;
  }

  getParameters() {
    return _nativeInterface.default.getConnectionParameters(this.userID);
  }

  getIdentity() {
    return _nativeInterface.default.getIdentity(this.userID);
  }

  getAccounts() {
    return _nativeInterface.default.getAccounts(this.userID);
  }

  getCards() {
    return _nativeInterface.default.getCards(this.userID);
  }

  getTransactionsForAccount(account, startDate, endDate) {
    return _nativeInterface.default.getTransactionsForAccount(this.userID, account.id, startDate.getTime(), endDate.getTime());
  }

  getTransactionsForCard(card, startDate, endDate) {
    return _nativeInterface.default.getTransactionsForCard(this.userID, card.id, startDate.getTime(), endDate.getTime());
  }

  getAccountsMetadata() {
    return _nativeInterface.default.getAccountsMetadata(this.userID);
  }

  delete() {
    return _nativeInterface.default.delete(this.userID);
  }

  createBeneficiary(beneficiary) {
    return _nativeInterface.default.createBeneficiary(this.userID, beneficiary);
  }

  getBeneficiaries() {
    return _nativeInterface.default.getBeneficiaries(this.userID);
  }

  async createTransfer(fromAccount, toBeneficiary, amount, remark) {
    let transferResponse = await _nativeInterface.default.createTransfer(this.userID, fromAccount ? fromAccount.id : null, toBeneficiary, amount, remark);
    let accountID = transferResponse.account;
    let amnt = transferResponse.amount;
    let operationID = transferResponse.operationID;
    let sendingAccount = this.getAccount(accountID);
    return new TransferResponse(amnt, operationID, sendingAccount);
  }

  async createTransferToExistingBeneficiary(fromAccount, toBeneficiaryID, amount, remark) {
    let transferResponse = await _nativeInterface.default.createTransferToExistingBeneficiary(this.userID, fromAccount.id, toBeneficiaryID, amount, remark);
    let accountID = transferResponse.account;
    let amnt = transferResponse.amount;
    let operationID = transferResponse.operationID;
    let sendingAccount = this.getAccount(accountID);
    return new TransferResponse(amnt, operationID, sendingAccount);
  }

  createWireBeneficiary(beneficiary) {
    return _nativeInterface.default.createWireBeneficiary(this.userID, beneficiary);
  }

  getWireBeneficiaries() {
    return _nativeInterface.default.getWireBeneficiaries(this.userID);
  }

  async createWireTransfer(toBeneficiary, fromAccount, amount, remark) {
    let transferResponse = await _nativeInterface.default.createWireTransfer(this.userID, toBeneficiary, fromAccount ? fromAccount.id : null, amount, remark);
    let accountID = transferResponse.account;
    let amnt = transferResponse.amount;
    let operationID = transferResponse.operationID;
    let sendingAccount = this.getAccount(accountID);
    return new TransferResponse(amnt, operationID, sendingAccount);
  }

  async createWireTransferToExistingBeneficiary(fromAccount, toBeneficiaryID, amount, remark) {
    let transferResponse = await _nativeInterface.default.createWireTransferToExistingBeneficiary(this.userID, fromAccount.id, toBeneficiaryID, amount, remark);
    let accountID = transferResponse.account;
    let amnt = transferResponse.amount;
    let operationID = transferResponse.operationID;
    let sendingAccount = this.getAccount(accountID);
    return new TransferResponse(amnt, operationID, sendingAccount);
  }

  getAccount(accountID) {
    var account = undefined;

    this._accounts.find((acc, i, accs) => {
      if (acc.id == accountID) {
        account = acc;
      }
    });

    return account;
  }

}

exports.DapiConnection = DapiConnection;

class DapiPair {
  constructor(code, name) {
    _defineProperty(this, "code", void 0);

    _defineProperty(this, "name", void 0);

    this.code = code;
    this.name = name;
  }

}

exports.DapiPair = DapiPair;

class DapiLineAddress {
  constructor(line1, line2, line3) {
    _defineProperty(this, "line1", void 0);

    _defineProperty(this, "line2", void 0);

    _defineProperty(this, "line3", void 0);

    this.line1 = line1;
    this.line2 = line2;
    this.line3 = line3;
  }

}

exports.DapiLineAddress = DapiLineAddress;

class DapiBeneficiary {
  constructor(linesAddress, accountNumber, name, bankName, swiftCode, iban, phoneNumber, country, branchAddress, branchName, nickname) {
    _defineProperty(this, "linesAddress", void 0);

    _defineProperty(this, "accountNumber", void 0);

    _defineProperty(this, "name", void 0);

    _defineProperty(this, "bankName", void 0);

    _defineProperty(this, "swiftCode", void 0);

    _defineProperty(this, "iban", void 0);

    _defineProperty(this, "phoneNumber", void 0);

    _defineProperty(this, "country", void 0);

    _defineProperty(this, "branchAddress", void 0);

    _defineProperty(this, "branchName", void 0);

    _defineProperty(this, "nickname", void 0);

    this.linesAddress = linesAddress;
    this.accountNumber = accountNumber;
    this.name = name;
    this.bankName = bankName;
    this.swiftCode = swiftCode;
    this.iban = iban;
    this.phoneNumber = phoneNumber;
    this.country = country;
    this.branchAddress = branchAddress;
    this.branchName = branchName;
    this.nickname = nickname;
  }

}

exports.DapiBeneficiary = DapiBeneficiary;

class DapiWireBeneficiary {
  constructor(linesAddress, name, firstName, lastName, nickname, city, state, country, zipCode, receiverType, receiverAccountType, routingNumber, accountNumber) {
    _defineProperty(this, "linesAddress", void 0);

    _defineProperty(this, "name", void 0);

    _defineProperty(this, "firstName", void 0);

    _defineProperty(this, "lastName", void 0);

    _defineProperty(this, "nickname", void 0);

    _defineProperty(this, "city", void 0);

    _defineProperty(this, "state", void 0);

    _defineProperty(this, "country", void 0);

    _defineProperty(this, "zipCode", void 0);

    _defineProperty(this, "receiverType", void 0);

    _defineProperty(this, "receiverAccountType", void 0);

    _defineProperty(this, "routingNumber", void 0);

    _defineProperty(this, "accountNumber", void 0);

    this.linesAddress = linesAddress;
    this.name = name;
    this.firstName = firstName;
    this.lastName = lastName;
    this.nickname = nickname;
    this.city = city;
    this.state = state;
    this.country = country;
    this.zipCode = zipCode;
    this.receiverType = receiverType;
    this.receiverAccountType = receiverAccountType;
    this.routingNumber = routingNumber;
    this.accountNumber = accountNumber;
  }

}

exports.DapiWireBeneficiary = DapiWireBeneficiary;

class DapiAccount {
  constructor(balance, iban, number, currency, type, id, name) {
    _defineProperty(this, "balance", void 0);

    _defineProperty(this, "iban", void 0);

    _defineProperty(this, "number", void 0);

    _defineProperty(this, "currency", void 0);

    _defineProperty(this, "type", void 0);

    _defineProperty(this, "id", void 0);

    _defineProperty(this, "name", void 0);

    this.balance = balance;
    this.iban = iban;
    this.number = number;
    this.currency = currency;
    this.type = type;
    this.id = id;
    this.name = name;
  }

}

exports.DapiAccount = DapiAccount;

class DapiCardBalance {
  constructor(amountDue, availableBalance, outstandingBalance, dueDate) {
    _defineProperty(this, "amountDue", void 0);

    _defineProperty(this, "availableBalance", void 0);

    _defineProperty(this, "outstandingBalance", void 0);

    _defineProperty(this, "dueDate", void 0);

    this.amountDue = amountDue;
    this.availableBalance = availableBalance;
    this.outstandingBalance = outstandingBalance;
    this.dueDate = dueDate;
  }

}

exports.DapiCardBalance = DapiCardBalance;

class DapiCard {
  constructor(balance, cardNumber, creditLimit, currency, expiryDate, id, name, status, type) {
    _defineProperty(this, "balance", void 0);

    _defineProperty(this, "cardNumber", void 0);

    _defineProperty(this, "creditLimit", void 0);

    _defineProperty(this, "currency", void 0);

    _defineProperty(this, "expiryDate", void 0);

    _defineProperty(this, "id", void 0);

    _defineProperty(this, "name", void 0);

    _defineProperty(this, "status", void 0);

    _defineProperty(this, "type", void 0);

    this.balance = balance;
    this.cardNumber = cardNumber;
    this.creditLimit = creditLimit;
    this.currency = currency;
    this.expiryDate = expiryDate;
    this.id = id;
    this.name = name;
    this.status = status;
    this.type = type;
  }

}

exports.DapiCard = DapiCard;

class Dapi {
  static get instance() {
    return this._instance;
  }

  constructor() {}

  start(appKey, clientUserID, configurations) {
    return _nativeInterface.default.start(appKey, clientUserID, configurations);
  }

  isStarted() {
    return _nativeInterface.default.isStarted();
  }

  presentConnect(bankID) {
    _nativeInterface.default.presentConnect(bankID);
  }

  setClientUserID(clientUserID) {
    _nativeInterface.default.setClientUserID(clientUserID);
  }

  clientUserID() {
    return _nativeInterface.default.clientUserID();
  }

  setConfigurations(configurations) {
    _nativeInterface.default.setConfigurations(configurations);
  }

  configurations() {
    return _nativeInterface.default.configurations();
  }

  dismissConnect() {
    _nativeInterface.default.dismissConnect();
  }

  async getConnections() {
    let jsonConnections = await _nativeInterface.default.getConnections();
    let connections = [];

    for (let i = 0; i < jsonConnections.length; i++) {
      let currentConnection = jsonConnections[i]; // convert json accounts array to IAccount array.

      let accounts = [];

      for (let j = 0; j < currentConnection.accounts.length; j++) {
        let currentAccount = currentConnection.accounts[j];
        let account = new DapiAccount(currentAccount.balance, currentAccount.iban, currentAccount.number, new DapiPair(currentAccount.currency.code, currentAccount.currency.name), currentAccount.type, currentAccount.id, currentAccount.name);
        accounts.push(account);
      } // convert json cards array to DapiCard array.


      let cards = [];

      for (let j = 0; j < currentConnection.cards.length; j++) {
        let currentCard = currentConnection.cards[j];
        let card = new DapiCard(new DapiCardBalance(currentCard.balance.amountDue, currentCard.balance.availableBalance, currentCard.balance.outstandingBalance, currentCard.balance.dueDate), currentCard.cardNumber, currentCard.creditLimit, new DapiPair(currentCard.currency.code, currentCard.currency.name), currentCard.expiryDate, currentCard.id, currentCard.name, currentCard.status, currentCard.type);
        cards.push(card);
      }

      let connection = new DapiConnection(currentConnection.clientUserID, currentConnection.userID, currentConnection.bankID, currentConnection.swiftCode, currentConnection.country, currentConnection.bankShortName, currentConnection.bankFullName, accounts, cards, currentConnection.fullLogo, currentConnection.halfLogo, currentConnection.miniLogo);
      connections.push(connection);
    }

    return connections;
  }

}

exports.default = Dapi;

_defineProperty(Dapi, "_instance", new Dapi());
//# sourceMappingURL=index.js.map