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

import type { Project } from "../../../shared/api";

const ProjectContext = createContext<null | Project>(null);

export function ProjectProvider({ children, project }: { children: ReactNode; project: Project }) {
  return <ProjectContext.Provider value={project}>{children}</ProjectContext.Provider>;
}

export function useCurrentProject(): Project {
  const project = useContext(ProjectContext);
  if (!project) {
    throw new Error("useCurrentProject 必须在 Workbench 项目上下文内使用");
  }
  return project;
}
