import { computed, Injectable, Signal, signal } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { AnnotationWrapper } from '@models/file/annotation.wrapper';
import { FileDataService } from './file-data.service';

@Injectable()
export class AnnotationReferencesService {
    readonly references: Signal<AnnotationWrapper[]>;
    readonly annotation: Signal<AnnotationWrapper | undefined>;
    private readonly _annotation$ = new BehaviorSubject<AnnotationWrapper | undefined>(undefined);
    readonly #annotation = signal<AnnotationWrapper | undefined>(undefined);

    constructor(private readonly _fileDataService: FileDataService) {
        this.annotation = this.#annotation.asReadonly();
        this.references = computed(() => {
            const annotation = this.annotation();
            return annotation ? this._fileDataService.annotations().filter(a => annotation.reference.includes(a.id)) : [];
        });
    }

    show(annotation: AnnotationWrapper) {
        this.#annotation.set(annotation);
    }

    hide() {
        this.#annotation.set(undefined);
    }
}
