export default class WsModalTriggerService {
  registeredModals = {};

  open = (triggerId) => {
    this.registeredModals[triggerId] = {};
    this.openModal = triggerId;

    // Hack. Stops the body from scrolling behind
    // the modal while it's open.
    angular.element('body')
      .addClass('noscroll');
  }

  setIsInProgress = (triggerId, isInProgress) => {
    const modalInfo = this.registeredModals[triggerId] || {};
    modalInfo.isInProgress = isInProgress;
    this.registeredModals[triggerId] = modalInfo;
  }

  setError = (triggerId, error) => {
    const modalInfo = this.registeredModals[triggerId] || {};
    modalInfo.error = error;
    this.registeredModals[triggerId] = modalInfo;
  }

  isInProgress = triggerId =>
    this.registeredModals[triggerId] ?
      this.registeredModals[triggerId].isInProgress :
      false;

  getError = triggerId =>
    this.registeredModals[triggerId] ?
      this.registeredModals[triggerId].error :
      null;

  close = (triggerId) => {
    delete this.registeredModals[triggerId];
    this.openModal = null;

    angular.element('body')
      .removeClass('noscroll');
  }

  isOpen = triggerId => this.openModal === triggerId;
}
