// Shared context for cross-pane focus state and projectId plumbing.
//
// `focusBlockId` drives diagram selection / chip hover styling. The richer
// term/finding/etc detail navigation now lives in the column-stack
// (openPanesStore), so this context is intentionally minimal.

"use client";

import { createContext, useContext, useMemo, useState, type ReactNode } from "react";

interface PaneContextValue {
  projectId: string;
  focusBlockId: string | null;
  setFocusBlockId: (id: string | null) => void;
}

const Ctx = createContext<PaneContextValue | null>(null);

interface ProviderProps {
  projectId: string;
  children: ReactNode;
}

export function EditorPaneContextProvider({ projectId, children }: ProviderProps) {
  const [focusBlockId, setFocusBlockId] = useState<string | null>(null);
  const value = useMemo<PaneContextValue>(
    () => ({ projectId, focusBlockId, setFocusBlockId }),
    [projectId, focusBlockId]
  );
  return <Ctx.Provider value={value}>{children}</Ctx.Provider>;
}

export function useEditorPaneContext(): PaneContextValue {
  const ctx = useContext(Ctx);
  if (!ctx) throw new Error("useEditorPaneContext must be inside <EditorPaneContextProvider>");
  return ctx;
}
