import { Selector } from '@ngxs/store';
import { CountryState, CountryStateModel } from '../state/countries.state';
import { mapToIterable } from '../../utils/app.utils';
import { Country } from '../../models/country.model';

export class CountrySelector {
  @Selector([CountryState])
  static getCountries(countryState: CountryStateModel) {
    return countryState.countries;
  }

  @Selector([CountryState])
  static getCountriesIterable(countryState: CountryStateModel): Country[] {
    const countries = mapToIterable(countryState.countries);
    const sortedCountires = countries.sort((a: Country, b: Country) => {
      return a.index - b.index;
    });

    return sortedCountires as Country[];
  }

  @Selector([CountryState])
  static getDeutchlandIterable(countryState: CountryStateModel): Country[] {
    const countries: Country[] = mapToIterable(countryState.countries);
    const deutchland = countries.filter((country) => country.key === 'DEU');

    return deutchland as Country[];
  }
}
