import { inject, Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';

import { isPresent } from 'app/core/util/operators';
import { ApplicationConfigService } from 'app/core/config/application-config.service';
import { ResilientService } from 'app/resilient/resilient-base/resilient-base.service';
import { createRequestOption } from 'app/core/request/request-util';
import { IEmissionFactor, NewEmissionFactor } from '../emission-factor.model';

export type EntityResponseType = HttpResponse<IEmissionFactor>;
export type EntityArrayResponseType = HttpResponse<IEmissionFactor[]>;

@Injectable({ providedIn: 'root' })
export class EmissionFactorService extends ResilientService<IEmissionFactor, NewEmissionFactor> {
  protected http = inject(HttpClient);
  protected applicationConfigService = inject(ApplicationConfigService);

  private readonly resourceUrl = this.applicationConfigService.getEndpointFor('api/emission-factors');
  override getResourceUrl(): string { return this.resourceUrl; }

  export(req?: any): Observable<Blob> {
    const options = createRequestOption(req);

    return this.http.get(`${this.resourceUrl}/export`, { params: options, responseType: 'blob' });
  }

  queryBySearch(year: number, scopeId: string, categoryId: string): Observable<HttpResponse<IEmissionFactor[]>> {
    return this.http.get<IEmissionFactor[]>(`${this.resourceUrl}/search/${year}/${scopeId}/${categoryId}`, { observe: 'response' });
  }
}
