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

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 systemspannung15VplusSlice = createSlice({
  name: "systemspannung15Vplus",
  initialState,
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(getSystemspannung15VplusThunk.pending, (state) => {
        state.isLoading = true;
        state.error = null;
      })
      .addCase(
        getSystemspannung15VplusThunk.fulfilled,
        (
          state,
          action: PayloadAction<{
            typ: "DIA0" | "DIA1" | "DIA2";
            data: unknown[];
          }>
        ) => {
          state.isLoading = false;
          state[action.payload.typ] = action.payload.data;
        }
      )
      .addCase(getSystemspannung15VplusThunk.rejected, (state, action) => {
        state.isLoading = false;
        state.error = action.payload as string;
      });
  },
});

export default systemspannung15VplusSlice.reducer;
