import { AfterContentInit , ChangeDetectionStrategy , ChangeDetectorRef , Component } from '@angular/core' ; import { Observable } from 'rxjs' ; import { debounceTime , filter , map , takeUntil , tap } from 'rxjs/operators' ; import { MercenariesBattleState , MercenariesBattleTeam } from '../../../../models/mercenaries/mercenaries-battle-state' ; import { Preferences } from '../../../../models/preferences' ; import { CardsFacadeService } from '../../../../services/cards-facade.service' ; import { PreferencesService } from '../../../../services/preferences.service' ; import { AppUiStoreFacadeService } from '../../../../services/ui-store/app-ui-store-facade.service' ; import { cdLog } from '../../../../services/ui-store/app-ui-store.service' ; import { buildMercenariesTasksList } from '../../../../services/ui-store/mercenaries-ui-helper' ; import { AbstractSubscriptionComponent } from '../../../abstract-subscription.component' ; import { Task } from './mercenaries-team-root..component' ; @ Component ( { selector : 'mercenaries-opponent-team' , styleUrls : [ ] , template : `
		<mercenaries-team-root
			[team]="team$ | async"
			[tasks]="tasks$ | async"
			[side]="'opponent'"
			[showTasksExtractor]="showTasksExtractor"
			[scaleExtractor]="scaleExtractor"
		></mercenaries-team-root>
	` , changeDetection : ChangeDetectionStrategy . OnPush , } ) export class MercenariesOpponentTeamComponent extends AbstractSubscriptionComponent implements AfterContentInit { teamExtractor = ( state ) => state . opponentTeam ; showTasksExtractor = ( prefs ) => prefs . mercenariesShowTaskButton ; scaleExtractor = ( prefs ) => prefs . mercenariesOpponentTeamOverlayScale ; team$ : Observable < MercenariesBattleTeam > ; tasks$ : Observable < readonly Task [ ] > ; constructor ( private readonly prefs , private readonly allCards , protected readonly store , protected readonly cdr ) { super ( store , cdr ) ; } ngAfterContentInit ( ) { this . tasks$ = this . store . listen$ ( ( [ main , nav , prefs ] ) => main . mercenaries . referenceData , ( [ main , nav , prefs ] ) => main . mercenaries . collectionInfo ?. Visitors ) . pipe ( filter ( ( [ referenceData , visitors ] ) => ! ! referenceData && ! ! visitors ?. length ) , map ( ( [ referenceData , visitors ] ) => buildMercenariesTasksList ( referenceData , visitors , this . allCards ) ) , takeUntil ( this . destroyed$ ) ) ; this . team$ = this . store . listenMercenaries$ ( ( [ battleState , prefs ] ) => battleState ) . pipe ( debounceTime ( 50 ) , filter ( ( [ battleState ] ) => ! ! battleState ) , map ( ( [ battleState ] ) => battleState . opponentTeam ) , map ( ( team ) => team . update ( { ... team , mercenaries : team . mercenaries ?. filter ( ( merc ) => ! merc . isDead || ! merc . creatorCardId ) ?? [ ] , } ) ) , filter ( ( team ) => ! ! team ) , tap ( ( filter ) => setTimeout ( ( ) => this . cdr . detectChanges ( ) , 0 ) ) , tap ( ( filter ) => cdLog ( 'emitting team in ' , this . constructor . name , filter ) ) , takeUntil ( this . destroyed$ ) ) ; } }