import * as moment from 'moment';
import StartOf = moment.unitOfTime.StartOf;

export function getUTC(dateString: string) {
    return moment.parseZone(dateString).format('Z');
}

export function theDayBefore(date: Date) {
    return moment(date).startOf('day').add(-1, 'day').toDate();
}

export function theDayAfter(date: Date) {
    return moment(date).startOf('day').add(1, 'day').toDate();
}

export function getYesterday() {
    return theDayBefore(new Date());
}

export function getTomorrow() {
    return theDayAfter(new Date());
}

export function isToday(date: Date) {
    return moment().isSame(date, 'day');
}

export function isTomorrow(date: Date) {
    const tomorrow = getTomorrow();

    return areSame(date, tomorrow);
}

export function isBetween(
    date: Date,
    minDate: Date,
    maxDate: Date,
    granularity: StartOf = 'day',
) {
    return moment(date).isBetween(minDate, maxDate, granularity, '[]');
}

export function areSame(
    first: Date,
    second: Date,
    granularity: StartOf = 'day',
) {
    return moment(first).isSame(second, granularity);
}

export function isSameOrBefore(
    first: Date,
    second: Date,
    granularity: StartOf = 'day',
) {
    return moment(first).isSameOrBefore(second, granularity);
}

export function getSunday(monday: Date) {
    return moment(monday).add(6, 'day').toDate();
}

export function getEndOfTheWeek(date: Date) {
    return moment(date).endOf('isoWeek').toDate();
}

export function getStartOfTheWeek(date: Date) {
    return moment(date).startOf('isoWeek').toDate();
}

export function getStartOfTheCurrentWeek() {
    return moment().startOf('isoWeek').toDate();
}
