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/pay/toC/utils.ts
var utils_exports = {};
__export(utils_exports, {
  calcWalletPassListAmount: () => calcWalletPassListAmount,
  clientPayment: () => clientPayment,
  formatFranchiseeRoute: () => formatFranchiseeRoute,
  formatWalletPassList2PreparePayments: () => formatWalletPassList2PreparePayments,
  formatWalletPassParams: () => formatWalletPassParams,
  getAvailableMaxAmount: () => getAvailableMaxAmount,
  getFranchiseeShopId: () => getFranchiseeShopId,
  getIsWalletPassAndTripartitePayment: () => getIsWalletPassAndTripartitePayment,
  getUniqueIdempotencyToken: () => getUniqueIdempotencyToken,
  isFranchiseeShop: () => isFranchiseeShop
});
module.exports = __toCommonJS(utils_exports);
var import_decimal = require("decimal.js");
var import_status = require("./status");
var import_dayjs = __toESM(require("dayjs"));
var import_utils = require("@pisell/utils");
var calcWalletPassListAmount = (list) => {
  return list.reduce((pre, cur) => pre.add(new import_decimal.Decimal(String(getAvailableMaxAmount(cur) || 0))), new import_decimal.Decimal("0")).toNumber();
};
var formatWalletPassList2PreparePayments = (list) => {
  return (list || []).map((item) => {
    return {
      voucher_id: item.id || 0,
      amount: Number(getAvailableMaxAmount(item)) || 0,
      tag: item.tag || ""
    };
  });
};
var formatWalletPassParams = (payGroup) => {
  const { order_id, payment_group_id, walletPass } = payGroup;
  const payments = (walletPass == null ? void 0 : walletPass.cards.map((item) => {
    var _a;
    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: (_a = item.metadata) == null ? void 0 : _a.unique_payment_number
      }
    };
  })) || [];
  return {
    order_id,
    payment_group_id,
    payments
  };
};
var getIsWalletPassAndTripartitePayment = (payGroup) => {
  var _a, _b, _c;
  return ((_b = (_a = payGroup == null ? void 0 : payGroup.walletPass) == null ? void 0 : _a.cards) == null ? void 0 : _b.length) && (0, import_status.isTripartitePayment)((_c = payGroup == null ? void 0 : payGroup.otherPayment) == null ? void 0 : _c.service);
};
var isFranchiseeShop = (path, history) => {
  const { pathname } = history.location;
  const _pathname = path || pathname;
  return _pathname && _pathname.includes("/franchisee");
};
var getFranchiseeShopId = (path, history) => {
  const { pathname } = history.location;
  const _pathname = path || pathname;
  const match = _pathname.match(/\/shop_(\d+)\//);
  const shopNumber = match ? match[1] : null;
  return Number(shopNumber);
};
var formatFranchiseeRoute = (url, history) => {
  if (isFranchiseeShop(null, history)) {
    return `/franchisee/shop/shop_${getFranchiseeShopId(null, history)}${url}`;
  }
  return url;
};
var getAvailableMaxAmount = (data) => {
  return data.tag !== "point_card" ? data.available_max_amount : data == null ? void 0 : data.recommended_usage_amount;
};
var clientPayment = (params) => {
  var _a, _b;
  const { interaction, type, order_id, values } = params;
  (_b = (_a = interaction == null ? void 0 : interaction.utils) == null ? void 0 : _a.postMessageToApp) == null ? void 0 : _b.call(_a, {
    module: "global",
    key: "paymentClient",
    data: {
      type,
      order_id,
      ...values
    }
  });
};
var getUniqueIdempotencyToken = () => {
  return `${(0, import_dayjs.default)().format("YYYYMMDDHHmmssSSS")}-${(0, import_utils.getUniqueId)()}`;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  calcWalletPassListAmount,
  clientPayment,
  formatFranchiseeRoute,
  formatWalletPassList2PreparePayments,
  formatWalletPassParams,
  getAvailableMaxAmount,
  getFranchiseeShopId,
  getIsWalletPassAndTripartitePayment,
  getUniqueIdempotencyToken,
  isFranchiseeShop
});
