import { createContext, type ReactNode, useContext } from "react";
import type { PlatformWebModule } from "../types";
import { moduleInstalled, uiCapabilities, uiCapability } from "./modules";

const PlatformModulesContext = createContext<PlatformWebModule[]>([]);

export function PlatformModulesProvider({ modules, children }: { modules: PlatformWebModule[]; children: ReactNode }) {
  return <PlatformModulesContext.Provider value={modules}>{children}</PlatformModulesContext.Provider>;
}

export function usePlatformModules(): PlatformWebModule[] {
  return useContext(PlatformModulesContext);
}

export function usePlatformModuleInstalled(moduleId: string): boolean {
  return moduleInstalled(moduleId, usePlatformModules());
}

export function usePlatformUiCapability<T = unknown>(capabilityName: string): T | null {
  return uiCapability<T>(capabilityName, usePlatformModules());
}

export function usePlatformUiCapabilities<T = unknown>(capabilityName: string): T[] {
  return uiCapabilities<T>(capabilityName, usePlatformModules());
}
