/**
 * AJL40 Digital Series
 * © 2026 sultanmuzaffar. Hak cipta terpelihara / All rights reserved.
 * Penggunaan, penyalinan atau pengedaran tanpa kebenaran bertulis adalah dilarang.
 * Unauthorized use, copying or distribution is prohibited.
 */

// AJL 40 — Shared site navigation for standalone pages (Tentang, Kredit, Data)
// Uses the same .ajl-header / .ajl-nav / .ajl-drawer / .ajl-footer classes as the homepage,
// but links point to REAL routes (not in-page anchors).

const { useState: useStateN, useEffect: useEffectN } = React;

/* ============== HEADER SHARE POPOVER ============== */
function HeaderShare() {
  const [open, setOpen] = React.useState(false);
  const wrapRef = React.useRef(null);

  React.useEffect(() => {
    if (!open) return;
    const onDoc = (e) => { if (wrapRef.current && !wrapRef.current.contains(e.target)) setOpen(false); };
    const onKey = (e) => { if (e.key === 'Escape') setOpen(false); };
    document.addEventListener('mousedown', onDoc);
    document.addEventListener('keydown', onKey);
    return () => { document.removeEventListener('mousedown', onDoc); document.removeEventListener('keydown', onKey); };
  }, [open]);

  const shareUrl = typeof window !== 'undefined' ? window.location.href : '';
  const shareText = 'AJL 40 — Dunia Kreatif MM, Antologi Jiwa Lagu';
  const openWin = (href) => { window.open(href, '_blank', 'noopener,noreferrer,width=600,height=500'); setOpen(false); };
  const handleWhatsApp = () => openWin(`https://api.whatsapp.com/send?text=${encodeURIComponent(shareText + '\n' + shareUrl)}`);
  const handleFacebook = () => openWin(`https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(shareUrl)}&quote=${encodeURIComponent(shareText)}`);
  const handleX = () => openWin(`https://x.com/intent/tweet?text=${encodeURIComponent(shareText)}&url=${encodeURIComponent(shareUrl)}`);
  const handleTelegram = () => openWin(`https://t.me/share/url?url=${encodeURIComponent(shareUrl)}&text=${encodeURIComponent(shareText)}`);
  const handleThreads = () => openWin(`https://www.threads.net/intent/post?text=${encodeURIComponent(shareText + ' ' + shareUrl)}`);
  const handleInstagram = () => {
    const copyText = `${shareText}\n${shareUrl}`;
    if (navigator.clipboard) {
      navigator.clipboard.writeText(copyText).then(() => { if (window.showToast) window.showToast('Disalin untuk Instagram!'); }).catch(() => {});
    } else {
      const ta = document.createElement('textarea'); ta.value = copyText; ta.style.cssText = 'position:fixed;opacity:0';
      document.body.appendChild(ta); ta.select(); document.execCommand('copy'); document.body.removeChild(ta);
      if (window.showToast) window.showToast('Disalin untuk Instagram!');
    }
    setOpen(false);
  };

  return (
    <div className="ajl-sharepop" ref={wrapRef}>
      <button className={"ajl-iconbtn" + (open ? " is-active" : "")} aria-label="Kongsi laman" title="Kongsi laman" aria-expanded={open} aria-haspopup="true" onClick={() => setOpen(!open)}>
        <Icon name="share" size={18} />
      </button>
      <div className={"ajl-sharepop__menu" + (open ? " is-open" : "")} role="menu">
        <span className="ajl-sharepop__label t-mono">KONGSI LAMAN</span>
        <div className="ajl-sharepop__row">
          <button className="ajl-sharepop__btn" onClick={handleWhatsApp} aria-label="Kongsi ke WhatsApp" title="WhatsApp">
            <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor"><path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 012.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0012.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 005.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893a11.821 11.821 0 00-3.48-8.413z"/></svg>
          </button>
          <button className="ajl-sharepop__btn" onClick={handleFacebook} aria-label="Kongsi ke Facebook" title="Facebook">
            <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor"><path d="M24 12.073c0-6.627-5.373-12-12-12s-12 5.373-12 12c0 5.99 4.388 10.954 10.125 11.854v-8.385H7.078v-3.47h3.047V9.43c0-3.007 1.792-4.669 4.533-4.669 1.312 0 2.686.235 2.686.235v2.953H15.83c-1.491 0-1.956.925-1.956 1.874v2.25h3.328l-.532 3.47h-2.796v8.385C19.612 23.027 24 18.062 24 12.073z"/></svg>
          </button>
          <button className="ajl-sharepop__btn" onClick={handleX} aria-label="Kongsi ke X" title="X (Twitter)">
            <svg viewBox="0 0 24 24" width="14" height="14" fill="currentColor"><path d="M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231zm-1.161 17.52h1.833L7.084 4.126H5.117z"/></svg>
          </button>
          <button className="ajl-sharepop__btn" onClick={handleThreads} aria-label="Kongsi ke Threads" title="Threads">
            <svg viewBox="0 0 24 24" width="15" height="15" fill="currentColor"><path d="M12.186 24h-.007C5.965 24 2.634 20.155 2.634 14.564V14.5c0-5.531 3.315-9.5 8.02-9.5 3.727 0 6.469 2.223 7.178 5.07l-2.46.56c-.474-1.864-2.15-3.396-4.69-3.396-3.288 0-5.515 2.75-5.515 7.266v.064c0 4.134 1.867 7.202 5.537 7.202 1.97 0 3.382-.68 4.296-1.693.85-.943 1.31-2.218 1.31-3.624v-.02c0-.32-.012-.623-.037-.91-.866.426-1.856.653-2.924.653-3.874 0-6.275-2.282-6.275-5.96 0-3.362 2.258-5.802 5.368-5.802 1.673 0 3.037.617 3.95 1.788.877 1.123 1.321 2.694 1.321 4.667v.204c.005.08.008.16.008.24v.02c0 2.06-.629 3.891-1.81 5.226C14.843 22.69 13.115 24 12.186 24zm.793-14.39c-1.783 0-2.882 1.37-2.882 3.568 0 2.322 1.193 3.724 3.189 3.724.717 0 1.378-.14 1.97-.417a7.106 7.106 0 01-.072-1.01v-.203c0-2.813-.77-5.662-2.205-5.662z"/></svg>
          </button>
          <button className="ajl-sharepop__btn ajl-sharepop__btn--ig" onClick={handleInstagram} aria-label="Salin untuk Instagram" title="Instagram (salin)">
            <svg viewBox="0 0 24 24" width="15" height="15" fill="currentColor"><path d="M12 2.163c3.204 0 3.584.012 4.85.07 3.252.148 4.771 1.691 4.919 4.919.058 1.265.069 1.645.069 4.849 0 3.205-.012 3.584-.069 4.849-.149 3.225-1.664 4.771-4.919 4.919-1.266.058-1.644.07-4.85.07-3.204 0-3.584-.012-4.849-.07-3.26-.149-4.771-1.699-4.919-4.92-.058-1.265-.07-1.644-.07-4.849 0-3.204.013-3.583.07-4.849.149-3.227 1.664-4.771 4.919-4.919 1.266-.057 1.645-.069 4.849-.069zM12 0C8.741 0 8.333.014 7.053.072 2.695.272.273 2.69.073 7.052.014 8.333 0 8.741 0 12c0 3.259.014 3.668.072 4.948.2 4.358 2.618 6.78 6.98 6.98C8.333 23.986 8.741 24 12 24c3.259 0 3.668-.014 4.948-.072 4.354-.2 6.782-2.618 6.979-6.98.059-1.28.073-1.689.073-4.948 0-3.259-.014-3.667-.072-4.947-.196-4.354-2.617-6.78-6.979-6.98C15.668.014 15.259 0 12 0zm0 5.838a6.162 6.162 0 100 12.324 6.162 6.162 0 000-12.324zM12 16a4 4 0 110-8 4 4 0 010 8zm6.406-11.845a1.44 1.44 0 100 2.881 1.44 1.44 0 000-2.881z"/></svg>
          </button>
          <button className="ajl-sharepop__btn" onClick={handleTelegram} aria-label="Kongsi ke Telegram" title="Telegram">
            <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor"><path d="M11.944 0A12 12 0 000 12a12 12 0 0012 12 12 12 0 0012-12A12 12 0 0012 0zm4.962 7.224c.1-.002.321.023.465.14a.506.506 0 01.171.325c.016.093.036.306.02.472-.18 1.898-.962 6.502-1.36 8.627-.168.9-.499 1.201-.82 1.23-.696.065-1.225-.46-1.9-.902-1.056-.693-1.653-1.124-2.678-1.8-1.185-.78-.417-1.21.258-1.91.177-.184 3.247-2.977 3.307-3.23.007-.032.014-.15-.056-.212s-.174-.041-.249-.024c-.106.024-1.793 1.14-5.061 3.345-.479.33-.913.49-1.302.48-.428-.008-1.252-.241-1.865-.44-.752-.245-1.349-.374-1.297-.789.027-.216.325-.437.893-.663 3.498-1.524 5.83-2.529 6.998-3.014 3.332-1.386 4.025-1.627 4.476-1.635z"/></svg>
          </button>
        </div>
      </div>
    </div>
  );
}

