// /redux/slices/tdrReferenceChartSlice.ts

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

interface TDRReferenceChartState {
  referenceData: any[]; // Array mit Slot-Daten (Index = Slot)
  loading: boolean;
  error: string | null;
}

const initialState: TDRReferenceChartState = {
  referenceData: [],
  loading: false,
  error: null,
};

const tdrReferenceChartSlice = createSlice({
  name: "tdrReferenceChartSlice",
  initialState,
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(fetchAllTDRReferenceChartThunk.pending, (state) => {
        state.loading = true;
        state.error = null;
      })
      .addCase(
        fetchAllTDRReferenceChartThunk.fulfilled,
        (state, action: PayloadAction<any[]>) => {
          state.loading = false;
          state.referenceData = action.payload;
        }
      )
      .addCase(fetchAllTDRReferenceChartThunk.rejected, (state, action) => {
        state.loading = false;
        state.error = action.payload as string | null;
      });
  },
});

export default tdrReferenceChartSlice.reducer;
