import { AfterContentInit , AfterViewInit , ChangeDetectionStrategy , ChangeDetectorRef , Component , HostListener , ViewEncapsulation , ViewRef , } from '@angular/core' ; import { Observable } from 'rxjs' ; import { debounceTime , distinctUntilChanged , map , takeUntil , tap } from 'rxjs/operators' ; import { GameState } from '../../models/decktracker/game-state' ; import { OverwolfService } from '../../services/overwolf.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 { AbstractSubscriptionComponent } from '../abstract-subscription.component' ; @ Component ( { selector : 'constructed' , styleUrls : [ `../../../css/global/reset-styles.scss` , `../../../css/component/constructed/constructed.component.scss` , ] , encapsulation : ViewEncapsulation . None , template : `
		<window-wrapper [activeTheme]="'decktracker'" [allowResize]="true">
			<ads [parentComponent]="'constructed'"></ads>
			<constructed-content [state]="state$ | async"> </constructed-content>
		</window-wrapper>
	` , changeDetection : ChangeDetectionStrategy . OnPush , } ) export class ConstructedComponent extends AbstractSubscriptionComponent implements AfterContentInit , AfterViewInit { state$ : Observable < GameState > ; windowId : string ; constructor ( private readonly ow , private readonly prefs , protected readonly store , protected readonly cdr ) { super ( store , cdr ) ; } async ngAfterContentInit ( ) { this . state$ = this . store . listenDeckState$ ( ( state ) => state ) . pipe ( debounceTime ( 100 ) , map ( ( [ info ] ) => info ) , distinctUntilChanged ( ) , tap ( ( filter ) => setTimeout ( ( ) => { if ( ! ( this . cdr as ViewRef ) ?. destroyed ) { this . cdr . detectChanges ( ) ; } } , 0 ) ) , tap ( ( filter ) => cdLog ( 'emitting pref in ' , this . constructor . name , filter ) ) , takeUntil ( this . destroyed$ ) ) ; } async ngAfterViewInit ( ) { this . windowId = ( await this . ow . getCurrentWindow ( ) ) . id ; await this . positionWindowOnSecondScreen ( ) ; this . ow . bringToFront ( this . windowId ) ; } @ HostListener ( 'mousedown' ) dragMove ( ) { this . ow . dragMove ( this . windowId ) ; } private async positionWindowOnSecondScreen ( ) { const [ monitorsList , gameInfo , prefs ] = await Promise . all ( [ this . ow . getMonitorsList ( ) , this . ow . getRunningGameInfo ( ) , this . prefs . getPreferences ( ) , ] ) ; if ( monitorsList . displays . length === 1 || prefs . bgsUseOverlay ) { return ; } const mainMonitor = gameInfo ?. monitorHandle ?. value ?? - 1 ; if ( mainMonitor !== - 1 ) { const secondMonitor = monitorsList . displays . filter ( ( monitor ) => monitor . handle . value !== mainMonitor ) [ 0 ] ; this . ow . changeWindowPosition ( this . windowId , secondMonitor . x + 100 , secondMonitor . y + 100 ) ; } } }