import {Store} from "vuex";
import {FORM_STATUS} from "~/types/enums";
import {Route} from "vue-router";

export default class FormStorage {
  private store:Store<any>

  constructor(store:Store<any>) {
    this.store = store
  }

  /**
   * Actions devant être gérées si on souhaite quitter une page
   * @param to
   */
  handleActionsAfterLeavePage(to: Route){
    if (this.store.state.form.dirty) {
      this.store.commit('form/setShowConfirmToLeave', true)
      this.store.commit('form/setGoAfterLeave', to)
    } else {
      this.store.commit('form/setFormStatus', FORM_STATUS.EDIT)
      this.store.commit('form/setViolations', [])
    }
  }

  /**
   * Ajout des violations dans le store
   * @param invalidFields
   */
  addViolations(invalidFields: Array<any>){
    this.store.commit('form/setViolations', invalidFields)
  }
}
