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/PaymentDetailModal/index.tsx
var PaymentDetailModal_exports = {};
__export(PaymentDetailModal_exports, {
  default: () => PaymentDetailModal_default
});
module.exports = __toCommonJS(PaymentDetailModal_exports);
var import_react = __toESM(require("react"));
var import_icon = require("@pisell/icon");
var import_utils = require("@pisell/utils");
var import_locales = __toESM(require("../../locales"));
var import_index = require("./index.less");
var import_classnames = __toESM(require("classnames"));
var import_useEngineContext = __toESM(require("../../../../hooks/useEngineContext"));
function PaymentDetailModal({
  amountSymbol,
  orderInfo,
  cartData,
  onClose,
  summaryData = [],
  labelMaps,
  paymentItems = [],
  customAmount,
  voidPayment,
  walletPassData,
  clientInfo
}) {
  console.log("🔄 PaymentDetailModal: orderInfo", orderInfo, cartData);
  const [orderTotalExpanded, setOrderTotalExpanded] = (0, import_react.useState)(true);
  const [orderPaidExpanded, setOrderPaidExpanded] = (0, import_react.useState)(true);
  const [balanceToPayExpanded, setBalanceToPayExpanded] = (0, import_react.useState)(true);
  const context = (0, import_useEngineContext.default)();
  const getText = (key, fallback) => {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    const currentLang = ((_e = (_d = (_c = (_b = (_a = context.appHelper.utils) == null ? void 0 : _a.getApp) == null ? void 0 : _b.call(_a)) == null ? void 0 : _c.locales) == null ? void 0 : _d.getLocale) == null ? void 0 : _e.call(_d)) || ((_g = (_f = context.appHelper.utils) == null ? void 0 : _f.storage) == null ? void 0 : _g.get("umi_locale")) || "en";
    const checkoutText = (_h = import_locales.default[currentLang]) == null ? void 0 : _h[key];
    if (checkoutText) {
      return checkoutText;
    }
    return import_utils.locales.getText(key) || fallback;
  };
  const getCustomerInfo = () => {
    var _a;
    const client = clientInfo || {};
    const getValidName = (value) => {
      return value !== void 0 && value !== null && String(value).trim() !== "" ? String(value).trim() : null;
    };
    const displayName = getValidName(client == null ? void 0 : client.display_name) || getValidName(client == null ? void 0 : client.name) || getText("pisell2.checkout.customer.default-name") || "Customer";
    return {
      nickname: (client == null ? void 0 : client.nickname) && (client == null ? void 0 : client.nickname) !== "Select a customer" ? client == null ? void 0 : client.nickname : import_utils.locales.getText("pisell2.checkout.customer.default-name"),
      display_name: displayName,
      email: (client == null ? void 0 : client.email) || ((_a = client == null ? void 0 : client.contacts) == null ? void 0 : _a.email) || ""
    };
  };
  const CollapseIcon = ({
    expanded,
    color = "black"
  }) => /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("collapse-icon", { expanded }) }, /* @__PURE__ */ import_react.default.createElement(import_icon.ChevronUp, { style: { color, fontSize: "24px" } }));
  const shouldShowVoidButton = (item) => {
    return (item.code === "cash" || item.type === "custom") && !item.isSynced;
  };
  const getTotalPaidAmount = () => {
    return paymentItems.filter((item) => !item.voucher_id).reduce((total, item) => total + Math.abs(item.amount) + Math.abs(item.rounding_amount), 0);
  };
  const getTotalVoucherAmount = () => {
    return paymentItems.filter((item) => item.voucher_id).reduce((total, item) => total + Math.abs(item.amount), 0);
  };
  const getBalanceDueAmount = () => {
    const baseAmount = customAmount ? parseFloat(customAmount) : 0;
    const voucherDeductions = getTotalVoucherAmount();
    return baseAmount + voucherDeductions;
  };
  const handleVoidPayment = (paymentItem) => {
    console.log("Void payment:", paymentItem);
    if (voidPayment) {
      voidPayment(paymentItem);
    }
  };
  const renderPaymentItems = () => {
    const regularPayments = paymentItems.filter((item) => !item.voucher_id);
    if (!paymentItems || paymentItems.length === 0 || regularPayments.length === 0) {
      return /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("section-content") }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("no-payment-text") }, getText("no.payments.recorded") || "No payments recorded"));
    }
    return /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("section-content") }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("payment-record-container") }, regularPayments.map((item) => /* @__PURE__ */ import_react.default.createElement("div", { key: item.uuid, className: (0, import_classnames.default)("payment-item") }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("payment-item-content") }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("payment-item-text") }, item.name, " - ", (/* @__PURE__ */ new Date()).toLocaleDateString(), " ", getText("pisell2.checkout.payment-detail.by") || "by", " ", getCustomerInfo().nickname), /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("payment-item-actions") }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("payment-item-amount") }, "-", (0, import_utils.formatAmountWithOptions)(Math.abs(item.amount) + Math.abs(item.rounding_amount), amountSymbol, {
      precision: 2,
      hideDecimalForWholeNumbers: false,
      useThousandsSeparator: true,
      showCurrencySymbol: true
    })), shouldShowVoidButton(item) && /* @__PURE__ */ import_react.default.createElement(
      "button",
      {
        className: (0, import_classnames.default)("void-button"),
        onClick: () => handleVoidPayment(item),
        type: "button"
      },
      getText("void") || "Void"
    )))))));
  };
  const renderVoucherItems = () => {
    var _a;
    const items = [];
    if ((_a = walletPassData == null ? void 0 : walletPassData.selectedWallet) == null ? void 0 : _a.length) {
      walletPassData.selectedWallet.forEach((wallet) => {
        items.push(
          /* @__PURE__ */ import_react.default.createElement(
            "div",
            {
              key: `wallet-${wallet.id}`,
              className: (0, import_classnames.default)("balance-item-row")
            },
            /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("balance-item-text") }, wallet.name || wallet.product_name || getText("wallet.pass") || "Wallet Pass"),
            /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("balance-item-amount") }, "-", wallet.redeem || (0, import_utils.formatAmountWithOptions)(
              Math.abs(wallet.savedAmount || 0),
              amountSymbol,
              {
                precision: 2,
                hideDecimalForWholeNumbers: false,
                useThousandsSeparator: true,
                showCurrencySymbol: true
              }
            ))
          )
        );
      });
    }
    if (items.length === 0) {
      return /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("balance-item-row", "empty-state") }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("balance-item-text", "empty-text") }, getText("pisell2.checkout.payment-detail.no-wallet-pass-items") || "No WalletPass payment items"));
    }
    return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, items);
  };
  const InfoIcon = () => /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("info-icon") }, /* @__PURE__ */ import_react.default.createElement(import_icon.HelpCircle, { style: { color: "#667085", fontSize: "16px" } }));
  const getDisplayLabel = (item) => {
    return item.label || labelMaps && labelMaps[item.key] || item.key;
  };
  const getTotalAmount = () => {
    const totalItem = summaryData.find((item) => item.key === "sub_total");
    if (totalItem) {
      return totalItem.value;
    }
    return 0;
  };
  const renderSummaryItems = () => {
    if (!summaryData || summaryData.length === 0) {
      return /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("order-details-grid") }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("order-details-column") }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("order-row") }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("order-row-text") }, getText("no.items.found") || "No items found"), /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("order-row-amount") }, (0, import_utils.formatAmountWithOptions)(0, amountSymbol, {
        precision: 2,
        hideDecimalForWholeNumbers: false,
        useThousandsSeparator: true,
        showCurrencySymbol: true
      })))));
    }
    const leftItems = summaryData.slice(0, Math.ceil(summaryData.length / 2));
    const rightItems = summaryData.slice(Math.ceil(summaryData.length / 2));
    return /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("order-details-grid") }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("order-details-column") }, leftItems.map((item) => {
      let prefix = "";
      if ((item == null ? void 0 : item.key) !== "discount") {
        prefix = item.value >= 0 ? "+" : "";
      }
      return /* @__PURE__ */ import_react.default.createElement("div", { key: item.key, className: (0, import_classnames.default)("order-row") }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("order-row-label") }, getDisplayLabel(item)), /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("order-row-amount") }, prefix, (0, import_utils.formatAmountWithOptions)(item.value, amountSymbol, {
        precision: 2,
        hideDecimalForWholeNumbers: false,
        useThousandsSeparator: true,
        showCurrencySymbol: true
      })));
    })), rightItems.length > 0 && /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("divider-vertical") }), /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("order-details-column") }, rightItems.map((item) => {
      let prefix = "";
      if ((item == null ? void 0 : item.key) !== "discount") {
        prefix = item.value >= 0 ? "+" : "";
      }
      return /* @__PURE__ */ import_react.default.createElement("div", { key: item.key, className: (0, import_classnames.default)("order-row") }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("order-row-label") }, getDisplayLabel(item)), /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("order-row-amount") }, prefix, (0, import_utils.formatAmountWithOptions)(item.value, amountSymbol, {
        precision: 2,
        hideDecimalForWholeNumbers: false,
        useThousandsSeparator: true,
        showCurrencySymbol: true
      })));
    }))));
  };
  return /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("payment-detail-modal") }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("main-content") }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("section-container") }, /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)("section-header"),
      onClick: () => setOrderTotalExpanded(!orderTotalExpanded)
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("section-title-container") }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("section-title") }, getText("order.total") || "Order Total")),
    /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("section-right") }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("section-amount") }, (0, import_utils.formatAmountWithOptions)(getTotalAmount(), amountSymbol, {
      precision: 2,
      hideDecimalForWholeNumbers: false,
      useThousandsSeparator: true,
      showCurrencySymbol: true
    })), /* @__PURE__ */ import_react.default.createElement(CollapseIcon, { expanded: orderTotalExpanded }))
  ), orderTotalExpanded && /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("section-content") }, renderSummaryItems())), /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("divider-horizontal") }), /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("section-container") }, /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)("section-header"),
      onClick: () => setOrderPaidExpanded(!orderPaidExpanded),
      role: "button",
      tabIndex: 0
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("section-title-container") }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("section-title") }, getText("order.paid") || "Order Paid")),
    /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("section-right") }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("section-amount") }, "-", (0, import_utils.formatAmountWithOptions)(getTotalPaidAmount(), amountSymbol, {
      precision: 2,
      hideDecimalForWholeNumbers: false,
      useThousandsSeparator: true,
      showCurrencySymbol: true
    })), /* @__PURE__ */ import_react.default.createElement(CollapseIcon, { expanded: orderPaidExpanded }))
  ), orderPaidExpanded && renderPaymentItems())), /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("balance-to-pay-section") }, /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)("balance-to-pay-header"),
      onClick: () => setBalanceToPayExpanded(!balanceToPayExpanded),
      role: "button",
      tabIndex: 0
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("section-title-container") }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("balance-to-pay-title") }, getText("balance.to.pay") || "Balance to Pay")),
    /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("balance-to-pay-right") }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("balance-to-pay-amount") }, (0, import_utils.formatAmountWithOptions)(getBalanceDueAmount(), amountSymbol, {
      precision: 2,
      hideDecimalForWholeNumbers: false,
      useThousandsSeparator: true,
      showCurrencySymbol: true
    })), /* @__PURE__ */ import_react.default.createElement(CollapseIcon, { expanded: balanceToPayExpanded, color: "#7F56DA" }))
  ), balanceToPayExpanded && /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("balance-to-pay-content") }, renderVoucherItems()), /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("balance-due-section") }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("balance-due-header") }, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("balance-due-title") }, getText("balance.due") || "Balance Due"), /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("balance-due-amount") }, (0, import_utils.formatAmountWithOptions)(customAmount || 0, amountSymbol, {
    precision: 2,
    hideDecimalForWholeNumbers: false,
    useThousandsSeparator: true,
    showCurrencySymbol: true
  }))))));
}
var PaymentDetailModal_default = PaymentDetailModal;
