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 { Decimal } from 'decimal.js';
import { isTripartitePayment } from "./status";
import dayjs from "dayjs";
import { getUniqueId } from "@pisell/utils";
/**
 * 计算walletPass列表数据的总金额
 * @param list
 */
export var calcWalletPassListAmount = function calcWalletPassListAmount(list) {
  return list.reduce(function (pre, cur) {
    return pre.add(new Decimal(String(getAvailableMaxAmount(cur) || 0)));
  }, new Decimal('0')).toNumber();
};

/**
 * 将walletPass列表数据转换为prepare_payments需要的数据格式
 * @param list
 */
export var formatWalletPassList2PreparePayments = function formatWalletPassList2PreparePayments(list) {
  return (list || []).map(function (item) {
    return {
      voucher_id: item.id || 0,
      amount: Number(getAvailableMaxAmount(item)) || 0,
      tag: item.tag || ''
    };
  });
};

/**
 *  walletPass支付params
 * @param payGroup
 */
export var formatWalletPassParams = function formatWalletPassParams(payGroup) {
  var order_id = payGroup.order_id,
    payment_group_id = payGroup.payment_group_id,
    walletPass = payGroup.walletPass;
  var payments = (walletPass === null || walletPass === void 0 ? void 0 : walletPass.cards.map(function (item) {
    var _item$metadata;
    return {
      amount: getAvailableMaxAmount(item),
      wallet_pass_use_value: item.recommended_usage_par_value,
      voucher_id: item.id,
      tag: item.tag,
      wallet_pass_usage_unit: item.wallet_pass_usage_unit,
      metadata: {
        unique_payment_number: (_item$metadata = item.metadata) === null || _item$metadata === void 0 ? void 0 : _item$metadata.unique_payment_number
      }
    };
  })) || [];
  return {
    order_id: order_id,
    payment_group_id: payment_group_id,
    payments: payments
  };
};

/**
 * @title: 获取是否walletPass和三方支付的组合支付
 * @description: 三方支付 (stripe  支付宝 微信  苹果 谷歌)
 * @return: *
 * @Author: shengjie.zuo
 * @Date: 2024-08-30 10:06:17
 */
export var getIsWalletPassAndTripartitePayment = function getIsWalletPassAndTripartitePayment(payGroup) {
  var _payGroup$walletPass, _payGroup$otherPaymen;
  return (payGroup === null || payGroup === void 0 || (_payGroup$walletPass = payGroup.walletPass) === null || _payGroup$walletPass === void 0 || (_payGroup$walletPass = _payGroup$walletPass.cards) === null || _payGroup$walletPass === void 0 ? void 0 : _payGroup$walletPass.length) && isTripartitePayment(payGroup === null || payGroup === void 0 || (_payGroup$otherPaymen = payGroup.otherPayment) === null || _payGroup$otherPaymen === void 0 ? void 0 : _payGroup$otherPaymen.service);
};
export var isFranchiseeShop = function isFranchiseeShop(path, history) {
  var pathname = history.location.pathname;
  var _pathname = path || pathname;
  return _pathname && _pathname.includes('/franchisee');
};

/**
 * @title: 获取当前连锁加盟店铺shopid
 * @description:
 * @param {any} param1
 * @return {*}
 * @Author: chensheng.gu
 * @Date: 2024-08-07 10:04
 */
export var getFranchiseeShopId = function getFranchiseeShopId(path, history) {
  var pathname = history.location.pathname;
  var _pathname = path || pathname;
  var match = _pathname.match(/\/shop_(\d+)\//);
  var shopNumber = match ? match[1] : null;
  return Number(shopNumber);
};
export var formatFranchiseeRoute = function formatFranchiseeRoute(url, history) {
  if (isFranchiseeShop(null, history)) {
    return "/franchisee/shop/shop_".concat(getFranchiseeShopId(null, history)).concat(url);
  }
  return url;
};

// 获取walletPass可用最大金额
export var getAvailableMaxAmount = function getAvailableMaxAmount(data) {
  return data.tag !== 'point_card' ? data.available_max_amount : data === null || data === void 0 ? void 0 : data.recommended_usage_amount;
};
export var clientPayment = function clientPayment(params) {
  var _interaction$utils, _interaction$utils$po;
  var interaction = params.interaction,
    type = params.type,
    order_id = params.order_id,
    values = params.values;
  interaction === null || interaction === void 0 || (_interaction$utils = interaction.utils) === null || _interaction$utils === void 0 || (_interaction$utils$po = _interaction$utils.postMessageToApp) === null || _interaction$utils$po === void 0 || _interaction$utils$po.call(_interaction$utils, {
    module: 'global',
    key: 'paymentClient',
    data: _objectSpread({
      type: type,
      order_id: order_id
    }, values)
  });
};

/**
 * 获取唯一幂等token
 * @returns
 */
export var getUniqueIdempotencyToken = function getUniqueIdempotencyToken() {
  return "".concat(dayjs().format('YYYYMMDDHHmmssSSS'), "-").concat(getUniqueId());
};