import { AfterContentInit , AfterViewInit , ChangeDetectionStrategy , ChangeDetectorRef , Component , EventEmitter , ViewRef , } from '@angular/core' ; import { MmrPercentile } from '@firestone-hs/bgs-global-stats' ; import { IOption } from 'ng-select' ; import { combineLatest , Observable } from 'rxjs' ; import { distinctUntilChanged , filter , map , takeUntil , tap } from 'rxjs/operators' ; import { BgsRankFilterType } from '../../../../models/mainwindow/battlegrounds/bgs-rank-filter.type' ; import { LocalizationFacadeService } from '../../../../services/localization-facade.service' ; import { BgsRankFilterSelectedEvent } from '../../../../services/mainwindow/store/events/battlegrounds/bgs-rank-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 { cdLog } from '../../../../services/ui-store/app-ui-store.service' ; import { arraysEqual } from '../../../../services/utils' ; import { AbstractSubscriptionComponent } from '../../../abstract-subscription.component' ; @ Component ( { selector : 'battlegrounds-rank-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-rank-filter-dropdown"
			[options]="options$ | async"
			[filter]="value.filter"
			[placeholder]="value.placeholder"
			[visible]="value.visible"
			(onOptionSelected)="onSelected($event)"
		></filter-dropdown>
	` , changeDetection : ChangeDetectionStrategy . OnPush , } ) export class BattlegroundsRankFilterDropdownComponent extends AbstractSubscriptionComponent implements AfterContentInit , AfterViewInit { options$ : Observable < readonly RankFilterOption [ ] > ; filter$ : Observable < { filter : string ; placeholder : string ; visible : boolean ; } > ; private stateUpdater : EventEmitter < MainWindowStoreEvent > ; constructor ( private readonly ow , private readonly i18n , protected readonly store , protected readonly cdr ) { super ( store , cdr ) ; } ngAfterContentInit ( ) { this . options$ = this . store . listen$ ( ( [ main , nav , prefs ] ) => main . battlegrounds . globalStats ?. mmrPercentiles ) . pipe ( filter ( ( [ mmrPercentiles ] ) => ! ! mmrPercentiles ?. length ) , distinctUntilChanged ( ) , map ( ( [ mmrPercentiles ] ) => mmrPercentiles . filter ( ( percentile ) => percentile . percentile > 1 ) . map ( ( percentile ) => ( { value : '' + percentile . percentile , label : getBgsRankFilterLabelFor ( percentile , this . i18n ) , } as RankFilterOption ) ) ) , tap ( ( filter ) => setTimeout ( ( ) => this . cdr . detectChanges ( ) , 0 ) ) , tap ( ( filter ) => cdLog ( 'emitting rank filter in ' , this . constructor . name , filter ) ) , takeUntil ( this . destroyed$ ) ) ; this . filter$ = combineLatest ( this . options$ , this . store . listen$ ( ( [ main , nav , prefs ] ) => prefs . bgsActiveRankFilter , ( [ main , nav ] ) => nav . navigationBattlegrounds . selectedCategoryId , ( [ main , nav ] ) => nav . navigationBattlegrounds . currentView ) ) . pipe ( filter ( ( [ options , [ filter , categoryId , currentView ] ] ) => ! ! filter && ! ! categoryId && ! ! currentView ) , distinctUntilChanged ( ( a , b ) => arraysEqual ( a , b ) ) , map ( ( [ options , [ filter , categoryId , currentView ] ] ) => ( { filter : '' + filter , placeholder : options . find ( ( option ) => + option . value === filter ) ?. label ?? options [ 0 ] . label , visible : ! [ 'categories' , 'category' ] . includes ( currentView ) && ! [ 'bgs-category-personal-stats' , 'bgs-category-simulator' , 'bgs-category-personal-rating' ] . includes ( categoryId ) , } ) ) , 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 BgsRankFilterSelectedEvent ( + option . value as BgsRankFilterType ) ) ; } } interface RankFilterOption extends IOption { value : string ; } export const getBgsRankFilterLabelFor = ( percentile , i18n ) : string => { if ( ! percentile ) { return i18n . translateString ( 'app.battlegrounds.filters.rank.all' ) ; } switch ( percentile . percentile ) { case 100 : return i18n . translateString ( 'app.battlegrounds.filters.rank.all' ) ; case 50 : case 25 : case 10 : case 1 : return i18n . translateString ( 'app.battlegrounds.filters.rank.percentile' , { percentile : percentile . percentile , mmr : getNiceMmrValue ( percentile . mmr , 2 ) , } ) ; } } ; const getNiceMmrValue = ( mmr , significantDigit ) => { return Math . pow ( 10 , significantDigit ) * Math . round ( mmr / Math . pow ( 10 , significantDigit ) ) ; } ;