import { Pipe, PipeTransform } from '@angular/core';
import { TranslatePipe } from '@ngx-translate/core';
import { ApiFormatterService } from '@shared/services/api/formatter.service';
import { IUrlTimeRange } from '@typings/common/url';

@Pipe({
    name: 'timeRange',
})
export class TimeRangePipe implements PipeTransform {
    constructor(
        private translate: TranslatePipe,
        private apiFormatter: ApiFormatterService,
    ) {}

    transform(timeRange?: IUrlTimeRange): string {
        if (!timeRange) {
            return this.translate.transform('SHARED.ALL_DAY');
        }

        const { timeFrom, timeTo } = timeRange;

        if (!timeFrom || !timeTo) {
            return this.translate.transform('SHARED.ALL_DAY');
        }

        if (timeFrom === '0000' && timeTo === '2400') {
            return this.translate.transform('SHARED.ALL_DAY');
        }

        const formattedTimeFrom = this.apiFormatter.formatTimeShort(timeFrom);
        const formattedTimeTo = this.apiFormatter.formatTimeShort(timeTo);

        return `${formattedTimeFrom} — ${formattedTimeTo}`;
    }
}
