export default class WsModalController {
  static $inject = ['$q'];
  constructor($q) {
    this.$q = $q;
  }

  $onInit = () => {
    this.triggerClass = this.triggerClass ||
      `button ${this.secondary ? 'secondary-action' : 'primary-action'}`;
  }

  showModal = () => {
    if (this.disabled) { return; }
    this.shouldShowModal = true;
    this.actionInProgress = false;
    this.err = null;
  }

  doAction = () => {
    this.err = null;
    this.actionInProgress = true;
    this.$q.when(this.onAction())
      .then(() => { this.shouldShowModal = false; })
      .catch((err) => { this.error = err; })
      .finally(() => { this.actionInProgress = false; });
  }

  doDismiss = () => {
    this.err = null;
    this.actionInProgress = false;
    this.shouldShowModal = false;
    this.onCancel();
  }
}
