import { Injectable } from '@angular/core';
import { WindowScrollingService } from './services/window-scrolling.service';

@Injectable({ providedIn: 'root' })
export class ModalService {
  private modals: any[] = [];

  constructor(private scollingService: WindowScrollingService) {}

  add(modal: any) {
    // add modal to array of active modals
    this.modals.push(modal);
  }

  remove(id: string) {
    // remove modal from array of active modals
    this.modals = this.modals.filter((x) => x.id !== id);
  }

  open(id: string) {
    // open modal specified by id
    const modal: any = this.modals.filter((x) => x.id === id)[0];
    if (modal) {
      modal.open();
      this.scollingService.disable();
    }
  }

  close(id: string) {
    // close modal specified by id
    const modal: any = this.modals.filter((x) => x.id === id)[0];
    if (!!modal) {
      modal.close();
      this.scollingService.enable();
    }
  }
}
