"use client"; import React, { useEffect, useState } from 'react'; import Link from 'next/link'; import { Layers } from 'lucide-react'; import { catalogService, CategoryResponse, ProductCardResponse, } from '@/services/api/catalogService'; import { formatCurrency, getImageUrl } from '@/lib/utils'; import { CmsMegaMenuPanel, hasCmsMegaGroups, } from '@/components/CmsMegaMenuPanel'; import type { MegaMenuData } from '@/services/api/storefrontService'; import { getMegaMenuProductSlugs, storefrontService, } from '@/services/api/storefrontService'; type ShopByItem = { name: string; slug: string; image?: string }; type RatedItem = { id: string; name: string; slug: string; image: string; price: number; oldPrice?: number; }; function mapCategories(cats: CategoryResponse[]): ShopByItem[] { return cats.map((c) => ({ name: c.name, slug: c.slug, image: c.image_url ? getImageUrl(c.image_url) : undefined, })); } function mapProducts(products: ProductCardResponse[]): RatedItem[] { return products.slice(0, 6).map((p) => ({ id: p.product_id, name: p.name, slug: p.slug, image: getImageUrl(p.thumbnail_url) || '', price: Number(p.price || 0), oldPrice: p.compare_price && Number(p.compare_price) > Number(p.price) ? Number(p.compare_price) : undefined, })); } export const TopDealsMegaMenu: React.FC<{ cms?: MegaMenuData | null }> = ({ cms, }) => { const [shopBy, setShopBy] = useState([]); const [topRated, setTopRated] = useState([]); const [loading, setLoading] = useState(true); const [remote, setRemote] = useState(cms ?? null); useEffect(() => { if (cms) { setRemote(cms); return; } let cancelled = false; storefrontService.getMegaMenu().then((menus) => { if (cancelled) return; setRemote(menus.deals || null); }); return () => { cancelled = true; }; }, [cms]); useEffect(() => { let cancelled = false; const pinned = getMegaMenuProductSlugs(remote); const load = async () => { setLoading(true); try { const cats = await catalogService.getCategories().catch(() => []); if (cancelled) return; if (cats?.length) { setShopBy(mapCategories(cats).slice(0, 8)); } else { setShopBy([]); } if (pinned.length) { const cards = await catalogService.getProductsBySlugs(pinned.slice(0, 6)); if (!cancelled && cards.length) { setTopRated(mapProducts(cards)); return; } } const productRes = await catalogService .getProductsPaginated({ sort: 'popular', limit: 6 }) .catch(() => catalogService.getProductsPaginated({ limit: 6 })); if (!cancelled && productRes?.products?.length) { setTopRated(mapProducts(productRes.products)); } else if (!cancelled) { setTopRated([]); } } finally { if (!cancelled) setLoading(false); } }; void load(); return () => { cancelled = true; }; }, [remote]); if (hasCmsMegaGroups(remote)) { return ; } return (
{/* Shop By — left half */}

Shop By

{shopBy.length > 0 ? (
{shopBy.map((item) => ( {item.image ? ( {item.name} ) : ( )} {item.name} ))}
) : (
No categories available
)}
{/* Top Rated — right half */}

Top Rated

{loading ? (
{Array.from({ length: 4 }).map((_, idx) => (
))}
) : topRated.length > 0 ? (
{topRated.map((product, idx) => ( {product.image ? ( {product.name} ) : ( No Img )} {product.name} {product.oldPrice != null && ( {formatCurrency(product.oldPrice)} )} {formatCurrency(product.price)} ))}
) : (
No top rated products
)}
); }; export function isTopDealsNavLabel(label: string): boolean { const n = label.toLowerCase().replace(/\s+/g, ''); return ( n.includes('topdeal') || n === 'deals' || n.includes('top-deals') || label.toLowerCase().includes('top deals') ); }