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

export type MatrixRainContextValue = {
  /** True when Crucible has exactly one online node selected for remote ops. */
  crucibleFocus: boolean;
  setCrucibleFocus: (active: boolean) => void;
};

const MatrixRainContext = createContext<MatrixRainContextValue | null>(null);

export function MatrixRainProvider({ children }: { children: ReactNode }) {
  const [crucibleFocus, setCrucibleFocus] = useState(false);
  const value = useMemo(
    () => ({ crucibleFocus, setCrucibleFocus }),
    [crucibleFocus]
  );
  return (
    <MatrixRainContext.Provider value={value}>{children}</MatrixRainContext.Provider>
  );
}

export function useMatrixRain(): MatrixRainContextValue {
  const ctx = useContext(MatrixRainContext);
  if (!ctx) {
    return { crucibleFocus: false, setCrucibleFocus: () => {} };
  }
  return ctx;
}
