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/EFTPOS/index.tsx
var EFTPOS_exports = {};
__export(EFTPOS_exports, {
  default: () => EFTPOS_default
});
module.exports = __toCommonJS(EFTPOS_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_ahooks = require("ahooks");
var import_EFTPos = __toESM(require("../../../../eftposPay/EFTPos"));
var import_utils = require("../../../../../utils");
var import_useLogger = __toESM(require("../../../../../hooks/useLogger"));
var import_hooks = require("../../../../../hooks");
var import_index = require("./index.less");
var updateTransactionReceiveStatus = async (params) => import_utils.request.getRequest().post("/shop/pay/card-reader/transaction/receive/status", params);
var EFTPOS = (props, ref) => {
  var _a;
  const context = (0, import_hooks.useEngineContext)();
  const { interaction } = ((_a = context.appHelper) == null ? void 0 : _a.utils) || {};
  const logger = (0, import_useLogger.default)();
  const network = (0, import_ahooks.useNetwork)();
  const [visible, setVisible] = (0, import_react.useState)(false);
  const eftposRef = (0, import_react.useRef)(null);
  const payCallbackRef = (0, import_react.useRef)(null);
  const eftposHandlingRef = (0, import_react.useRef)(false);
  const eftposOpenParamsRef = (0, import_react.useRef)(null);
  const eftposInitDataRef = (0, import_react.useRef)(null);
  const [isEftposLoading, setIsEftposLoading] = (0, import_react.useState)(false);
  (0, import_react.useEffect)(() => {
    var _a2, _b;
    if (eftposRef.current) {
      (_b = (_a2 = eftposRef.current) == null ? void 0 : _a2.updateNetwork) == null ? void 0 : _b.call(_a2, { network: network.online });
    }
  }, [network.online]);
  const closeModal = () => {
    setIsEftposLoading(false);
    eftposHandlingRef.current = false;
    setVisible(false);
  };
  const onOpen = async ({ source, data, payCallback }) => {
    var _a2, _b, _c, _d, _e;
    if (eftposHandlingRef.current || visible) {
      logger == null ? void 0 : logger.addLog({
        type: "info",
        title: "ToB_EFTPOS_onOpen 重复触发被拦截",
        metadata: { data }
      });
      return;
    }
    eftposHandlingRef.current = true;
    let device_number = "";
    try {
      device_number = await ((_b = (_a2 = interaction == null ? void 0 : interaction.utils) == null ? void 0 : _a2.asyncDataManager) == null ? void 0 : _b.call(_a2, {
        module: "get",
        key: "device_number"
      }));
    } catch (error) {
      console.error("获取设备编号失败", error);
    }
    const { type, payMethod, params, mode } = data;
    const newData = {
      type,
      code: payMethod.code,
      terminal: {
        network: network.online
      },
      apiParams: {
        order_id: params.orderId,
        name: payMethod.name,
        symbol: params.symbol,
        amount: params.amount,
        device_number
      },
      mode
    };
    console.log("newData>>>>>>>>>>>>>>", newData);
    if (!(newData == null ? void 0 : newData.type) && (newData == null ? void 0 : newData.code)) {
      newData.type = (_e = (_d = (_c = newData.code) == null ? void 0 : _c.split("_")) == null ? void 0 : _d[1]) == null ? void 0 : _e.toLocaleLowerCase();
    }
    logger == null ? void 0 : logger.addLog({
      type: "info",
      title: "ToB_EFTPOS_onOpen 开始调用",
      metadata: { params: data, initData: newData }
    });
    eftposOpenParamsRef.current = data;
    payCallbackRef.current = payCallback;
    eftposInitDataRef.current = { data: newData, source };
    setVisible(true);
    setIsEftposLoading(true);
  };
  const onEftposLoad = (status) => {
    logger == null ? void 0 : logger.addLog({
      type: "info",
      title: "ToB_EFTPOS_onEftposLoad",
      metadata: { status }
    });
    setIsEftposLoading(false);
    if (!status) {
      closeModal();
    }
  };
  const onAfterOpenChange = (v) => {
    var _a2, _b;
    logger == null ? void 0 : logger.addLog({
      type: "info",
      title: "ToB_EFTPOS_modal_onAfterOpenChange",
      metadata: { eftposModalVisible: v }
    });
    if (v) {
      const { data, source } = eftposInitDataRef.current;
      (_b = (_a2 = eftposRef.current) == null ? void 0 : _a2.update) == null ? void 0 : _b.call(_a2, data, source);
      eftposInitDataRef.current = null;
    }
  };
  const handleResult = (status, params) => {
    if (!eftposHandlingRef.current) {
      return;
    }
    const isUpdateTransactionReceiveStatus = status === "mark_tx_processed" || (params == null ? void 0 : params.uniquePaymentNumber);
    if (isUpdateTransactionReceiveStatus) {
      updateTransactionReceiveStatus({
        numbers: params,
        receive_status: "processed"
      });
    }
    if (status === "mark_tx_processed") {
      return;
    }
    if (status === "page" || status === "print") {
      return;
    }
    if (status === "cache") {
      return;
    }
    eftposHandlingRef.current = false;
  };
  const onPayResult = ({ status, params }) => {
    var _a2;
    logger == null ? void 0 : logger.addLog({
      type: "info",
      title: "ToB_EFTPOS_onPayResult",
      metadata: { status, params }
    });
    handleResult(status, params);
    if (status === "success" || status === "fail") {
      (_a2 = payCallbackRef.current) == null ? void 0 : _a2.call(payCallbackRef, { status, params });
      closeModal();
    }
  };
  (0, import_react.useImperativeHandle)(ref, () => ({
    open: onOpen,
    close: closeModal
  }));
  return /* @__PURE__ */ import_react.default.createElement(
    import_antd.Modal,
    {
      open: visible,
      onCancel: closeModal,
      footer: null,
      closable: false,
      width: "fit-content",
      destroyOnClose: true,
      bodyStyle: { padding: 0 },
      centered: true,
      afterOpenChange: onAfterOpenChange,
      className: "tob-pay-eftpos-modal",
      maskClosable: false
    },
    isEftposLoading ? /* @__PURE__ */ import_react.default.createElement("div", { className: "tob-pay-eftpos-modal-skeleton" }, /* @__PURE__ */ import_react.default.createElement(import_antd.Skeleton, { loading: true, active: true, round: true, paragraph: { rows: 3 } }), /* @__PURE__ */ import_react.default.createElement(import_antd.Skeleton, { loading: true, active: true, round: true, paragraph: { rows: 3 } })) : null,
    /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        style: {
          width: "600px",
          height: isEftposLoading ? "1px" : "auto"
        }
      },
      /* @__PURE__ */ import_react.default.createElement(
        import_EFTPos.default,
        {
          ref: eftposRef,
          onEftposLoad,
          onPayResult,
          styles: {
            deviceContainer: {
              maxHeight: (props == null ? void 0 : props.dynamicMaxHeight) || "600px"
            }
          },
          isWebPos: props == null ? void 0 : props.isWebPos
        }
      )
    )
  );
};
var EFTPOS_default = (0, import_react.forwardRef)(EFTPOS);
