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/payment-modal-utils.ts
var payment_modal_utils_exports = {};
__export(payment_modal_utils_exports, {
  calculateAutoCloseConfig: () => calculateAutoCloseConfig,
  calculateStatusClassName: () => calculateStatusClassName,
  calculateSubtitle: () => calculateSubtitle,
  calculateTitle: () => calculateTitle,
  createPaymentModalCallbackEvent: () => createPaymentModalCallbackEvent,
  formatCurrency: () => formatCurrency,
  getPaymentModalEventOrderId: () => getPaymentModalEventOrderId,
  getSubtitleClassName: () => getSubtitleClassName,
  normalizePaymentModalCallbackEvent: () => normalizePaymentModalCallbackEvent
});
module.exports = __toCommonJS(payment_modal_utils_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var formatCurrency = (amount, amountSymbol = "$") => {
  return (0, import_utils.formatAmount)(amount, 2, amountSymbol);
};
function isObject(value) {
  return !!value && typeof value === "object";
}
function getFirstPresentValue(values) {
  return values.find((value) => value !== void 0 && value !== null && value !== "");
}
function getLegacyPayload(value) {
  if ((value == null ? void 0 : value.payload) !== void 0) return value.payload;
  if ((value == null ? void 0 : value.type) === "callback" && isObject(value.data)) return value.data;
  return value;
}
function getLegacyOrderId(value, payload) {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
  return getFirstPresentValue([
    value == null ? void 0 : value.orderId,
    value == null ? void 0 : value.order_id,
    (_a = value == null ? void 0 : value.data) == null ? void 0 : _a.orderId,
    (_b = value == null ? void 0 : value.data) == null ? void 0 : _b.order_id,
    (_d = (_c = value == null ? void 0 : value.response) == null ? void 0 : _c.data) == null ? void 0 : _d.order_id,
    (_g = (_f = (_e = value == null ? void 0 : value.data) == null ? void 0 : _e.response) == null ? void 0 : _f.data) == null ? void 0 : _g.order_id,
    payload == null ? void 0 : payload.orderId,
    payload == null ? void 0 : payload.order_id,
    (_i = (_h = payload == null ? void 0 : payload.response) == null ? void 0 : _h.data) == null ? void 0 : _i.order_id,
    (_j = payload == null ? void 0 : payload.data) == null ? void 0 : _j.order_id,
    (_m = (_l = (_k = payload == null ? void 0 : payload.data) == null ? void 0 : _k.response) == null ? void 0 : _l.data) == null ? void 0 : _m.order_id
  ]);
}
function getLegacyStatus(value, payload) {
  var _a, _b, _c, _d;
  return (value == null ? void 0 : value.status) || ((_a = value == null ? void 0 : value.data) == null ? void 0 : _a.status) || (payload == null ? void 0 : payload.status) || ((_b = payload == null ? void 0 : payload.toastInfo) == null ? void 0 : _b.status) || ((_d = (_c = payload == null ? void 0 : payload.response) == null ? void 0 : _c.data) == null ? void 0 : _d.payment_status);
}
function getLegacyReason(value, payload) {
  var _a, _b;
  const reason = (value == null ? void 0 : value.reason) || ((_a = value == null ? void 0 : value.data) == null ? void 0 : _a.reason) || (payload == null ? void 0 : payload.reason);
  if (reason) return reason;
  const source = (value == null ? void 0 : value.source) || ((_b = value == null ? void 0 : value.data) == null ? void 0 : _b.source) || (payload == null ? void 0 : payload.source);
  if (source === "back") return "back";
  if ((value == null ? void 0 : value.type) === "close" || (value == null ? void 0 : value.type) === "modal-close") return (value == null ? void 0 : value.reset) ? "paid" : "cancel";
  if (getLegacyStatus(value, payload) === "cancelled") return "cancel";
  return void 0;
}
function getDefaultCloseCaller(params) {
  if (params.type === "toast-close") return false;
  if (params.type === "error") return false;
  if (params.reason === "back") return false;
  if (params.status === "failed") return false;
  if (params.type === "modal-close") return params.reset === true;
  return params.status === "success" || params.status === "paid";
}
function getDefaultRefreshOrder(params) {
  if (params.type === "toast-close") return false;
  if (params.type === "error") return false;
  if (params.reason === "back") return false;
  if (params.status === "failed") return false;
  return params.reset === true || params.status === "success" || params.status === "paid" || params.status === "partially_paid";
}
function createPaymentModalCallbackEvent(event) {
  const shouldRefreshOrder = event.shouldRefreshOrder ?? getDefaultRefreshOrder({
    type: event.type,
    status: event.status,
    reason: event.reason,
    reset: event.reset
  });
  const shouldCloseCaller = event.shouldCloseCaller ?? getDefaultCloseCaller({
    type: event.type,
    status: event.status,
    reason: event.reason,
    reset: event.reset
  });
  return {
    ...event,
    shouldRefreshOrder,
    shouldCloseCaller,
    data: event.data ?? event.payload,
    isCallbackFromToast: event.type === "toast-close" || event.isCallbackFromToast,
    source: event.source ?? event.reason
  };
}
function normalizePaymentModalCallbackEvent(value) {
  var _a;
  if (!isObject(value)) {
    return createPaymentModalCallbackEvent({
      isChangePaymentNotIncludeVoucher: false,
      type: "modal-close",
      reason: "cancel",
      status: "cancelled",
      payload: value,
      reset: false
    });
  }
  const payload = getLegacyPayload(value);
  if (value.type === "toast-close" || value.isCallbackFromToast || ((_a = value == null ? void 0 : value.data) == null ? void 0 : _a.isCallbackFromToast)) {
    return createPaymentModalCallbackEvent({
      ...value,
      type: "toast-close",
      status: getLegacyStatus(value, payload),
      reason: value.reason || (payload == null ? void 0 : payload.reason),
      orderId: getLegacyOrderId(value, payload),
      payload,
      reset: value.reset,
      shouldRefreshOrder: false,
      shouldCloseCaller: false
    });
  }
  if (value.type === "error") {
    return createPaymentModalCallbackEvent({
      ...value,
      type: "error",
      orderId: getLegacyOrderId(value, payload),
      payload: value.payload ?? value.data,
      shouldRefreshOrder: false,
      shouldCloseCaller: false
    });
  }
  const reason = getLegacyReason(value, payload);
  const status = getLegacyStatus(value, payload);
  const reset = value.reset ?? (payload == null ? void 0 : payload.reset);
  const type = value.type === "modal-close" || value.type === "close" || reason === "back" ? "modal-close" : "payment-result";
  return createPaymentModalCallbackEvent({
    ...value,
    type,
    status,
    reason,
    orderId: getLegacyOrderId(value, payload),
    reset,
    payload: value.payload ?? payload
  });
}
function getPaymentModalEventOrderId(event) {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
  const normalizedEvent = normalizePaymentModalCallbackEvent(event);
  return getFirstPresentValue([
    normalizedEvent.orderId,
    (_a = normalizedEvent.payload) == null ? void 0 : _a.order_id,
    (_b = normalizedEvent.payload) == null ? void 0 : _b.orderId,
    (_e = (_d = (_c = normalizedEvent.payload) == null ? void 0 : _c.response) == null ? void 0 : _d.data) == null ? void 0 : _e.order_id,
    (_g = (_f = normalizedEvent.payload) == null ? void 0 : _f.data) == null ? void 0 : _g.order_id,
    (_k = (_j = (_i = (_h = normalizedEvent.payload) == null ? void 0 : _h.data) == null ? void 0 : _i.response) == null ? void 0 : _j.data) == null ? void 0 : _k.order_id
  ]);
}
var calculateTitle = (status, failureReason, getText = () => "") => {
  switch (status) {
    case "paid":
    case "partially_paid":
      return getText("payment.result.success");
    case "failed":
      const reason = failureReason ? ` - ${failureReason}` : "";
      return `${getText("payment.result.failed")}${reason}`;
    case "wallet-pass-redemption-failed":
      return getText("pisell2.checkout.wallet-pass.redemption-failed") || "Wallet pass redeem failed";
    default:
      return getText("payment.result.success");
  }
};
var getSubtitleClassName = (colorClass) => {
  return `payment-result-toast-subtitle-text ${colorClass}`;
};
var calculateSubtitle = (status, amount, gapAmount, getText = () => "", amountSymbol = "$") => {
  let text;
  let colorClass;
  switch (status) {
    case "paid":
      if (gapAmount) {
        text = `${getText("payment.result.changeDue") || "Change Due"} ${formatCurrency(Math.abs(gapAmount), amountSymbol)}`;
        colorClass = "partially-paid";
      } else {
        text = `${getText("payment.result.paid") || "Paid"} ${formatCurrency(amount, amountSymbol)}`;
        colorClass = "paid";
      }
      break;
    case "partially_paid":
      const displayAmount = gapAmount ?? amount;
      text = `${getText("payment.result.partiallyPaid") || "Partially Paid"} ${formatCurrency(displayAmount, amountSymbol)}`;
      colorClass = "partially-paid";
      break;
    case "failed":
      text = `${getText("payment.result.amountDue") || "Amount Due"} ${formatCurrency(amount, amountSymbol)}`;
      colorClass = "amount-due";
      break;
    case "wallet-pass-redemption-failed":
      const successAmount = gapAmount ?? amount;
      text = `${getText("payment.result.partiallyPaid") || "Partially Paid"} ${formatCurrency(successAmount, amountSymbol)}`;
      colorClass = "partially-paid";
      break;
    default:
      return null;
  }
  return import_react.default.createElement(
    "div",
    { className: "payment-result-toast-subtitle" },
    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) => {
  const autoCloseDelay = 30;
  switch (status) {
    case "paid":
      return {
        autoClose: true,
        autoCloseDelay,
        maskClosable: true
      };
    case "partially_paid":
      return {
        autoClose: true,
        autoCloseDelay,
        maskClosable: true
      };
    case "failed":
    case "wallet-pass-redemption-failed":
      return {
        autoClose: false,
        autoCloseDelay,
        maskClosable: false
      };
    default:
      return {
        autoClose: false,
        autoCloseDelay,
        maskClosable: false
      };
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  calculateAutoCloseConfig,
  calculateStatusClassName,
  calculateSubtitle,
  calculateTitle,
  createPaymentModalCallbackEvent,
  formatCurrency,
  getPaymentModalEventOrderId,
  getSubtitleClassName,
  normalizePaymentModalCallbackEvent
});
