import { Injectable } from '@angular/core';
import { BaseButtonService } from './base-button.service';

@Injectable({
  providedIn: 'root'
})
export class ButtonVisibilityService {
  _buttons: { [key: string]: BaseButtonService } = {};
  public get buttons() {
    return this._buttons;
  }

  setVisibility(key: string, show: boolean) {
    const bttn = this._buttons[key];

    if (show && !bttn.isVisible) {
      bttn.isVisible = true
      return
    }

    if (!show && bttn.isVisible) {
      bttn.isVisible = false
    }
  }

  setVisibleOnly(...buttons: BaseButtonService[]) {
    const keys = buttons.map(b => b.id)
    for (const key in this._buttons) {
      if (keys.includes(key)) {
        this.setVisibility(key, true)
      }
      else {
        this.setVisibility(key, false)
      }
    }
  }

  get anyVisible(): boolean {
    for (const key in this._buttons)
      if (this._buttons[key].isVisible)
        return true;
    return false;
  }
}