export class EmitController {
  constructor(param) {
    this.param = param;
    this._blocked_emit = false;
    this._blocked_parent_emit = false;
    this._count_by_event_name = {};
  }
  emitAllowed() {
    if (this._blocked_emit === true) {
      return false;
    }
    if (this.param.scene().loadingController.isLoading()) {
      return false;
    }
    return this.param.scene().dispatchController.emitAllowed();
  }
  blockEmit() {
    this._blocked_emit = true;
    if (this.param.is_multiple && this.param.components) {
      for (let component of this.param.components) {
        component.emitController.blockEmit();
      }
    }
    return true;
  }
  unblockEmit() {
    this._blocked_emit = false;
    if (this.param.is_multiple && this.param.components) {
      for (let component of this.param.components) {
        component.emitController.unblockEmit();
      }
    }
    return true;
  }
  blockParentEmit() {
    this._blocked_parent_emit = true;
    return true;
  }
  unblockParentEmit() {
    this._blocked_parent_emit = false;
    return true;
  }
  incrementCount(event_name) {
    this._count_by_event_name[event_name] = this._count_by_event_name[event_name] || 0;
    this._count_by_event_name[event_name] += 1;
  }
  eventsCount(event_name) {
    return this._count_by_event_name[event_name] || 0;
  }
  emit(event) {
    if (this.emitAllowed()) {
      this.param.emit(event);
      if (this.param.parent_param != null && this._blocked_parent_emit !== true) {
        this.param.parent_param.emit(event);
      }
    }
  }
}
