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/PaymentResultToastV2/resolve.ts
var resolve_exports = {};
__export(resolve_exports, {
  mapPaymentStatusToV2Status: () => mapPaymentStatusToV2Status,
  resolveFooterView: () => resolveFooterView,
  resolveHeadline: () => resolveHeadline,
  resolvePaymentResultToastV2ViewModel: () => resolvePaymentResultToastV2ViewModel,
  resolveTone: () => resolveTone
});
module.exports = __toCommonJS(resolve_exports);
var import_decimal = __toESM(require("decimal.js"));
var import_utils = require("@pisell/utils");
var EPS = 1e-4;
function toDecimal(value) {
  try {
    if (value instanceof import_decimal.default) return value;
    if (value === void 0 || value === null || value === "") {
      return new import_decimal.default(0);
    }
    return new import_decimal.default(value);
  } catch {
    return new import_decimal.default(0);
  }
}
function formatMoney(value, symbol) {
  return String((0, import_utils.formatAmount)(toDecimal(value).toString(), 2, symbol));
}
var PAYMENT_STATUS_COLOR_MAP = {
  unpaid: "#B2DDFF",
  payment_processing: "#FEDF89",
  payment_pending: "#FEDF89",
  authorized: "#ABEFC6",
  paid: "#ABEFC6",
  refunded: "#FECDCA",
  partially_refunded: "#FCCEEE",
  partially_paid: "#FDB022",
  voided: "#D0D5DD"
};
function hexToRgba(hex, alpha) {
  const normalized = hex.replace("#", "");
  const value = normalized.length === 3 ? normalized.split("").map((char) => char + char).join("") : normalized;
  const red = parseInt(value.slice(0, 2), 16);
  const green = parseInt(value.slice(2, 4), 16);
  const blue = parseInt(value.slice(4, 6), 16);
  return `rgba(${red}, ${green}, ${blue}, ${alpha})`;
}
function getPaymentStatusStyle(status) {
  const key = status == null ? void 0 : status.toLowerCase();
  const color = key && PAYMENT_STATUS_COLOR_MAP[key] || "#D0D5DD";
  return {
    "--pisell-sale-detail-summary-status-color": color,
    "--pisell-sale-detail-summary-status-bg": hexToRgba(color, 0.16),
    "--pisell-sale-detail-summary-status-border": hexToRgba(color, 0.46),
    "--pisell-sale-detail-summary-status-glow": hexToRgba(color, 0.14)
  };
}
function mapPaymentStatusToV2Status(paymentStatus) {
  switch (String(paymentStatus ?? "").toLowerCase()) {
    case "paid":
      return "success";
    case "partially_paid":
      return "partially-paid";
    case "failed":
    case "error":
      return "failed";
    case "wallet-pass-failed":
    case "wallet-pass-redemption-failed":
      return "wallet-pass-failed";
    case "deposit_paid":
      return "deposit-paid";
    default:
      return "success";
  }
}
function resolveTone(status) {
  switch (status) {
    case "success":
    case "deposit-paid":
      return "success";
    case "partially-paid":
    case "wallet-pass-failed":
      return "warning";
    case "failed":
      return "error";
    default:
      return "success";
  }
}
function resolveHeadline(status, failureReason) {
  switch (status) {
    case "success":
      return String(import_utils.locales.getText("payment.result.success"));
    case "partially-paid":
      return String(import_utils.locales.getText("payment.result.partiallyPaid"));
    case "failed": {
      const base = String(import_utils.locales.getText("payment.result.failed"));
      return failureReason ? `${base} - ${failureReason}` : base;
    }
    case "wallet-pass-failed":
      return String(import_utils.locales.getText("payment.result.walletPassFailed"));
    case "deposit-paid":
      return String(import_utils.locales.getText("payment.result.depositPaid"));
    default:
      return String(import_utils.locales.getText("payment.result.success"));
  }
}
function resolveStatusLabel(params) {
  const { paymentStatus, summary, isFullyPaid, isRefund } = params;
  const totalRefund = Number(summary.total_refund_amount) || 0;
  const isRefunded = paymentStatus === "refunded" && totalRefund > 0;
  const isPartiallyRefunded = paymentStatus === "partially_refunded" && totalRefund > 0;
  let key;
  if (isRefunded) {
    key = "payment.result.refunded";
  } else if (isPartiallyRefunded) {
    key = "payment.result.partiallyRefunded";
  } else if (isRefund) {
    key = "payment.result.refund";
  } else if (isFullyPaid) {
    key = "payment.result.fullyPaid";
  } else if (paymentStatus === "partially_paid") {
    key = "payment.result.partiallyPaid";
  } else {
    key = "payment.result.toPay";
  }
  return String(import_utils.locales.getText(key));
}
function resolveFooterView(input) {
  const { summary } = input;
  if (!summary) return null;
  const symbol = input.currencySymbol || "";
  const paymentStatus = String(input.paymentStatus ?? "").toLowerCase();
  const isFullyPaid = paymentStatus === "paid";
  const balanceDueAmount = paymentStatus === "payment_processing" ? summary.expect_amount : summary.amount_gap;
  const isRefund = toDecimal(balanceDueAmount).lt(0);
  const statusLabel = resolveStatusLabel({
    paymentStatus,
    summary,
    isFullyPaid,
    isRefund
  });
  const detailDataSource = input.disableDetail ? null : {
    summary,
    payments: input.payments,
    paymentStatus: input.paymentStatus,
    shopDiscount: input.shopDiscount,
    currencySymbol: symbol,
    taxTitle: input.taxTitle,
    paymentMethodNameMap: input.paymentMethodNameMap,
    translationOriginal: input.translationOriginal
  };
  return {
    itemCount: Number(summary.product_quantity) || 0,
    totalText: formatMoney(summary.expect_amount, symbol),
    paidText: formatMoney(summary.order_paid_amount, symbol),
    hasPaymentSurcharge: toDecimal(summary.surcharge_fee).gt(EPS),
    statusLabel,
    statusStyle: getPaymentStatusStyle(input.paymentStatus),
    balanceDueText: formatMoney(balanceDueAmount, symbol),
    isRefund,
    detailDataSource
  };
}
function resolveChangeDueText(input) {
  if (input.status !== "success") return void 0;
  const change = toDecimal(input.changeGivenAmount);
  if (change.abs().lte(EPS)) return void 0;
  return `${import_utils.locales.getText("payment.result.changeDue")} ${formatMoney(
    change.abs(),
    input.currencySymbol || ""
  )}`;
}
function resolvePaymentResultToastV2ViewModel(input) {
  const isFullyPaid = String(input.paymentStatus ?? "").toLowerCase() === "paid";
  return {
    status: input.status,
    tone: resolveTone(input.status),
    headline: resolveHeadline(input.status, input.failureReason),
    changeDueText: resolveChangeDueText(input),
    failureReason: input.status === "failed" ? input.failureReason : void 0,
    isFullyPaid,
    sessionLines: input.sessionLines,
    footer: resolveFooterView(input)
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  mapPaymentStatusToV2Status,
  resolveFooterView,
  resolveHeadline,
  resolvePaymentResultToastV2ViewModel,
  resolveTone
});
