import { createContext, useContext, useMemo } from "react";

const PageTitleContext = createContext<Map<string, string>>(new Map());

interface PageTitleProviderProps {
  pages: { id: string; title: string }[];
  children: React.ReactNode;
}

export function PageTitleProvider({ pages, children }: PageTitleProviderProps) {
  const titleMap = useMemo(
    () => new Map(pages.map((p) => [p.id, p.title])),
    [pages],
  );

  return (
    <PageTitleContext.Provider value={titleMap}>
      {children}
    </PageTitleContext.Provider>
  );
}

export function usePageTitle(pageId: string): string {
  const titleMap = useContext(PageTitleContext);
  return titleMap.get(pageId) ?? "Untitled";
}
