"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FormAdyen = void 0;
var _locale = require("@mirai/locale");
var _ui = require("@mirai/ui");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = _interopRequireWildcard(require("react"));
var _FormAdyen = require("./FormAdyen.constants");
var _FormAdyen2 = require("./FormAdyen.l10n");
var style = _interopRequireWildcard(require("./FormAdyen.module.css"));
var _loadScript = require("../../../../../helpers/loadScript");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const FormAdyen = _ref => {
  let {
    response
  } = _ref;
  const {
    translate
  } = (0, _locale.useLocale)();
  const [error, setError] = (0, _react.useState)();
  const [ready, setReady] = (0, _react.useState)();
  const refContainer = (0, _react.useRef)();
  (0, _react.useEffect)(() => {
    const {
      configuration: {
        environment = 'test'
      } = {}
    } = response || {};
    const url = _FormAdyen.URL.replace('{environment}', environment);
    const exists = document.getElementById('addyenCSS');
    if (!exists) {
      const css = document.createElement('link');
      css.crossOrigin = 'anonymous';
      css.id = 'addyenCSS';
      css.integrity = 'sha384-pC2TKxh6gMtWIUiXzlw5Jnq9ZtnyuPT0ymaHY6skdegLVNDMNiiQ7/NFixA87/Wb';
      css.rel = 'stylesheet';
      css.href = "".concat(url, "adyen.css");
      document.head.appendChild(css);
    }
    let remove;
    (async () => {
      remove = await (0, _loadScript.loadScript)({
        async: false,
        crossOrigin: 'anonymous',
        domain: 'payments',
        id: 'addyenScript',
        integrity: 'sha384-PpkKjJdtlCZuAK7HWeMaOwzrQMGgAWThLG+QZe0sxlohYwfVPP+G65HBrWbgwSoE',
        src: "".concat(url, "adyen.js"),
        onLoad: handleLoad
      });
    })();
    return () => {
      const el = document.getElementById('addyenCSS');
      el.parentNode.removeChild(el);
      remove && remove();
    };
  }, [response]);
  (0, _react.useEffect)(() => {
    if (!response || !ready) return;
    (async () => {
      const {
        AdyenCheckout,
        Dropin
      } = window.AdyenWeb || {};
      const {
        configuration: {
          amount,
          clientKey,
          countryCode,
          environment = 'test',
          id,
          returnUrl,
          shopperLocale: locale,
          sessionData
        } = {}
      } = response;
      const checkout = await AdyenCheckout({
        amount,
        clientKey,
        countryCode,
        environment,
        locale,
        returnUrl,
        session: {
          id,
          sessionData
        },
        onPaymentCompleted: (result, component) => {
          // eslint-disable-next-line no-console
          console.info(result, component);
          document.location.href = "".concat(returnUrl, "?").concat(new URLSearchParams(result).toString());
        },
        onPaymentFailed: (result, component) => {
          setError(error);
          // eslint-disable-next-line no-console
          console.info(result, component);
        },
        onError: (error, component) => {
          setError(error);
          // eslint-disable-next-line no-console
          console.error(error.name, error.message, error.stack, component);
        }
      });
      new Dropin(checkout, {
        paymentMethodsConfiguration: {
          card: {
            hasHolderName: true,
            holderNameRequired: true
          }
        }
      }).mount(refContainer.current);
    })();

    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [ready, response]);
  const handleLoad = async () => {
    setReady(true);
  };
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ui.View, {
    ref: refContainer,
    className: style.container
  }, /*#__PURE__*/_react.default.createElement(_ui.Progress, {
    indeterminate: true,
    visible: !ready
  }), error && /*#__PURE__*/_react.default.createElement(_ui.Notification, {
    error: true,
    large: true,
    wide: true
  }, translate(_FormAdyen2.L10N.NOTIFICATION_ERROR))));
};
exports.FormAdyen = FormAdyen;
FormAdyen.displayName = 'Mirai:Payments:ButtonPayment:FormAdyen';
FormAdyen.propTypes = {
  cards: _propTypes.default.array,
  response: _propTypes.default.object
};
//# sourceMappingURL=FormAdyen.js.map