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

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

export default temperaturAdWandlerSlice.reducer;
