import { Injectable } from '@angular/core' ; import { DeckCard } from '../../models/decktracker/deck-card' ; import { DeckState } from '../../models/decktracker/deck-state' ; @ Injectable ( ) export class ZoneOrderingService { public orderZones ( deckState , stateFromTracker ) { if ( ! stateFromTracker ) { return deckState ; } const newBoard = stateFromTracker . Board ? this . orderZone ( deckState . board , stateFromTracker . Board ) : deckState . board ; const newHand = stateFromTracker . Hand ? this . orderZone ( deckState . hand , stateFromTracker . Hand ) : deckState . hand ; return Object . assign ( new DeckState ( ) , deckState , { board : newBoard , hand : newHand , } as DeckState ) ; } private orderZone ( zone , stateFromTracker ) { if ( zone . some ( ( card ) => ! card . entityId ) ) { console . error ( 'Trying to order zone without entityId' , zone ) ; } if ( zone . length !== stateFromTracker . length ) { return zone ; } const sorted = Array ( stateFromTracker . length ) . fill ( null ) ; for ( let i = 0 ; i < stateFromTracker . length ; i ++ ) { sorted [ i ] = zone . find ( ( card ) => card . entityId === stateFromTracker [ i ] . entityId ) ; if ( ! sorted [ i ] ) { return zone ; } } return sorted ; } private indexOf ( collection , item ) { const index = collection . indexOf ( item ) ; if ( index === - 1 ) { return 99 ; } return index ; } }