'use client'

import { useEffect } from 'react'
import { useTranslations } from 'next-intl'
import { useCartStore } from '@/store/cartStore'
import { useWishlistStore } from '@/store/wishlistStore'

// Zustand actions can't call useTranslations() themselves, so their toast
// strings used to be hardcoded English. This component (mounted once in the
// storefront layout, same idea as CurrencyStoreSync) hands the current
// locale's translated strings to the stores, which read them at toast time.
// Until it runs, the stores fall back to their built-in English defaults.
export default function StoreMessagesSync() {
  const t = useTranslations('StoreToasts')

  useEffect(() => {
    useCartStore.getState().setMessages({
      networkError: t('networkError'),
      quantityFailed: t('cart.quantityFailed'),
      itemRemoved: t('cart.itemRemoved'),
      removeFailed: t('cart.removeFailed'),
      cartCleared: t('cart.cartCleared'),
      clearFailed: t('cart.clearFailed'),
      // Kept as a template — the store substitutes the coupon code.
      couponApplied: t('cart.couponApplied', { code: '{code}' }),
      couponInvalid: t('cart.couponInvalid'),
      couponFailed: t('cart.couponFailed'),
      couponRemoved: t('cart.couponRemoved'),
    })
    useWishlistStore.getState().setMessages({
      networkError: t('networkError'),
      added: t('wishlist.added'),
      alreadyIn: t('wishlist.alreadyIn'),
      addFailed: t('wishlist.addFailed'),
      removed: t('wishlist.removed'),
      removeFailed: t('wishlist.removeFailed'),
    })
  }, [t])

  return null
}
