"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
module.exports = PromotionSection;
var _jsxRuntime = require("react/jsx-runtime");
var _context = require("@arcblock/ux/lib/Locale/context");
var _iconsMaterial = require("@mui/icons-material");
var _material = require("@mui/material");
var _promotionCode = _interopRequireDefault(require("../../components/promotion-code"));
var _util = require("../../libs/util");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function PromotionSection({
  checkoutSessionId,
  currency,
  currencyId,
  discounts,
  allowPromotionCodes,
  completed = false,
  disabled = false,
  onPromotionUpdate,
  onRemovePromotion,
  calculatedDiscountAmount = null,
  isRateLoading = false
}) {
  const {
    t,
    locale
  } = (0, _context.useLocaleContext)();
  const hasDiscounts = discounts?.length > 0;
  const getAppliedPromotionCodes = () => {
    if (!discounts?.length) return [];
    return discounts.filter(discount => discount.promotion_code || discount.coupon).map(discount => ({
      id: discount.promotion_code || discount.coupon,
      code: discount.verification_data?.code || "APPLIED",
      discount_amount: discount.discount_amount
    }));
  };
  if (allowPromotionCodes && !hasDiscounts) {
    return /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Box, {
      sx: {
        mt: 1
      },
      children: /* @__PURE__ */(0, _jsxRuntime.jsx)(_promotionCode.default, {
        checkoutSessionId,
        initialAppliedCodes: getAppliedPromotionCodes(),
        disabled: completed,
        onUpdate: onPromotionUpdate,
        currencyId
      })
    });
  }
  if (!hasDiscounts) {
    return null;
  }
  return /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Box, {
    children: discounts.map(discount => {
      const promotionCodeInfo = discount.promotion_code_details;
      const couponInfo = discount.coupon_details;
      const discountDescription = couponInfo ? (0, _util.formatCouponTerms)(couponInfo, currency, locale) : "";
      const notSupported = discountDescription === t("payment.checkout.coupon.noDiscount");
      return /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
        children: [/* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
          direction: "row",
          spacing: 1,
          sx: {
            justifyContent: "space-between",
            alignItems: "center"
          },
          children: [/* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
            direction: "row",
            spacing: 1,
            sx: {
              alignItems: "center",
              backgroundColor: "grey.100",
              width: "fit-content",
              px: 1,
              py: 1,
              borderRadius: 1
            },
            children: [/* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Typography, {
              sx: {
                fontWeight: "medium",
                display: "flex",
                alignItems: "center",
                gap: 0.5
              },
              children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_iconsMaterial.LocalOffer, {
                sx: {
                  color: "warning.main",
                  fontSize: "small"
                }
              }), promotionCodeInfo?.code || discount.verification_data?.code || t("payment.checkout.discount")]
            }), !completed && /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Button, {
              size: "small",
              disabled,
              onClick: () => onRemovePromotion(checkoutSessionId),
              sx: {
                minWidth: "auto",
                width: 16,
                height: 16,
                color: "text.secondary",
                "&.Mui-disabled": {
                  color: "text.disabled"
                }
              },
              children: /* @__PURE__ */(0, _jsxRuntime.jsx)(_iconsMaterial.Close, {
                sx: {
                  fontSize: 14
                }
              })
            })]
          }), /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Typography, {
            sx: {
              color: "text.secondary",
              opacity: isRateLoading ? 0 : 1,
              transition: "opacity 300ms ease-in-out"
            },
            children: ["-", (0, _util.formatAmount)(calculatedDiscountAmount || "0", currency.decimal), " ", currency.symbol]
          })]
        }), discountDescription && /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Typography, {
          sx: {
            fontSize: "small",
            color: notSupported ? "error.main" : "text.secondary",
            mt: 0.5
          },
          children: discountDescription
        })]
      }, discount.promotion_code || discount.coupon || `discount-${discount.discount_amount}`);
    })
  });
}