import { createSlice } from '@reduxjs/toolkit';
import { isAmityAd } from '../../../social/hooks/useCustomRankingGlobalFeed';
const initialState = {
  postList: [],
  paginationData: {
    next: null,
    previous: null
  }
};
const globalFeedSlice = createSlice({
  name: 'globalFeed',
  initialState,
  reducers: {
    setNewGlobalFeed: (state, action) => {
      const seen = new Set();
      state.postList = action.payload.filter(post => {
        if (seen.has(post.postId)) return false;
        seen.add(post.postId);
        return true;
      });
    },
    updateGlobalFeed: (state, action) => {
      const uniqueIds = new Set(state.postList.map(post => isAmityAd(post) ? post.adId : post.postId));
      const newUnique = [];
      for (const post of action.payload) {
        const id = isAmityAd(post) ? post.adId : post.postId;
        if (!uniqueIds.has(id)) {
          uniqueIds.add(id);
          newUnique.push(post);
        }
      }
      state.postList = [...state.postList, ...newUnique];
    },
    setPaginationData: (state, action) => {
      state.paginationData = action.payload;
    },
    addPostToGlobalFeed: (state, action) => {
      const alreadyExists = state.postList.some(item => !isAmityAd(item) && item.postId === action.payload.postId);
      if (!alreadyExists) {
        state.postList = [action.payload, ...state.postList];
      }
    },
    updateByPostId: (state, action) => {
      const {
        postId,
        postDetail
      } = action.payload;
      const index = state.postList.findIndex(item => !isAmityAd(item) && item.postId === postId);
      state.postList[index] = postDetail;
    },
    deleteByPostId: (state, action) => {
      const {
        postId
      } = action.payload;
      const prevPostList = [...state.postList];
      const updatedPostList = prevPostList.filter(item => !isAmityAd(item) && item.postId !== postId);
      state.postList = updatedPostList;
    },
    clearFeed: state => {
      state.postList = [];
    }
  }
});
export default globalFeedSlice;
//# sourceMappingURL=globalfeedSlice.js.map