import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
    name: 'tzOffset',
})
export class TzOffsetPipe implements PipeTransform {
    transform(timeZoneOffset: number): string {
        timeZoneOffset = timeZoneOffset || 0;
        const isNegative = timeZoneOffset < 0;
        const totalMinutes = isNegative ? timeZoneOffset * -1 : timeZoneOffset;

        const hours = totalMinutes / 60;
        const roundedHours = Math.floor(hours);
        const minutes = (hours - roundedHours) * 60;
        const roundedMinutes = Math.round(minutes);
        const timeString = `${isNegative ? '-' : '+'}${roundedHours.toString().padStart(2, '0')}:${roundedMinutes
            .toString()
            .padStart(2, '0')}`;

        return timeString;
    }
}
