import { format } from 'date-fns';
import ArrayUtils from "~/services/utils/arrayUtils";

export default class DateUtils {

  public static format(date: Date, fmt: string): string {
    return format(date, fmt)
  }

  /**
   * Formate la ou les dates au format donné et retourne la liste concaténée
   *
   * @param dates
   * @param fmt
   * @param sep
   */
  public static formatAndConcat (dates: Date | Array<Date>, fmt: string, sep: string = ' - '): string {
    dates = Array.isArray(dates) ? dates : [dates]
    return dates.map((d) => this.format(d, fmt)).join(sep)
  }

  /**
   * Trie les dates par ordre chronologique
   *
   * @param dates
   * @param reverse
   */
  public static sort(dates: Array<Date>, reverse: boolean = false): Array<Date> {
    return ArrayUtils.sort(dates, reverse) as Array<Date>
  }
}
