import { HttpClient , HttpErrorResponse } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { BehaviorSubject , Observable , of } from 'rxjs' ; import { catchError } from 'rxjs/operators' ; import { xmlToJson } from '../xml-to-json' ; /**
 * Parses pager.xml into the observable seqeunce pagerXml$
 */ @ Injectable ( ) export class PagerXmlService { error : any = null ; pagerXml$ : BehaviorSubject < any > = new BehaviorSubject < any > ( null ) ; constructor ( public httpClient ) { } param xml return { Object } _parseAlerts ( pager ) { let alert , alerts , data ; if ( ! pager || ! pager . alert ) { return null ; } data = { } ; alerts = pager . alert ; for ( let i = 0 , len = alerts . length ; i < len ; i ++ ) { alert = alerts [ i ] ; data [ alert . type ] = alert ; } return data ; } param xml { Document } return { Array } _parseCities ( pager ) { let cities , city , data ; if ( ! pager || ! pager . city ) { return null ; } data = [ ] ; cities = pager . city ; for ( let i = 0 , len = cities . length ; i < len ; i ++ ) { city = cities [ i ] ; data . push ( { isCapital : city . iscapital === '1' , latitude : parseFloat ( city . lat ) , longitude : parseFloat ( city . lon ) , mmi : parseFloat ( city . mmi ) , name : city . name , population : parseInt ( city . population , 10 ) } ) ; } return data ; } param xml { Document } return { Object } _parseComments ( pager ) { let effects , data , structure ; if ( ! pager || ( ! pager . structcomment && ! pager . secondary_effects && ! pager . impact_comment ) ) { return null ; } data = { } ; structure = pager . structcomment ; if ( structure && structure !== '' && typeof structure !== 'object' ) { data . structure = structure . trim ( ) ; } effects = pager . secondary_effects ; if ( effects && effects !== '' && typeof effects !== 'object' ) { data . effects = effects . trim ( ) ; } data . impact = this . _parseImpactComments ( pager . impact_comment ) ; return data ; } param xml { Document } return { Array } _parseExposures ( pager ) { let data , exposure , exposures , rangeInsideMap ; if ( ! pager || ! pager . exposure ) { return null ; } const roman = [ 'I' , 'II' , 'III' , 'IV' , 'V' , 'VI' , 'VII' , 'VIII' , 'IX' , 'X' ] ; const shaking = [ 'Not Felt' , 'Weak' , 'Weak' , 'Light' , 'Moderate' , 'Strong' , 'Very Strong' , 'Severe' , 'Violent' , 'Extreme' ] ; data = [ ] ; exposures = pager . exposure ; for ( let i = 0 , len = exposures . length ; i < len ; i ++ ) { data . push ( { dmax : parseFloat ( exposures [ i ] . dmax ) , dmin : parseFloat ( exposures [ i ] . dmin ) , exposure : parseInt ( exposures [ i ] . exposure , 10 ) , rangeInsideMap : exposures [ i ] . rangeInsideMap === '1' , roman : roman [ i ] , shaking : shaking [ i ] } ) ; } data . sort ( function ( a , b ) { return a . dmin - b . dmin ; } ) ; if ( data [ 1 ] && data [ 2 ] ) { exposure = data [ 1 ] . exposure + data [ 2 ] . exposure ; rangeInsideMap = data [ 1 ] . rangeInsideMap && data [ 2 ] . rangeInsideMap ; data . splice ( 1 , 2 , { dmax : 3.5 , dmin : 1.5 , exposure : exposure , rangeInsideMap : rangeInsideMap , roman : 'II-III' , shaking : 'Weak' } ) ; } return data ; } param comment { String } return { Object } _parseImpactComments ( comment ) { let impact ; if ( ! comment || comment === '' || typeof comment === 'object' ) { return null ; } impact = { } ; comment = comment . trim ( ) . split ( '#' ) . reverse ( ) ; if ( comment [ 0 ] . indexOf ( 'economic' ) !== - 1 ) { comment . reverse ( ) ; } if ( comment . length === 2 ) { if ( comment [ 0 ] !== '' ) { impact . fatality = comment [ 0 ] ; impact . economic = comment [ 1 ] ; } else { impact . fatality = comment [ 1 ] ; } } else { impact . fatality = comment [ 0 ] ; } return impact ; } param product getPagerXml ( product ) { try { const contents = product . contents [ 'pager.xml' ] ; const options = { responseType : 'text' as 'text' } ; this . httpClient . get ( contents . url , options ) . pipe ( catchError ( this . handleError ( ) ) ) . subscribe ( response => { try { this . pagerXml$ . next ( this . parseResponse ( response ) ) ; } catch ( e ) { this . error = e ; this . pagerXml$ . next ( { } ) ; } } ) ; } catch ( e ) { this . error = e ; this . pagerXml$ . next ( { } ) ; } } param response parseResponse ( response ) { let pager , xml ; if ( response === null ) { return { } ; } xml = xmlToJson ( response ) ; pager = xml . pager ; if ( ! pager ) { return null ; } return { alerts : this . _parseAlerts ( pager ) , cities : this . _parseCities ( pager ) , comments : this . _parseComments ( pager ) , exposures : this . _parseExposures ( pager ) } ; } /**
     * Error handler for http requests.
     */ private handleError ( ) { return ( error ) : Observable < any > => { this . error = error ; return of ( { } ) ; } ; } }