import { CompatibleDate , DateEntry , RangeEntry , instanceOfDateEntry , instanceOfRangeEntry , CompatibleValue , PanelMode } from './standard-types' ; import { fromUnixTime } from 'date-fns' ; import { helpers , TinyDate } from 'ngx-tethys/util' ; export function transformDateValue ( value ) { if ( ! value ) { return { value : null } ; } let withTime ; if ( value && typeof value === 'number' ) { value = convertDate ( value ) ; } if ( value && instanceOfDateEntry ( value as DateEntry ) ) { const { date , with_time } = value as DateEntry ; value = date ? convertDate ( date ) : null ; withTime = ! ! with_time ; } if ( value && instanceOfRangeEntry ( value as RangeEntry ) ) { const rangeValue = value as RangeEntry ; value = [ ] ; if ( rangeValue . begin && rangeValue . end ) { value [ 0 ] = convertDate ( rangeValue . begin ) ; value [ 1 ] = convertDate ( rangeValue . end ) ; } } return { value : value as CompatibleDate , withTime } ; } export function convertDate ( date ) { if ( typeof date === 'number' ) { if ( date . toString ( ) . length < 13 ) { return fromUnixTime ( date ) ; } else { return new Date ( date ) ; } } else { return date ; } } export function hasValue ( value ) { if ( Array . isArray ( value ) ) { return ! ! value [ 0 ] && ! ! value [ 1 ] ; } else { return ! ! value ; } } export function makeValue ( value , isRange = false ) { if ( isRange ) { return value ? ( value as Date [ ] ) . map ( val => new TinyDate ( val ) ) : [ ] ; } else { return value ? new TinyDate ( value as Date ) : null ; } } export function dateAddAmount ( value , amount , mode ) { let date ; switch ( mode ) { case 'decade' : date = value . addYears ( amount * 10 ) ; break ; case 'year' : date = value . addYears ( amount ) ; break ; case 'month' : date = value . addMonths ( amount ) ; break ; default : date = value . addMonths ( amount ) ; break ; } return date ; } export function isAfterMoreThanOneMonth ( rightDate , leftDate ) { rightDate = rightDate ? rightDate : leftDate ? leftDate : new TinyDate ( ) ; leftDate = leftDate ? leftDate : rightDate ; if ( rightDate . getYear ( ) < leftDate . getYear ( ) ) { return false ; } if ( rightDate . getYear ( ) === leftDate . getYear ( ) && leftDate . getMonth ( ) + 1 >= rightDate . getMonth ( ) ) { return false ; } return ! ( rightDate . getYear ( ) - leftDate . getYear ( ) === 1 && rightDate . getMonth ( ) === 0 && leftDate . getMonth ( ) === 11 ) ; } export function isAfterMoreThanLessOneYear ( rightDate , leftDate ) { rightDate = rightDate ? rightDate : leftDate ? leftDate : new TinyDate ( ) ; leftDate = leftDate ? leftDate : rightDate ; if ( rightDate . getYear ( ) <= leftDate . getYear ( ) ) { return false ; } return ! ( rightDate . getYear ( ) - leftDate . getYear ( ) === 1 && rightDate . getMonth ( ) <= leftDate . getMonth ( ) ) ; } export function isAfterMoreThanOneYear ( rightDate , leftDate ) { rightDate = rightDate ? rightDate : leftDate ? leftDate : new TinyDate ( ) ; leftDate = leftDate ? leftDate : rightDate ; if ( leftDate . getYear ( ) + 1 >= rightDate . getYear ( ) ) { return false ; } else { return true ; } } export function isAfterMoreThanOneDecade ( rightDate , leftDate ) { rightDate = rightDate ? rightDate : leftDate ? leftDate : new TinyDate ( ) ; leftDate = leftDate ? leftDate : rightDate ; return rightDate . getYear ( ) - leftDate . getYear ( ) >= 20 ; }