const SITE_NAV_LINKS = [
  { label: '17 Esei', href: '/esei/' },
  { label: 'Data', href: '/data/' },
  { label: 'Tentang', href: '/tentang/' },
  { label: 'Kredit', href: '/kredit/' },
  { label: 'Arkib Visual', href: '/arkib/' },
];

function SiteHeader() {
  const [menuOpen, setMenuOpen] = useStateN(false);

  useEffectN(() => {
    const onKey = (e) => { if (e.key === 'Escape') setMenuOpen(false); };
    if (menuOpen) {
      document.addEventListener('keydown', onKey);
      document.body.style.overflow = 'hidden';
    } else {
      document.body.style.overflow = '';
    }
    return () => { document.removeEventListener('keydown', onKey); document.body.style.overflow = ''; };
  }, [menuOpen]);

  const shareSite = () => {
    const url = window.location.href;
    const title = 'AJL 40 — Dunia Kreatif MM, Antologi Jiwa Lagu';
    if (navigator.share) { navigator.share({ title, url }).catch(() => {}); }
    else if (navigator.clipboard) {
      navigator.clipboard.writeText(url).then(() => {
        if (window.showToast) window.showToast('Pautan disalin!');
      }).catch(() => {});
    }
  };

  return (
    <header className="ajl-header">
      <div className="ajl-container ajl-header__row">
        <a href="/" className="ajl-wordmark" aria-label="AJL 40">
          <img className="ajl-wordmark__logo" src="/esei/assets/logo.png" alt="AJL 40" />
        </a>
        <nav className="ajl-nav">
          {SITE_NAV_LINKS.map((l) => (
            <a key={l.href} className="ajl-nav__link" href={l.href}
              style={l.ruby ? {color: 'var(--ruby-bright)'} : undefined}>{l.label}</a>
          ))}
        </nav>
        <div className="ajl-header__end">
          <HeaderShare />
          <button className="ajl-hamburger" aria-label="Menu" onClick={() => setMenuOpen(!menuOpen)}>
            <span className={"ajl-hamburger__bar" + (menuOpen ? " is-open" : "")} />
            <span className={"ajl-hamburger__bar" + (menuOpen ? " is-open" : "")} />
            <span className={"ajl-hamburger__bar" + (menuOpen ? " is-open" : "")} />
          </button>
        </div>
      </div>

      <div className={"ajl-drawer-overlay" + (menuOpen ? " is-visible" : "")} onClick={() => setMenuOpen(false)} />
      <nav className={"ajl-drawer" + (menuOpen ? " is-open" : "")}>
        <div className="ajl-drawer__head">
          <a href="/" className="ajl-wordmark" aria-label="AJL 40" onClick={() => setMenuOpen(false)}>
            <img className="ajl-wordmark__logo" src="/esei/assets/logo.png" alt="AJL 40" />
          </a>
          <button className="ajl-drawer__close" aria-label="Tutup menu" onClick={() => setMenuOpen(false)}>
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>
          </button>
        </div>
        <div className="ajl-drawer__links">
          {SITE_NAV_LINKS.map((l) => (
            <a key={l.href} className="ajl-drawer__link" href={l.href}
              style={l.ruby ? {color: 'var(--ruby-bright)'} : undefined}
              onClick={() => setMenuOpen(false)}>{l.label}</a>
          ))}
        </div>
        <div className="ajl-drawer__foot">
          <span style={{fontFamily:'var(--font-mono)',fontSize:'10px',color:'var(--ivory-40)',letterSpacing:'0.08em'}}>AJL 40 · SIRI DIGITAL · 2026</span>
        </div>
      </nav>
    </header>
  );
}

