import { createContext, ReactNode, useContext, useState } from "react";
import { RouteData, StationData, SightData } from "./types";

const MapDataContext = createContext<{
  routeData?: RouteData;
  stationData?: StationData[];
  stationDataEn?: StationData[];
  stationDataZh?: StationData[];
  sightData?: SightData[]; // <--- ИЗМЕНЕНО: теперь это массив SightData[]
  setRouteData: React.Dispatch<React.SetStateAction<RouteData | undefined>>;
  setStationData: React.Dispatch<
    React.SetStateAction<StationData[] | undefined>
  >;
  setStationDataEn: React.Dispatch<
    React.SetStateAction<StationData[] | undefined>
  >;
  setStationDataZh: React.Dispatch<
    React.SetStateAction<StationData[] | undefined>
  >;
  setSightData: React.Dispatch<React.SetStateAction<SightData[] | undefined>>; // <--- ИЗМЕНЕНО: теперь это массив SightData[]
}>({
  routeData: undefined,
  stationData: undefined,
  stationDataEn: undefined,
  sightData: undefined, // Инициализация sightData
  setRouteData: () => {},
  setStationData: () => {},
  setStationDataEn: () => {},
  setStationDataZh: () => {},
  setSightData: () => {},
});

export const MapDataProvider = ({ children }: { children: ReactNode }) => {
  const [routeData, setRouteData] = useState<RouteData>();
  const [stationData, setStationData] = useState<StationData[]>();
  const [stationDataEn, setStationDataEn] = useState<StationData[]>();
  const [stationDataZh, setStationDataZh] = useState<StationData[]>();
  const [sightData, setSightData] = useState<SightData[]>(); // <--- ИЗМЕНЕНО: теперь это массив SightData[]

  const value = {
    routeData,
    stationData,
    stationDataEn,
    stationDataZh,
    sightData,
    setRouteData,
    setStationData,
    setStationDataEn,
    setSightData,
    setStationDataZh,
  };

  return (
    <MapDataContext.Provider value={value}>{children}</MapDataContext.Provider>
  );
};

export const useMapData = () => {
  const context = useContext(MapDataContext);
  if (!context) {
    throw new Error("useMapData must be used within a MapDataProvider");
  }
  return context;
};
