import { forwardRef, useImperativeHandle } from 'react';
import useLogger from "../../../../../hooks/useLogger";
import useEngineContext from "../../../../../hooks/useEngineContext";
var getUuid = function getUuid() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxx'.replace(/[xy]/g, function (c) {
    var r = Math.random() * 16 | 0;
    var v = c === 'x' ? r : r & 0x3 | 0x8;
    return v.toString(16);
  });
};
var MX51 = function MX51(props, ref) {
  var _context$appHelper;
  var logger = useLogger();
  var context = useEngineContext();
  var action = context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.action;
  var onOpen = function onOpen(_ref) {
    var data = _ref.data,
      payCallback = _ref.payCallback;
    var params = data.params,
      mode = data.mode;
    var uniquePaymentNumber = getUuid();
    var newData = {
      module: mode === 'refund' ? 'refund' : 'payment',
      key: 'mx51',
      data: {
        order_id: params.orderId,
        amount: params.amount,
        surcharge: params.surcharge,
        uniquePaymentNumber: uniquePaymentNumber
      }
    };
    logger === null || logger === void 0 || logger.addLog({
      type: 'info',
      title: 'ToB_MX51_onOpen 开始调用',
      metadata: {
        params: newData
      }
    });

    // 通过action调用mx51支付
    action({
      type: 'webpos.handleMX51Pay',
      data: newData,
      callback: function callback(res) {
        var isSame = false;
        if (res !== null && res !== void 0 && res.uniquePaymentNumber) {
          // jsbridge调用场景结果
          var _res = res;
          // 判断是否是同一个支付
          isSame = (_res === null || _res === void 0 ? void 0 : _res.uniquePaymentNumber) === uniquePaymentNumber;
          if (isSame) {
            payCallback === null || payCallback === void 0 || payCallback(_res);
          }
        } else {
          var _res2$data, _res2$data2, _res2$data3;
          // 原生调用场景结果
          var _res2 = res;
          // 转换为MX51PayResult类型，上层调用需要
          var newRes = {
            result: (_res2 === null || _res2 === void 0 ? void 0 : _res2.code) == '200' ? true : false,
            msg: _res2 === null || _res2 === void 0 ? void 0 : _res2.msg,
            amount: _res2 === null || _res2 === void 0 || (_res2$data = _res2.data) === null || _res2$data === void 0 ? void 0 : _res2$data.amount,
            surcharge: _res2 === null || _res2 === void 0 || (_res2$data2 = _res2.data) === null || _res2$data2 === void 0 ? void 0 : _res2$data2.surcharge,
            uniquePaymentNumber: _res2 === null || _res2 === void 0 ? void 0 : _res2.uuid,
            action: (_res2 === null || _res2 === void 0 ? void 0 : _res2.action) || (_res2 === null || _res2 === void 0 || (_res2$data3 = _res2.data) === null || _res2$data3 === void 0 ? void 0 : _res2$data3.action)
          };
          // 判断是否是同一个支付
          isSame = (_res2 === null || _res2 === void 0 ? void 0 : _res2.uuid) === uniquePaymentNumber;
          if (isSame) {
            payCallback === null || payCallback === void 0 || payCallback(newRes);
          }
        }
        logger === null || logger === void 0 || logger.addLog({
          type: 'info',
          title: 'ToB_MX51_onOpen_callback',
          metadata: {
            msg: isSame ? '交易流水号一致' : '交易流水号不一致',
            res: res
          }
        });
      }
    });
  };
  useImperativeHandle(ref, function () {
    return {
      open: onOpen
    };
  });
  return null;
};
export default /*#__PURE__*/forwardRef(MX51);