function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { useEffect, useRef } from 'react';
import PaymentMethod from "../utils/payment";
import { ModeEnum } from "../../const";
import { useStoreRef } from "../../hooks";
import { getUuid } from "../../helper";
import { useDispatch, updateState } from "../../store";
import { useContextData } from "../../store";
export var usePayment = function usePayment(options) {
  var _useContextData = useContextData(),
    custom = _useContextData.custom,
    originalParams = _useContextData.originalParams;
  var _useStoreRef = useStoreRef(),
    modeRef = _useStoreRef.modeRef,
    payRef = _useStoreRef.payRef,
    orderIdRef = _useStoreRef.orderIdRef,
    eftposRef = _useStoreRef.eftposRef,
    deviceRef = _useStoreRef.deviceRef,
    dataRef = _useStoreRef.dataRef,
    amountRef = _useStoreRef.amountRef;
  var paymentMethodRef = useRef(null);
  var dispatch = useDispatch();
  var uniquePaymentNumberRef = useRef(null);
  var isPay = modeRef.current === ModeEnum.Pay || modeRef.current === ModeEnum.FullPay;
  useEffect(function () {
    if (!paymentMethodRef.current) {
      var _deviceRef$current;
      // 如果不存在交易号需要先创建一个交易号
      if (!options.paymentNum) {
        var paymentNum = getUuid();
        dispatch(updateState({
          number: paymentNum
        }));
        options.paymentNum = paymentNum;
        uniquePaymentNumberRef.current = paymentNum;
      } else {
        uniquePaymentNumberRef.current = options.paymentNum;
      }
      options.orderId = orderIdRef.current;
      options.deviceId = (_deviceRef$current = deviceRef.current) === null || _deviceRef$current === void 0 ? void 0 : _deviceRef$current.id;
      paymentMethodRef.current = new PaymentMethod(options);
    }
    return function () {
      var _paymentMethodRef$cur, _paymentMethodRef$cur2;
      // 卸载组件时，如果paymentMethodRef存在需要先执行销毁，再清空
      (_paymentMethodRef$cur = paymentMethodRef.current) === null || _paymentMethodRef$cur === void 0 || (_paymentMethodRef$cur2 = _paymentMethodRef$cur.destroy) === null || _paymentMethodRef$cur2 === void 0 || _paymentMethodRef$cur2.call(_paymentMethodRef$cur);
      paymentMethodRef.current = null;
    };
  }, []);
  var run = function run() {
    var _payRef$current, _deviceRef$current2, _options$config;
    var params = {
      order_id: orderIdRef.current,
      unique_payment_number: uniquePaymentNumberRef.current,
      payment_code: options.paymentCode,
      amount: isPay ? (_payRef$current = payRef.current) === null || _payRef$current === void 0 ? void 0 : _payRef$current.total : amountRef.current,
      operator_id: custom === null || custom === void 0 ? void 0 : custom.operator_id,
      operator_type: custom === null || custom === void 0 ? void 0 : custom.operator_type,
      platform: custom === null || custom === void 0 ? void 0 : custom.platform,
      eftpos: {
        card_reader_type: eftposRef.current,
        card_reader_id: (_deviceRef$current2 = deviceRef.current) === null || _deviceRef$current2 === void 0 ? void 0 : _deviceRef$current2.id,
        pay_param_base64: encodeURIComponent(btoa(encodeURIComponent(JSON.stringify(_objectSpread(_objectSpread({}, dataRef.current), {}, {
          number: uniquePaymentNumberRef.current
        })))))
      }
    };
    if (options !== null && options !== void 0 && (_options$config = options.config) !== null && _options$config !== void 0 && _options$config.requestDelaySeconds) {
      params.delay_seconds = options.config.requestDelaySeconds;
    }
    if (isPay) {
      var _payRef$current2, _payRef$current3, _payRef$current4, _paymentMethodRef$cur3;
      params.payment = {
        original_amount: (_payRef$current2 = payRef.current) === null || _payRef$current2 === void 0 ? void 0 : _payRef$current2.amount,
        order_payment_type: 'normal',
        // TODO: 需要使用方提供
        custom_payment_id: custom === null || custom === void 0 ? void 0 : custom.custom_payment_id,
        custom_payment_code: custom === null || custom === void 0 ? void 0 : custom.custom_payment_code,
        custom_payment_name: custom === null || custom === void 0 ? void 0 : custom.custom_payment_name,
        custom_payment_type: custom === null || custom === void 0 ? void 0 : custom.custom_payment_type,
        service_charge: {
          amount: (_payRef$current3 = payRef.current) === null || _payRef$current3 === void 0 ? void 0 : _payRef$current3.surMoney,
          percentage: (_payRef$current4 = payRef.current) === null || _payRef$current4 === void 0 ? void 0 : _payRef$current4.surPercentRate
        },
        metadata: {
          unique_payment_number: uniquePaymentNumberRef.current
        }
      };
      (_paymentMethodRef$cur3 = paymentMethodRef.current) === null || _paymentMethodRef$cur3 === void 0 || _paymentMethodRef$cur3.pay(params);
    } else {
      var _paymentMethodRef$cur4;
      var refundPaymentContext = originalParams === null || originalParams === void 0 ? void 0 : originalParams.refund_payment_context;
      if (refundPaymentContext) {
        var _refundPaymentContext;
        // 需要给退款上下文中支付方式注入交易号
        var payments = refundPaymentContext === null || refundPaymentContext === void 0 || (_refundPaymentContext = refundPaymentContext.payments) === null || _refundPaymentContext === void 0 ? void 0 : _refundPaymentContext.map(function (payment) {
          return _objectSpread(_objectSpread({}, payment), {}, {
            metadata: _objectSpread(_objectSpread({}, (payment === null || payment === void 0 ? void 0 : payment.metadata) || {}), {}, {
              unique_refund_number: uniquePaymentNumberRef.current
            })
          });
        });
        params.refund_payment_context = _objectSpread(_objectSpread({}, refundPaymentContext || {}), {}, {
          payments: payments
        });
      }
      (_paymentMethodRef$cur4 = paymentMethodRef.current) === null || _paymentMethodRef$cur4 === void 0 || _paymentMethodRef$cur4.refund(params);
    }
  };
  var query = function query() {
    if (isPay) {
      var _paymentMethodRef$cur5;
      (_paymentMethodRef$cur5 = paymentMethodRef.current) === null || _paymentMethodRef$cur5 === void 0 || _paymentMethodRef$cur5.payQuery();
    } else {
      var _paymentMethodRef$cur6;
      (_paymentMethodRef$cur6 = paymentMethodRef.current) === null || _paymentMethodRef$cur6 === void 0 || _paymentMethodRef$cur6.refundQuery();
    }
  };
  var action = function action(data, config) {
    var params = {
      order_id: orderIdRef.current,
      unique_payment_number: uniquePaymentNumberRef.current,
      payment_code: options.paymentCode,
      data: data
    };
    if (isPay) {
      var _paymentMethodRef$cur7;
      (_paymentMethodRef$cur7 = paymentMethodRef.current) === null || _paymentMethodRef$cur7 === void 0 || _paymentMethodRef$cur7.payAction(params, config);
    } else {
      var _paymentMethodRef$cur8;
      (_paymentMethodRef$cur8 = paymentMethodRef.current) === null || _paymentMethodRef$cur8 === void 0 || _paymentMethodRef$cur8.refundAction(params, config);
    }
  };
  return {
    run: run,
    query: query,
    action: action,
    _instance: paymentMethodRef.current
  };
};