import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';

export const formatDate = (date: Date) => {
  if (date) {
    return dayjs(new Date(date)).format('DD-MM-YYYY HH:MM');
  }
  return '';
};

export const getDate = (date: Date) => {
  if (date) {
    return dayjs(new Date(date)).format('DD-MM-YYYY');
  }
  return '';
};

export const getDay = (date: Date) => {
  if (date) {
    return dayjs(new Date(date)).format('DD');
  }
  return '';
};

export const getRestOfDate = (date: Date) => {
  if (date) {
    return dayjs(new Date(date)).format('MM-YYYY');
  }
  return '';
};

export const getTime = (date: Date) => {
  if (date) {
    dayjs.extend(utc);
    return dayjs.utc(new Date(date)).local().format('HH:mm');
  }
  return '';
};

interface StringDate {
  endsWith(searchString: string, endPosition?: number): boolean;
}

export const addTimezone = (date: StringDate): any =>
  date.endsWith('Z') ? date : `${date}Z`;

export const getDifference = (to: Date, from: Date) =>
  dayjs(to).diff(dayjs(from), 'days');
