import { __spreadArray } from "tslib";
import { endOfDay, isAfter, isBefore, isSameDay, startOfDay } from 'date-fns';
import { RangeTypeMap, } from './types';
var initArray = function (targetNum) {
    return Array.from({ length: targetNum }, function (_, index) { return index; });
};
var initRangeArray = function (start, end) {
    return Array.from({ length: end - start }, function (_, index) { return end - index; });
};
export var disabledTimeWithMin = function (date, min) {
    var hour = min.getHours();
    var minute = min.getMinutes();
    var second = min.getSeconds();
    var isSame = isSameDay(date, min);
    return isSame
        ? {
            disabledHours: function () { return initArray(hour); },
            disabledMinutes: function (hourValue) {
                return hourValue === hour ? initArray(minute) : [];
            },
            disabledSeconds: function (hourValue, minuteValue) {
                return hourValue === hour && minuteValue === minute ? initArray(second) : [];
            },
        }
        : {};
};
export var disabledTimeWithMax = function (date, max) {
    var hour = max.getHours();
    var minute = max.getMinutes();
    var second = max.getSeconds();
    var isSame = isSameDay(date, max);
    return isSame
        ? {
            disabledHours: function () { return initRangeArray(hour, 23); },
            disabledMinutes: function (hourValue) {
                return hourValue === hour ? initRangeArray(minute, 59) : [];
            },
            disabledSeconds: function (hourValue, minuteValue) {
                return hourValue === hour && minuteValue === minute
                    ? initRangeArray(second, 59)
                    : [];
            },
        }
        : {};
};
export var disabledTimeWithRange = function (date, range) {
    var min = range[0], max = range[1];
    if (min && !max) {
        return disabledTimeWithMin(date, min);
    }
    if (max && !min) {
        return disabledTimeWithMax(date, max);
    }
    var minHour = min.getHours();
    var minMinute = min.getMinutes();
    var minSecond = min.getSeconds();
    var maxHour = max.getHours();
    var maxMinute = max.getMinutes();
    var maxSecond = max.getSeconds();
    var isMinSame = isSameDay(date, min);
    var isMaxSame = isSameDay(date, max);
    return {
        disabledHours: function () {
            var minDisabledHours = isMinSame ? initArray(minHour) : [];
            var maxDisabledHours = isMaxSame ? initRangeArray(maxHour, 23) : [];
            return __spreadArray(__spreadArray([], minDisabledHours), maxDisabledHours);
        },
        disabledMinutes: function (hourValue) {
            var minDisabledMinutes = isMinSame && hourValue === minHour ? initArray(minMinute) : [];
            var maxDisabledMinutes = isMaxSame && hourValue === maxHour ? initRangeArray(maxMinute, 59) : [];
            return __spreadArray(__spreadArray([], minDisabledMinutes), maxDisabledMinutes);
        },
        disabledSeconds: function (hourValue, minuteValue) {
            var minDisabledSeconds = isMinSame && hourValue === minHour && minuteValue === minMinute
                ? initArray(minSecond)
                : [];
            var maxDisabledSeconds = isMaxSame && hourValue === maxHour && minuteValue === maxMinute
                ? initRangeArray(maxSecond, 59)
                : [];
            return __spreadArray(__spreadArray([], minDisabledSeconds), maxDisabledSeconds);
        },
    };
};
export var disabledDateWithRange = function (range) {
    var min = range[0], max = range[1];
    return function (date) {
        return (!!min && isBefore(endOfDay(date), min)) ||
            (!!max && isAfter(startOfDay(date), max));
    };
};
export var getDisabledDateAndTimeWithRangeProps = function (range) {
    var min = range[0], max = range[1];
    return {
        disabledDate: disabledDateWithRange(range),
        disabledTime: function (date, type) {
            return type === RangeTypeMap.START
                ? disabledTimeWithMin(date, min)
                : disabledTimeWithMax(date, max);
        },
    };
};
