import type { GalaxyConnection, GalaxySystem, NavigationRoute } from "../shared/types";
import { combineRoutes, findShortestRoute } from "../shared/routing";

export type StarMapState = {
  systems: GalaxySystem[];
  connections: GalaxyConnection[];
  selectedSystemId: string | null;
  hoveredSystemId: string | null;
  currentSystemId: string;
  destinationSystemId: string | null;
  waypointSystemIds: string[];
  route: NavigationRoute | null;
  searchQuery: string;
  securityFilter: "all" | "highsec" | "lowsec" | "nullsec";
};

export function computeWaypointRoute(state: StarMapState, destinationId = state.destinationSystemId, waypointIds = state.waypointSystemIds) {
  const stops = [state.currentSystemId, ...waypointIds, ...(destinationId ? [destinationId] : [])];
  if (stops.length < 2) return null;
  return combineRoutes(stops.slice(0, -1).map((id, index) => findShortestRoute(id, stops[index + 1], state.systems, state.connections)));
}

export function createStarMapState(systems: GalaxySystem[], connections: GalaxyConnection[]): StarMapState {
  return {
    systems,
    connections,
    selectedSystemId: null,
    hoveredSystemId: null,
    currentSystemId: "sol",
    destinationSystemId: null,
    waypointSystemIds: [],
    route: null,
    searchQuery: "",
    securityFilter: "all",
  };
}
