var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/pay/toC/payGroup.ts
var payGroup_exports = {};
__export(payGroup_exports, {
  buildPayGroupStashToken: () => buildPayGroupStashToken,
  clearStashPayGroup: () => clearStashPayGroup,
  getPayGroupFromStorage: () => getPayGroupFromStorage,
  getPayGroupParamsFromStorage: () => getPayGroupParamsFromStorage,
  removePayGroupFromStorage: () => removePayGroupFromStorage,
  setPayNumber: () => setPayNumber,
  syncPayGroupToStorage: () => syncPayGroupToStorage
});
module.exports = __toCommonJS(payGroup_exports);
var import_serve = require("./serve");
var STORAGE_KEY = "payGroup";
var STASH_RETRY_DELAY_MS = 400;
var STASH_MAX_ATTEMPTS = 3;
var delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
var createPayGroupKey = (orderId) => {
  return `${STORAGE_KEY}_${orderId}`;
};
var buildPayGroupStashToken = (orderId) => {
  return `payGroup_stash_${orderId}`;
};
var formatWalletCards = (cards) => {
  return cards.map((card) => {
    return {
      id: card.id,
      available_max_amount: card.actualDeduction ?? card.available_max_amount,
      available_max_par_value: card.available_max_par_value,
      tag: card.tag,
      wallet_pass_usage_unit: card.wallet_pass_usage_unit,
      metadata: card.metadata
    };
  });
};
var stashPayGroupWithRetry = async (payGroup, orderId, tokenStr) => {
  var _a;
  const token = buildPayGroupStashToken(orderId);
  for (let attempt = 0; attempt < STASH_MAX_ATTEMPTS; attempt++) {
    try {
      await (0, import_serve.postCoreStash)({ token, data: { ...payGroup, token: tokenStr, orderDetail: void 0, otherPayment: void 0, walletPass: {
        ...(payGroup == null ? void 0 : payGroup.walletPass) || {},
        noApplicableVoucher: [],
        transformList: [],
        cards: formatWalletCards(((_a = payGroup == null ? void 0 : payGroup.walletPass) == null ? void 0 : _a.cards) || [])
      } }, ttl: 3600 });
      return true;
    } catch {
      if (attempt === STASH_MAX_ATTEMPTS - 1) {
        return false;
      }
      await delay(STASH_RETRY_DELAY_MS);
    }
  }
  return false;
};
var syncPayGroupToStorage = (payGroup) => {
  localStorage.setItem(createPayGroupKey(payGroup.order_id), JSON.stringify(payGroup));
};
var getPayGroupFromStorage = (orderId) => {
  const key = createPayGroupKey(orderId);
  const payGroupStr = localStorage.getItem(key);
  if (payGroupStr) {
    return JSON.parse(payGroupStr);
  }
  return null;
};
var removePayGroupFromStorage = (orderId) => {
  const key = createPayGroupKey(orderId);
  localStorage.removeItem(key);
};
var getPayGroupParamsFromStorage = (orderId, rest) => {
  try {
    const payGroup = getPayGroupFromStorage(orderId);
    if (!payGroup) {
      return null;
    }
    return {
      ...rest,
      amount: payGroup == null ? void 0 : payGroup.wait_amount,
      payment_group_id: payGroup == null ? void 0 : payGroup.payment_group_id
    };
  } catch (err) {
  }
  return null;
};
var setPayNumber = async (orderId, payNumber, token) => {
  const payGroup = getPayGroupFromStorage(orderId);
  if (!payGroup || !orderId) {
    return;
  }
  const merged = { ...payGroup, pay_number: payNumber };
  await stashPayGroupWithRetry(merged, orderId, token);
  syncPayGroupToStorage(merged);
};
var clearStashPayGroup = async (orderId) => {
  const token = buildPayGroupStashToken(orderId);
  try {
    await (0, import_serve.postCoreStash)({ token, data: {
      status: "done"
    }, ttl: 3600 });
  } catch (err) {
    console.error("清除服务端暂存失败", err);
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildPayGroupStashToken,
  clearStashPayGroup,
  getPayGroupFromStorage,
  getPayGroupParamsFromStorage,
  removePayGroupFromStorage,
  setPayNumber,
  syncPayGroupToStorage
});
