"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FormSipay = void 0;
var _locale = require("@mirai/locale");
var _ui = require("@mirai/ui");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = _interopRequireWildcard(require("react"));
var _FormSipay = require("./FormSipay.constants");
var _FormSipay2 = require("./FormSipay.l10n");
var style = _interopRequireWildcard(require("./FormSipay.module.css"));
var _helpers = require("./helpers");
const _excluded = ["currencyDecimals", "info", "publicKey", "onError", "onSuccess"];
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 }; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
const FormSipay = _ref => {
  let {
      currencyDecimals = 2,
      info: {
        cardValidation = false,
        amount = 0
      } = {},
      publicKey = process.env.PROVIDER_SIPAY_PUBLICKEY,
      onError = () => {},
      onSuccess = () => {}
    } = _ref,
    others = _objectWithoutProperties(_ref, _excluded);
  const {
    currency,
    translate
  } = (0, _locale.useLocale)();
  const [ready, setReady] = (0, _react.useState)(false);
  const [success, setSuccess] = (0, _react.useState)({});
  (0, _react.useEffect)(() => {
    const exists = document.getElementById(_FormSipay.SCRIPT_ID);
    const script = exists || document.createElement('script');
    script.id = _FormSipay.SCRIPT_ID;
    script.src = process.env.PROVIDER_SIPAY_SCRIPT;
    script.onload = handleLoad;
    document.head.appendChild(script);
    return () => {
      const el = document.getElementById(_FormSipay.SCRIPT_ID);
      el.parentNode.removeChild(el);
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
  (0, _react.useEffect)(() => {
    window.FastpayCallback = handleSuccess;
    return () => window.FastpayCallback = undefined;
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [ready, success]);
  (0, _react.useEffect)(() => {
    const {
      name,
      token,
      type
    } = success || {};
    if (type === 'success') onSuccess({
      name,
      token
    });else onError({});
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [success]);
  const handleLoad = () => {
    const el = document.getElementById(_FormSipay.BUTTON_PROPS.id);
    window.Fastpay.loadButton(el);
    window.Fastpay.customize((0, _helpers.getStyle)());
    el.click();
    setReady(true);
  };
  const handleSuccess = _ref2 => {
    let {
      type,
      request_id: token,
      payload: {
        cadholder_name: name
      } = {}
    } = _ref2;
    return setSuccess({
      name,
      token,
      type
    });
  };
  return /*#__PURE__*/_react.default.createElement(_ui.View, _extends({}, others, {
    className: (0, _ui.styles)(style.container, ready && style.ready, others.className)
  }), ready ? /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: true
  }, translate(_FormSipay2.L10N.LABEL_SECURE_PAGE)) : /*#__PURE__*/_react.default.createElement(_ui.Notification, {
    inline: true,
    large: true,
    warning: true,
    wide: true
  }, translate(_FormSipay2.L10N.NOTIFICATION_LOADING)), /*#__PURE__*/_react.default.createElement("button", _extends({}, _FormSipay.BUTTON_PROPS, {
    "data-amount": cardValidation ? 1 : (amount * Math.pow(10, currencyDecimals)).toFixed(),
    "data-currency": currency,
    "data-callback": "FastpayCallback",
    "data-paymentbutton": translate(cardValidation ? _FormSipay2.L10N.ACTION_AUTHORIZE : _FormSipay2.L10N.ACTION_PAY),
    "data-key": publicKey,
    className: style.button
  })));
};
exports.FormSipay = FormSipay;
FormSipay.displayName = 'Mirai:Payments:ButtonPayment:FormSipay';
FormSipay.propTypes = {
  currencyDecimals: _propTypes.default.number,
  info: _propTypes.default.shape({
    amount: _propTypes.default.number,
    cardValidation: _propTypes.default.bool,
    date: _propTypes.default.instanceOf(Date)
  }),
  publicKey: _propTypes.default.string,
  onError: _propTypes.default.func,
  onSuccess: _propTypes.default.func
};
//# sourceMappingURL=FormSipay.js.map