import { getUnique } from './unique' ; export class Event { deleted = false ; geometry : any ; id : string ; product : any = null ; properties : any ; sources : Array < String > ; constructor ( public data ) { if ( ! this . data ) { this . geometry = null ; this . id = null ; this . properties = { } ; this . sources = [ ] ; return ; } this . geometry = this . data . geometry || null ; this . id = this . data . id || null ; this . properties = this . data . properties || { } ; this . deleted = this . properties . status === 'deleted' ; this . sources = this . getSources ( ) ; try { this . properties . products . origin . forEach ( o => { o . phasedata = this . getProduct ( 'phase-data' , o . source , o . code , o . updateTime ) ; } ) ; } catch ( e ) { } this . setPreferred ( ) ; } param type param source param code getProduct ( type , source ? , code ? , updateTime ? ) { return this . getProducts ( type ) . find ( product => { if ( ( source && product . source !== source ) || ( code && product . code !== code ) || ( ( updateTime || updateTime === 0 ) && product . updateTime !== updateTime ) ) { return false ; } return true ; } ) ; } param type getProducts ( type ) { let products ; if ( ! type ) { return [ ] ; } try { products = this . properties . products [ type ] || [ ] ; } catch ( e ) { products = [ ] ; } return products ; } getSources ( ) { let products ; let sources ; try { products = this . properties . products || { } ; } catch ( e ) { products = { } ; } sources = [ ] ; Object . keys ( products ) . forEach ( type => { products [ type ] . forEach ( product => { sources . push ( product . source ) ; } ) ; } ) ; sources = getUnique ( sources ) ; sources . sort ( ) ; return sources ; } hasProducts ( types ) { try { if ( ! Array . isArray ( types ) ) { types = [ types ] ; } return types . some ( type => { return this . getProducts ( type ) . length > 0 ; } ) ; } catch ( e ) { return false ; } } /**
     * Set a preferred status on each product
     *
     */ setPreferred ( ) { const products = this . properties . products ; if ( ! products ) { return ; } Object . keys ( products ) . forEach ( type => { products [ type ] . forEach ( ( product , index ) => { let preferred = false ; if ( index === 0 ) { preferred = true ; } else if ( ( type === 'finite-fault' || type === 'focal-mechanism' ) && product . source === products [ type ] [ 0 ] . source ) { preferred = true ; } product . preferred = preferred ; } ) ; } ) ; } }