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/utils/PaymentResultToastUtils.tsx
var PaymentResultToastUtils_exports = {};
__export(PaymentResultToastUtils_exports, {
  calculateAutoCloseConfig: () => calculateAutoCloseConfig,
  calculateStatusClassName: () => calculateStatusClassName,
  calculateSubtitle: () => calculateSubtitle,
  calculateTitle: () => calculateTitle,
  createShowSuccessToast: () => createShowSuccessToast,
  displayPaymentResultToast: () => displayPaymentResultToast,
  formatCurrency: () => formatCurrency
});
module.exports = __toCommonJS(PaymentResultToastUtils_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var import_decimal = __toESM(require("decimal.js"));
var import_PaymentResultToast = require("../components/PaymentResultToast");
var import_payment_result_flow = require("../payment-result-flow");
var formatCurrency = (amount, amountSymbol = "$") => {
  return (0, import_utils.formatAmount)(amount, 2, amountSymbol);
};
var calculateTitle = (status, failureReason, resultKind) => {
  const preset = (0, import_payment_result_flow.getPaymentResultPreset)(status, resultKind);
  const title = import_utils.locales.getText(preset.titleKey) || preset.fallbackTitle;
  if ((0, import_payment_result_flow.getPaymentResultKind)(status, resultKind) !== "failed") return title;
  const reason = failureReason ? ` - ${failureReason}` : "";
  return `${title}${reason}`;
};
var calculateSubtitle = (status, amount, gapAmount, getData, changeGivenAmount, resultKind, depositAmount) => {
  const getSubtitleClassName = (colorClass) => {
    return `payment-result-toast-subtitle-text ${colorClass}`;
  };
  let text;
  const amountSymbol = (getData == null ? void 0 : getData("shop_symbol")) || "$";
  const hasChangeGivenAmount = !!changeGivenAmount;
  const preset = (0, import_payment_result_flow.getPaymentResultPreset)(status, resultKind);
  switch (preset.subtitleMode) {
    case "paid":
      if (hasChangeGivenAmount || gapAmount) {
        text = `${import_utils.locales.getText("payment.result.changeDue") || "Change Due"} ${formatCurrency(Math.abs(changeGivenAmount ?? gapAmount ?? 0), amountSymbol)}`;
      } else {
        text = `${import_utils.locales.getText("payment.result.paid") || "Paid"} ${formatCurrency(amount, amountSymbol)}`;
      }
      break;
    case "partial":
      if (hasChangeGivenAmount) {
        text = `${import_utils.locales.getText("payment.result.changeDue") || "Change Due"} ${formatCurrency(Math.abs(changeGivenAmount), amountSymbol)}`;
      } else {
        const displayAmount = new import_decimal.default(amount).minus(gapAmount ?? 0).toNumber();
        text = `${import_utils.locales.getText("payment.result.partiallyPaid") || "Partially Paid"} ${formatCurrency(displayAmount, amountSymbol)}`;
      }
      break;
    case "deposit": {
      const paidText = `${import_utils.locales.getText("payment.result.depositPaid") || "Deposit Fully Paid"} ${formatCurrency(depositAmount || amount, amountSymbol)}`;
      if (hasChangeGivenAmount) {
        text = `${paidText} · ${import_utils.locales.getText("payment.result.changeDue") || "Change Due"} ${formatCurrency(Math.abs(changeGivenAmount), amountSymbol)}`;
      } else {
        text = paidText;
      }
      break;
    }
    case "amount_due":
      text = `${import_utils.locales.getText("payment.result.amountDue") || "Amount Due"} ${formatCurrency(amount, amountSymbol)}`;
      break;
    case "wallet_pass_partial":
      const successAmount = gapAmount ?? amount;
      text = `${import_utils.locales.getText("payment.result.partiallyPaid") || "Partially Paid"} ${formatCurrency(successAmount, amountSymbol)}`;
      break;
    default:
      return null;
  }
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "payment-result-toast-subtitle" }, /* @__PURE__ */ import_react.default.createElement("div", { className: getSubtitleClassName(preset.colorClass) }, text));
};
var calculateStatusClassName = (status, resultKind) => {
  return (0, import_payment_result_flow.getPaymentResultPreset)(status, resultKind).statusClassName;
};
var calculateAutoCloseConfig = (status, _hasChangeDue, resultKind) => {
  const autoCloseDelay = 30;
  const preset = (0, import_payment_result_flow.getPaymentResultPreset)(status, resultKind);
  return {
    autoClose: preset.autoClose,
    autoCloseDelay,
    maskClosable: preset.maskClosable
  };
};
var displayPaymentResultToast = async (params, config) => {
  const {
    paymentStatus,
    resultKind,
    orderTotalAmount,
    gapAmount,
    depositAmount,
    changeGivenAmount,
    failureReason,
    autoCloseConfig,
    actionButtons
  } = params;
  const { getData, onToastClose } = config;
  const resolvedKind = (0, import_payment_result_flow.getPaymentResultKind)(paymentStatus, resultKind);
  if (resolvedKind && resolvedKind !== "failed" && resolvedKind !== "wallet-pass-redemption-failed") {
    await (0, import_PaymentResultToast.showPaymentResultToastSuccess)({
      mask: !(autoCloseConfig == null ? void 0 : autoCloseConfig.autoClose) || false,
      title: calculateTitle(paymentStatus, void 0, resultKind),
      subtitle: calculateSubtitle(
        paymentStatus,
        orderTotalAmount,
        gapAmount,
        getData,
        changeGivenAmount,
        resultKind,
        depositAmount
      ),
      autoClose: (autoCloseConfig == null ? void 0 : autoCloseConfig.autoClose) || false,
      autoCloseDelay: (autoCloseConfig == null ? void 0 : autoCloseConfig.autoCloseDelay) || 3e3,
      clickOutsideToClose: (autoCloseConfig == null ? void 0 : autoCloseConfig.autoClose) || false,
      showCountdownProgress: true,
      actionButtons: actionButtons || [],
      onClose: onToastClose || (() => {
      }),
      // 提供默认空函数
      closable: !(autoCloseConfig == null ? void 0 : autoCloseConfig.autoClose)
      // 控制关闭按钮显示
    });
  } else if (resolvedKind === "failed" || resolvedKind === "wallet-pass-redemption-failed" || paymentStatus === "error") {
    await (0, import_PaymentResultToast.showPaymentResultToastFailed)({
      mask: !(autoCloseConfig == null ? void 0 : autoCloseConfig.autoClose) || false,
      title: calculateTitle(paymentStatus, failureReason, resultKind),
      subtitle: calculateSubtitle(
        paymentStatus,
        orderTotalAmount / 100,
        gapAmount,
        getData,
        void 0,
        resultKind
      ),
      autoClose: (autoCloseConfig == null ? void 0 : autoCloseConfig.autoClose) || false,
      actionButtons: actionButtons || [],
      clickOutsideToClose: (autoCloseConfig == null ? void 0 : autoCloseConfig.autoClose) || false,
      closable: !(autoCloseConfig == null ? void 0 : autoCloseConfig.autoClose),
      // 控制关闭按钮显示
      onClose: onToastClose || (() => {
      })
      // 提供默认空函数
    });
  }
};
var createShowSuccessToast = (config, getActionButtons) => {
  return async (result) => {
    var _a, _b, _c;
    const responseData = (_a = result.response) == null ? void 0 : _a.data;
    const paymentStatus = responseData == null ? void 0 : responseData.payment_status;
    const orderTotalAmount = Number((responseData == null ? void 0 : responseData.total_amount) || (responseData == null ? void 0 : responseData.expect_amount) || 0);
    const gapAmount = Number((responseData == null ? void 0 : responseData.amount_gap) || 0);
    const autoCloseConfig = calculateAutoCloseConfig(
      paymentStatus,
      !!gapAmount
    );
    const submittedOrderId = (responseData == null ? void 0 : responseData.order_id) ?? (responseData == null ? void 0 : responseData.orderId) ?? ((_b = result.response) == null ? void 0 : _b.order_id) ?? ((_c = result.response) == null ? void 0 : _c.orderId);
    const actionButtons = getActionButtons ? getActionButtons(paymentStatus, false, submittedOrderId) : [];
    await displayPaymentResultToast({
      paymentStatus,
      orderTotalAmount,
      gapAmount,
      autoCloseConfig,
      actionButtons
    }, config);
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  calculateAutoCloseConfig,
  calculateStatusClassName,
  calculateSubtitle,
  calculateTitle,
  createShowSuccessToast,
  displayPaymentResultToast,
  formatCurrency
});
