/* GNBS — Shared Components
   GNBSHeader / GNBSFooter / GNBSHero / GNBSPageHero / GNBSCtaBanner
*/
(function () {
  const { useState, useEffect, useRef } = React;
  const D = window.GNBS_DATA;

  /* ─────────────────────────────────────────
     SVG path data (logo)
     GN_D   : "gn" 파트 (viewBox 0 0 1081 181)
     BIO_DS : "biosolution" 파트
  ───────────────────────────────────────── */
  const GN_D = 'M202.99,49.47c-7.42-7.47-17.88-11.2-31.39-11.2-8.46,0-15.6,1.54-21.42,4.61-5.83,3.08-10.6,7.41-14.34,13.02h-6.92v-15.82h-38.47v15.82h-6.92c-3.52-5.71-8.18-10.1-14.01-13.18-5.82-3.07-12.8-4.61-20.92-4.61-14.83,0-26.45,4.56-34.85,13.68C5.36,60.9,1.16,73.15,1.16,88.52c0,10.22,1.87,19.11,5.6,26.69,3.73,7.58,9.06,13.43,15.98,17.55,6.92,4.12,14.99,6.18,24.22,6.18,7.47,0,13.89-1.43,19.28-4.28,5.38-2.85,9.5-7.03,12.36-12.52h.82v14.17c0,7.8-2.45,13.81-7.33,18.04-4.89,4.23-11.89,6.34-21.01,6.34-13.73,0-21.97-4.56-24.72-13.68H.5c1.65,10.43,7,18.62,16.06,24.55,9.06,5.93,20.84,8.9,35.34,8.9,16.48,0,29.16-3.82,38.06-11.45,8.9-7.64,13.35-18.43,13.35-32.38v-72h12.6v74.31h24.55v-55.03c0-7.8,2.11-13.73,6.34-17.79,4.23-4.06,10.3-6.1,18.21-6.1s14.25,2.03,18.37,6.1c4.12,4.06,6.18,10,6.18,17.79v55.03h24.55v-59.64c0-12.41-3.71-22.35-11.12-29.82ZM78.76,92.81c0,7.58-2.3,13.59-6.92,18.04-4.61,4.45-10.87,6.67-18.78,6.67-8.68,0-15.41-2.5-20.18-7.5-4.78-5-7.17-12.11-7.17-21.34s2.39-16.39,7.17-21.5c4.78-5.11,11.5-7.66,20.18-7.66,7.8,0,14.03,2.2,18.7,6.59,4.67,4.4,7,10.33,7,17.79v8.9Z';

  const BIO_DS = [
    'M286.03,140.92c-7.91,0-14.66-1.43-20.26-4.28-5.6-2.86-10.05-7.03-13.35-12.52h-6.92v14.83h-17.14v-16.48h8.24V.54h15.49v56.35h.66c3.07-6.15,7.61-10.82,13.59-14.01,5.99-3.18,12.99-4.78,21.01-4.78,9.34,0,17.38,2.06,24.14,6.18,6.76,4.12,11.95,10.02,15.57,17.71,3.63,7.69,5.44,16.86,5.44,27.52s-1.84,19.83-5.52,27.51c-3.68,7.69-8.98,13.59-15.9,17.71-6.92,4.12-15.27,6.18-25.05,6.18ZM283.89,127.74c10.43,0,18.56-3.35,24.39-10.05,5.82-6.7,8.73-16.09,8.73-28.18s-2.91-21.47-8.73-28.18c-5.82-6.7-13.95-10.05-24.39-10.05-9.56,0-17.25,2.75-23.07,8.24-5.82,5.49-8.73,12.8-8.73,21.91v16.15c0,9.12,2.91,16.42,8.73,21.91,5.82,5.49,13.51,8.24,23.07,8.24Z',
    'M349.3,25.25c-2.75,0-5.08-.96-7-2.88-1.92-1.92-2.88-4.26-2.88-7s.96-5.08,2.88-7c1.92-1.92,4.26-2.88,7-2.88s5.08.96,7,2.88c1.92,1.92,2.88,4.26,2.88,7s-.96,5.08-2.88,7c-1.92,1.92-4.26,2.88-7,2.88ZM341.55,138.94V40.08h15.49v98.86h-15.49Z',
    'M416.02,140.92c-10.11,0-18.89-2.11-26.36-6.34-7.47-4.23-13.26-10.19-17.38-17.88-4.12-7.69-6.18-16.75-6.18-27.19s2.03-19.5,6.09-27.19c4.06-7.69,9.86-13.65,17.38-17.88,7.52-4.23,16.34-6.34,26.45-6.34s19.2,2.09,26.61,6.26c7.41,4.18,13.15,10.11,17.22,17.8,4.06,7.69,6.09,16.8,6.09,27.35s-2.03,19.52-6.09,27.27c-4.07,7.74-9.83,13.71-17.3,17.88-7.47,4.18-16.31,6.26-26.53,6.26ZM416.02,127.41c10.87,0,19.33-3.32,25.37-9.97,6.04-6.64,9.06-15.95,9.06-27.93s-3.02-21.42-9.06-28.01c-6.04-6.59-14.5-9.89-25.37-9.89s-19.2,3.32-25.29,9.97c-6.1,6.65-9.15,15.96-9.15,27.93s3.05,21.28,9.15,27.93c6.09,6.65,14.53,9.97,25.29,9.97Z',
    'M510.6,140.92c-13.4,0-24-2.77-31.8-8.32-7.8-5.55-12.08-13.32-12.85-23.32h15.49c.77,6.37,3.57,11.1,8.4,14.17,4.83,3.08,11.92,4.61,21.25,4.61,16.81,0,25.21-5.38,25.21-16.15,0-4.39-1.29-7.77-3.87-10.13-2.58-2.36-6.95-4.09-13.1-5.19l-21.58-3.95c-17.9-3.29-26.86-11.86-26.86-25.7,0-8.9,3.35-15.93,10.05-21.09,6.7-5.16,15.87-7.74,27.51-7.74s21.56,2.64,28.75,7.91c7.19,5.27,11.34,12.63,12.44,22.08h-15.16c-1.21-5.82-3.95-10.13-8.24-12.93-4.28-2.8-10.27-4.2-17.96-4.2s-13.02,1.29-16.97,3.87c-3.95,2.58-5.93,6.34-5.93,11.29,0,3.73,1.29,6.67,3.87,8.81,2.58,2.14,6.78,3.76,12.61,4.86l21.42,3.95c9.45,1.76,16.37,4.83,20.76,9.23,4.39,4.4,6.59,10.33,6.59,17.79,0,9.56-3.46,16.97-10.38,22.24-6.92,5.27-16.8,7.91-29.66,7.91Z',
    'M600.56,140.92c-10.11,0-18.89-2.11-26.36-6.34-7.47-4.23-13.26-10.19-17.38-17.88-4.12-7.69-6.18-16.75-6.18-27.19s2.03-19.5,6.09-27.19c4.06-7.69,9.86-13.65,17.38-17.88,7.52-4.23,16.34-6.34,26.45-6.34s19.2,2.09,26.61,6.26c7.41,4.18,13.15,10.11,17.22,17.8,4.06,7.69,6.09,16.8,6.09,27.35s-2.03,19.52-6.09,27.27c-4.07,7.74-9.83,13.71-17.3,17.88-7.47,4.18-16.31,6.26-26.53,6.26ZM600.56,127.41c10.87,0,19.33-3.32,25.37-9.97,6.04-6.64,9.06-15.95,9.06-27.93s-3.02-21.42-9.06-28.01c-6.04-6.59-14.5-9.89-25.37-9.89s-19.2,3.32-25.29,9.97c-6.1,6.65-9.15,15.96-9.15,27.93s3.05,21.28,9.15,27.93c6.09,6.65,14.53,9.97,25.29,9.97Z',
    'M659.54,138.94V.54h15.49v138.4h-15.49Z',
    'M729.23,140.75c-12.3,0-22.08-3.52-29.33-10.55-7.25-7.03-10.87-16.47-10.87-28.34v-61.79h15.49v58.49c0,9.34,2.47,16.45,7.42,21.34s12.08,7.33,21.42,7.33,17.41-2.52,22.9-7.58c5.49-5.05,8.24-12.08,8.24-21.09v-58.49h15.49v82.38h8.24v16.48h-17.14v-14.83h-6.92c-3.41,5.38-8.1,9.5-14.09,12.36-5.99,2.85-12.93,4.28-20.84,4.28Z',
    'M819.19,138.94c-4.61,0-8.21-1.18-10.79-3.54-2.58-2.36-3.87-5.9-3.87-10.63V51.95h-18.95v-11.86h19.11V10.43h15.16v29.66h27.02v11.86h-26.86v74.8h29.49v12.19h-30.32Z',
    'M861.7,25.25c-2.75,0-5.08-.96-7-2.88-1.92-1.92-2.88-4.26-2.88-7s.96-5.08,2.88-7c1.92-1.92,4.26-2.88,7-2.88s5.08.96,7,2.88c1.92,1.92,2.88,4.26,2.88,7s-.96,5.08-2.88,7c-1.92,1.92-4.26,2.88-7,2.88ZM853.95,138.94V40.08h15.49v98.86h-15.49Z',
    'M928.43,140.92c-10.11,0-18.89-2.11-26.36-6.34-7.47-4.23-13.26-10.19-17.38-17.88-4.12-7.69-6.18-16.75-6.18-27.19s2.03-19.5,6.09-27.19c4.06-7.69,9.86-13.65,17.38-17.88,7.52-4.23,16.34-6.34,26.45-6.34s19.2,2.09,26.61,6.26c7.41,4.18,13.15,10.11,17.22,17.8,4.06,7.69,6.09,16.8,6.09,27.35s-2.03,19.52-6.09,27.27c-4.07,7.74-9.83,13.71-17.3,17.88-7.47,4.18-16.31,6.26-26.53,6.26ZM928.43,127.41c10.87,0,19.33-3.32,25.37-9.97,6.04-6.64,9.06-15.95,9.06-27.93s-3.02-21.42-9.06-28.01c-6.04-6.59-14.5-9.89-25.37-9.89s-19.2,3.32-25.29,9.97c-6.1,6.65-9.15,15.96-9.15,27.93s3.05,21.28,9.15,27.93c6.09,6.65,14.53,9.97,25.29,9.97Z',
    'M989.55,138.94V56.56h-8.24v-16.48h17.14v14.83h6.92c3.3-5.49,7.94-9.64,13.92-12.44,5.99-2.8,12.82-4.2,20.51-4.2,12.41,0,22.3,3.62,29.66,10.87,7.36,7.25,11.04,16.92,11.04,29v60.8h-15.49v-58.49c0-9.34-2.55-16.45-7.66-21.34-5.11-4.89-12.55-7.33-22.32-7.33s-17.08,2.45-22.24,7.33c-5.16,4.89-7.74,12-7.74,21.34v58.49h-15.49Z',
  ];

  const lerp  = (a, b, t) => a + (b - a) * t;
  const clamp = (v, lo, hi) => Math.max(lo, Math.min(hi, v));

  /* ── GNB Header ─────────────────────────────── */
  function GNBSHeader({ currentPage, onNavigate }) {
    const [scrolled, setScrolled] = useState(false);
    const [brandOpen, setBrandOpen] = useState(false);

    useEffect(() => {
      const onScroll = () => setScrolled(window.scrollY > 64);
      window.addEventListener('scroll', onScroll, { passive: true });
      return () => window.removeEventListener('scroll', onScroll);
    }, []);

    const nav = (page) => {
      onNavigate(page);
      setBrandOpen(false);
      window.scrollTo({ top: 0, behavior: 'instant' });
    };

    const isActive = (page) => currentPage === page;
    const isBrandActive = currentPage === 'brand-bestrong373' || currentPage === 'brand-micro-miracle';

    return (
      <header className={`gnbs-header${scrolled ? ' scrolled' : ''}`}>
        <button className="header-logo" onClick={() => nav('home')} aria-label="GNBIO 홈">
          <svg
            xmlns="http://www.w3.org/2000/svg"
            viewBox="0 0 215 181"
            height="100%"
            preserveAspectRatio="xMinYMid meet"
            style={{ display: 'block', fill: 'currentColor' }}
          >
            <path d={GN_D} />
          </svg>
        </button>

        <nav className="header-nav">
          <button
            className="nav-item"
            onClick={() => nav('company')}
            style={{
              opacity: isActive('company') ? 1 : undefined,
              textDecoration: isActive('company') ? 'underline' : 'none',
              textUnderlineOffset: '0.3em',
            }}
          >
            COMPANY
          </button>

          <div
            style={{ position: 'relative' }}
            onMouseEnter={() => setBrandOpen(true)}
            onMouseLeave={() => setBrandOpen(false)}
          >
            <button
              className="nav-item"
              onClick={() => nav('brand-bestrong373')}
              style={{
                opacity: isBrandActive ? 1 : undefined,
                display: 'flex',
                alignItems: 'center',
                gap: '0.3em',
              }}
            >
              BRAND
              <span style={{
                fontSize: '0.7em',
                transition: 'transform 0.2s',
                display: 'inline-block',
                transform: brandOpen ? 'rotate(180deg)' : 'none',
              }}>▾</span>
            </button>
            {brandOpen && (
              <div className="nav-dropdown">
                <div className="nav-dropdown-inner">
                  <button className="nav-dropdown-item" onClick={() => nav('brand-bestrong373')}>
                    BESTRONG373
                  </button>
                  <button className="nav-dropdown-item" onClick={() => nav('brand-micro-miracle')}>
                    MICRO MIRACLE
                  </button>
                </div>
              </div>
            )}
          </div>

          <button
            className="nav-item"
            onClick={() => nav('culture')}
            style={{
              textDecoration: isActive('culture') ? 'underline' : 'none',
              textUnderlineOffset: '0.3em',
            }}
          >
            CULTURE
          </button>
          <button
            className="nav-item"
            onClick={() => nav('jobs')}
            style={{
              textDecoration: isActive('jobs') ? 'underline' : 'none',
              textUnderlineOffset: '0.3em',
            }}
          >
            JOBS
          </button>
        </nav>
      </header>
    );
  }

  /* ── Footer ─────────────────────────────────── */
  function GNBSFooter({ onNavigate }) {
    const nav = (page) => {
      onNavigate(page);
      window.scrollTo({ top: 0, behavior: 'instant' });
    };

    return (
      <footer className="gnbs-footer">
        <div className="footer-top">
          <div>
            <div className="footer-logo" onClick={() => nav('home')}>
              <img src="./assets/logo-gnbio-b.svg" alt="지앤바이오솔루션" style={{ height: '100%', display: 'block' }} />
            </div>
            <div className="footer-info">
              {D.company.name}<br />
              대표이사: {D.company.ceo.name}<br />
              {D.company.postcode} {D.company.address}<br />
              사업자등록번호: {D.company.bizNo}<br />
              통신판매업: {D.company.ecommerceNo}<br />
              <br />
              ☎ {D.company.phone} ({D.company.csHours})<br />
              {D.company.email}
            </div>
          </div>

          <div className="footer-nav-cols">
            <div>
              <div className="footer-col-title">Company</div>
              <button className="footer-link" onClick={() => nav('company')}>회사 소개</button>
              <button className="footer-link" onClick={() => nav('company')}>CEO 인사말</button>
              <button className="footer-link" onClick={() => nav('company')}>연혁</button>
              <button className="footer-link" onClick={() => nav('company')}>파트너</button>
            </div>
            <div>
              <div className="footer-col-title">Brand</div>
              <button className="footer-link" onClick={() => nav('brand-bestrong373')}>BESTRONG373</button>
              <button className="footer-link" onClick={() => nav('brand-micro-miracle')}>MICRO MIRACLE</button>
            </div>
            <div>
              <div className="footer-col-title">Culture</div>
              <button className="footer-link" onClick={() => nav('culture')}>핵심 가치</button>
              <button className="footer-link" onClick={() => nav('jobs')}>채용 공고</button>
            </div>
            <div>
              <div className="footer-col-title">Legal</div>
              <button className="footer-link">개인정보처리방침</button>
              <button className="footer-link">이용약관</button>
            </div>
          </div>
        </div>

        <div className="footer-bottom">
          <span>© 2026 지앤바이오솔루션. All rights reserved.</span>
          <span>GN BIO SOLUTION</span>
        </div>
      </footer>
    );
  }

  /* ── Hero v2 ─────────────────────────────────
     Phase AB (0→2/3): 로고 축소 + 영상 상승
     Phase C  (2/3→1): 영상 전체화면 확장
  ───────────────────────────────────────────── */
  function GNBSHero() {
    const driverRef  = useRef(null);
    const videoWrap  = useRef(null);
    const taglineRef = useRef(null);
    const overlayRef = useRef(null);

    useEffect(() => {
      const driver  = driverRef.current;
      const vid     = videoWrap.current;
      const tagline = taglineRef.current;
      const overlay = overlayRef.current;
      if (!driver || !vid || !tagline || !overlay) return;

      document.documentElement.style.setProperty('--hero-progress', '0');

      const AB_END = 2 / 3;

      const getNavH = () => {
        const el = document.querySelector('.gnbs-header');
        return el ? el.offsetHeight : window.innerWidth * 0.036;
      };

      let phaseDone  = false;
      let phaseCDone = false;

      const update = () => {
        const W  = window.innerWidth;
        const H  = window.innerHeight;
        const nH = getNavH();
        const secH = W * 0.02;

        const scrollable = Math.max(1, driver.offsetHeight - H);
        const rawP = clamp(window.scrollY / scrollable, 0, 1);

        const pAB  = clamp(rawP / AB_END, 0, 1);
        const rawPC = clamp((rawP - AB_END) / (1 - AB_END), 0, 1);
        if (rawPC >= 1) phaseCDone = true;
        const pC = Math.max(rawPC, phaseCDone ? 1 : 0);

        const initW  = W - 2 * secH;
        const initH  = initW * (181 / 1081);
        const finalH = nH * 0.55;

        const descH   = nH * 1.4;
        const descPad = secH * 2;
        const descY   = H - descH - descPad;

        const G        = (descY - nH - initH) / 3;
        const initT    = nH + G;
        const initL    = secH;
        const initVidT = initT + initH + G;
        const finalVidH = H - nH;
        const finalVidT = nH;

        const rAB = Math.max(pAB, phaseCDone ? 1 : 0);

        const vidTop = lerp(initVidT, finalVidT, rAB);
        vid.style.top    = vidTop + 'px';
        vid.style.height = finalVidH + 'px';
        vid.style.opacity = '1';

        vid.style.left         = '0';
        vid.style.width        = W + 'px';
        vid.style.borderRadius = '0';
        if (pC > 0) {
          vid.style.top    = lerp(nH, 0, pC) + 'px';
          vid.style.height = lerp(H - nH, H, pC) + 'px';
        }

        tagline.style.top   = descY + 'px';
        tagline.style.color = 'rgba(255,255,255,0.9)';

        if (!phaseDone) {
          const svg = overlay.querySelector('svg');
          overlay.style.display = '';

          const logoH   = lerp(initH, finalH, pAB);
          const logoW   = logoH * (1081 / 181);
          const logoTop = lerp(initT, nH - finalH, pAB);

          overlay.style.top    = logoTop + 'px';
          overlay.style.left   = initL + 'px';
          overlay.style.width  = logoW + 'px';
          overlay.style.height = logoH + 'px';
          if (svg) svg.setAttribute('viewBox', '0 0 1081 181');

          if (pAB >= 1) {
            phaseDone = true;
            overlay.style.display = 'none';
            document.documentElement.style.setProperty('--hero-progress', '1');
          }
        }
      };

      window.addEventListener('scroll', update, { passive: true });
      window.addEventListener('resize', update);
      update();

      return () => {
        window.removeEventListener('scroll', update);
        window.removeEventListener('resize', update);
        document.documentElement.style.setProperty('--hero-progress', '1');
      };
    }, []);

    return (
      <>
        <div ref={driverRef} className="hero2-driver" style={{ height: '400vh' }}>
          <div className="hero2-sticky">
            <div ref={videoWrap} className="hero2-video">
              <video
                src="https://cogit1.cafe24.com/cogit1_29480dbc638338d2bd5cf9f885c8f15d_HD.mp4"
                loop muted playsInline autoPlay
              />
            </div>
            <p ref={taglineRef} className="hero2-tagline">
              {'우리는 테크가 곧 미래라는 신념으로\n최첨단 기술을 발굴하고 디벨롭하여 시장을 만드는 그룹입니다.'}
            </p>
          </div>
        </div>

        <div ref={overlayRef} className="hero2-logo-overlay">
          <svg
            xmlns="http://www.w3.org/2000/svg"
            viewBox="0 0 1081 181"
            width="100%"
            height="100%"
            preserveAspectRatio="xMinYMid meet"
          >
            <path fill="#010101" d={GN_D} />
            <g>
              {BIO_DS.map((d, i) => (
                <path key={i} fill="#010101" d={d} />
              ))}
            </g>
          </svg>
        </div>
      </>
    );
  }

  /* ── Page Hero — 서브 페이지 공용 ─────────── */
  function GNBSPageHero() {
    useEffect(() => {
      document.documentElement.style.setProperty('--hero-progress', '1');
    }, []);

    return (
      <div className="page-hero-driver">
        <div className="page-hero-sticky">
          <div className="page-hero-video">
            <img src="./assets/page-hero-sample.png" alt="" />
          </div>
          <div className="page-hero-center">
            <span className="page-hero-label">PAGE TITLE</span>
            <p className="page-hero-desc">
              {'여기는 페이지 디스크립터나 리드 메시지가\n들어가는 자리이니 페이지 카피 확정후 입력해주세요.'}
            </p>
          </div>
        </div>
      </div>
    );
  }

  /* ── CTA Banner ─────────────────────────────── */
  function GNBSCtaBanner({ eyebrow, headline, body, ctaLabel, onCta, videoSrc }) {
    const DEFAULT_VIDEO = 'https://cogit1.cafe24.com/cogit1_52439c715fa62e42ceee7550e6d69f38_HD.mp4';
    return (
      <section className="cta-banner">
        <video
          className="cta-banner-video"
          src={videoSrc || DEFAULT_VIDEO}
          autoPlay loop muted playsInline
        />
        <div className="cta-banner-overlay" />
        <div className="cta-banner-content">
          <div>
            {eyebrow && (
              <span className="sec-label cta-banner-eyebrow">{eyebrow}</span>
            )}
            <h2 className="cta-banner-headline">{headline}</h2>
          </div>
          <div className="cta-banner-bottom">
            <p className="cta-banner-body">{body}</p>
            {ctaLabel && (
              <button
                className="btn btn--outline-inv"
                onClick={() => { onCta && onCta(); window.scrollTo({ top: 0, behavior: 'instant' }); }}
              >
                {ctaLabel}
              </button>
            )}
          </div>
        </div>
      </section>
    );
  }

  /* ── 전역 노출 ─────────────────────────────── */
  window.GNBSHeader   = GNBSHeader;
  window.GNBSFooter   = GNBSFooter;
  window.GNBSHero     = GNBSHero;
  window.GNBSPageHero = GNBSPageHero;
  window.GNBSCtaBanner = GNBSCtaBanner;

})();