function SiteFooter() {
  return (
    <footer className="ajl-footer">
      <div className="ajl-container ajl-footer__inner">
        <div className="ajl-footer__col">
          <a href="/" className="ajl-wordmark ajl-footer__mark" aria-label="AJL 40">
            <img className="ajl-wordmark__logo" src="/esei/assets/logo.png" alt="AJL 40" />
          </a>
          <p className="ajl-footer__brand">
            <em>Dunia Kreatif<br />Budaya Sebuah Negara</em>
          </p>
          <div className="ajl-footer__credits t-mono">
            SIRI DIGITAL · 2026<br />
            EDISI KE-40 ANUGERAH JUARA LAGU
          </div>
        </div>

        <div className="ajl-footer__col">
          <div className="ajl-eyebrow">Nota Editorial</div>
          <p className="ajl-footer__body" style={{fontSize:'0.85rem',lineHeight:1.6,opacity:0.85,maxWidth:'38ch'}}>AJL40 Digital Series ialah inisiatif editorial bebas oleh sultanmuzaffar. Portal ini tidak mewakili penganjur Anugerah Juara Lagu, TV3 atau Media Prima. Semua rujukan digunakan dalam konteks ulasan budaya, penyelidikan terbuka dan penghargaan terhadap sejarah muzik popular Malaysia.</p>
          <div className="ajl-social-row" style={{marginTop:'16px'}}>
            <a className="ajl-social-row__btn" href="https://www.x.com/sultanmuzaffar" target="_blank" rel="noopener noreferrer" aria-label="X (Twitter)" title="X (Twitter)">
              <svg viewBox="0 0 24 24" width="14" height="14" fill="currentColor"><path d="M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231zm-1.161 17.52h1.833L7.084 4.126H5.117z"/></svg>
            </a>
            <a className="ajl-social-row__btn" href="https://www.instagram.com/sultanmuzaffar" target="_blank" rel="noopener noreferrer" aria-label="Instagram" title="Instagram">
              <svg viewBox="0 0 24 24" width="14" height="14" fill="currentColor"><path d="M12 2.163c3.204 0 3.584.012 4.85.07 3.252.148 4.771 1.691 4.919 4.919.058 1.265.069 1.645.069 4.849 0 3.205-.012 3.584-.069 4.849-.149 3.225-1.664 4.771-4.919 4.919-1.266.058-1.644.07-4.85.07-3.204 0-3.584-.012-4.849-.07-3.26-.149-4.771-1.699-4.919-4.92-.058-1.265-.07-1.644-.07-4.849 0-3.204.013-3.583.07-4.849.149-3.227 1.664-4.771 4.919-4.919 1.266-.057 1.645-.069 4.849-.069zM12 0C8.741 0 8.333.014 7.053.072 2.695.272.273 2.69.073 7.052.014 8.333 0 8.741 0 12c0 3.259.014 3.668.072 4.948.2 4.358 2.618 6.78 6.98 6.98C8.333 23.986 8.741 24 12 24c3.259 0 3.668-.014 4.948-.072 4.354-.2 6.782-2.618 6.979-6.98.059-1.28.073-1.689.073-4.948 0-3.259-.014-3.667-.072-4.947-.196-4.354-2.617-6.78-6.979-6.98C15.668.014 15.259 0 12 0zm0 5.838a6.162 6.162 0 100 12.324 6.162 6.162 0 000-12.324zM12 16a4 4 0 110-8 4 4 0 010 8zm6.406-11.845a1.44 1.44 0 100 2.881 1.44 1.44 0 000-2.881z"/></svg>
            </a>
            <a className="ajl-social-row__btn" href="https://www.facebook.com/muzaffar.sm.mustapa" target="_blank" rel="noopener noreferrer" aria-label="Facebook" title="Facebook">
              <svg viewBox="0 0 24 24" width="14" height="14" fill="currentColor"><path d="M24 12.073c0-6.627-5.373-12-12-12s-12 5.373-12 12c0 5.99 4.388 10.954 10.125 11.854v-8.385H7.078v-3.47h3.047V9.43c0-3.007 1.792-4.669 4.533-4.669 1.312 0 2.686.235 2.686.235v2.953H15.83c-1.491 0-1.956.925-1.956 1.874v2.25h3.328l-.532 3.47h-2.796v8.385C19.612 23.027 24 18.062 24 12.073z"/></svg>
            </a>
            <a className="ajl-social-row__btn" href="https://www.linkedin.com/in/sultanmuzaffar" target="_blank" rel="noopener noreferrer" aria-label="LinkedIn" title="LinkedIn">
              <svg viewBox="0 0 24 24" width="14" height="14" fill="currentColor"><path d="M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433a2.062 2.062 0 01-2.063-2.065 2.064 2.064 0 112.063 2.065zm1.782 13.019H3.555V9h3.564v11.452zM22.225 0H1.771C.792 0 0 .774 0 1.729v20.542C0 23.227.792 24 1.771 24h20.451C23.2 24 24 23.227 24 22.271V1.729C24 .774 23.2 0 22.222 0h.003z"/></svg>
            </a>
          </div>
        </div>

        <div className="ajl-footer__col">
          <div className="ajl-eyebrow">Layari</div>
          <div style={{marginTop:'0.5rem',display:'flex',flexDirection:'column',gap:'8px'}}>
            <a className="ajl-footer__link" href="/esei/" style={{color:'var(--ivory-80)',textDecoration:'none',fontSize:'0.9rem'}}>17 Esei Editorial</a>
            <a className="ajl-footer__link" href="/arkib/" style={{color:'var(--ivory-80)',textDecoration:'none',fontSize:'0.9rem'}}>Arkib Editorial</a>
            <a className="ajl-footer__link" href="/archive/" style={{color:'var(--ivory-80)',textDecoration:'none',fontSize:'0.9rem'}}>Arkib Digital</a>
          </div>
        </div>

        <div className="ajl-footer__col">
          <div className="ajl-eyebrow">Projek</div>
          <div style={{marginTop:'0.5rem',display:'flex',flexDirection:'column',gap:'8px'}}>
            <a className="ajl-footer__link" href="/tentang/" style={{color:'var(--ivory-80)',textDecoration:'none',fontSize:'0.9rem'}}>Tentang</a>
            <a className="ajl-footer__link" href="/kredit/" style={{color:'var(--ivory-80)',textDecoration:'none',fontSize:'0.9rem'}}>Kredit</a>
          </div>
        </div>
      </div>

      <div className="ajl-footer__bar">
        <div className="ajl-container ajl-footer__legal">
          <span className="t-mono">© 2026 SULTANMUZAFFAR · HAK CIPTA TERPELIHARA</span>
          <span className="t-mono" style={{opacity:0.6,fontSize:'0.7rem'}}>Semua kandungan, kod sumber dan reka bentuk dilindungi hak cipta. Penggunaan tanpa kebenaran adalah dilarang.</span>
        </div>
      </div>
    </footer>
  );
}

function SiteBackToTop() {
  const [visible, setVisible] = useStateN(false);
  useEffectN(() => {
    const onScroll = () => setVisible(window.scrollY > 600);
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => window.removeEventListener('scroll', onScroll);
  }, []);
  if (!visible) return null;
  return (
    <button className="ajl-btt" aria-label="Kembali ke atas" title="Kembali ke atas"
      onClick={() => window.scrollTo({ top: 0, behavior: 'smooth' })}>
      <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round">
        <polyline points="18 15 12 9 6 15" />
      </svg>
    </button>
  );
}

Object.assign(window, { SiteHeader, SiteFooter, SiteBackToTop });
