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

@Injectable({ providedIn: 'root' })
export class ModalService {
  private modals: Map<string, BehaviorSubject<boolean>> = new Map();
  private imageFilesSubject = new BehaviorSubject<File[] | null>(null);

  getModalState(id: string): BehaviorSubject<boolean> {
    if (!this.modals.has(id)) {
      this.modals.set(id, new BehaviorSubject<boolean>(false));
    }
    return this.modals.get(id)!;
  }

  openModal(id: string, images?: File[]) {
    if (images) {
      this.imageFilesSubject.next(images);
    }

    this.getModalState(id).next(true);
  }

  closeModal(id: string) {
    this.getModalState(id).next(false);
    this.imageFilesSubject.next(null);
  }

  getImageFiles(): BehaviorSubject<File[] | null> {
    return this.imageFilesSubject;
  }
}
