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, useState } from 'react';
import { useMemoizedFn } from 'ahooks';
import { PisellModal } from '@pisell/materials';
import "./index.less";
import usePrefix from "../../../hooks/usePrefix";
import Action from "./Action";
import { EButtonKeyType, ESettlementStatus } from "./types";
import { parseMX51Params, settlementQuery } from "./utils";
import useEngineContext from "../../../hooks/useEngineContext";
var SettlementModal = function SettlementModal(_ref) {
  var _context$appHelper;
  var open = _ref.open,
    onClose = _ref.onClose,
    data = _ref.data;
  var context = useEngineContext();
  var prefix = usePrefix();
  var _ref2 = (context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {},
    isTerminal = _ref2.isTerminal,
    interaction = _ref2.interaction;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    receiptModalShow = _useState2[0],
    setReceiptModalShow = _useState2[1];
  var _useState3 = useState(null),
    _useState4 = _slicedToArray(_useState3, 2),
    receiptModalData = _useState4[0],
    setReceiptModalData = _useState4[1];
  var _useState5 = useState(null),
    _useState6 = _slicedToArray(_useState5, 2),
    action = _useState6[0],
    setAction = _useState6[1];
  var _useState7 = useState(ESettlementStatus.Processing),
    _useState8 = _slicedToArray(_useState7, 2),
    status = _useState8[0],
    setStatus = _useState8[1];
  var runSettlementQuery = useMemoizedFn(function () {
    setAction(null);
    setStatus(ESettlementStatus.Processing);
    console.log('settlementQuery_start');
    settlementQuery(data, function (status, res) {
      console.log('settlementQuery_callback', status, res);
      var parsedData = parseMX51Params(res === null || res === void 0 ? void 0 : res.data, status, res);
      console.log('settlementQuery_parsedData', parsedData);
      if (parsedData) {
        setAction(parsedData);
      }
      setStatus(status);
    });
  });
  var onCloseReceiptModal = function onCloseReceiptModal() {
    setReceiptModalData(null);
    setReceiptModalShow(false);
  };
  var onElementClick = function onElementClick(element) {
    console.log('onElementClick', element);
    switch (element.key) {
      case EButtonKeyType.SettlementComplete:
        onClose();
        break;
      case EButtonKeyType.RetrySettlement:
        runSettlementQuery();
        break;
      case EButtonKeyType.PrintMerchantReceipt:
        var _isTerminal = typeof isTerminal === 'function' ? isTerminal() : false;
        if (_isTerminal) {
          var _interaction$utils, _interaction$utils$po;
          var receipt = element !== null && element !== void 0 && element.receipt ? Array.isArray(element === null || element === void 0 ? void 0 : element.receipt) ? element === null || element === void 0 ? void 0 : element.receipt : [element === null || element === void 0 ? void 0 : element.receipt] : [];
          interaction === null || interaction === void 0 || (_interaction$utils = interaction.utils) === null || _interaction$utils === void 0 || (_interaction$utils$po = _interaction$utils.postMessageToApp) === null || _interaction$utils$po === void 0 || _interaction$utils$po.call(_interaction$utils, {
            module: 'pos',
            key: 'print',
            data: receipt
          });
        } else {
          setReceiptModalShow(true);
          setReceiptModalData(element === null || element === void 0 ? void 0 : element.receipt);
        }
        break;
    }
  };
  useEffect(function () {
    if (open && data) {
      runSettlementQuery();
    }
  }, [open, data, runSettlementQuery]);
  return /*#__PURE__*/React.createElement(PisellModal, {
    open: open,
    closable: false,
    header: false,
    footer: null,
    width: 600
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-eftpos-settlement-modal"
  }, status === ESettlementStatus.Processing && /*#__PURE__*/React.createElement("div", {
    style: {
      backgroundImage: "url(".concat(prefix, "/loading.png)"),
      marginBottom: '24px'
    },
    className: "pisell-lowcode__eft-pay-img pisell-lowcode__eft-pay-load"
  }), status === ESettlementStatus.Failed && /*#__PURE__*/React.createElement("div", {
    style: {
      backgroundImage: "url(".concat(prefix, "/warn.png)"),
      marginBottom: '24px'
    },
    className: "pisell-lowcode__eft-pay-img pisell-lowcode__eft-pay-warn"
  }), /*#__PURE__*/React.createElement(Action, {
    data: action,
    onClick: onElementClick
  })), receiptModalShow ? /*#__PURE__*/React.createElement(PisellModal, {
    open: receiptModalShow,
    onCancel: onCloseReceiptModal,
    maskClosable: true,
    footer: null
  }, /*#__PURE__*/React.createElement("div", {
    style: {
      padding: '16px',
      display: 'flex',
      alignItems: 'center',
      justifyContent: 'center'
    }
  }, /*#__PURE__*/React.createElement("span", {
    style: {
      display: 'inline-block',
      whiteSpace: 'pre-wrap',
      fontFamily: 'monospace',
      backgroundColor: '#fff'
    }
  }, receiptModalData))) : null);
};
export default SettlementModal;