import { ReportFunnel } from '@/services/data/tuples/report-types' ; import { isXaNumber } from '@/services/utils' ; import dayjs from 'dayjs' ; import { CALENDAR_DATE_FORMAT } from '../basic/calendar' ; import { DropdownOption } from '../basic/types' ; export const tryCastToNumeric = ( value ? ) : number | undefined => { return isXaNumber ( value ) ? Number ( value ) : ( void 0 ) ; } ; export const getAsString = ( funnel , index ) : string | undefined => { if ( funnel . values == null || funnel . values [ index ] == null ) { return ( void 0 ) ; } return funnel . values [ index ] || ( void 0 ) ; } ; export const getAsNumeric = ( funnel , index ) : number | undefined => { if ( funnel . values == null || funnel . values [ index ] == null ) { return ( void 0 ) ; } try { return Number ( funnel . values [ index ] ) ; } catch { return ( void 0 ) ; } } ; export const getAsDate = ( funnel , index ) : string | undefined => { if ( funnel . values == null || funnel . values [ index ] == null ) { return ( void 0 ) ; } try { if ( ( funnel . values [ index ] || '' ) . trim ( ) . length === 0 ) { return ( void 0 ) ; } const date = dayjs ( funnel . values [ index ] ) ; if ( date . isValid ( ) ) { return date . format ( CALENDAR_DATE_FORMAT ) ; } else { return ( void 0 ) ; } } catch { return ( void 0 ) ; } } ; const tryToWriteValues = ( funnel , index , value ? ) : boolean => { funnel . values = funnel . values || [ ] ; const oldValue = getAsString ( funnel , index ) ; switch ( true ) { case ( value == null || value === '' ) && ( oldValue == null || oldValue === '' ) : funnel . values [ index ] = null ; return false ; case ( value == null || value === '' ) && ( oldValue != null && oldValue !== '' ) : funnel . values [ index ] = null ; return true ; case ( oldValue == null || oldValue === '' ) : funnel . values [ index ] = value ! ; return true ; default : funnel . values [ index ] = value ! ; return value != oldValue ; } } ; export const onNumericValueChange = ( funnel , index , onChange ) => ( value ? ) : number | undefined => { if ( tryToWriteValues ( funnel , index , value ) ) { onChange ( ) ; } return tryCastToNumeric ( value ) ; } ; export const onDropdownValueChange = ( funnel , index , onChange ) => ( option ) => { if ( tryToWriteValues ( funnel , index , option . value ) ) { onChange ( ) ; } } ; export const onDateValueChange = ( funnel , index , onChange ) => ( value ? ) => { if ( tryToWriteValues ( funnel , index , value ) ) { onChange ( ) ; } } ;