import { BoxrecRole } from "boxrec-requests/dist/boxrec-requests.constants" ; import { BoutsGetter , BoutsInterface } from "../../decorators/bouts.decorator" ; import { getLocationValue , townRegionCountryRegex , trimRemoveLineBreaks } from "../../helpers" ; import { BoxrecBasic , BoxrecBoutLocation , BoxrecLocation } from "../boxrec.constants" ; import { BoxrecPromoter } from "./boxrec.event.constants" ; import { BoxrecPageEventBoutRow } from "./boxrec.page.event.bout.row" ; import { BoxrecParseBouts } from "./boxrec.parse.bouts" ; /**
 * Used specifically for Events page and Dates page
 */ @ BoutsGetter ( "table" , BoxrecPageEventBoutRow , 2 ) export abstract class BoxrecEvent extends BoxrecParseBouts implements BoutsInterface { bouts : BoxrecPageEventBoutRow [ ] ; protected static getVenueInformation ( links ) { const obj = { id : null , name : null , } ; if ( links . length > 1 ) { const venueId = links . get ( 0 ) . attribs . href . match ( /(\d+)$/ ) ; const venueName = links . get ( 0 ) . children [ 0 ] . data ; if ( venueId && venueId [ 1 ] && venueName ) { obj . id = parseInt ( venueId [ 1 ] , 10 ) ; obj . name = venueName ; } } return obj ; } protected static getLocationInformation ( links ) { const hrefPosition = + ( links . length === 3 || links . length === 4 ) ; const locationObject = { country : { id : null , name : null , } , region : { id : null , name : null , } , town : { id : null , name : null , } } ; const locationMatches = links . get ( hrefPosition ) . attribs . href . match ( townRegionCountryRegex ) as string [ ] ; if ( locationMatches ) { const [ , , , townId ] = locationMatches ; if ( townId ) { locationObject . town . id = parseInt ( townId , 10 ) ; locationObject . town . name = links . get ( 1 ) . children [ 0 ] . data as string ; } if ( links . length === 4 ) { locationObject . region = { id : getLocationValue ( links . get ( 2 ) . attribs . href , "region" ) , name : links . get ( 2 ) . children [ 0 ] . data as string , } ; locationObject . country = { id : getLocationValue ( links . get ( 3 ) . attribs . href , "country" ) , name : links . get ( 3 ) . children [ 0 ] . data as string , } ; } else if ( links . length === 3 ) { locationObject . country = { id : getLocationValue ( links . get ( 2 ) . attribs . href , "country" ) , name : links . get ( 2 ) . children [ 0 ] . data as string , } ; } else if ( links . length === 2 ) { locationObject . town = { id : getLocationValue ( links . get ( 0 ) . attribs . href , "town" ) , name : links . get ( 0 ) . children [ 0 ] . data as string , } ; } if ( links . length === 2 || links . length === 1 ) { locationObject . country = { id : getLocationValue ( links . get ( 1 ) . attribs . href , "country" ) , name : links . get ( 1 ) . children [ 0 ] . data as string , } ; } } return locationObject ; } get location ( ) : BoxrecBoutLocation { const locationObject = { location : { country : { id : null , name : null , } , region : { id : null , name : null , } , town : { id : null , name : null , } } , venue : { id : null , name : null , } , } ; const html = this . $ ( `<div>${ this . parseLocation ( ) }</div>` ) ; const links = html . find ( "a" ) ; locationObject . venue = BoxrecEvent . getVenueInformation ( links ) ; locationObject . location = BoxrecEvent . getLocationInformation ( links ) ; return locationObject ; } /**
     * The order of returned matchmakers can change
     */ get matchmakers ( ) : BoxrecBasic [ ] { const html = this . $ ( `<div>${ this . parseMatchmakers ( ) }</div>` ) ; const matchmaker = [ ] ; html . find ( "a" ) . each ( ( i , elem ) => { const href = this . $ ( elem ) . get ( 0 ) . attribs . href . match ( /(\d+)$/ ) ; if ( href ) { const name = this . $ ( elem ) . text ( ) ; matchmaker . push ( { id : parseInt ( href [ 1 ] , 10 ) , name , } ) ; } } ) ; return matchmaker ; } get promoters ( ) : BoxrecPromoter [ ] { const html = this . $ ( `<div>${ this . parsePromoters ( ) }</div>` ) ; const promoter = [ ] ; html . find ( "a" ) . each ( ( i , elem ) => { const href = this . $ ( elem ) . get ( 0 ) . attribs . href ; const name = this . $ ( elem ) . text ( ) ; let id = null ; let company = null ; const matches = href . match ( /(\d+)$/ ) ; if ( matches ) { id = parseInt ( matches [ 0 ] , 10 ) ; } const htmlString = html . html ( ) ; if ( htmlString ) { const promoterEventsPageRegex = /([\w\d\/\-\s]+)\s-\s<a\shref/g ; const promoterBoutsPageRegex = /\(([\w\d\/\-\s]+)\)/g ; const eventsRegexReturnsResults = promoterEventsPageRegex . exec ( htmlString ) ; let regexThatGetsResults ; if ( eventsRegexReturnsResults !== null ) { regexThatGetsResults = promoterEventsPageRegex ; } else { const boutsRegexReturnsResults = promoterBoutsPageRegex . exec ( htmlString ) ; if ( boutsRegexReturnsResults !== null ) { regexThatGetsResults = promoterBoutsPageRegex ; } else { return promoter ; } } regexThatGetsResults . lastIndex = 0 ; let m ; let j = 0 ; do { m = regexThatGetsResults . exec ( htmlString ) ; if ( m && m [ 1 ] ) { if ( j === promoter . length ) { company = m [ 1 ] . trim ( ) ; } } j ++ ; } while ( m ) ; if ( company ) { promoter . push ( { company , id , name , } ) ; } } } ) ; return promoter ; } /**
     * Returns contact information on how to buy tickets for this event
     * example: boxrec.com/en/date?ByV%5Bdate%5D%5Byear%5D=2019&ByV%5Bdate%5D%5Bmonth%5D=11&ByV%5Bdate%5D%5Bday%5D=16
     */ get tickets ( ) : string | null { const tickets = this . parseEventData ( "tickets" , false ) ; return tickets ? trimRemoveLineBreaks ( tickets ) : null ; } protected getPeopleTable ( ) { return this . $ ( "table thead table tbody tr" ) ; } protected parseEventData ( role , parseHTML = true ) { let results = "" ; this . getPeopleTable ( ) . each ( ( i , elem ) => { const tag = this . $ ( elem ) . find ( "td:nth-child(1)" ) . text ( ) . trim ( ) ; const val = this . $ ( elem ) . find ( "td:nth-child(2)" ) ; if ( tag === role ) { results = parseHTML ? val . html ( ) : val . text ( ) ; } } ) ; return results ; } protected parseLocation ( ) { throw new Error ( "Needs to be overridden by child class" ) ; } protected parseMatchmakers ( ) { throw new Error ( "Needs to be overridden by child class" ) ; } protected parsePromoters ( ) { throw new Error ( "Needs to be overridden by child class" ) ; } }