"use client" import { useEffect, useState } from 'react' import Link from '../LocalizedLink' import { ChevronLeft, ChevronRight, Globe, X } from 'lucide-react' import type { MegaMenuPayload, MenuColumn, MenuLink } from './menuTypes' import { getLocalizedMenuText } from './menuTypes' type MenuNode = | { type: 'root'; data: MegaMenuPayload[]; title: string | Record } | { type: 'mega'; data: MenuColumn[]; title: string | Record } | { type: 'links'; data: MenuLink[]; title: string | Record } interface MobileDrawerProps { isOpen: boolean items: MegaMenuPayload[] lang: string customerName?: string | null onClose: () => void onOpenLang: () => void onLogin: () => void onLogout: () => void } export function MobileDrawer({ isOpen, items, lang, customerName, onClose, onOpenLang, onLogin, onLogout, }: MobileDrawerProps) { const [stack, setStack] = useState([{ type: 'root', data: items, title: 'Menu' }]) useEffect(() => { if (!isOpen) { const timer = setTimeout(() => { setStack([{ type: 'root', data: items, title: 'Menu' }]) }, 300) return () => clearTimeout(timer) } const timer = setTimeout(() => { setStack((current) => { if (current.length === 1 && current[0].data !== items) { return [{ type: 'root', data: items, title: 'Menu' }] } return current }) }, 0) return () => clearTimeout(timer) }, [isOpen, items]) const pushView = (node: MenuNode) => setStack((prev) => [...prev, node]) const popView = () => setStack((prev) => (prev.length > 1 ? prev.slice(0, -1) : prev)) const linkClass = 'ui-menu-link py-4' const nestedLinkClass = 'ui-menu-link-compact py-2' const renderView = (view: MenuNode) => { if (view.type === 'root') { return (
{view.data.map((item, index) => (
{item.columns?.length ? ( ) : ( {getLocalizedMenuText(item.label, lang)} )}
))} {customerName && (
My Profile
)}
) } if (view.type === 'mega') { return (
{view.data.map((column, columnIndex) => (

{getLocalizedMenuText(column.title, lang)}

{column.links.map((link, linkIndex) => (
{link.links?.length ? ( ) : ( {getLocalizedMenuText(link.label, lang)} )}
))}
))}
) } return (
{view.data.map((link, linkIndex) => (
{link.links?.length ? ( ) : ( {getLocalizedMenuText(link.label, lang)} )}
))}
) } return (
Menu
{stack.map((view, index) => { const isCurrent = index === stack.length - 1 const isPast = index < stack.length - 1 return (
{index > 0 && ( )} {index > 0 && (

{getLocalizedMenuText(view.title, lang)}

)}
{renderView(view)}
) })}
Shop Now {customerName ? ( ) : ( )}
) }