import { createContext } from "preact";
import { useContext } from "preact/hooks";

import type {
  RealtimeStation,
  RealtimeStop,
  RealtimeStopSequence,
} from "mobility-toolbox-js/types";

import type { StopStatus } from "../getStopStatus";

export interface RouteStopContextType {
  index?: number;
  invertColor?: boolean;
  station?: RealtimeStation;
  status?: StopStatus;
  stop?: {
    platform?: string;
  } & RealtimeStop;
  stopSequence?: RealtimeStopSequence;
}

export const RouteStopContext = createContext({
  index: null,
  invertColor: null,
  station: null,
  status: null,
  stop: null,
  stopSequence: null,
} as RouteStopContextType);

const useRouteStop = (): RouteStopContextType => {
  const context = useContext<RouteStopContextType>(RouteStopContext);
  if (!context) {
    throw new Error("useRouteStop must be used within a ContextProvider");
  }
  return context;
};

export default useRouteStop;
