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

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

  private closePanelSubject = new Subject<any>();
  closePanel$ = this.closePanelSubject.asObservable();

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

  public openPanel(component: any, data: any) {
    this.detailsPanelSubject.next({ component, data });
  }

  // Is called from the dynamic component to close the offcanvas
  public closePanel(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.closePanelSubject.next('close');
  }
}
