import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { getSystemspannung15VminusThunk } from "../thunks/getSystemspannung15VminusThunk";

type StateType = {
  DIA0: unknown[];
  DIA1: unknown[];
  DIA2: unknown[];
  isLoading: boolean;
  error: string | null;
};

const initialState: StateType = {
  DIA0: [],
  DIA1: [],
  DIA2: [],
  isLoading: false,
  error: null,
};

export const systemspannung15VminusSlice = createSlice({
  name: "systemspannung15Vminus",
  initialState,
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(getSystemspannung15VminusThunk.pending, (state) => {
        state.isLoading = true;
        state.error = null;
      })
      .addCase(
        getSystemspannung15VminusThunk.fulfilled,
        (
          state,
          action: PayloadAction<{
            typ: "DIA0" | "DIA1" | "DIA2";
            data: unknown[];
          }>
        ) => {
          state.isLoading = false;
          state[action.payload.typ] = action.payload.data;
        }
      )
      .addCase(getSystemspannung15VminusThunk.rejected, (state, action) => {
        state.isLoading = false;
        state.error = action.payload as string;
      });
  },
});

export default systemspannung15VminusSlice.reducer;
