import { BoxrecRole } from "boxrec-requests/dist/boxrec-requests.constants" ; import { BoxrecCommonTablesColumnsClass } from "../../boxrec-common-tables/boxrec-common-tables-columns.class" ; import { OutputGetter , OutputInterface } from "../../decorators/output.decorator" ; import { trimRemoveLineBreaks } from "../../helpers" ; import { BoxrecBasic , BoxrecBoutLocation } from "../boxrec.constants" ; import { BoxrecEvent } from "./boxrec.event" ; import { BoxrecEventOutput } from "./boxrec.event.constants" ; import { emptyLocationObject } from "./boxrec.event.helpers" ; import { BoxrecPageEventBoutRow } from "./boxrec.page.event.bout.row" ; /**
 * Parse an Event page
 */ @ OutputGetter ( [ { function : ( bouts ) => bouts . map ( bout => bout . output ) , method : "bouts" , } , "commission" , "date" , "doctors" , "id" , "inspector" , "location" , "matchmakers" , "media" , "numberOfBouts" , "promoters" , "television" ] ) export class BoxrecPageEvent extends BoxrecEvent implements OutputInterface { output : BoxrecEventOutput ; get commission ( ) : string | null { const commission = this . parseEventData ( "commission" ) ; if ( commission ) { return commission . trim ( ) ; } return null ; } get date ( ) : string | null { const date = this . parseDate ( ) ; if ( date ) { return trimRemoveLineBreaks ( date ) ; } return date ; } /**
     * The list is not sorted and therefore the order can change.  Up to the developer how they want to sort
     */ get doctors ( ) : BoxrecBasic [ ] { return this . $ ( `<div>${ this . parseEventData ( BoxrecRole . doctor ) }</div>` ) . find ( "a" ) . map ( ( i , elem ) => this . $ . html ( elem ) ) . get ( ) . map ( item => BoxrecCommonTablesColumnsClass . parseNameAndId ( item ) ) ; } get id ( ) : number | null { const getLink = ( href ) : number | null => { if ( href ) { const wikiLink = href . match ( /(\d+)$/ ) ; if ( wikiLink && wikiLink [ 1 ] ) { return parseInt ( wikiLink [ 1 ] , 10 ) ; } } return null ; } ; const parent = this . $ ( this . parseEventResults ( ) ) . find ( "h2" ) . next ( ) ; let wikiHref = parent . find ( ".bio_closedP" ) . parent ( ) . attr ( "href" ) ; if ( wikiHref ) { return getLink ( wikiHref ) ; } wikiHref = parent . find ( ".bio_openP" ) . parent ( ) . attr ( "href" ) ; return getLink ( wikiHref ) ; } get inspector ( ) : BoxrecBasic { const html = this . $ ( `<div>${ this . parseEventData ( BoxrecRole . inspector ) }</div>` ) ; let inspector = { id : null , name : null , } ; html . find ( "a" ) . each ( ( i , elem ) => { inspector = BoxrecCommonTablesColumnsClass . parseNameAndId ( this . $ ( elem ) . text ( ) ) ; } ) ; return inspector ; } get location ( ) : BoxrecBoutLocation { const locationObject = Object . assign ( { } , emptyLocationObject ) ; let location = this . $ ( this . parseEventResults ( ) ) . find ( "thead table > tbody tr:nth-child(2) b" ) . html ( ) ; if ( location === null ) { const locationClone = this . $ ( "h2" ) . next ( ".flag" ) . parent ( ) . clone ( ) ; locationClone . remove ( "h2" ) ; locationClone . find ( "a:nth-child(1)" ) . remove ( ) ; location = locationClone . html ( ) ; } if ( location !== null ) { const html = this . $ ( `<div>${ trimRemoveLineBreaks ( location ) }</div>` ) ; const links = html . find ( "a" ) ; locationObject . venue = BoxrecPageEvent . getVenueInformation ( links ) ; locationObject . location = BoxrecPageEvent . getLocationInformation ( links ) ; } return locationObject ; } returns { string } protected parseMatchmakers ( ) { return this . parseEventData ( BoxrecRole . matchmaker ) ; } /**
     * Was previously `television` but now is media
     * returns the same content as `television`
     */ get media ( ) : string [ ] { return this . television ; } deprecated get television ( ) : string [ ] { const television = this . parseEventData ( "media" ) ; if ( television ) { return television . split ( "," ) . map ( item => { const text = this . $ ( `<span>${ item }</span>` ) . text ( ) ; return trimRemoveLineBreaks ( text ) ; } ) ; } return [ ] ; } protected parsePromoters ( ) { return this . parseEventData ( BoxrecRole . promoter ) ; } private parseDate ( ) { const eventResults = this . parseEventResults ( ) ; const date = this . $ ( eventResults ) . find ( "h2" ) . text ( ) ; if ( date ) { return new Date ( date ) . toISOString ( ) . slice ( 0 , 10 ) ; } return null ; } private parseEventResults ( ) { return this . $ ( "table" ) ; } }