"use client";

import { createContext, useContext, useState, type ReactNode } from "react";
import { useSiteScripts } from "@/app/(site)/_components/scripts";

const PageRootCtx = createContext<HTMLElement | null>(null);
/** The page's root element: where the mobile menu is moved (it used to go to <body>). */
export const usePageRoot = () => useContext(PageRootCtx);

/**
 * Wraps every website page. The static site put a per-page class on <body>
 * (e.g. "about-page"); here it sits on this wrapper, which uses display: contents
 * so it creates no box. Layout is exactly as if its children were <body>'s children,
 * while selectors like `.about-page .hero` and inherited styles still apply.
 */
export function SitePage({ className, children }: { className?: string; children: ReactNode }) {
  const [root, setRoot] = useState<HTMLDivElement | null>(null);
  useSiteScripts(root);
  return (
    <PageRootCtx.Provider value={root}>
      <div ref={setRoot} className={className} style={{ display: "contents" }}>
        {children}
      </div>
    </PageRootCtx.Provider>
  );
}
