// /redux/slices/tdrReferenceChartDataBySlotSlice.ts
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { fetchReferenceCurveBySlotThunk } from "../thunks/fetchReferenceCurveBySlotThunk";

interface TDRReferenceChartState {
  referenceData: { [slot: number]: any[] };
  loading: boolean;
  error: string | null;
}

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

const tdrReferenceChartDataBySlotSlice = createSlice({
  name: "tdrReferenceChartDataBySlotSlice",
  initialState,
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(fetchReferenceCurveBySlotThunk.pending, (state) => {
        state.loading = true;
        state.error = null;
      })
      .addCase(
        fetchReferenceCurveBySlotThunk.fulfilled,
        (state, action: PayloadAction<{ slot: number; data: any[] }>) => {
          state.loading = false;
          state.referenceData[action.payload.slot] = action.payload.data;
        }
      )
      .addCase(fetchReferenceCurveBySlotThunk.rejected, (state, action) => {
        state.loading = false;
        state.error = action.error.message ?? "Unbekannter Fehler";
      });
  },
});

export default tdrReferenceChartDataBySlotSlice.reducer;
