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/eftposPay/huifu/index.tsx
var huifu_exports = {};
__export(huifu_exports, {
  default: () => huifu_default
});
module.exports = __toCommonJS(huifu_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var import_index = require("./index.less");
var import_store = require("../store");
var import_Action = require("./Action");
var import_aggregatePayment = require("../aggregatePayment");
var import_types = require("../aggregatePayment/types");
var import_hooks = require("../hooks");
var import_store2 = require("../store");
var import_const = require("../const");
var import_helper = require("../helper");
var import_useEngineContext = __toESM(require("../../../hooks/useEngineContext"));
var Huifu = ({ isNativeApp, onChange }) => {
  var _a, _b;
  const data = (0, import_store.useContextData)();
  const context = (0, import_useEngineContext.default)();
  const { interaction } = ((_a = context.appHelper) == null ? void 0 : _a.utils) || {};
  const {
    statusRef,
    numRef,
    netRef,
    modeRef,
    amountRef,
    orderIdRef,
    deviceRef,
    payRef
  } = (0, import_hooks.useStoreRef)();
  const dispatch = (0, import_store2.useDispatch)();
  const fail = (0, import_hooks.useFail)(0);
  const failRef = (0, import_react.useRef)(fail);
  const initRef = (0, import_react.useRef)(false);
  const unloadRef = (0, import_react.useRef)(false);
  const clearRef = (0, import_react.useRef)(null);
  const payment = (0, import_aggregatePayment.useMicropay)({
    paymentCode: ((_b = data == null ? void 0 : data.custom) == null ? void 0 : _b.custom_payment_code) || "EFTPOS_HUIFU",
    paymentNum: numRef.current || "",
    shouldContinue: () => !(unloadRef.current || !netRef.current),
    callback: (_instance, response) => {
      var _a2;
      console.log("callback = ", response);
      const { status, data: data2 } = response || {};
      if (status === import_types.EPaymentStatus.Success) {
        dispatch((0, import_store2.updateNextStep)());
        dispatch((0, import_store2.updateCustom)({ card_reader_surcharge: 0, receipt: [] }));
        dispatch(
          (0, import_store.updateState)({
            status: "success",
            title: import_utils.locales.getText("page.eftpos-pay.text.successful")(
              modeRef.current
            )
          })
        );
        clearRef.current = setTimeout(() => {
          const params = (0, import_helper.getPayParams)(
            {
              mode: modeRef.current,
              amount: amountRef.current,
              order_id: orderIdRef.current,
              number: numRef.current,
              device: deviceRef.current,
              pay: payRef.current,
              custom: {
                card_reader_surcharge: 0,
                receipt: []
              }
            },
            "success"
          );
          onChange("success", { ...params, receipt: [] });
        }, 1e3);
      } else if (status === import_types.EPaymentStatus.Failed) {
        (_a2 = failRef.current) == null ? void 0 : _a2.call(failRef, data2, false);
      }
    }
  });
  (0, import_react.useEffect)(() => {
    failRef.current = fail;
  }, [fail]);
  (0, import_react.useEffect)(() => {
    var _a2;
    if (statusRef.current === import_const.StatusEnum.Success || statusRef.current === import_const.StatusEnum.Fail) {
      return;
    }
    if (initRef.current) {
      return;
    }
    initRef.current = true;
    if (numRef.current) {
      console.log("payment.query");
      payment.query();
      return;
    }
    console.log("data?.custom =>>>> ", data == null ? void 0 : data.custom);
    const isPay = modeRef.current === import_const.ModeEnum.Pay || modeRef.current === import_const.ModeEnum.FullPay;
    if (!isPay) {
      payment.run({ orderPaymentId: (_a2 = data == null ? void 0 : data.custom) == null ? void 0 : _a2.order_payment_id });
      return;
    }
  }, []);
  (0, import_react.useEffect)(() => {
    var _a2, _b2;
    if (isNativeApp && (data == null ? void 0 : data.status) === import_const.StatusEnum.Init) {
      (_b2 = (_a2 = interaction == null ? void 0 : interaction.utils) == null ? void 0 : _a2.postMessageToApp) == null ? void 0 : _b2.call(_a2, {
        module: "pos",
        key: "other",
        data: {
          key: "show_close_button"
        }
      });
    }
  }, [isNativeApp, data == null ? void 0 : data.status]);
  (0, import_react.useEffect)(() => {
    return () => {
      unloadRef.current = true;
      if (clearRef.current) {
        clearTimeout(clearRef.current);
      }
    };
  }, []);
  const updateToLoading = () => {
    var _a2, _b2;
    if (isNativeApp) {
      (_b2 = (_a2 = interaction == null ? void 0 : interaction.utils) == null ? void 0 : _a2.postMessageToApp) == null ? void 0 : _b2.call(_a2, {
        module: "pos",
        key: "other",
        data: {
          key: "hide_close_button"
        }
      });
    }
    dispatch((0, import_store.updateStatus)(import_const.StatusEnum.Loading));
    dispatch(
      (0, import_store.updateState)({
        title: import_utils.locales.getText("page.eftpos-pay.text.loading")(modeRef.current)
      })
    );
  };
  const onClose = () => {
    onChange("fail", {
      number: data.number,
      uniquePaymentNumber: data.number,
      device: data.device,
      order_id: data.order_id
    });
  };
  const onPayClick = (scanResult) => {
    console.log("scanResult = ", scanResult);
    updateToLoading();
    payment.run({ authCode: scanResult });
  };
  const onScanCallback = (scanResult) => {
    console.log("scanResult = ", scanResult);
    updateToLoading();
    payment.run({ authCode: scanResult });
  };
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, (data == null ? void 0 : data.status) === import_const.StatusEnum.Init && /* @__PURE__ */ import_react.default.createElement(
    import_Action.ScanAction,
    {
      onClose,
      onScanCallback,
      onPayClick,
      isNativeApp
    }
  ), (data == null ? void 0 : data.status) === import_const.StatusEnum.Loading ? /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-tip" }, import_utils.locales.getText("page.eftpos-pay.text.processing")) : null);
};
var huifu_default = Huifu;
