import { CountryService as ApiCountryService } from '@generated/swagger/crm-api';
import { Cache, CacheTimeToLive } from '@isa/common/decorators';
import { inject, Injectable } from '@angular/core';
import { Country } from '../models';
import {
  catchResponseArgsErrorPipe,
  ResponseArgs,
  takeUntilAborted,
} from '@isa/common/data-access';
import { firstValueFrom } from 'rxjs';

@Injectable({ providedIn: 'root' })
export class CountryService {
  #apiCountryService = inject(ApiCountryService);

  @Cache()
  async getCountries(abortSignal?: AbortSignal): Promise<Country[]> {
    let req$ = this.#apiCountryService.CountryGetCountries({});

    if (abortSignal) {
      req$ = req$.pipe(takeUntilAborted(abortSignal));
    }

    req$ = req$.pipe(catchResponseArgsErrorPipe());

    const res = await firstValueFrom(req$);

    return res.result as Country[];
  }
}
