import { AfterContentInit , AfterViewInit , ChangeDetectionStrategy , ChangeDetectorRef , Component , EventEmitter , ViewRef , } from '@angular/core' ; import { CardsFacadeService } from '@services/cards-facade.service' ; import { IOption } from 'ng-select' ; import { Observable } from 'rxjs' ; import { distinctUntilChanged , filter , map , takeUntil , tap } from 'rxjs/operators' ; import { LocalizationFacadeService } from '../../../../services/localization-facade.service' ; import { BgsHeroFilterSelectedEvent } from '../../../../services/mainwindow/store/events/battlegrounds/bgs-hero-filter-selected-event' ; import { MainWindowStoreEvent } from '../../../../services/mainwindow/store/events/main-window-store-event' ; import { OverwolfService } from '../../../../services/overwolf.service' ; import { AppUiStoreFacadeService } from '../../../../services/ui-store/app-ui-store-facade.service' ; import { arraysEqual } from '../../../../services/utils' ; import { AbstractSubscriptionComponent } from '../../../abstract-subscription.component' ; @ Component ( { selector : 'battlegrounds-hero-filter-dropdown' , styleUrls : [ `../../../../../css/global/filters.scss` , `../../../../../css/component/app-section.component.scss` , `../../../../../css/component/filter-dropdown.component.scss` , ] , template : `
		<filter-dropdown
			*ngIf="filter$ | async as value"
			class="battlegrounds-hero-filter-dropdown"
			[options]="options"
			[filter]="value.filter"
			[placeholder]="value.placeholder"
			[visible]="value.visible"
			(onOptionSelected)="onSelected($event)"
		></filter-dropdown>
	` , changeDetection : ChangeDetectionStrategy . OnPush , } ) export class BattlegroundsHeroFilterDropdownComponent extends AbstractSubscriptionComponent implements AfterContentInit , AfterViewInit { options : readonly HeroFilterOption [ ] ; filter$ : Observable < { filter : string ; placeholder : string ; visible : boolean ; } > ; private stateUpdater : EventEmitter < MainWindowStoreEvent > ; private collator = new Intl . Collator ( 'en-US' ) ; constructor ( private readonly ow , private readonly allCards , private readonly i18n , protected readonly store , protected readonly cdr ) { super ( store , cdr ) ; this . options = [ { value : 'all' , label : this . i18n . translateString ( 'app.battlegrounds.filters.hero.all' ) , } as HeroFilterOption , ... this . allCards . getCards ( ) . filter ( ( card ) => card . battlegroundsHero ) . map ( ( card ) => ( { label : card . name , value : card . id , } as HeroFilterOption ) ) . sort ( ( a , b ) => this . collator . compare ( a . label , b . label ) ) , ] as readonly HeroFilterOption [ ] ; } ngAfterContentInit ( ) { this . filter$ = this . store . listen$ ( ( [ main , nav , prefs ] ) => prefs . bgsActiveHeroFilter , ( [ main , nav ] ) => nav . navigationBattlegrounds . selectedCategoryId ) . pipe ( filter ( ( [ filter , selectedCategoryId ] ) => ! ! filter && ! ! selectedCategoryId ) , distinctUntilChanged ( ( a , b ) => arraysEqual ( a , b ) ) , map ( ( [ filter , selectedCategoryId ] ) => ( { filter : filter , placeholder : this . options . find ( ( option ) => option . value === filter ) ?. label , visible : selectedCategoryId === 'bgs-category-perfect-games' , } ) ) , tap ( ( filter ) => setTimeout ( ( ) => { if ( ! ( this . cdr as ViewRef ) ?. destroyed ) { this . cdr . detectChanges ( ) ; } } , 0 ) ) , takeUntil ( this . destroyed$ ) ) ; } ngAfterViewInit ( ) { this . stateUpdater = this . ow . getMainWindow ( ) . mainWindowStoreUpdater ; } onSelected ( option ) { this . stateUpdater . next ( new BgsHeroFilterSelectedEvent ( option . value ) ) ; } } interface HeroFilterOption extends IOption { value : string ; }