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 { IUnitConverter, NewUnitConverter } from '../unit-converter.model';

export type PartialUpdateUnitConverter = Partial<IUnitConverter> & Pick<IUnitConverter, 'id'>;

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

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

  protected resourceUrl = this.applicationConfigService.getEndpointFor('api/unit-converters');
  getResourceUrl(): string { return this.resourceUrl; }

  // Get a list of UnitConverters, that applies to fromUnit and toUnit
  queryByFromAndTo(fromUnitId: number, toUnitId: number): Observable<EntityArrayResponseType> {
    return this.http.get<IUnitConverter[]>(`${this.resourceUrl}/from/${fromUnitId}/to/${toUnitId}`, { observe: 'response' });
  }

  // Converts a value from a Unit, to the variable base unit
  convertValue(periodId: number, variableId: number, variableClassCode: string | null, fromUnitId: number, value: number): Observable<HttpResponse<number>> {
    return this.http.get<number>(`${this.resourceUrl}/convert/${periodId}/${variableId}/${variableClassCode}/${fromUnitId}/${value}`, { observe: 'response' });
  }
}
