// /hooks/useMapComponentState.js

import { useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";

// Redux: POI-Typen
import { fetchPoiTypThunk } from "@/redux/thunks/database/pois/fetchPoiTypThunk";
import { selectPoiTypData, selectPoiTypStatus } from "@/redux/slices/database/pois/poiTypSlice";

// Redux: GIS Geräte
import { fetchGisStationsStaticDistrictThunk } from "@/redux/thunks/webservice/fetchGisStationsStaticDistrictThunk";
import { selectGisStationsStaticDistrict } from "@/redux/slices/webservice/gisStationsStaticDistrictSlice";

// Redux: priorityConfig
import { fetchPriorityConfigThunk } from "@/redux/thunks/database/fetchPriorityConfigThunk";
import { selectPriorityConfig } from "@/redux/slices/database/priorityConfigSlice";

export const useMapComponentState = () => {
  const dispatch = useDispatch();

  // Redux: Sichtbarkeit des POI-Layers
  const poiLayerVisible = useSelector(state => state.poiLayerVisible.visible);

  // Redux: POI-Typen
  const poiTypData = useSelector(selectPoiTypData);
  const poiTypStatus = useSelector(selectPoiTypStatus);

  // Redux: Geräte
  const locationDeviceData = useSelector(selectGisStationsStaticDistrict);
  const deviceName = locationDeviceData?.Points?.[0]?.LD_Name || "";

  // Redux: Prioritätskonfiguration
  const priorityConfig = useSelector(selectPriorityConfig);

  // UI-interner Zustand
  const [menuItemAdded, setMenuItemAdded] = useState(false);

  // POI-Typen laden
  useEffect(() => {
    if (poiTypStatus === "idle") {
      dispatch(fetchPoiTypThunk());
    }
  }, [dispatch, poiTypStatus]);

  // GIS Geräte laden
  useEffect(() => {
    dispatch(fetchGisStationsStaticDistrictThunk());
  }, [dispatch]);

  // PriorityConfig laden
  useEffect(() => {
    dispatch(fetchPriorityConfigThunk());
  }, [dispatch]);

  return {
    poiTypData,
    isPoiTypLoaded: poiTypStatus === "succeeded",
    deviceName,
    locationDeviceData,
    priorityConfig,
    menuItemAdded,
    setMenuItemAdded,
    poiLayerVisible,
  };
};
