"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _toolkit = require("@reduxjs/toolkit");
const initialState = {
  postList: []
};
const feedSlice = (0, _toolkit.createSlice)({
  name: 'feed',
  initialState,
  reducers: {
    updateFeed: (state, action) => {
      const getUniqueArrayById = arr => {
        const uniqueIds = new Set(state.postList.map(post => post.postId));
        return arr.filter(post => !uniqueIds.has(post.postId));
      };
      state.postList = [...getUniqueArrayById(action.payload), ...state.postList];
    },
    addPostToFeed: (state, action) => {
      state.postList = [action.payload, ...state.postList];
    },
    updateByPostId: (state, action) => {
      const {
        postId,
        postDetail
      } = action.payload;
      const index = state.postList.findIndex(item => item.postId === postId);
      state.postList[index] = postDetail;
    },
    deleteByPostId: (state, action) => {
      const {
        postId
      } = action.payload;
      const prevPostList = [...state.postList];
      const updatedPostList = prevPostList.filter(item => item.postId !== postId);
      state.postList = updatedPostList;
    },
    clearFeed: state => {
      state.postList = [];
    }
  }
});

// const {actions: globalFeedActions, reducer: globalFeedReducer } = globalFeedSlice
var _default = exports.default = feedSlice;
//# sourceMappingURL=feedSlice.js.map