// Sticky top nav + mobile menu + tree badge
const { useEffect, useState } = React;

function Nav({ page, setPage, cartCount, wishlistCount, forceScrolled }) {
  const [scrolled, setScrolled] = useState(false);
  const [heroActive, setHeroActive] = useState(page === 'home');
  const [mobileOpen, setMobileOpen] = useState(false);

  useEffect(() => {
    const onScroll = () => {
      setScrolled(window.scrollY > 60);
      if (page === 'home') {
        setHeroActive(window.scrollY < (window.innerHeight - 100));
      } else {
        setHeroActive(false);
      }
    };
    onScroll();
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => window.removeEventListener('scroll', onScroll);
  }, [page]);

  useEffect(() => {
    setMobileOpen(false);
  }, [page]);

  const isScrolled = scrolled || forceScrolled;

  const go = (p) => (e) => {
    e && e.preventDefault();
    setPage(p);
    setMobileOpen(false);
    window.scrollTo({ top: 0, behavior: 'instant' });
  };

  return (
    <React.Fragment>
      <nav className={'nav ' + (isScrolled ? 'scrolled' : '')}>
        <button className="nav-mobile-toggle" aria-label="Menu" onClick={() => setMobileOpen(true)}>
          <span></span><span></span><span></span>
        </button>

        <div className="nav-links">
          <a href="/" onClick={go('home')} className={page === 'home' ? 'active' : ''}>Home</a>
          <a href="/shop/" onClick={go('shop')} className={page === 'shop' ? 'active' : ''}>Shop</a>
          <a href="/about/" onClick={go('about')} className={page === 'about' ? 'active' : ''}>About</a>
          <a href="/contact/" onClick={go('contact')} className={page === 'contact' ? 'active' : ''}>Contact</a>
        </div>
        <a href="/" onClick={go('home')} className="nav-logo">
          <img src="/l.png" alt="Bamboos Culture - Handcrafted Bamboo Products" />
        </a>
        <div className="nav-right">
          <a href="/wishlist/" onClick={go('wishlist')} className="nav-cart nav-wishlist" style={{marginRight: '20px'}}>
            Wishlist
            {wishlistCount > 0 && <span className="nav-wishlist-count">{wishlistCount}</span>}
          </a>
          <a href="/cart/" onClick={go('cart')} className="nav-cart">
            Cart
            {cartCount > 0 && <span className="nav-cart-count">{cartCount}</span>}
          </a>
        </div>

        <a href="/wishlist/" onClick={go('wishlist')} className="nav-mobile-right-wishlist" aria-label="Wishlist">
          <svg viewBox="0 0 24 24">
            <path d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"/>
          </svg>
          {wishlistCount > 0 && <span className="nav-wishlist-count-badge">{wishlistCount}</span>}
        </a>
      </nav>
      {mobileOpen && (
        <div className="nav-mobile-open">
          <div className="nav-mobile-panel" style={{display:'flex'}}>
            <button className="nav-mobile-close" onClick={() => setMobileOpen(false)}>✕</button>
            <a href="/" onClick={go('home')}>Home</a>
            <a href="/shop/" onClick={go('shop')}>Shop</a>
            <a href="/about/" onClick={go('about')}>About</a>
            <a href="/contact/" onClick={go('contact')}>Contact</a>
            <a href="/wishlist/" onClick={go('wishlist')}>Wishlist ({wishlistCount})</a>
            <a href="/cart/" onClick={go('cart')}>Cart ({cartCount})</a>
          </div>
        </div>
      )}
      {/* App-style Mobile Bottom Navigation Bar */}
      <nav className={'mobile-bottom-nav ' + (heroActive ? 'hero-dark' : 'scrolled-light')} aria-label="Mobile Navigation">
        <a href="/" onClick={go('home')} className={'mobile-nav-tab ' + (page === 'home' ? 'active' : '')}>
          <div className="mobile-nav-pill">
            <svg viewBox="0 0 24 24" width="20" height="20" fill={page === 'home' ? 'currentColor' : 'none'} stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
              <path d="M3 10.2L12 3l9 7.2V20a1 1 0 0 1-1 1h-5v-6h-6v6H4a1 1 0 0 1-1-1v-9.8z" />
            </svg>
          </div>
          <span className="mobile-nav-label">HOME</span>
        </a>
        <a href="/shop/" onClick={go('shop')} className={'mobile-nav-tab ' + (page === 'shop' ? 'active' : '')}>
          <div className="mobile-nav-pill">
            <svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
              <path d="M3 9l1.5-6h15L21 9" />
              <path d="M3 9a2.5 2.5 0 0 0 5 0 2.5 2.5 0 0 0 5 0 2.5 2.5 0 0 0 5 0 2.5 2.5 0 0 0 5 0" />
              <path d="M4 11.5V20a1 1 0 0 0 1 1h14a1 1 0 0 0 1-1v-8.5" />
              <path d="M9 21v-7a1 1 0 0 1 1-1h4a1 1 0 0 1 1 1v7" />
            </svg>
          </div>
          <span className="mobile-nav-label">SHOP</span>
        </a>
        <a href="/about/" onClick={go('about')} className={'mobile-nav-tab ' + (page === 'about' ? 'active' : '')}>
          <div className="mobile-nav-pill">
            <svg viewBox="0 0 24 24" width="20" height="20" fill={page === 'about' ? 'currentColor' : 'none'} stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
              <path d="M11 20A7 7 0 0 1 9.8 6.1C15.5 5 17 4.5 19 2c1 2.5 1.5 4.5 1 10.5A7.5 7.5 0 0 1 11 20z" />
              <path d="M2 21c0-3 1.8-6.5 5.5-8.5" />
            </svg>
          </div>
          <span className="mobile-nav-label">ABOUT</span>
        </a>
        <a href="/contact/" onClick={go('contact')} className={'mobile-nav-tab ' + (page === 'contact' ? 'active' : '')}>
          <div className="mobile-nav-pill">
            <svg viewBox="0 0 24 24" width="20" height="20" fill={page === 'contact' ? 'currentColor' : 'none'} stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
              <rect x="2" y="4" width="20" height="16" rx="2" />
              <path d="M22 6l-10 7L2 6" fill="none" />
            </svg>
          </div>
          <span className="mobile-nav-label">CONTACT</span>
        </a>
      </nav>
    </React.Fragment>
  );
}

function Toast({ msg }) {
  return <div className={'toast ' + (msg ? 'show' : '')}>{msg || ' '}</div>;
}

Object.assign(window, { Nav, Toast });
