// /redux/thunks/fetchAllTDMThunk.ts

import { createAsyncThunk } from "@reduxjs/toolkit";
import { RootState } from "../store";
import { fetchAllTDMDataFromServer } from "../../services/fetchAllTDMDataService";

export const fetchAllTDMData = createAsyncThunk(
  "tdmChart/fetchAllTDMData",
  async (_, { getState, rejectWithValue }) => {
    const currentData = (getState() as RootState).tdmChartSlice.data;
    const newData = await fetchAllTDMDataFromServer();

    if (newData.every((entry) => !entry)) {
      return rejectWithValue("Keine TDM-Daten empfangen.");
    }

    if (JSON.stringify(currentData) !== JSON.stringify(newData)) {
      return newData;
    }

    return rejectWithValue("Keine Änderungen in den TDM-Daten.");
  }
);
