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

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

class AppDataService {
  constructor() { }
  static getInstance() {
    if (!AppDataService.instance) {
      AppDataService.instance = new AppDataService();
    }
    return AppDataService.instance;
  }
  async getAllAppData(appId) {
    let result = await HttpService.http.get(`api/v1/AppData/${appId}`);
    return result;
  }
  async createAppData(payload) {
    let result = await HttpService.http.post(`api/v1/AppData/`, payload);
    return result;
  }
  async updateAppData(payload) {
    let result = await HttpService.http.put(`api/v1/AppData/${payload.Id}`, payload);
    return result;
  }
  async getAllGoals() {
    goalStore.state.goalsLoading = true;
    let result = await this.getAllAppData(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;
  }
}

export { AppDataService as A, goalStore as g };
