var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/pay/toB/components/MX51/index.tsx
var MX51_exports = {};
__export(MX51_exports, {
  default: () => MX51_default
});
module.exports = __toCommonJS(MX51_exports);
var import_react = require("react");
var import_useLogger = __toESM(require("../../../../../hooks/useLogger"));
var import_useEngineContext = __toESM(require("../../../../../hooks/useEngineContext"));
var getUuid = () => {
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxx".replace(/[xy]/g, (c) => {
    const r = Math.random() * 16 | 0;
    const v = c === "x" ? r : r & 3 | 8;
    return v.toString(16);
  });
};
var MX51 = (props, ref) => {
  var _a, _b;
  const logger = (0, import_useLogger.default)();
  const context = (0, import_useEngineContext.default)();
  const action = (_b = (_a = context == null ? void 0 : context.appHelper) == null ? void 0 : _a.utils) == null ? void 0 : _b.action;
  const onOpen = ({ data, payCallback }) => {
    const { params, mode } = data;
    const uniquePaymentNumber = getUuid();
    const newData = {
      module: mode === "refund" ? "refund" : "payment",
      key: "mx51",
      data: {
        order_id: params.orderId,
        amount: params.amount,
        surcharge: params.surcharge,
        uniquePaymentNumber
      }
    };
    logger == null ? void 0 : logger.addLog({
      type: "info",
      title: "ToB_MX51_onOpen 开始调用",
      metadata: { params: newData }
    });
    action({
      type: "webpos.handleMX51Pay",
      data: newData,
      callback: (res) => {
        var _a2, _b2, _c;
        let isSame = false;
        if (res == null ? void 0 : res.uniquePaymentNumber) {
          const _res = res;
          isSame = (_res == null ? void 0 : _res.uniquePaymentNumber) === uniquePaymentNumber;
          if (isSame) {
            payCallback == null ? void 0 : payCallback(_res);
          }
        } else {
          const _res = res;
          const newRes = {
            result: (_res == null ? void 0 : _res.code) == "200" ? true : false,
            msg: _res == null ? void 0 : _res.msg,
            amount: (_a2 = _res == null ? void 0 : _res.data) == null ? void 0 : _a2.amount,
            surcharge: (_b2 = _res == null ? void 0 : _res.data) == null ? void 0 : _b2.surcharge,
            uniquePaymentNumber: _res == null ? void 0 : _res.uuid,
            action: (_res == null ? void 0 : _res.action) || ((_c = _res == null ? void 0 : _res.data) == null ? void 0 : _c.action)
          };
          isSame = (_res == null ? void 0 : _res.uuid) === uniquePaymentNumber;
          if (isSame) {
            payCallback == null ? void 0 : payCallback(newRes);
          }
        }
        logger == null ? void 0 : logger.addLog({
          type: "info",
          title: "ToB_MX51_onOpen_callback",
          metadata: {
            msg: isSame ? "交易流水号一致" : "交易流水号不一致",
            res
          }
        });
      }
    });
  };
  (0, import_react.useImperativeHandle)(ref, () => ({
    open: onOpen
  }));
  return null;
};
var MX51_default = (0, import_react.forwardRef)(MX51);
