// redux/slices/analogInputs/selectedAnalogInputSlice.ts
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import type { AnalogInput } from "@/types/analogInput";

// Anfangszustand: noch kein Eingang ausgewählt
const initialState: AnalogInput = {
  id: 0,
  label: "",
  value: 0,
  name: "",
};

const selectedAnalogInputSlice = createSlice({
  name: "selectedAnalogInput",
  initialState,
  reducers: {
    setSelectedAnalogInput(state, action: PayloadAction<AnalogInput>) {
      if (state) {
        // Mutiert vorhandene Struktur (optional, wenn initialState nicht null sein darf)
        Object.assign(state, action.payload);
      } else {
        return action.payload;
      }
    },
    clearSelectedAnalogInput() {
      return initialState;
    },
  },
});

export const { setSelectedAnalogInput, clearSelectedAnalogInput } =
  selectedAnalogInputSlice.actions;

export default selectedAnalogInputSlice.reducer;
