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/usePayment.ts
var usePayment_exports = {};
__export(usePayment_exports, {
  usePayment: () => usePayment
});
module.exports = __toCommonJS(usePayment_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 usePayment = (options) => {
  const { custom, originalParams } = (0, import_store2.useContextData)();
  const {
    modeRef,
    payRef,
    orderIdRef,
    eftposRef,
    deviceRef,
    dataRef,
    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 = () => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _i;
    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,
      eftpos: {
        card_reader_type: eftposRef.current,
        card_reader_id: (_b = deviceRef.current) == null ? void 0 : _b.id,
        pay_param_base64: encodeURIComponent(
          btoa(
            encodeURIComponent(
              JSON.stringify({
                ...dataRef.current,
                number: uniquePaymentNumberRef.current
              })
            )
          )
        )
      }
    };
    if ((_c = options == null ? void 0 : options.config) == null ? void 0 : _c.requestDelaySeconds) {
      params.delay_seconds = options.config.requestDelaySeconds;
    }
    if (isPay) {
      params.payment = {
        original_amount: (_d = payRef.current) == null ? void 0 : _d.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: (_e = payRef.current) == null ? void 0 : _e.surMoney,
          percentage: (_f = payRef.current) == null ? void 0 : _f.surPercentRate
        },
        metadata: {
          unique_payment_number: uniquePaymentNumberRef.current
        }
      };
      (_g = paymentMethodRef.current) == null ? void 0 : _g.pay(params);
    } else {
      const refundPaymentContext = originalParams == null ? void 0 : originalParams.refund_payment_context;
      if (refundPaymentContext) {
        const payments = (_h = refundPaymentContext == null ? void 0 : refundPaymentContext.payments) == null ? void 0 : _h.map((payment) => {
          return {
            ...payment,
            metadata: {
              ...(payment == null ? void 0 : payment.metadata) || {},
              unique_refund_number: uniquePaymentNumberRef.current
            }
          };
        });
        params.refund_payment_context = {
          ...refundPaymentContext || {},
          payments
        };
      }
      (_i = paymentMethodRef.current) == null ? void 0 : _i.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, config) => {
    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, config);
    } else {
      (_b = paymentMethodRef.current) == null ? void 0 : _b.refundAction(params, config);
    }
  };
  return { run, query, action, _instance: paymentMethodRef.current };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  usePayment
});
