function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
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(arr) { if (Array.isArray(arr)) return arr; }
import React, { useEffect, useMemo, useState, useRef } from 'react';
import dayjs from 'dayjs';
import { locales } from '@pisell/utils';
import { QRCode, Button, PisellModal } from '@pisell/materials';
import "./index.less";
import usePrefix from "../../../hooks/usePrefix";
export var AdyenScanQrcode = function AdyenScanQrcode(_ref) {
  var loading = _ref.loading,
    qrCode = _ref.qrCode,
    payMethodType = _ref.payMethodType,
    isPay = _ref.isPay,
    message = _ref.message,
    onCancelPay = _ref.onCancelPay;
  var prefix = usePrefix();
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    cancelModalOpen = _useState2[0],
    setCancelModalOpen = _useState2[1];
  var countdownDuration = 5 * 60;
  var _useState3 = useState(countdownDuration),
    _useState4 = _slicedToArray(_useState3, 2),
    remainingSeconds = _useState4[0],
    setRemainingSeconds = _useState4[1];
  var timerRef = useRef(null);
  useEffect(function () {
    if (!qrCode) {
      setRemainingSeconds(countdownDuration);
      return;
    }
    setRemainingSeconds(countdownDuration);
    timerRef.current = setInterval(function () {
      setRemainingSeconds(function (prev) {
        if (prev <= 1) {
          timerRef.current && clearInterval(timerRef.current);
          return 0;
        }
        return prev - 1;
      });
    }, 1000);
    return function () {
      timerRef.current && clearInterval(timerRef.current);
    };
  }, [qrCode]);
  var formattedCountdown = useMemo(function () {
    return dayjs().startOf('day').add(remainingSeconds, 'second').format('mm:ss');
  }, [remainingSeconds]);
  var onCancel = function onCancel() {
    setCancelModalOpen(true);
  };
  var onModalCancel = function onModalCancel() {
    setCancelModalOpen(false);
  };
  var onModalOk = function onModalOk() {
    onCancelPay();
    // 清除定时器
    timerRef.current && clearInterval(timerRef.current);
    setCancelModalOpen(false);
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-eftpos-pay-adyen-scan-qrcode-container"
  }, loading ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-eftpos-pay-adyen-scan-methods-loading-container"
  }, /*#__PURE__*/React.createElement("div", {
    style: {
      backgroundImage: "url(".concat(prefix, "/loading.png)")
    },
    className: "pisell-lowcode__eft-pay-img pisell-lowcode__eft-pay-load"
  }), /*#__PURE__*/React.createElement("span", null, isPay ? locales.getText('page.eftpos-pay.adyen.scan.qrcode.processing') : message || '')) : null, qrCode ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("span", {
    className: "pisell-eftpos-pay-adyen-scan-qrcode-title"
  }, locales.getText('page.eftpos-pay.adyen.scan.qrcode.title')(payMethodType)), /*#__PURE__*/React.createElement(QRCode, {
    value: qrCode,
    size: 240
  }), /*#__PURE__*/React.createElement("div", {
    className: "pisell-eftpos-pay-adyen-scan-qrcode-timeout"
  }, locales.getText('page.eftpos-pay.adyen.scan.qrcode.timeout')(formattedCountdown))) : null, isPay ? /*#__PURE__*/React.createElement("div", {
    style: {
      width: '100%'
    }
  }, /*#__PURE__*/React.createElement(Button, {
    className: "pisell-lowcode__eft-pay-step-item-btn",
    block: true,
    onClick: onCancel
  }, locales.getText('page.eftpos-pay.adyen.scan.cancel-btn'))) : null, cancelModalOpen && /*#__PURE__*/React.createElement(PisellModal.Information, {
    open: cancelModalOpen,
    title: locales.getText('page.eftpos-pay.adyen.scan.cancel-confirm.title'),
    describe: locales.getText('page.eftpos-pay.adyen.scan.cancel-confirm.content'),
    okText: locales.getText('page.eftpos-pay.adyen.scan.cancel-confirm.ok'),
    cancelText: locales.getText('page.eftpos-pay.adyen.scan.cancel-confirm.cancel'),
    okButtonProps: {
      danger: true
    },
    onOk: onModalOk,
    onCancel: onModalCancel
  }));
};