import { H as HttpService } from './httpService-7986aeed.js';
import { d as createStore } from './auth.store-dba2c2da.js';

const { state, reset } = createStore({
  posts: [],
  postsLoading: false,
  createPostLoading: false,
  postIdLoading: null
});
const feedStore = { state, reset };

class FeedService {
  constructor() { }
  static getInstance() {
    if (!FeedService.instance) {
      FeedService.instance = new FeedService();
    }
    return FeedService.instance;
  }
  async getAllPosts() {
    feedStore.state.postsLoading = true;
    let result = await HttpService.http.get(`api/v1/Feed/GetAll`);
    if (result) {
      feedStore.state.posts = result.Data;
      feedStore.state.postsLoading = false;
    }
    return result;
  }
  async createPost(payload) {
    feedStore.state.createPostLoading = true;
    let result = await HttpService.http.post(`api/v1/Feed/PostFeed`, payload);
    if (result) {
      feedStore.state.posts = [result.Data, ...feedStore.state.posts];
      feedStore.state.createPostLoading = false;
    }
    return result;
  }
  async createPostComment(payload) {
    feedStore.state.postIdLoading = payload.PostId;
    let result = await HttpService.http.post(`api/v1/Feed/Comment`, payload);
    if (result) {
      feedStore.state.posts = feedStore.state.posts.map((item) => {
        if (item.Id == result.Data.ClassFeedPostId) {
          return Object.assign(Object.assign({}, item), { Comments: [...item.Comments, result.Data] });
        }
        return item;
      });
      feedStore.state.postIdLoading = null;
    }
    return result;
  }
}

export { FeedService as F, feedStore as f };
