import { useRef, useCallback, useMemo } from 'react';
import { isSameDay, isSameHour, isSameMinute } from 'date-fns';
import { RangeTypeMap } from '../types';
var START = RangeTypeMap.START, END = RangeTypeMap.END;
function initArray(targetNum) {
    return Array.from({ length: targetNum }, function (_, index) { return index; });
}
function initRemainArray(targetNum, total) {
    return Array.from({ length: total - targetNum }, function (_, index) { return total - index; });
}
export default function useRangeDisabledTime(_a) {
    var selected = _a.selected, disabledTime = _a.disabledTime;
    var disabledTimeRef = useRef(disabledTime);
    disabledTimeRef.current = disabledTime;
    var start = selected[0], end = selected[1];
    var disabledEndTimes = useCallback(function (date) {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j;
        var sameDay = start && date ? isSameDay(start, date) : false;
        var sameHour = start && date ? isSameHour(start, date) : false;
        var sameMinute = start && date ? isSameMinute(start, date) : false;
        var endHour = (date === null || date === void 0 ? void 0 : date.getHours()) || 0;
        var endMinute = (date === null || date === void 0 ? void 0 : date.getMinutes()) || 0;
        var startHour = (start === null || start === void 0 ? void 0 : start.getHours()) || 0;
        var startMinute = (start === null || start === void 0 ? void 0 : start.getMinutes()) || 0;
        var startSecond = (start === null || start === void 0 ? void 0 : start.getSeconds()) || 0;
        var defaultDisabledHours = ((_c = (_a = disabledTimeRef.current) === null || _a === void 0 ? void 0 : (_b = _a.call(disabledTimeRef, date, END)).disabledHours) === null || _c === void 0 ? void 0 : _c.call(_b)) || [];
        var defaultDisabledMinutes = ((_f = (_d = disabledTimeRef.current) === null || _d === void 0 ? void 0 : (_e = _d.call(disabledTimeRef, date, END)).disabledMinutes) === null || _f === void 0 ? void 0 : _f.call(_e, endHour)) || [];
        var defaultDisabledSeconds = ((_j = (_g = disabledTimeRef
            .current) === null || _g === void 0 ? void 0 : (_h = _g.call(disabledTimeRef, date, END)).disabledSeconds) === null || _j === void 0 ? void 0 : _j.call(_h, endHour, endMinute)) || [];
        var disabledHours = function () {
            return defaultDisabledHours.concat(sameDay ? initArray(startHour) : []);
        };
        var disabledMinutes = function () {
            return defaultDisabledMinutes.concat(sameDay && sameHour ? initArray(startMinute) : []);
        };
        var disabledSeconds = function () {
            return defaultDisabledSeconds.concat(sameDay && sameHour && sameMinute ? initArray(startSecond) : []);
        };
        return { disabledHours: disabledHours, disabledMinutes: disabledMinutes, disabledSeconds: disabledSeconds };
    }, [start, disabledTimeRef]);
    var disabledStartTimes = useCallback(function (date) {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j;
        var sameDay = date && end ? isSameDay(date, end) : false;
        var sameHour = date && end ? isSameHour(date, end) : false;
        var sameMinute = date && end ? isSameMinute(date, end) : false;
        var startHour = (date === null || date === void 0 ? void 0 : date.getHours()) || 0;
        var startMinute = (date === null || date === void 0 ? void 0 : date.getMinutes()) || 0;
        var endHour = (end === null || end === void 0 ? void 0 : end.getHours()) || 0;
        var endMinute = (end === null || end === void 0 ? void 0 : end.getMinutes()) || 0;
        var endSecond = (end === null || end === void 0 ? void 0 : end.getSeconds()) || 0;
        var defaultDisabledHours = ((_c = (_a = disabledTimeRef.current) === null || _a === void 0 ? void 0 : (_b = _a.call(disabledTimeRef, date, START)).disabledHours) === null || _c === void 0 ? void 0 : _c.call(_b)) || [];
        var defaultDisabledMinutes = ((_f = (_d = disabledTimeRef.current) === null || _d === void 0 ? void 0 : (_e = _d.call(disabledTimeRef, date, START)).disabledMinutes) === null || _f === void 0 ? void 0 : _f.call(_e, startHour)) ||
            [];
        var defaultDisabledSeconds = ((_j = (_g = disabledTimeRef
            .current) === null || _g === void 0 ? void 0 : (_h = _g.call(disabledTimeRef, date, START)).disabledSeconds) === null || _j === void 0 ? void 0 : _j.call(_h, startHour, startMinute)) || [];
        var disabledHours = function () {
            return defaultDisabledHours.concat(sameDay ? initRemainArray(endHour, 23) : []);
        };
        var disabledMinutes = function () {
            return defaultDisabledMinutes.concat(sameDay && sameHour ? initRemainArray(endMinute, 59) : []);
        };
        var disabledSeconds = function () {
            return defaultDisabledSeconds.concat(sameDay && sameHour && sameMinute
                ? initRemainArray(endSecond, 59)
                : []);
        };
        return { disabledHours: disabledHours, disabledMinutes: disabledMinutes, disabledSeconds: disabledSeconds };
    }, [end, disabledTimeRef]);
    var disabledConfirm = useMemo(function () { return !start || !end || (!!start && !!end && start > end); }, [start, end]);
    return { disabledStartTimes: disabledStartTimes, disabledEndTimes: disabledEndTimes, disabledConfirm: disabledConfirm };
}
