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/PaymentList/utils.ts
var utils_exports = {};
__export(utils_exports, {
  calculateSurcharge: () => calculateSurcharge,
  formatCreditCardSurcharge: () => formatCreditCardSurcharge,
  formatCreditCardSurchargeList: () => formatCreditCardSurchargeList,
  formatCreditCardSurchargeText: () => formatCreditCardSurchargeText,
  isCreditCardSurchargeSame: () => isCreditCardSurchargeSame
});
module.exports = __toCommonJS(utils_exports);
var import_visa = __toESM(require("./payment/visa.png"));
var import_credit_card = __toESM(require("./payment/credit_card.png"));
var isCreditCardSurchargeSame = (creditCardConfig) => {
  try {
    const configMap = {};
    creditCardConfig.forEach((config) => {
      const key = `${(config == null ? void 0 : config.credit_card_type) || "-"}_${(config == null ? void 0 : config.area) || "-"}`;
      configMap[key] = config;
    });
    const visaDomestic = configMap["visa_domestic"];
    const visaWorld = configMap["visa_world"];
    const masterDomestic = configMap["master_domestic"];
    const masterWorld = configMap["master_world"];
    const formatFee = (config) => {
      if (!config) return null;
      const percent = parseFloat(config.percent ?? config.percentage ?? "0");
      const amount = parseFloat(config.amount ?? "0");
      return {
        percentStr: percent,
        amountStr: amount
      };
    };
    const visaDomesticFee = formatFee(visaDomestic);
    const visaWorldFee = formatFee(visaWorld);
    const masterDomesticFee = formatFee(masterDomestic);
    const masterWorldFee = formatFee(masterWorld);
    const isFeeSame = (fee1, fee2) => {
      if (!fee1 || !fee2) return false;
      return fee1.percentStr === fee2.percentStr && fee1.amountStr === fee2.amountStr;
    };
    const isDomesticSame = isFeeSame(visaDomesticFee, masterDomesticFee);
    const isWorldSame = isFeeSame(visaWorldFee, masterWorldFee);
    const isAllSame = isDomesticSame && isWorldSame;
    return isAllSame;
  } catch (error) {
    return false;
  }
};
var totalFee = (config, amount) => {
  const percentageFee = parseFloat(config.percent ?? config.percentage) * Number(amount);
  const totalFee2 = percentageFee + parseFloat(config.amount);
  return totalFee2;
};
var formatCreditCardSurcharge = (item, amount, isSame) => {
  try {
    const { surcharge_config, currency_symbol } = item;
    const formatSurchargeList = (list) => {
      return list == null ? void 0 : list.map((config) => {
        const percentageFee = parseFloat(
          (config == null ? void 0 : config.percent) ?? (config == null ? void 0 : config.percentage) ?? 0
        );
        const percent = (percentageFee * 100).toFixed(2);
        const amountFee = parseFloat(config == null ? void 0 : config.amount).toFixed(2);
        const _totalFee = totalFee(config, amount).toFixed(2);
        const areaText = (config == null ? void 0 : config.area) === "world" ? "tocPay.text.rest-of-world" : "tocPay.text.domestic";
        return {
          ...config,
          area_text: areaText,
          surcharge_text: `${percent}% + ${currency_symbol}${amountFee}`,
          surcharge_fee: `${currency_symbol}${_totalFee}`
        };
      });
    };
    if (isSame) {
      const visaResult = surcharge_config.filter((config) => {
        return config.credit_card_type === "visa";
      });
      return [
        {
          icons: [import_visa.default, import_credit_card.default],
          name: "Visa / Mastercard",
          lists: formatSurchargeList(visaResult)
        }
      ];
    } else {
      const visaResult = surcharge_config.filter((config) => {
        return config.credit_card_type === "visa";
      });
      const masterResult = surcharge_config.filter((config) => {
        return config.credit_card_type === "master";
      });
      return [
        {
          icons: [import_visa.default],
          name: "Visa",
          lists: formatSurchargeList(visaResult)
        },
        {
          icons: [import_credit_card.default],
          name: "Mastercard",
          lists: formatSurchargeList(masterResult)
        }
      ];
    }
  } catch (error) {
    console.error(error);
    return [];
  }
};
var formatCreditCardSurchargeList = (item, amount) => {
  const isSame = isCreditCardSurchargeSame((item == null ? void 0 : item.surcharge_config) || []);
  return formatCreditCardSurcharge(item || {}, amount, isSame);
};
var formatCreditCardSurchargeText = (surcharge_config, surcharge_text) => {
  try {
    if (!Array.isArray(surcharge_config) || surcharge_config.length === 0) {
      return surcharge_text;
    }
    const percentList = surcharge_config.map((config) => {
      const percent = parseFloat(config.percent ?? config.percentage ?? "0") * 100;
      return Number(percent.toFixed(2));
    });
    const minPercent = Math.min(...percentList);
    const maxPercent = Math.max(...percentList);
    if (minPercent === maxPercent) {
      return `${surcharge_text}${minPercent}%`;
    }
    return `${surcharge_text}${minPercent}% ~ ${maxPercent}%`;
  } catch (error) {
    return surcharge_text;
  }
};
var calculateSurcharge = (paymentList, amount) => {
  return paymentList.map((payment) => {
    const { surcharge_config, currency_symbol, surcharge_text } = payment;
    if (!surcharge_config || surcharge_config.length === 0) {
      payment.surcharge = "";
      return payment;
    }
    const fees = surcharge_config.map((config) => {
      return totalFee(config, amount);
    });
    const minFee = Math.min(...fees);
    const maxFee = Math.max(...fees);
    if (minFee === 0 && maxFee === 0) {
      payment.surcharge = "";
    } else if (minFee === maxFee) {
      payment.surcharge = `${surcharge_text}${currency_symbol}${minFee.toFixed(
        2
      )}`;
    } else {
      payment.surcharge = `${surcharge_text}${currency_symbol}${minFee.toFixed(
        2
      )} ~ ${currency_symbol}${maxFee.toFixed(2)}`;
    }
    if (payment.service === "mw.credit_card") {
      payment.surcharge = formatCreditCardSurchargeText(surcharge_config, surcharge_text);
      payment.surcharge_config_result = formatCreditCardSurchargeList(
        payment,
        amount
      );
    }
    return payment;
  });
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  calculateSurcharge,
  formatCreditCardSurcharge,
  formatCreditCardSurchargeList,
  formatCreditCardSurchargeText,
  isCreditCardSurchargeSame
});
