import { ChangeDetectionStrategy , Component , EventEmitter , Output , } from '@angular/core' ; import { ComposerService } from '../../../services/composer.service' ; import { isSafari } from '../../../../../helpers/is-safari' ; /**
 * Composer's Schedule popup modal
 */ @ Component ( { selector : 'm-composer__schedule' , changeDetection : ChangeDetectionStrategy . OnPush , templateUrl : 'schedule.component.html' , styleUrls : [ 'schedule.component.ng.scss' ] , } ) export class ScheduleComponent { /**
     * Signal event emitter to parent's popup service
     */ @ Output ( ) dismissIntent : EventEmitter < any > = new EventEmitter < any > ( ) ; /**
     * Current state.
     */ state : { date : Date ; time : string ; } ; /**
     * Minimum date to allow selecting
     */ readonly minDate : Date ; /**
     * Maximum date to allow selecting
     */ readonly maxDate : Date ; /**
     * System date/time format options
     */ readonly dateTimeFormatOptions : Intl . ResolvedDateTimeFormatOptions = new Intl . DateTimeFormat ( 'default' ) . resolvedOptions ( ) ; /**
     * Validation error
     */ error : boolean = false ; param service constructor ( protected service ) { this . minDate = new Date ( ) ; this . maxDate = new Date ( ) ; this . maxDate . setMonth ( this . maxDate . getMonth ( ) + 3 ) ; this . setState ( new Date ( Date . now ( ) + 600000 ) ) ; } /**
     * Initialize. Set state to service's scheduler value
     */ ngOnInit ( ) { const currentValue = this . service . schedule$ . getValue ( ) ; if ( currentValue ) { this . setState ( new Date ( currentValue * 1000 ) ) ; } } param date setState ( date ) { this . state = { date , time : [ `${ date . getHours ( ) }` . padStart ( 2 , '0' ) , `${ date . getMinutes ( ) }` . padStart ( 2 , '0' ) , ] . join ( ':' ) , } ; } param date onDateChange ( date ) { this . state = { ... this . state , date , } ; } param time onTimeChange ( time ) { this . state = { ... this . state , time , } ; } todo getParsedTime ( ) { const [ hours , minutes ] = this . state . time . trim ( ) . split ( ':' ) . map ( _ => parseInt ( _ , 10 ) ) ; if ( isNaN ( hours ) || hours < 0 || hours > 23 || isNaN ( minutes ) || minutes < 0 || minutes > 59 ) { return null ; } return { hours , minutes } ; } /**
     * Builds a Unix timestamp based off current state (up to seconds)
     */ buildTimestamp ( ) { const date = new Date ( this . state . date . getTime ( ) ) ; const time = this . getParsedTime ( ) ; if ( ! time ) { return null ; } date . setHours ( time . hours ) ; date . setMinutes ( time . minutes ) ; date . setSeconds ( 0 ) ; return Math . floor ( date . getTime ( ) / 1000 ) ; } /**
     * Check if can be saved (state is valid)
     */ hasError ( ) { const formatError = ! this . getParsedTime ( ) ; const futureError = Math . floor ( Date . now ( ) / 1000 ) + 300 >= this . buildTimestamp ( ) ; if ( isSafari ( ) ) { this . error = formatError || futureError ; } if ( formatError || futureError ) { return true ; } else { return false ; } } /**
     * Emits the internal state to the composer service and attempts to dismiss the modal
     */ save ( ) { if ( ! this . state || this . hasError ( ) ) { return ; } this . service . schedule$ . next ( this . buildTimestamp ( ) ) ; this . dismissIntent . emit ( ) ; } /**
     * Get the current timezone from browser as a human-readable string
     */ get currentTimezone ( ) : string { return this . dateTimeFormatOptions . timeZone ; } }