import { DatePipe } from '@angular/common';
import { Inject, LOCALE_ID, Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'orderDeadline',
  pure: true,
  standalone: true,
})
export class OrderDeadlinePipe implements PipeTransform {
  private datePipe: DatePipe;

  constructor(@Inject(LOCALE_ID) private locale: string) {
    this.datePipe = new DatePipe(this.locale);
  }

  transform(orderDeadline: string | Date | number): any {
    if (!orderDeadline) return 'Unbekannte Bestelldeadline';

    const date = new Date(orderDeadline);

    const isToday = date.toDateString() === new Date().toDateString();

    let timeString = '';

    if (isToday) {
      timeString = this.datePipe.transform(date, 'HH:mm', this.locale);
    } else {
      timeString = this.datePipe.transform(date, 'dd.MM HH:mm', this.locale);
    }

    return `Bestelldeadline ${timeString} Uhr`;
  }
}
