import { Injectable, inject } from '@angular/core'
import { SourcesApiService } from '../../../../../../libs/data-access/gn4/src'
import { Observable } from 'rxjs'
import { filter, map, shareReplay } from 'rxjs/operators'
import { CatalogSource } from './sources.model'
import { TranslateService } from '@ngx-translate/core'
import { toLang3 } from '../../../../../../libs/util/i18n/src'

@Injectable({
  providedIn: 'root',
})
export class SourcesService {
  private sourcesApiService = inject(SourcesApiService)
  private translateService = inject(TranslateService)

  sources$: Observable<CatalogSource[]> = (
    this.sourcesApiService.getSubPortals1() as Observable<CatalogSource[]>
  ).pipe(shareReplay())

  getSourceLabel(uuid: string): Observable<string> {
    return this.sources$.pipe(
      map((sources) => sources.filter((source) => source.uuid === uuid)[0]),
      filter((source) => !!source),
      map((source) => source.label[toLang3(this.translateService.currentLang)])
    )
  }
}
