"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Transaction = exports.Tokenization = void 0;
var _reactNative = require("react-native");
var _package = _interopRequireDefault(require("../package.json"));
var _Response = _interopRequireDefault(require("@pixelpay/sdk-core/lib/base/Response"));
var _ErrorResponse = _interopRequireDefault(require("@pixelpay/sdk-core/lib/responses/ErrorResponse"));
var _FailureResponse = _interopRequireDefault(require("@pixelpay/sdk-core/lib/responses/FailureResponse"));
var _InputErrorResponse = _interopRequireDefault(require("@pixelpay/sdk-core/lib/responses/InputErrorResponse"));
var _NetworkFailureResponse = _interopRequireDefault(require("@pixelpay/sdk-core/lib/responses/NetworkFailureResponse"));
var _NoAccessResponse = _interopRequireDefault(require("@pixelpay/sdk-core/lib/responses/NoAccessResponse"));
var _NotFoundResponse = _interopRequireDefault(require("@pixelpay/sdk-core/lib/responses/NotFoundResponse"));
var _PayloadResponse = _interopRequireDefault(require("@pixelpay/sdk-core/lib/responses/PayloadResponse"));
var _PaymentDeclinedResponse = _interopRequireDefault(require("@pixelpay/sdk-core/lib/responses/PaymentDeclinedResponse"));
var _PreconditionalResponse = _interopRequireDefault(require("@pixelpay/sdk-core/lib/responses/PreconditionalResponse"));
var _SuccessResponse = _interopRequireDefault(require("@pixelpay/sdk-core/lib/responses/SuccessResponse"));
var _TimeoutResponse = _interopRequireDefault(require("@pixelpay/sdk-core/lib/responses/TimeoutResponse"));
var _Transaction = _interopRequireDefault(require("@pixelpay/sdk-core/lib/services/Transaction"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const LINKING_ERROR = `The package '@pixelpay/react-native-plugin' doesn't seem to be linked. Make sure: \n\n` + _reactNative.Platform.select({
  ios: "- You have run 'pod install'\n",
  default: ''
}) + '- You rebuilt the app after installing the package\n' + '- You are not using Expo Go\n';
const ReactNativePlugin = _reactNative.NativeModules.ReactNativePlugin ? _reactNative.NativeModules.ReactNativePlugin : new Proxy({}, {
  get() {
    throw new Error(LINKING_ERROR);
  }
});
class ServiceBehaviour {
  constructor(settings) {
    this._settings = settings;
    this._settings.sdk = 'sdk-react-native';
    this._settings.sdk_version = _package.default === null || _package.default === void 0 ? void 0 : _package.default.version;
  }
  getSettings() {
    return JSON.stringify(this._settings);
  }
  parseResult(result) {
    let response = new _Response.default();
    const data = JSON.parse(result.response);
    if (typeof result.status === 'string') {
      result.status = Number.parseInt(result.status, 10);
    }
    switch (result.status) {
      case 200:
        response = new _SuccessResponse.default();
        break;
      case 202:
        response = new _PayloadResponse.default();
        break;
      case 400:
        response = new _ErrorResponse.default();
        break;
      case 401:
      case 403:
        response = new _NoAccessResponse.default();
        break;
      case 402:
        response = new _PaymentDeclinedResponse.default();
        break;
      case 404:
      case 405:
      case 406:
        response = new _NotFoundResponse.default();
        break;
      case 408:
        response = new _TimeoutResponse.default();
        break;
      case 412:
      case 418:
        response = new _PreconditionalResponse.default();
        break;
      case 422:
        response = new _InputErrorResponse.default();
        break;
      case 500:
        response = new _FailureResponse.default();
        break;
      default:
        if (result.status > 500) {
          response = new _NetworkFailureResponse.default();
        }
        break;
    }
    response.setStatus(result.status);
    response.action = (data === null || data === undefined ? undefined : data.action) || null;
    response.success = (data === null || data === undefined ? undefined : data.success) || false;
    response.message = (data === null || data === undefined ? undefined : data.message) || null;
    response.data = (data === null || data === undefined ? undefined : data.data) || null;
    response.errors = (data === null || data === undefined ? undefined : data.errors) || null;
    return response;
  }
}
class Transaction extends ServiceBehaviour {
  /**
   * Send and proccesing SALE transaction
   *
   * @param transaction
   * @return
   * @throws InvalidCredentialsException
   */
  async doSale(transaction) {
    return this.parseResult(await ReactNativePlugin.transactionDoSale(this.getSettings(), transaction.toJson()));
  }

  /**
   * Send and proccesing AUTH transaction
   *
   * @param transaction
   * @return
   * @throws InvalidCredentialsException
   */
  async doAuth(transaction) {
    return this.parseResult(await ReactNativePlugin.transactionDoAuth(this.getSettings(), transaction.toJson()));
  }

  /**
   * Send and proccesing CAPTURE transaction
   *
   * @param transaction
   * @return
   * @throws InvalidCredentialsException
   */
  async doCapture(transaction) {
    return this.parseResult(await ReactNativePlugin.transactionDoCapture(this.getSettings(), transaction.toJson()));
  }

  /**
   * Send and proccesing VOID transaction
   *
   * @param transaction
   * @return
   * @throws InvalidCredentialsException
   */
  async doVoid(transaction) {
    return this.parseResult(await ReactNativePlugin.transactionDoVoid(this.getSettings(), transaction.toJson()));
  }

  /**
   * Send and proccesing SALE transaction
   *
   * @param transaction
   * @return
   * @throws InvalidCredentialsException
   */
  async getStatus(transaction) {
    return this.parseResult(await ReactNativePlugin.transactionGetStatus(this.getSettings(), transaction.toJson()));
  }

  /**
   * Verify a payment hash and returns true if payment response is not modified
   *
   * @param hash
   * @param order_id
   * @param secret
   * @return
   */
  verifyPaymentHash(hash, order_id, secret) {
    return new _Transaction.default(this._settings).verifyPaymentHash(hash, order_id, secret);
  }
}
exports.Transaction = Transaction;
class Tokenization extends ServiceBehaviour {
  /**
   * Vault credit/debit card and obtain a token card identifier (T-* format)
   *
   * @param card
   * @return
   * @throws InvalidCredentialsException
   */
  async vaultCard(card) {
    return this.parseResult(await ReactNativePlugin.tokenizationVaultCard(this.getSettings(), card.toJson()));
  }

  /**
   * Update credit/debit card by token card identifier
   *
   * @param token
   * @param card
   * @return
   * @throws InvalidCredentialsException
   */
  async updateCard(token, card) {
    return this.parseResult(await ReactNativePlugin.tokenizationUpdateCard(this.getSettings(), token, card.toJson()));
  }

  /**
   * Show credit/debit card metadata by token card identifier
   *
   * @param token
   * @return
   * @throws InvalidCredentialsException
   */
  async showCard(token) {
    return this.parseResult(await ReactNativePlugin.tokenizationShowCard(this.getSettings(), token));
  }

  /**
   * Show credit/debit card metadata by token card identifier
   *
   * @param token
   * @return
   * @throws InvalidCredentialsException
   */
  async showCards(tokens) {
    return this.parseResult(await ReactNativePlugin.tokenizationShowCards(this.getSettings(), tokens.join('|')));
  }

  /**
   * Delete credit/debit card metadata by token card identifier
   *
   * @param token
   * @return
   * @throws InvalidCredentialsException
   */
  async deleteCard(token) {
    return this.parseResult(await ReactNativePlugin.tokenizationDeleteCard(this.getSettings(), token));
  }
}
exports.Tokenization = Tokenization;
//# sourceMappingURL=index.js.map