/* KYDT multi-page router — each page is a real URL. onNav() navigates for real
   (so refresh, share and the back button all work), or scrolls to top if the
   link points at the page you're already on. */
const ROUTES = { home:'/', about:'/about', closer:'/closer', partner:'/partner' };

function normPath(p){
  // strip index.html / .html and any trailing slash, so /about, /about/ and
  // /about.html all compare equal.
  p = p.replace(/index\.html$/, '').replace(/\.html$/, '');
  if (p.length > 1 && p.endsWith('/')) p = p.slice(0, -1);
  return p === '' ? '/' : p;
}

function KYDTNav(id){
  const target = ROUTES[id] || '/';
  if (normPath(location.pathname) === normPath(target)){
    window.scrollTo({ top:0, behavior:'smooth' });
    return;
  }
  window.location.href = target;
}

function KYDTRender(pageId, PageComponent){
  const { useEffect, useRef } = React;
  function App(){
    const mainRef = useRef(null);
    useEffect(()=>{ if(window.lucide) window.lucide.createIcons(); });
    useEffect(()=>{
      if(mainRef.current && window.gsap){
        window.gsap.fromTo(mainRef.current, {opacity:0, y:14}, {opacity:1, y:0, duration:0.5, ease:'power2.out'});
      }
    },[]);
    return (
      <React.Fragment>
        <window.SiteHeader current={pageId} onNav={KYDTNav} />
        <main ref={mainRef}><PageComponent onNav={KYDTNav} /></main>
        <window.SiteFooter current={pageId} onNav={KYDTNav} />
      </React.Fragment>
    );
  }
  ReactDOM.createRoot(document.getElementById('root')).render(<App/>);
}
window.KYDTRender = KYDTRender;
window.KYDTNav = KYDTNav;
