import { getErrorCode } from '@/core/lib/add-to-cart-error'; /** * Message keys (under `coupon.*`) for the stable codes `applyCoupon` / * `applyCheckoutCoupon` return. The API's `message` is English whatever the * shopper's language, so a known code is always shown from our own messages. * A refusal with no code (a checkout past its editable stage, say) returns * `null` and the caller falls back to the API's message. */ const COUPON_ERROR_KEYS = { COUPON_NOT_FOUND: 'invalidCode', COUPON_NOT_ACTIVE: 'notActive', COUPON_EXPIRED: 'expired', COUPON_USAGE_LIMIT_REACHED: 'usageLimitReached', COUPON_PER_CUSTOMER_LIMIT_REACHED: 'perCustomerLimitReached', COUPON_MINIMUM_NOT_MET: 'minimumNotMet', COUPON_REGION_RESTRICTED: 'regionRestricted', COUPON_NO_ELIGIBLE_ITEMS: 'noEligibleItems', // An automatic discount rule's cap (total uses, budget, per customer) was reached before payment: // the checkout step or the payment intent refuses with this code, nothing was charged. DISCOUNT_LIMIT_EXCEEDED: 'discountLimitReached', } as const; export type CouponErrorKey = (typeof COUPON_ERROR_KEYS)[keyof typeof COUPON_ERROR_KEYS]; export function couponErrorKey(err: unknown): CouponErrorKey | null { const code = getErrorCode(err); if (code && code in COUPON_ERROR_KEYS) { return COUPON_ERROR_KEYS[code as keyof typeof COUPON_ERROR_KEYS]; } return null; }