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 optionsRef = (0, import_react.useRef)(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;
  const initPayment = () => {
    var _a, _b, _c;
    if (!paymentMethodRef.current) {
      if (!((_a = optionsRef.current) == null ? void 0 : _a.paymentNum)) {
        const paymentNum = (0, import_helper.getUuid)();
        dispatch((0, import_store.updateState)({ number: paymentNum }));
        optionsRef.current.paymentNum = paymentNum;
        uniquePaymentNumberRef.current = paymentNum;
      } else {
        uniquePaymentNumberRef.current = (_b = optionsRef.current) == null ? void 0 : _b.paymentNum;
      }
      optionsRef.current.orderId = orderIdRef.current;
      optionsRef.current.deviceId = (_c = deviceRef.current) == null ? void 0 : _c.id;
      console.log("optionsRef.current =>>>> ", optionsRef.current);
      paymentMethodRef.current = new import_payment.default(optionsRef.current);
    }
  };
  (0, import_react.useEffect)(() => {
    return () => {
      var _a, _b;
      (_b = (_a = paymentMethodRef.current) == null ? void 0 : _a.destroy) == null ? void 0 : _b.call(_a);
      paymentMethodRef.current = null;
    };
  }, []);
  const run = (runParams) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
    initPayment();
    const extraParams = {
      ...(runParams == null ? void 0 : runParams.params) || {},
      ...((_a = runParams == null ? void 0 : runParams.getParams) == null ? void 0 : _a.call(runParams, {
        uniquePaymentNumber: uniquePaymentNumberRef.current
      })) || {}
    };
    let params = {
      order_id: orderIdRef.current,
      unique_payment_number: uniquePaymentNumberRef.current,
      payment_code: (_b = optionsRef.current) == null ? void 0 : _b.paymentCode,
      amount: isPay ? (_c = payRef.current) == null ? void 0 : _c.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: (_d = deviceRef.current) == null ? void 0 : _d.id,
        pay_param_base64: encodeURIComponent(
          btoa(
            encodeURIComponent(
              JSON.stringify({
                ...dataRef.current,
                number: uniquePaymentNumberRef.current
              })
            )
          )
        )
      }
    };
    if ((_e = options == null ? void 0 : options.config) == null ? void 0 : _e.requestDelaySeconds) {
      params.delay_seconds = options.config.requestDelaySeconds;
    }
    if (isPay) {
      params.payment = {
        original_amount: (_f = payRef.current) == null ? void 0 : _f.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: (_g = payRef.current) == null ? void 0 : _g.surMoney,
          percentage: (_h = payRef.current) == null ? void 0 : _h.surPercentRate
        },
        metadata: {
          unique_payment_number: uniquePaymentNumberRef.current
        }
      };
      if (Object.keys(extraParams).length) {
        params = { ...params, ...extraParams };
      }
      (_i = paymentMethodRef.current) == null ? void 0 : _i.pay(params, runParams == null ? void 0 : runParams.config);
    } else {
      if (Object.keys(extraParams).length) {
        params = { ...params, ...extraParams };
      }
      const refundPaymentContext = originalParams == null ? void 0 : originalParams.refund_payment_context;
      if (refundPaymentContext) {
        const payments = (_j = refundPaymentContext == null ? void 0 : refundPaymentContext.payments) == null ? void 0 : _j.map((payment) => {
          return {
            ...payment,
            metadata: {
              ...(payment == null ? void 0 : payment.metadata) || {},
              unique_refund_number: uniquePaymentNumberRef.current
            }
          };
        });
        params.refund_payment_context = {
          ...refundPaymentContext || {},
          payments
        };
      }
      (_k = paymentMethodRef.current) == null ? void 0 : _k.refund(params, runParams == null ? void 0 : runParams.config);
    }
  };
  const query = () => {
    var _a, _b;
    initPayment();
    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, _c;
    initPayment();
    const params = {
      order_id: orderIdRef.current,
      unique_payment_number: uniquePaymentNumberRef.current,
      payment_code: (_a = optionsRef.current) == null ? void 0 : _a.paymentCode,
      data
    };
    if (isPay) {
      (_b = paymentMethodRef.current) == null ? void 0 : _b.payAction(params, config);
    } else {
      (_c = paymentMethodRef.current) == null ? void 0 : _c.refundAction(params, config);
    }
  };
  const reset = () => {
    var _a, _b;
    (_b = (_a = paymentMethodRef.current) == null ? void 0 : _a.stop) == null ? void 0 : _b.call(_a);
    paymentMethodRef.current = null;
    uniquePaymentNumberRef.current = null;
    optionsRef.current.paymentNum = "";
  };
  return { run, query, action, reset, _instance: paymentMethodRef.current };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  usePayment
});
