import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';

@Injectable({
  providedIn: 'root',
})
export class ModalService {
  private _modalSubject = new Subject<any>();
  modal$ = this._modalSubject.asObservable();

  private _closeModalSubject = new Subject<any>();
  closeModal$ = this._closeModalSubject.asObservable();

  private _resultSubject = new Subject<any>();
  result$ = this._resultSubject.asObservable();

  public openModal(component: any, data: any) {
    console.log('ModalService: openModal');
    this._modalSubject.next({ component, data });
  }

  // Is called from the dynamic content component to close the modal
  public handleModalClosing(result: any, data?: any) {
    // Is listened to in the host component where the panel was opened, to initiate further steps
    this._resultSubject.next({ state: result, data: data });
    this._closeModalSubject.next('close');
  }
}
