import { useRef, useCallback } from 'react';
import { endOfDay, isAfter, isBefore, startOfDay } from 'date-fns';
import { RangeTypeMap, } from '../types';
var START = RangeTypeMap.START, END = RangeTypeMap.END;
export default function useRangeDisabledDate(selected, disabledDate, generateDate, dateSpan) {
    if (dateSpan === void 0) { dateSpan = 0; }
    var disabledDateRef = useRef(disabledDate);
    disabledDateRef.current = disabledDate;
    var disabledStartDate = useCallback(function (value) {
        var _a;
        var date = endOfDay(value);
        var end = selected[1];
        var isSame = generateDate.isSame, offsetDate = generateDate.offsetDate;
        if ((_a = disabledDateRef.current) === null || _a === void 0 ? void 0 : _a.call(disabledDateRef, date, START)) {
            return true;
        }
        if (end) {
            return ((!isSame(date, end) && isAfter(date, end)) ||
                (!!dateSpan && isBefore(date, offsetDate(end, -dateSpan + 1))));
        }
        return false;
    }, [selected, disabledDateRef, generateDate, dateSpan]);
    var disabledEndDate = useCallback(function (value) {
        var _a;
        var date = startOfDay(value);
        var isSame = generateDate.isSame, offsetDate = generateDate.offsetDate;
        var start = selected[0];
        if ((_a = disabledDateRef.current) === null || _a === void 0 ? void 0 : _a.call(disabledDateRef, date, END)) {
            return true;
        }
        var isInDateSpan = !!dateSpan && isAfter(date, offsetDate(start, dateSpan - 1));
        if (start) {
            return (!isSame(date, start) && isBefore(date, start)) || isInDateSpan;
        }
        return false;
    }, [selected, disabledDateRef, generateDate, dateSpan]);
    return [disabledStartDate, disabledEndDate];
}
