'use strict';

const auth_store = require('./auth.store-ff3fda09.js');
const util = require('./util-d383acb8.js');

const { state: state$1, onChange } = auth_store.createStore({
  goals: [],
  goalsLoading: false,
  goalLoading: false
});
const goalStore = { state: state$1, onChange };

class AppDataService {
  constructor() { }
  static getInstance() {
    if (!AppDataService.instance) {
      AppDataService.instance = new AppDataService();
    }
    return AppDataService.instance;
  }
  async getAllAppData(appId) {
    let result = await auth_store.HttpService.http.get(`api/v1/AppData/${appId}`);
    return result;
  }
  async createAppData(payload) {
    let result = await auth_store.HttpService.http.post(`api/v1/AppData/`, payload);
    return result;
  }
  async updateAppData(payload) {
    let result = await auth_store.HttpService.http.put(`api/v1/AppData/${payload.Id}`, payload);
    return result;
  }
  async getAllGoals() {
    goalStore.state.goalsLoading = true;
    let result = await this.getAllAppData(util.SparkleGlobal.MY_GOALS_APP_ID);
    if (result) {
      goalStore.state.goals = result.Data.map(item => {
        return Object.assign(Object.assign({}, item), { AppData: JSON.parse(item.AppJsonData) });
      });
      goalStore.state.goalsLoading = false;
    }
    return result;
  }
  async createGoal(payload) {
    goalStore.state.goalLoading = true;
    let result = await this.createAppData(payload);
    if (result) {
      result.Data.AppData = JSON.parse(result.Data.AppJsonData);
      goalStore.state.goals = [...goalStore.state.goals, result.Data];
      goalStore.state.goalLoading = false;
    }
    return result;
  }
  async updateGoal(payload) {
    goalStore.state.goalLoading = true;
    let result = await this.updateAppData(payload);
    if (result) {
      result.Data.AppData = JSON.parse(result.Data.AppJsonData);
      goalStore.state.goals = goalStore.state.goals.map(item => {
        if (item.Id == result.Data.Id) {
          return Object.assign(Object.assign({}, result.Data), { AppData: JSON.parse(result.Data.AppJsonData) });
        }
        return item;
      });
      goalStore.state.goalLoading = false;
    }
    return result;
  }
}

const { state, reset } = auth_store.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 auth_store.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 auth_store.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 auth_store.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;
  }
}

exports.AppDataService = AppDataService;
exports.FeedService = FeedService;
exports.feedStore = feedStore;
exports.goalStore = goalStore;
