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

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

export default temperaturProzessorSlice.reducer;
