import { useMemo } from 'react';
import { useRequest } from 'ahooks';
import { getPaymentMethods } from "../serve";
var paymentMethodsRequest = null;
function requestPaymentMethods() {
  if (!paymentMethodsRequest) {
    paymentMethodsRequest = getPaymentMethods().catch(function (error) {
      paymentMethodsRequest = null;
      throw error;
    });
  }
  return paymentMethodsRequest;
}
export function usePaymentMethodNameMap() {
  var _useRequest = useRequest(requestPaymentMethods, {
      cacheKey: 'pisell-sales-management-payment-methods'
    }),
    data = _useRequest.data;
  var paymentMethods = useMemo(function () {
    var list = data === null || data === void 0 ? void 0 : data.data;
    return Array.isArray(list) ? list : [];
  }, [data]);
  return useMemo(function () {
    return paymentMethods.reduce(function (map, item) {
      var code = String((item === null || item === void 0 ? void 0 : item.code) || '').trim();
      var name = String((item === null || item === void 0 ? void 0 : item.name) || '').trim();
      if (code && name) {
        map[code] = name;
      }
      return map;
    }, {});
  }, [paymentMethods]);
}
export function resolvePaymentMethodName(payment, paymentMethodNameMap) {
  var paymentCode = String((payment === null || payment === void 0 ? void 0 : payment.code) || (payment === null || payment === void 0 ? void 0 : payment.payment_method) || '').trim();
  var customPaymentId = String((payment === null || payment === void 0 ? void 0 : payment.custom_payment_id) || '').trim();
  return paymentCode ? paymentMethodNameMap[paymentCode] || paymentCode : customPaymentId || '-';
}