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/checkout/components/WalletPassModule/utils.tsx
var utils_exports = {};
__export(utils_exports, {
  formatDiscountWalletData: () => formatDiscountWalletData,
  formatMachineCodeServer2CardList: () => formatMachineCodeServer2CardList,
  getAvailableMaxAmount: () => getAvailableMaxAmount,
  getPointCardAmount: () => getPointCardAmount,
  isSameContent: () => isSameContent,
  voucherTime: () => voucherTime
});
module.exports = __toCommonJS(utils_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var import_materials = require("@pisell/materials");
var import_decimal = __toESM(require("decimal.js"));
var Amount = import_materials.PisellText.Amount;
var getAvailableMaxAmount = (data) => {
  return data.tag !== "point_card" ? data.available_max_amount : data == null ? void 0 : data.recommended_usage_amount;
};
var voucherTime = (data, key = "expire_date") => {
  const config = import_materials.globalConfig.getConfig();
  if (data[key]) {
    return (0, import_utils.formatDate)(
      data[key],
      (config == null ? void 0 : config.locale) === "en" ? "DD/MM/YYYY" : "YYYY-MM-DD"
    );
  }
  return import_utils.locales.getText("pisell2.text.walletPassPermanent");
};
var formatMachineCodeServer2CardList = (data, selectId = [], otherProps = {}) => {
  return (data || []).map((item) => {
    var _a, _b;
    return {
      id: item.id,
      name: item.product_name,
      // storeName: item.shop?.name,
      balance: item.balance_par_value,
      cardStatus: false,
      redeem: item.edit_current_amount !== void 0 ? item.edit_current_amount : getAvailableMaxAmount(item),
      maxRedeem: getAvailableMaxAmount(item),
      showRedeem: selectId.includes(item.id),
      showStoreName: false,
      showBalanceSymbol: item.tag !== "point_card",
      showActions: false,
      showDetail: false,
      showQrCode: false,
      showStatus: true,
      code: item.code,
      disabled: !item.unified_available_status,
      disabledReason: (_a = item.unified_messages) == null ? void 0 : _a.title,
      cover: (_b = item.product) == null ? void 0 : _b.cover,
      qrCode: item.code,
      validDate: voucherTime(item),
      originData: item,
      ...otherProps
    };
  });
};
var formatDiscountWalletData = (data, selectId = []) => {
  return (data || []).map((item) => {
    var _a, _b, _c;
    let name = "";
    let storeName = "";
    if (item.tag === "product_discount_card" || item.tag === "discount_card") {
      if (((_a = item == null ? void 0 : item.metadata) == null ? void 0 : _a.discount_card_type) === "fixed_amount") {
        const amountValue = (item == null ? void 0 : item.savedAmount) ? item == null ? void 0 : item.savedAmount : item.par_value;
        name = /* @__PURE__ */ import_react.default.createElement("span", null, import_utils.locales.getText("pisell2.checkout.discount.fixed_amount"), " ", /* @__PURE__ */ import_react.default.createElement(Amount, { value: amountValue, showCurrencySymbol: true }));
      } else {
        name = import_utils.locales.getText("pisell2.checkout.discount.percentage.off")(item.par_value || item.discount.percent);
      }
      storeName = item.product_name || item.format_title.auto;
    } else if (item.tag === "good_pass") {
      name = item.product_name;
      const left = Number(item.balance) || 0;
      const use = 1;
      storeName = (_b = import_utils.locales.getText("pisell2.checkout.wallet-pass.good-pass.format")) == null ? void 0 : _b(left, use);
    } else {
      name = item.product_name;
    }
    if (!name && item.isEditMode) {
      name = item.amount;
      storeName = item.format_title.auto;
    }
    return {
      ...item,
      id: item.id,
      name,
      storeName,
      balance: item.balance_par_value,
      showBalance: false,
      showStoreName: true,
      redeem: getAvailableMaxAmount(item),
      showRedeem: selectId.includes(item.id),
      showBalanceSymbol: item.tag !== "point_card",
      showActions: false,
      error: item.isDisabled,
      showDisabledReason: false,
      showDetail: false,
      showQrCode: false,
      code: item.code,
      disabled: item.isDisabled,
      showStatus: true,
      cardStatus: item.isEditMode ? "lastPayment" : item.isDisabled ? "conflict" : void 0,
      cover: (_c = item.product) == null ? void 0 : _c.cover,
      qrCode: item.code,
      validDate: voucherTime(item, "expire_time"),
      originData: item,
      enableEditAmount: false
    };
  });
};
var getPointCardAmount = (walletPassValue, walletPassUsageUnit) => {
  if (!walletPassUsageUnit.points || !walletPassUsageUnit.price) {
    return new import_decimal.default(walletPassValue).toFixed(2);
  }
  const baseRatio = new import_decimal.default(walletPassValue).div(walletPassUsageUnit.points);
  return baseRatio.mul(walletPassUsageUnit.price).toFixed(2, import_decimal.default.ROUND_DOWN);
};
var isSameContent = (arr1, arr2) => {
  return [...arr1].sort().join(",") === [...arr2].sort().join(",");
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  formatDiscountWalletData,
  formatMachineCodeServer2CardList,
  getAvailableMaxAmount,
  getPointCardAmount,
  isSameContent,
  voucherTime
});
