"use strict";
exports.__esModule = true;
exports.useCurrentVisibleDate = exports.getDaysOfWeek = exports.getMouthLabelWithYear = exports.getMonthTitle = exports.getHandleSelectDate = exports.getDaysOfMonth = exports.isValueSelectedBackwards = exports.isValueSelected = exports.isDateSelected = exports.getStartAndEndDate = exports.dateComparer = exports.calendarPropTypeDefault = exports.calendarPropType = exports.calendarPropViewDefault = exports.calendarPropView = void 0;
var tslib_1 = require("tslib");
// @ts-nocheck
/* eslint-disable react-hooks/exhaustive-deps */
var react_1 = require("react");
var date_fns_1 = require("date-fns");
var eachDayOfInterval_1 = (0, tslib_1.__importDefault)(require("date-fns/eachDayOfInterval"));
var array_1 = require("../../utils/array");
var date_1 = require("../../utils/date");
var type_guards_1 = require("../../utils/type-guards");
exports.calendarPropView = ['oneMonth'];
exports.calendarPropViewDefault = exports.calendarPropView[0];
exports.calendarPropType = ['date', 'date-range'];
exports.calendarPropTypeDefault = exports.calendarPropType[0];
var isEqualDate = function (date1, date2) { return date1.getTime() === date2.getTime(); };
var isEqualDay = function (date1, date2) {
    return isEqualDate((0, date_fns_1.startOfDay)(date1), (0, date_fns_1.startOfDay)(date2));
};
var dateComparer = function (a, b) { var _a, _b; return ((_a = a === null || a === void 0 ? void 0 : a.getTime()) !== null && _a !== void 0 ? _a : 0) - ((_b = b === null || b === void 0 ? void 0 : b.getTime()) !== null && _b !== void 0 ? _b : 0); };
exports.dateComparer = dateComparer;
var getStartAndEndDate = function (date1, date2) {
    return [date1, date2].sort(exports.dateComparer);
};
exports.getStartAndEndDate = getStartAndEndDate;
var isDateSelected = function (_a) {
    var date = _a.date, value = _a.value;
    return value ? (0, date_fns_1.isSameDay)(value, date) : false;
};
exports.isDateSelected = isDateSelected;
var isValueSelected = function (_a) {
    var date = _a.date, value = _a.value;
    if ((0, date_1.isDateRange)(value)) {
        if (value[0] && value[1]) {
            var _b = (0, tslib_1.__read)((0, exports.getStartAndEndDate)(value[0], value[1]), 2), start = _b[0], end = _b[1];
            return (0, date_fns_1.isWithinInterval)(date, { start: start, end: end });
        }
        return (0, exports.isDateSelected)({ date: date, value: value[0] || value[1] });
    }
    return (0, exports.isDateSelected)({ date: date, value: value });
};
exports.isValueSelected = isValueSelected;
var isValueSelectedBackwards = function (_a) {
    var value = _a.value, hoveredDate = _a.hoveredDate;
    return (hoveredDate &&
        (0, date_1.isDateRange)(value) &&
        (0, date_1.isOnlyOneDateInRange)(value) &&
        (((0, type_guards_1.isDefined)(value[0]) && (0, date_fns_1.isBefore)(hoveredDate, value[0])) ||
            ((0, type_guards_1.isDefined)(value[1]) && (0, date_fns_1.isBefore)(hoveredDate, value[1]))));
};
exports.isValueSelectedBackwards = isValueSelectedBackwards;
var isSelected = function (_a) {
    var date = _a.date, value = _a.value;
    if (!value) {
        return false;
    }
    if (Array.isArray(value)) {
        return !!value.find(function (item) { return (item ? isEqualDay(date, item) : false); });
    }
    return isEqualDay(date, value);
};
var isDateInRange = function (date, range) {
    if (!range[0] || !range[1]) {
        return false;
    }
    var dateTime = date.getTime();
    var rangeTime = [range[0].getTime(), range[1].getTime()];
    if (dateTime === rangeTime[0]) {
        return 'first';
    }
    if (dateTime === rangeTime[1]) {
        return 'last';
    }
    if (dateTime > rangeTime[0] && dateTime < rangeTime[1]) {
        return true;
    }
    return false;
};
var hasEvent = function (date, events) {
    return !!events.find(function (eventDate) { return isEqualDay(eventDate, date); });
};
var isToday = function (date) { return isEqualDay(new Date(), date); };
var isWithInIntervalMinMaxDade = function (date, minDate, maxDate) {
    var minDateTime = minDate === null || minDate === void 0 ? void 0 : minDate.getTime();
    var maxDateTime = maxDate === null || maxDate === void 0 ? void 0 : maxDate.getTime();
    if (minDate && maxDate && minDateTime && maxDateTime) {
        return minDateTime < maxDateTime
            ? (0, date_fns_1.isWithinInterval)(date, { start: minDate, end: maxDate })
            : false;
    }
    var dateTime = date.getTime();
    if (minDateTime && !maxDateTime) {
        return minDateTime < dateTime;
    }
    if (!minDateTime && maxDateTime) {
        return maxDateTime > dateTime;
    }
    return true;
};
var getDaysOfMonth = function (props) {
    var date = props.date, locale = props.locale, handleDayClick = props.handleDayClick, value = props.value, events = props.events, minDate = props.minDate, maxDate = props.maxDate;
    var currentMonth = date.getMonth();
    var startDate = (0, date_fns_1.startOfWeek)((0, date_fns_1.startOfMonth)(date), { locale: locale });
    var endDate = (0, date_fns_1.endOfWeek)((0, date_fns_1.endOfMonth)(date), { locale: locale });
    var diffDays = (0, date_fns_1.differenceInDays)(endDate, startDate) + 1;
    return (0, array_1.range)(diffDays).map(function (index) {
        var date = (0, date_fns_1.addDays)(startDate, index);
        var number = (0, date_fns_1.format)(date, 'd');
        if (date.getMonth() === currentMonth) {
            return {
                number: number,
                onClick: handleDayClick
                    ? function (e) { return handleDayClick({ e: e, value: date }); }
                    : undefined,
                selected: isSelected({ date: date, value: value }),
                range: Array.isArray(value) && isDateInRange(date, value),
                event: events && hasEvent(date, events),
                today: isToday(date),
                disabled: !isWithInIntervalMinMaxDade(date, minDate, maxDate)
            };
        }
        return {
            number: number,
            disabled: true
        };
    });
};
exports.getDaysOfMonth = getDaysOfMonth;
var isDateRangeParams = function (params) {
    return params.type === exports.calendarPropType[1];
};
var isNotDateRangeParams = function (params) {
    return params.type === exports.calendarPropType[0];
};
function getHandleSelectDate(params) {
    if (isDateRangeParams(params)) {
        var currentValue_1 = params.value || [undefined, undefined];
        return function (_a) {
            var date = _a.value, e = _a.e;
            if (params.minDate &&
                params.maxDate &&
                !(0, date_fns_1.isWithinInterval)(date, { start: params.minDate, end: params.maxDate })) {
                return;
            }
            if (!(0, date_1.isOnlyOneDateInRange)(currentValue_1) && typeof params.onChange === 'function') {
                return params.onChange({ e: e, value: [date, undefined] });
            }
            var _b = (0, tslib_1.__read)(currentValue_1, 2), startDate = _b[0], endDate = _b[1];
            if (((0, type_guards_1.isDefined)(startDate) && isEqualDay(startDate, date)) ||
                ((0, type_guards_1.isDefined)(endDate) && isEqualDay(endDate, date))) {
                return;
            }
            if ((0, type_guards_1.isDefined)(startDate) && typeof params.onChange === 'function') {
                return params.onChange({
                    e: e,
                    value: startDate > date ? [date, startDate] : [startDate, date]
                });
            }
            if ((0, type_guards_1.isDefined)(endDate) && typeof params.onChange === 'function') {
                return params.onChange({
                    e: e,
                    value: endDate > date ? [date, endDate] : [endDate, date]
                });
            }
        };
    }
    if (isNotDateRangeParams(params)) {
        return function (props) {
            if (!isWithInIntervalMinMaxDade(props.value, params.minDate, params.maxDate)) {
                return;
            }
            if (typeof params.onChange === 'function') {
                return params.onChange(props);
            }
        };
    }
    return function () { return undefined; };
}
exports.getHandleSelectDate = getHandleSelectDate;
var getMonthTitle = function (date, locale) {
    return (0, date_fns_1.format)(date, 'LLLL', { locale: locale });
};
exports.getMonthTitle = getMonthTitle;
var getMouthLabelWithYear = function (date, locale) {
    return "".concat((0, exports.getMonthTitle)(date, locale), " ").concat(date.getFullYear());
};
exports.getMouthLabelWithYear = getMouthLabelWithYear;
var getDaysOfWeek = function (locale) {
    var now = new Date();
    return (0, eachDayOfInterval_1["default"])({
        start: (0, date_fns_1.startOfWeek)(now, { locale: locale }),
        end: (0, date_fns_1.endOfWeek)(now, { locale: locale })
    }).map(function (date) { return (0, date_fns_1.format)(date, 'EEEEEE', { locale: locale }); });
};
exports.getDaysOfWeek = getDaysOfWeek;
var getCurrentVisibleDate = function (_a) {
    var currentVisibleDate = _a.currentVisibleDate, minDate = _a.minDate, maxDate = _a.maxDate, value = _a.value;
    var currentDate = new Date();
    if (currentVisibleDate) {
        return currentVisibleDate;
    }
    if (Array.isArray(value) && value[0]) {
        return value[0];
    }
    if (Array.isArray(value) && value[1]) {
        return value[1];
    }
    if (value && !Array.isArray(value)) {
        return value;
    }
    if (minDate && maxDate && !isDateInRange(currentDate, [minDate, maxDate])) {
        return minDate;
    }
    if (minDate && !maxDate) {
        var minDateTime = minDate.getTime();
        var currentDateTime = currentDate.getTime();
        if (currentDateTime < minDateTime) {
            return minDate;
        }
    }
    if (!minDate && maxDate) {
        var maxDateTime = maxDate.getTime();
        var currentDateTime = currentDate.getTime();
        if (currentDateTime > maxDateTime) {
            return maxDate;
        }
    }
    return currentDate;
};
var useCurrentVisibleDate = function (props) {
    var _a, _b, _c;
    var currentVisibleDate = (0, react_1.useMemo)(function () {
        return (0, date_fns_1.startOfMonth)(getCurrentVisibleDate(props));
    }, [
        ((_a = props.currentVisibleDate) === null || _a === void 0 ? void 0 : _a.getTime()) || 0,
        ((_b = props.minDate) === null || _b === void 0 ? void 0 : _b.getTime()) || 0,
        ((_c = props.maxDate) === null || _c === void 0 ? void 0 : _c.getTime()) || 0,
    ]);
    var state = (0, react_1.useState)(currentVisibleDate);
    // eslint-disable-next-line react-hooks/exhaustive-deps
    (0, react_1.useEffect)(function () { return state[1](currentVisibleDate); }, [currentVisibleDate.getTime()]);
    return state;
};
exports.useCurrentVisibleDate = useCurrentVisibleDate;
