import { Pipe, PipeTransform } from '@angular/core';
import { DateFormatOptions } from '@msfa-models/date-format-options.model';

@Pipe({
  name: 'localDate',
})
export class LocalDatePipe implements PipeTransform {
  private defaultFormatOptions: DateFormatOptions = {
    year: 'numeric',
    month: 'numeric',
    day: 'numeric',
  };
  private defaultLocale = 'sv-SE';

  transform(
    date: string | Date,
    formatOptions: DateFormatOptions = this.defaultFormatOptions,
    locale: string = this.defaultLocale
  ): string {
    if (!date) {
      console.warn('WARNING: No date provided.');
      return '';
    }
    return new Date(date).toLocaleDateString(locale, formatOptions);
  }
}
