import { forwardRef, useImperativeHandle } from 'react';
import { webPayment } from '@pisell/utils';
import useLogger from "../../../../../hooks/useLogger";
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 logger = useLogger();
  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
      }
    });
    // 通过jsbridge调用mx51支付
    webPayment(newData, function (res) {
      // 判断是否是同一个支付
      var isSame = (res === null || res === void 0 ? void 0 : res.uniquePaymentNumber) === uniquePaymentNumber;
      logger === null || logger === void 0 || logger.addLog({
        type: 'info',
        title: 'ToB_MX51_onOpen_callback',
        metadata: {
          msg: isSame ? 'uniquePaymentNumber一致' : 'uniquePaymentNumber不一致',
          res: res
        }
      });
      if (isSame) {
        payCallback === null || payCallback === void 0 || payCallback(res);
      }
    });
  };
  useImperativeHandle(ref, function () {
    return {
      open: onOpen
    };
  });
  return null;
};
export default /*#__PURE__*/forwardRef(MX51);