import { Injectable } from '@angular/core'
import { Observable } from 'rxjs'
import { map } from 'rxjs/operators'

import { ApiService, SurveyMeta, Project, Surveyor, Equipment } from '../openapi'

@Injectable()
export class AdminDataService {
    surveyMeta: SurveyMeta
    projectMap: Map<number, Project>
    surveyorMap: Map<number, Surveyor>
    equipmentMap: Map<number, Equipment>

    constructor(
        public apiService: ApiService,
    ) {}

    getSurveyMeta(): Observable<SurveyMeta> {
        return this.apiService.getSurveyMetaApiSurveyMetaGet().pipe(map(
            surveyMeta => {
                this.surveyMeta = surveyMeta
                this.projectMap = new Map(surveyMeta.projects.map(i => [i.id, i]))
                this.surveyorMap = new Map(surveyMeta.surveyors.map(i => [i.id, i]))
                this.equipmentMap = new Map(surveyMeta.equipments.map(i => [i.id, i]))
                return surveyMeta
            }
        ))
    }
}