license import { DateService } from './date.service' ; import { CalendarRange } from '../type' ; export class RangeDateService < D > { private dateService : DateService < D > ; constructor ( dateService ) { this . dateService = dateService ; } public createRange ( range , date ) { switch ( true ) { case ( ! range . startDate && ! range . endDate ) : return { startDate : date , endDate : null } ; case ( range . startDate && ! range . endDate ) : return this . createRangeForStart ( range , date ) ; case ( range . startDate !== null && range . endDate !== null ) : return this . createRangeForStartEnd ( date ) ; default : return range ; } } private createRangeForStart ( range , date ) { if ( this . dateService . compareDatesSafe ( date , range . startDate ) === 1 ) { return { startDate : range . startDate , endDate : date } ; } else if ( this . dateService . compareDatesSafe ( date , range . startDate ) === - 1 ) { return { startDate : date , endDate : range . startDate } ; } else { return range ; } } private createRangeForStartEnd ( date ) { return { startDate : date , endDate : null } ; } }