import { Injectable, Type } from "@angular/core";
import { BehaviorSubject, Observable, Subject } from "rxjs";

@Injectable({
    providedIn: 'root'
})
export class ModalService {
    _modalContent: BehaviorSubject<Type<unknown> | undefined> = new BehaviorSubject<Type<unknown> | undefined>(undefined);
    _modalData: any;
    _onClose: Subject<void> = new Subject();

    open(componentClass: Type<unknown>, data: any = undefined): void {
        this._modalContent.next(componentClass);
        this._modalData = data;
    }

    close(): void {
        this._modalContent.next(undefined);
        this._onClose.next();
    }

    get modalContent(): Observable<Type<unknown> | undefined> {
        return this._modalContent.asObservable();
    }

    get data(): any {
        return this._modalData;
    }

    get onClose(): Observable<void> {
        return this._onClose.asObservable();
    }
}