'use strict';

const httpService = require('./httpService-d2e7bf8f.js');
const auth_store = require('./auth.store-1e630a7d.js');

const { state, onChange } = auth_store.createStore({
  pageNotes: [],
  allPageNotes: [],
  onlineStudents: [],
  dashboardData: null,
  isConnected: false,
  loading: false,
  dashboardDataLoading: false,
  notesLoading: false,
  classes: [],
  classesLoading: false,
});
const facilitatorStore = { state, onChange };

class FacilitatorService {
  constructor() { }
  static getInstance() {
    if (!FacilitatorService.instance) {
      FacilitatorService.instance = new FacilitatorService();
    }
    return FacilitatorService.instance;
  }
  async fetchClasses() {
    facilitatorStore.state.classesLoading = true;
    let result = await httpService.HttpService.http.get(`api/v1/Teacher/Classes`);
    if (result) {
      facilitatorStore.state.classes = result.Data;
      facilitatorStore.state.classesLoading = false;
    }
    return result;
  }
  async fetchDashboardData(payload) {
    facilitatorStore.state.dashboardDataLoading = true;
    let result = await httpService.HttpService.http.get(`api/v1/Teacher/Dashboard/${payload.ClassId}`);
    if (result) {
      facilitatorStore.state.dashboardData = result.Data;
      facilitatorStore.state.dashboardDataLoading = false;
    }
    return result;
  }
  async fetchNotes(payload) {
    facilitatorStore.state.notesLoading = true;
    let result = await httpService.HttpService.http.get(`api/v1/Teacher/Notes/${payload.showAllNotes}`);
    if (result) {
      if (payload.showAllNotes) {
        facilitatorStore.state.allPageNotes = result.Data;
      }
      else {
        facilitatorStore.state.pageNotes = result.Data;
      }
      facilitatorStore.state.notesLoading = false;
    }
    return result;
  }
  async addFacilitatorNote(payload) {
    facilitatorStore.state.notesLoading = true;
    let result = await httpService.HttpService.http.post(`api/v1/Teacher/Notes`, payload);
    if (result) {
      facilitatorStore.state.pageNotes = [...facilitatorStore.state.pageNotes, result.Data];
    }
    facilitatorStore.state.notesLoading = false;
    return result;
  }
  async updateFacilitatorNote(payload) {
    facilitatorStore.state.notesLoading = true;
    let result = await httpService.HttpService.http.put(`api/v1/Teacher/Notes/${payload.Id}`, payload);
    if (result) {
      facilitatorStore.state.pageNotes = facilitatorStore.state.pageNotes.map((item) => {
        if (item.Id == result.Data.Id) {
          return result.Data;
        }
        return item;
      });
    }
    facilitatorStore.state.notesLoading = false;
    return result;
  }
  async deleteFacilitatorNote(id) {
    facilitatorStore.state.notesLoading = true;
    let result = await httpService.HttpService.http.delete(`api/v1/Teacher/Notes/${id}`);
    if (result) {
      facilitatorStore.state.pageNotes = facilitatorStore.state.pageNotes.filter(x => x.Id != id);
    }
    facilitatorStore.state.notesLoading = false;
    return result;
  }
}

exports.FacilitatorService = FacilitatorService;
exports.facilitatorStore = facilitatorStore;
