import {MercureEntityUpdate, sseState} from "~/types/interfaces";
import {repositoryHelper} from "~/services/store/repository";
import ModelsUtils from "~/services/utils/modelsUtils";

export const state = () => ({
  connected: false,
})

export const mutations = {
  setConnected(state: sseState, connected: boolean) {
    state.connected = connected
  },
  addEvent(state: sseState, event: MercureEntityUpdate) {

    const model = ModelsUtils.getModelFromIri(event.iri)

    switch (event.operation) {
      case "update":
      case "create":
        repositoryHelper.persist(model, JSON.parse(event.data))
        break

      case "delete":
        break

      default:
        throw new Error('SSE: unknown operation type')
    }
  }
}
