"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FormShift4 = void 0;
var _dataSources = require("@mirai/data-sources");
var _locale = require("@mirai/locale");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = require("react");
var _FormShift = require("./FormShift4.constants");
var _loadScript = require("../../../../../helpers/loadScript");
var _helpers = require("../../../../helpers");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
const FormShift4 = _ref => {
  let response = _ref.response,
    _ref$onClose = _ref.onClose,
    onClose = _ref$onClose === void 0 ? () => {} : _ref$onClose;
  const _useLocale = (0, _locale.useLocale)(),
    language = _useLocale.language;
  const _useState = (0, _react.useState)(),
    _useState2 = _slicedToArray(_useState, 2),
    ready = _useState2[0],
    setReady = _useState2[1];
  (0, _react.useEffect)(() => {
    (async () => {
      await (0, _loadScript.loadScript)({
        async: false,
        domain: 'payments',
        id: 'shift4Script',
        src: _FormShift.SCRIPT_URL,
        onLoad: handleLoad
      });
    })();
  }, [response]);
  (0, _react.useEffect)(() => {
    if (!response || !ready) return;
    const _ref2 = window || {},
      Shift4Checkout = _ref2.Shift4Checkout;
    const _response$configurati = response.configuration,
      _response$configurati2 = _response$configurati === void 0 ? {} : _response$configurati,
      clientSecret = _response$configurati2.clientSecret,
      email = _response$configurati2.email,
      publicKey = _response$configurati2.publicKey;
    Shift4Checkout.key = publicKey;
    Shift4Checkout.success = handleSuccess;
    Shift4Checkout.error = handleError;
    Shift4Checkout.close = onClose;
    Shift4Checkout.open({
      clientSecret,
      email,
      locale: _FormShift.SUPPORTED_LANGUAGES.includes(language) ? language : _FormShift.DEFAULT_LANGUAGE
    });

    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [ready, response]);
  const handleError = errorMessage => {
    _dataSources.Event.publish(_helpers.EVENT.NOTIFICATION, {
      error: true,
      defaultMessage: errorMessage
    });
  };
  const handleLoad = async () => {
    setReady(true);
  };
  const handleSuccess = result => {
    if (!result) return;
    const _response$configurati3 = response.configuration,
      _response$configurati4 = _response$configurati3 === void 0 ? {} : _response$configurati3,
      paymentTransactionId = _response$configurati4.paymentTransactionId,
      returnUrl = _response$configurati4.returnUrl;
    const url = new URL(returnUrl);
    paymentTransactionId && url.searchParams.append('paymentTransactionId', paymentTransactionId);
    window.location.href = url.toString();
  };
  return null;
};
exports.FormShift4 = FormShift4;
FormShift4.displayName = 'Mirai:Payments:ButtonPayment:FormShift4';
FormShift4.propTypes = {
  response: _propTypes.default.object,
  onClose: _propTypes.default.func
};
//# sourceMappingURL=FormShift4.js.map