// redux/slices/mapLayersSlice.js
import { createSlice } from "@reduxjs/toolkit";

const initialState = {};

const mapLayersSlice = createSlice({
  name: "mapLayers",
  initialState,
  reducers: {
    toggleLayer: (state, action) => {
      const layer = action.payload;
      if (state[layer] !== undefined) {
        state[layer] = !state[layer]; // Toggle Sichtbarkeit
      }
    },
    setLayerVisibility: (state, action) => {
      const { layer, visibility } = action.payload;
      if (state[layer] !== undefined) {
        state[layer] = visibility;
      }
    },
    setInitialLayers: (state, action) => {
      const systems = action.payload; // Array of GisSystem
      systems.forEach((system) => {
        const key = `system-${system.IdSystem}`;
        state[key] = true; // oder false, je nach Default
      });
    },
  },
});

export const { toggleLayer, setLayerVisibility, setInitialLayers } = mapLayersSlice.actions;
export const selectMapLayersState = (state) => state.mapLayers || initialState;
export default mapLayersSlice.reducer;
