"use client"; import Link from 'next/link' import { useRouter, usePathname } from 'next/navigation' import { useState, useEffect } from 'react' import type { Category } from '@irelax/types' import { ShoppingBag, MapPin } from 'lucide-react' import { useCartStore } from '../store/cartStore' import { useTranslation } from 'react-i18next' interface NavbarProps { categories?: Category[] } export default function Navbar({ categories = [] }: NavbarProps) { const [searchOpen, setSearchOpen] = useState(false) const [mobileOpen, setMobileOpen] = useState(false) const [searchQuery, setSearchQuery] = useState('') const [scrolled, setScrolled] = useState(false) const [localOrdersCount, setLocalOrdersCount] = useState(0) const router = useRouter() const pathname = usePathname() const { toggleCart, getCartCount } = useCartStore() const cartCount = getCartCount() const { t, i18n } = useTranslation() const toggleLanguage = () => { const newLang = i18n.language.startsWith('en') ? 'th' : 'en' i18n.changeLanguage(newLang) localStorage.setItem('irelax_lang', newLang) } // Detect scroll for navbar bg change useEffect(() => { const onScroll = () => setScrolled(window.scrollY > 20) window.addEventListener('scroll', onScroll, { passive: true }) // Check local orders periodically (or just on mount) const stored = localStorage.getItem('irelax_local_orders') if (stored) { try { setLocalOrdersCount(JSON.parse(stored).length) } catch (e) {} } return () => window.removeEventListener('scroll', onScroll) }, []) // Close mobile menu on route change useEffect(() => { setMobileOpen(false) }, [pathname]) // Prevent body scroll when overlays open useEffect(() => { document.body.style.overflow = (searchOpen || mobileOpen) ? 'hidden' : '' return () => { document.body.style.overflow = '' } }, [searchOpen, mobileOpen]) const handleSearch = (e: React.FormEvent) => { e.preventDefault() if (searchQuery.trim()) { router.push(`/search?q=${encodeURIComponent(searchQuery.trim())}`) setSearchOpen(false) setMobileOpen(false) setSearchQuery('') } } const navLinkClass = (path: string) => pathname === path ? 'text-cyan-glow' : 'text-slate-300 hover:text-white transition-colors' return ( <> {/* ── NAVBAR BAR ── */} {/* ── MOBILE DRAWER ── */}
{/* Backdrop */}
setMobileOpen(false)} /> {/* Drawer */}
{/* Drawer Header */}
{t('nav.menu')}
{/* Drawer Nav Links */}
{[ { to: '/', label: t('nav.home') }, { to: '/shop', label: t('nav.shop') }, { to: '/coupons', label: `🔥 ${t('nav.promotions') || 'Promotions'}` }, { to: '/scent-guide', label: `✨ ${t('nav.scentGuide')}` }, { to: '/about', label: t('nav.about') }, { to: '/faq', label: t('nav.faq') }, { to: '/contact', label: t('nav.contact') }, ].map(item => ( setMobileOpen(false)} className={ `block px-4 py-3 rounded-xl text-sm font-medium tracking-wide transition-all duration-200 ${ pathname === item.to ? 'bg-cyan-glow/10 text-cyan-glow border border-cyan-glow/20' : 'hover:bg-white/5 text-slate-300 hover:text-white' }` } > {item.label} ))} {/* Collections sub-section */} {categories.length > 0 && (

{t('nav.collections')}

{categories.map(cat => ( setMobileOpen(false)} className="flex items-center justify-between px-4 py-2.5 rounded-xl text-sm text-slate-400 hover:text-white hover:bg-white/5 transition-all" > {cat.name} ))}
)}
{/* Drawer Footer CTA */}
setMobileOpen(false)} className="block w-full py-3 bg-white text-night-900 font-bold text-center rounded-xl text-xs uppercase tracking-widest hover:bg-cyan-glow transition-colors duration-300" > {t('nav.shopNow')} setMobileOpen(false)} className="block w-full py-3 glass text-center rounded-xl text-xs font-bold uppercase tracking-widest hover:bg-white/10 transition-colors border border-white/10" > {t('nav.findMyScent')}
{/* ── SEARCH OVERLAY ── */} {searchOpen && (
{ if (e.target === e.currentTarget) setSearchOpen(false) }} >

{t('nav.search.title')}

setSearchQuery(e.target.value)} placeholder="Search scents, categories..." className="bg-transparent border-none outline-none text-lg md:text-xl p-4 pl-0 w-full text-white placeholder-slate-600" autoFocus />

Quick Links

{[ ...categories.map(c => ({ label: c.name, to: `/category/${c.id}` })), { label: '✨ Scent Guide', to: '/scent-guide' }, { label: 'All Products', to: '/shop' }, ].map(item => ( setSearchOpen(false)} className="glass px-4 py-1.5 rounded-full text-xs font-medium hover:bg-white/10 hover:text-cyan-glow transition-all border border-white/5" > {item.label} ))}
)} ) }