import pxd from 'parse-xsd-duration';

export class DurationConverter {
    static fromDuration(duration: string): { hours: number, minutes: number } {
        const obj = pxd(duration, true);
        if (!obj['hours']) {
            obj['hours'] = 0;
        }
        if (!obj['minutes']) {
            obj['minutes'] = 0;
        }

        return { hours: obj.hours, minutes: obj.minutes };
    }

    static fromDurationToString(duration: string): string {
        const obj = this.fromDuration(duration);
        const hours = obj.hours.toString();
        let minutes = obj.minutes.toString();
        if (minutes.length === 1) {
            minutes = '0' + minutes;
        }

        return hours + ':' + minutes;
    }

    static toDuration(time: { hours, minutes }): string {
        return 'PT' + time.hours + 'H' + time.minutes + 'M';
    }
}
