import { baseAPI } from "@/lib/API";
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";

export const fetchAbout = createAsyncThunk(
  "about/fetchAbout",
  async ({ locale }: { locale: string }) => {
    const res = await fetch(`${baseAPI}settings/about_us`, {
      headers: {
        "Accept-Language": locale,
      },
    });

    const data = await res.json();

    return data;
  }
);

interface AboutType {
  aboutData: string;
}

const initialState: AboutType = {
  aboutData: "",
};

const aboutSlice = createSlice({
  name: "aboutUs",
  initialState,
  reducers: {
    setAboutUsData(state, action) {
      state.aboutData = action.payload;
    },
  },

  extraReducers: (builder) => {
    builder.addCase(fetchAbout.pending, (state) => {});

    builder.addCase(fetchAbout.fulfilled, (state, action) => {
      state.aboutData = action.payload;
    });

    builder.addCase(fetchAbout.rejected, (state) => {
      console.log("error");
    });
  },
});

export const { setAboutUsData } = aboutSlice.actions;

export default aboutSlice.reducer;
