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 formatCurrency = (amount, amountSymbol = "$") => {
  return (0, import_utils.formatAmount)(amount, 2, amountSymbol);
};
var calculateTitle = (status, failureReason) => {
  switch (status) {
    case "paid":
    case "partially_paid":
      return import_utils.locales.getText("payment.result.success");
    case "failed":
      const reason = failureReason ? ` - ${failureReason}` : "";
      return `${import_utils.locales.getText("payment.result.failed")}${reason}`;
    case "wallet-pass-redemption-failed":
      return import_utils.locales.getText("pisell2.checkout.wallet-pass.redemption-failed") || "Wallet pass redeem failed";
    default:
      return import_utils.locales.getText("payment.result.success");
  }
};
var calculateSubtitle = (status, amount, gapAmount, getData) => {
  const getSubtitleClassName = (colorClass2) => {
    return `payment-result-toast-subtitle-text ${colorClass2}`;
  };
  let text;
  let colorClass;
  const amountSymbol = (getData == null ? void 0 : getData("shop_symbol")) || "$";
  switch (status) {
    case "paid":
      if (gapAmount) {
        text = `${import_utils.locales.getText("payment.result.changeDue") || "Change Due"} ${formatCurrency(Math.abs(gapAmount), amountSymbol)}`;
        colorClass = "partially-paid";
      } else {
        text = `${import_utils.locales.getText("payment.result.paid") || "Paid"} ${formatCurrency(amount, amountSymbol)}`;
        colorClass = "paid";
      }
      break;
    case "partially_paid":
      const displayAmount = new import_decimal.default(amount).minus(gapAmount ?? 0).toNumber();
      text = `${import_utils.locales.getText("payment.result.partiallyPaid") || "Partially Paid"} ${formatCurrency(displayAmount, amountSymbol)}`;
      colorClass = "partially-paid";
      break;
    case "failed":
      text = `${import_utils.locales.getText("payment.result.amountDue") || "Amount Due"} ${formatCurrency(amount, amountSymbol)}`;
      colorClass = "amount-due";
      break;
    case "wallet-pass-redemption-failed":
      const successAmount = gapAmount ?? amount;
      text = `${import_utils.locales.getText("payment.result.partiallyPaid") || "Partially Paid"} ${formatCurrency(successAmount, amountSymbol)}`;
      colorClass = "partially-paid";
      break;
    default:
      return null;
  }
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "payment-result-toast-subtitle" }, /* @__PURE__ */ import_react.default.createElement("h1", { className: getSubtitleClassName(colorClass) }, text));
};
var calculateStatusClassName = (status) => {
  switch (status) {
    case "paid":
    case "partially_paid":
      return "success";
    case "failed":
      return "failed";
    case "wallet-pass-redemption-failed":
      return "wallet-pass-failed";
    default:
      return "success";
  }
};
var calculateAutoCloseConfig = (status, hasChangeDue) => {
  switch (status) {
    case "paid":
      return {
        autoClose: true,
        autoCloseDelay: hasChangeDue ? 30 : 5,
        // 有找零30秒，无找零5秒
        maskClosable: true
      };
    case "partially_paid":
      return {
        autoClose: true,
        autoCloseDelay: 5,
        maskClosable: true
      };
    case "failed":
    case "wallet-pass-redemption-failed":
      return {
        autoClose: false,
        autoCloseDelay: 0,
        maskClosable: false
      };
    default:
      return {
        autoClose: false,
        autoCloseDelay: 5,
        maskClosable: false
      };
  }
};
var displayPaymentResultToast = (params, config) => {
  const {
    paymentStatus,
    orderTotalAmount,
    gapAmount,
    failureReason,
    autoCloseConfig,
    actionButtons
  } = params;
  const { getData, onToastClose } = config;
  if (paymentStatus === "paid") {
    (0, import_PaymentResultToast.showPaymentResultToastSuccess)({
      title: calculateTitle(paymentStatus),
      subtitle: calculateSubtitle(
        paymentStatus,
        orderTotalAmount,
        gapAmount,
        getData
      ),
      autoClose: (autoCloseConfig == null ? void 0 : autoCloseConfig.autoClose) || false,
      autoCloseDelay: (autoCloseConfig == null ? void 0 : autoCloseConfig.autoCloseDelay) || 3e3,
      showCountdownProgress: true,
      actionButtons: actionButtons || [],
      onClose: onToastClose
    });
  } else if (paymentStatus === "partially_paid") {
    (0, import_PaymentResultToast.showPaymentResultToastSuccess)({
      title: calculateTitle(paymentStatus),
      subtitle: calculateSubtitle(
        paymentStatus,
        orderTotalAmount,
        gapAmount,
        getData
      ),
      autoClose: (autoCloseConfig == null ? void 0 : autoCloseConfig.autoClose) || false,
      actionButtons: actionButtons || [],
      onClose: onToastClose
    });
  } else if (paymentStatus === "failed" || paymentStatus === "error") {
    (0, import_PaymentResultToast.showPaymentResultToastFailed)({
      title: calculateTitle(paymentStatus, failureReason),
      subtitle: calculateSubtitle(
        paymentStatus,
        orderTotalAmount / 100,
        gapAmount,
        getData
      ),
      autoClose: (autoCloseConfig == null ? void 0 : autoCloseConfig.autoClose) || false,
      actionButtons: actionButtons || [],
      onClose: onToastClose
    });
  }
};
var createShowSuccessToast = (config, getActionButtons) => {
  return (result) => {
    var _a;
    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) || 0);
    const gapAmount = Number((responseData == null ? void 0 : responseData.amount_gap) || 0);
    const autoCloseConfig = calculateAutoCloseConfig(
      paymentStatus,
      !!gapAmount
    );
    const actionButtons = getActionButtons ? getActionButtons(paymentStatus) : [];
    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
});
