import { Internationalization } from './../../constants/constants';
import moment from 'moment';
export var CALENDAR_VIEWS;
(function (CALENDAR_VIEWS) {
    CALENDAR_VIEWS[CALENDAR_VIEWS["DAYS"] = 0] = "DAYS";
    CALENDAR_VIEWS[CALENDAR_VIEWS["MONTHS"] = 1] = "MONTHS";
    CALENDAR_VIEWS[CALENDAR_VIEWS["YEARS"] = 2] = "YEARS";
    CALENDAR_VIEWS[CALENDAR_VIEWS["DECADES"] = 3] = "DECADES";
})(CALENDAR_VIEWS || (CALENDAR_VIEWS = {}));
export var CALENDAR_NAVIGATION;
(function (CALENDAR_NAVIGATION) {
    CALENDAR_NAVIGATION["FIRST"] = "first";
    CALENDAR_NAVIGATION["LAST"] = "last";
    CALENDAR_NAVIGATION["PREV"] = "prev";
    CALENDAR_NAVIGATION["NEXT"] = "next";
})(CALENDAR_NAVIGATION || (CALENDAR_NAVIGATION = {}));
export var CALENDAR_GRAIN;
(function (CALENDAR_GRAIN) {
    CALENDAR_GRAIN["DAY"] = "$day";
    CALENDAR_GRAIN["WEEK"] = "$week";
    CALENDAR_GRAIN["MONTH"] = "$month";
    CALENDAR_GRAIN["YEAR"] = "$year";
})(CALENDAR_GRAIN || (CALENDAR_GRAIN = {}));
export var parseDate = function (date, dateUtils) {
    if (isString(date)) {
        var dt = dateUtils.getStartOfMinute(moment());
        if (date.startsWith(CALENDAR_GRAIN.DAY)) {
            return dateUtils.addDays(dt, parseInt(date.replace(CALENDAR_GRAIN.DAY, "") || "0", 10));
        }
        else if (date.startsWith(CALENDAR_GRAIN.WEEK)) {
            return dateUtils.addWeeks(dt, parseInt(date.replace(CALENDAR_GRAIN.WEEK, "") || "0", 10));
        }
        else if (date.startsWith(CALENDAR_GRAIN.MONTH)) {
            return dateUtils.addMonths(dt, parseInt(date.replace(CALENDAR_GRAIN.MONTH, "") || "0", 10));
        }
        else if (date.startsWith(CALENDAR_GRAIN.YEAR)) {
            return dateUtils.addYears(dt, parseInt(date.replace(CALENDAR_GRAIN.YEAR, "") || "0", 10));
        }
        else {
            return dateUtils.toDate(date);
        }
    }
    else if (date) {
        return date;
    }
    return null;
};
export var parseRange = function (date, dateUtils) {
    if (isString(date)) {
        if (date.startsWith(CALENDAR_GRAIN.DAY)) {
            var today = dateUtils.getStartOfDay(moment());
            var diff = dateUtils.addDays(moment(), parseInt(date.replace(CALENDAR_GRAIN.DAY, "") || "0", 10));
            return date.includes("-") ? [diff, today] : [today, diff];
        }
        if (date.startsWith(CALENDAR_GRAIN.WEEK)) {
            var diff = parseInt(date.replace(CALENDAR_GRAIN.WEEK, "") || "0", 10);
            return [dateUtils.addWeeks(dateUtils.getStartOfWeek(moment()), diff), dateUtils.addWeeks(dateUtils.getEndOfWeek(moment()), diff)];
        }
        if (date.startsWith(CALENDAR_GRAIN.MONTH)) {
            var diff = parseInt(date.replace(CALENDAR_GRAIN.MONTH, "") || "0", 10);
            return [dateUtils.addMonths(dateUtils.getStartOfMonth(moment()), diff), dateUtils.addMonths(dateUtils.getEndOfMonth(moment()), diff)];
        }
        if (date.startsWith(CALENDAR_GRAIN.YEAR)) {
            var diff = parseInt(date.replace(CALENDAR_GRAIN.YEAR, "") || "0", 10);
            return [dateUtils.addYears(dateUtils.getStartOfYear(moment()), diff), dateUtils.addYears(dateUtils.getEndOfYear(moment()), diff)];
        }
    }
    else if (isArray(date)) {
        return [parseDate(date[0], dateUtils), parseDate(date[1], dateUtils)];
    }
    return null;
};
export var getTitle = function (date, view, translations, dateUtils) {
    if (view === CALENDAR_VIEWS.DAYS) {
        return translations.months[date.getMonth()] + " " + dateUtils.format(date, "yyyy");
    }
    if (view === CALENDAR_VIEWS.MONTHS) {
        return dateUtils.format(date, "yyyy");
    }
    if (view === CALENDAR_VIEWS.YEARS) {
        return dateUtils.format(dateUtils.getStartOfDecade(date), "yyyy") + "-" + dateUtils.format(dateUtils.getEndOfDecade(date), "yyyy");
    }
    return "-";
};
export var changeMonth = function (date, view, grain, dateUtils) {
    if (view === CALENDAR_VIEWS.DAYS) {
        switch (grain) {
            case CALENDAR_NAVIGATION.FIRST:
                return dateUtils.subtractMonths(date, 12);
            case CALENDAR_NAVIGATION.LAST:
                return dateUtils.addMonths(date, 12);
            case CALENDAR_NAVIGATION.PREV:
                return dateUtils.subtractMonths(date, 1);
            case CALENDAR_NAVIGATION.NEXT:
                return dateUtils.addMonths(date, 1);
        }
    }
    if (view === CALENDAR_VIEWS.MONTHS) {
        switch (grain) {
            case CALENDAR_NAVIGATION.PREV:
                return dateUtils.subtractYears(date, 1);
            case CALENDAR_NAVIGATION.NEXT:
                return dateUtils.addYears(date, 1);
        }
    }
    if (view === CALENDAR_VIEWS.YEARS) {
        switch (grain) {
            case CALENDAR_NAVIGATION.PREV:
                return dateUtils.subtractYears(date, 10);
            case CALENDAR_NAVIGATION.NEXT:
                return dateUtils.addYears(date, 10);
        }
    }
    return date;
};
export var buildHeaderConfig = function (date, view, config, dateUtils, translations) {
    if (translations === void 0) { translations = Internationalization.datePickerTranslationsEN; }
    if (view === CALENDAR_VIEWS.DAYS) {
        return {
            firstDisabled: config.minDate ? isBeforeMin(date, dateUtils.getStartOfYear(config.minDate), -12, dateUtils) : false,
            lastDisabled: config.maxDate ? isAfterMax(date, dateUtils.getEndOfYear(config.maxDate), 12, dateUtils) : false,
            prevDisabled: config.minDate ? isBeforeMin(date, dateUtils.getStartOfMonth(config.minDate), -1, dateUtils) : false,
            nextDisabled: config.maxDate ? isAfterMax(date, dateUtils.getEndOfMonth(config.maxDate), 1, dateUtils) : false,
            firstTooltip: getTitle(dateUtils.subtractMonths(date, 12), view, translations, dateUtils),
            lastTooltip: getTitle(dateUtils.addMonths(date, 12), view, translations, dateUtils),
            prevTooltip: getTitle(dateUtils.subtractMonths(date, 1), view, translations, dateUtils),
            nextTooltip: getTitle(dateUtils.addMonths(date, 1), view, translations, dateUtils)
        };
    }
    if (view === CALENDAR_VIEWS.MONTHS) {
        return {
            prevDisabled: config.minDate ? dateUtils.isBeforeYear(dateUtils.subtractMonths(date, 12), config.minDate) : false,
            nextDisabled: config.maxDate ? dateUtils.isAfterYear(dateUtils.addMonths(date, 12), config.maxDate) : false,
            prevTooltip: getTitle(dateUtils.subtractYears(date, 1), view, translations, dateUtils),
            nextTooltip: getTitle(dateUtils.addYears(date, 1), view, translations, dateUtils)
        };
    }
    if (view === CALENDAR_VIEWS.YEARS) {
        var start = dateUtils.getStartOfDecade(date);
        var end = dateUtils.getEndOfDecade(date);
        return {
            prevDisabled: config.minDate ? dateUtils.isBeforeYear(dateUtils.subtractMonths(date, 120), dateUtils.getStartOfDecade(config.minDate)) : false,
            nextDisabled: config.maxDate ? dateUtils.isAfterYear(dateUtils.addMonths(date, 120), dateUtils.getEndOfDecade(config.maxDate)) : false,
            prevTooltip: dateUtils.format(dateUtils.addYears(start, -10), "yyyy") + "-" + dateUtils.format(dateUtils.addYears(start, -1), "yyyy"),
            nextTooltip: dateUtils.format(dateUtils.addYears(end, 1), "yyyy") + "-" + dateUtils.format(dateUtils.addYears(end, 10), "yyyy")
        };
    }
};
export var isBeforeMin = function (date, minDate, n, dateUtils) {
    if (n === void 0) { n = 0; }
    return dateUtils.isValid(minDate) ? dateUtils.isBefore(dateUtils.addMonths(dateUtils.getStartOfDay(date), n), minDate) : false;
};
export var isAfterMax = function (date, maxDate, n, dateUtils) {
    if (n === void 0) { n = 0; }
    return dateUtils.isValid(maxDate) ? dateUtils.isAfter(dateUtils.addMonths(dateUtils.getStartOfDay(date), n), maxDate) : false;
};
export var isDisabled = function (config, date, dateUtils) {
    var min = config.minDate;
    var max = config.maxDate;
    if (config.page === CALENDAR_VIEWS.DAYS) {
        min = dateUtils.getStartOfDay(min);
        max = dateUtils.getStartOfDay(max);
    }
    if (config.page === CALENDAR_VIEWS.MONTHS) {
        min = dateUtils.getStartOfDay(dateUtils.getStartOfMonth(min));
        max = dateUtils.getStartOfDay(dateUtils.getStartOfMonth(max));
    }
    if (config.page === CALENDAR_VIEWS.YEARS) {
        min = dateUtils.getStartOfDay(dateUtils.getStartOfYear(min));
        max = dateUtils.getStartOfDay(dateUtils.getStartOfYear(max));
    }
    if (dateUtils.isBefore(date, min)) {
        return true;
    }
    if (dateUtils.isAfter(date, max)) {
        return true;
    }
    if (config.page === CALENDAR_VIEWS.DAYS && config.disabled) {
        var disabled = config.disabled;
        if (isArray(disabled)) {
            return disabled.includes(dateUtils.getStartOfDay(date).toISOString());
        }
        else if (isFunction(disabled)) {
            return disabled({ date: date });
        }
    }
    return false;
};

//# sourceMappingURL=calendar-utils.js.map
