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/aggregatePayment/hooks/useMicropay.ts
var useMicropay_exports = {};
__export(useMicropay_exports, {
  default: () => useMicropay_default
});
module.exports = __toCommonJS(useMicropay_exports);
var import_react = require("react");
var import_payment = __toESM(require("../utils/payment"));
var import_const = require("../../const");
var import_hooks = require("../../hooks");
var import_helper = require("../../helper");
var import_store = require("../../store");
var import_store2 = require("../../store");
var useMicropay = (options) => {
  const { custom } = (0, import_store2.useContextData)();
  const { modeRef, payRef, orderIdRef, eftposRef, deviceRef, amountRef } = (0, import_hooks.useStoreRef)();
  const paymentMethodRef = (0, import_react.useRef)(null);
  const dispatch = (0, import_store.useDispatch)();
  const uniquePaymentNumberRef = (0, import_react.useRef)(null);
  const isPay = modeRef.current === import_const.ModeEnum.Pay || modeRef.current === import_const.ModeEnum.FullPay;
  (0, import_react.useEffect)(() => {
    var _a;
    if (!paymentMethodRef.current) {
      if (!options.paymentNum) {
        const paymentNum = (0, import_helper.getUuid)();
        dispatch((0, import_store.updateState)({ number: paymentNum }));
        options.paymentNum = paymentNum;
        uniquePaymentNumberRef.current = paymentNum;
      } else {
        uniquePaymentNumberRef.current = options.paymentNum;
      }
      options.orderId = orderIdRef.current;
      options.deviceId = (_a = deviceRef.current) == null ? void 0 : _a.id;
      paymentMethodRef.current = new import_payment.default(options);
    }
    return () => {
      var _a2, _b;
      (_b = (_a2 = paymentMethodRef.current) == null ? void 0 : _a2.destroy) == null ? void 0 : _b.call(_a2);
      paymentMethodRef.current = null;
    };
  }, []);
  const run = (runParams) => {
    var _a, _b, _c, _d, _e, _f, _g;
    const params = {
      order_id: orderIdRef.current,
      unique_payment_number: uniquePaymentNumberRef.current,
      payment_code: options.paymentCode,
      amount: isPay ? (_a = payRef.current) == null ? void 0 : _a.total : amountRef.current,
      operator_id: custom == null ? void 0 : custom.operator_id,
      operator_type: custom == null ? void 0 : custom.operator_type,
      platform: custom == null ? void 0 : custom.platform,
      micropay: {
        card_reader_type: eftposRef.current,
        card_reader_id: (_b = deviceRef.current) == null ? void 0 : _b.id
      }
    };
    if (isPay) {
      params.payment = {
        original_amount: (_c = payRef.current) == null ? void 0 : _c.amount,
        order_payment_type: "normal",
        // TODO: 需要使用方提供
        custom_payment_id: custom == null ? void 0 : custom.custom_payment_id,
        custom_payment_code: custom == null ? void 0 : custom.custom_payment_code,
        custom_payment_name: custom == null ? void 0 : custom.custom_payment_name,
        custom_payment_type: custom == null ? void 0 : custom.custom_payment_type,
        service_charge: {
          amount: (_d = payRef.current) == null ? void 0 : _d.surMoney,
          percentage: (_e = payRef.current) == null ? void 0 : _e.surPercentRate
        },
        metadata: {
          unique_payment_number: uniquePaymentNumberRef.current
        }
      };
      params.micropay.auth_code = (runParams == null ? void 0 : runParams.authCode) || "";
      (_f = paymentMethodRef.current) == null ? void 0 : _f.pay(params);
    } else {
      params.micropay.order_payment_id = (runParams == null ? void 0 : runParams.orderPaymentId) || "";
      (_g = paymentMethodRef.current) == null ? void 0 : _g.refund(params);
    }
  };
  const query = () => {
    var _a, _b;
    if (isPay) {
      (_a = paymentMethodRef.current) == null ? void 0 : _a.payQuery();
    } else {
      (_b = paymentMethodRef.current) == null ? void 0 : _b.refundQuery();
    }
  };
  const action = (data) => {
    var _a, _b;
    const params = {
      order_id: orderIdRef.current,
      unique_payment_number: uniquePaymentNumberRef.current,
      payment_code: options.paymentCode,
      data
    };
    if (isPay) {
      (_a = paymentMethodRef.current) == null ? void 0 : _a.payAction(params);
    } else {
      (_b = paymentMethodRef.current) == null ? void 0 : _b.refundAction(params);
    }
  };
  return { run, query, action };
};
var useMicropay_default = useMicropay;
