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

import type { MissionRuntimeController } from "./contracts";

const idleRuntime: MissionRuntimeController = {
  state: {
    phase: "unconfigured",
    message: "Сначала выберите модель локального устройства.",
    sourceMode: "idle",
  },
  backendStatus: "unconfigured",
  pendingAction: null,
  refresh: () => undefined,
  updateViewerSettings: async () => false,
  setObservationSourceActive: async () => false,
};

const MissionRuntimeContext = createContext<MissionRuntimeController>(idleRuntime);

export function MissionRuntimeProvider({
  value,
  children,
}: {
  value: MissionRuntimeController;
  children: ReactNode;
}) {
  return <MissionRuntimeContext.Provider value={value}>{children}</MissionRuntimeContext.Provider>;
}

export function IdleMissionRuntimeProvider({ children }: { children: ReactNode }) {
  return <MissionRuntimeProvider value={idleRuntime}>{children}</MissionRuntimeProvider>;
}

export function useMissionRuntime(): MissionRuntimeController {
  return useContext(MissionRuntimeContext);
}
