import { Injectable } from '@angular/core';
import * as moment from 'moment';

@Injectable({
    providedIn: 'root',
})
export class ApiFormatterService {
    /**
     * time - a four digit string. The first two digits - hours, the second
     * two - minutes.
     *
     * 0130 -> 01:30:00
     * */
    formatTime(time: string | undefined) {
        if (!time) {
            return;
        }

        if (time === '2400') {
            return '23:59:59';
        }

        return `${this.formatTimeShort(time)}:00`;
    }

    formatTimeShort(time: string | undefined) {
        if (!time) {
            return;
        }

        if (time === '2400') {
            return '23:59';
        }

        return `${time.slice(0, 2)}:${time.slice(2)}`;
    }

    formatDate(date?: Date) {
        if (!date) {
            return undefined;
        }

        return moment(date).format('yyyy-MM-DDTHH:mm:ss');
    }

    formatDateOnly(date?: Date) {
        if (!date) {
            return undefined;
        }

        return moment(date).format('yyyy-MM-DD');
    }
}
