import { inject, Injectable } from '@angular/core';
import { Observable, BehaviorSubject } from 'rxjs';
import { IOrganization, } from 'app/entities/organization/organization.model';
import { IPeriod } from 'app/entities/period/period.model';
import { createRequestOption } from 'app/core/request/request-util';
import { ApplicationConfigService } from 'app/core/config/application-config.service';
import { HttpClient, HttpResponse } from '@angular/common/http';

export type EntityArrayResponseType = HttpResponse<IOrganization[]>;

@Injectable({
  providedIn: 'root'
})
export class ResilientEnvironmentService {
  private sourceWorkOrganization = new BehaviorSubject<IOrganization | null | undefined >(undefined);
  selectedWorkOrganization = this.sourceWorkOrganization.asObservable();
	
  private sourcePeriod = new BehaviorSubject<IPeriod | null | undefined >(undefined);
  selectedPeriod = this.sourcePeriod.asObservable();
  
  protected http = inject(HttpClient);
  protected applicationConfigService = inject(ApplicationConfigService);
  protected resourceUrl = this.applicationConfigService.getEndpointFor('api/env');

  organizations(req?: any): Observable<EntityArrayResponseType> {
    const options = createRequestOption(req);
    return this.http.get<IOrganization[]>(`${this.resourceUrl}/organizations`, { params: options, observe: 'response' });
  }
  
  selectWorkOrganization(selectedOrganization: IOrganization | null):void {
    this.sourceWorkOrganization.next(selectedOrganization??null);
  }
  
  selectPeriod(selectedPeriod: IPeriod | null):void {
    this.sourcePeriod.next(selectedPeriod??null);
  }
  
}
