import { ref, reactive } from 'vue';
/**
 * el-date-picker 规定的时间范围
 */
function useTimeRange(days = 31, isSpace?: boolean) {
    const defaultTime = new Date(2000, 1, 1, 6, 0, 0) // 默认'6:00:00'
    const stateDay = reactive({
        firstSelectedDays: null
    })
    // 一天时间戳
    const millisecondsPerDay = 24 * 60 * 60 * 1000;
    //  禁用当日及之后的时间
    const disabledCurrentDate = (time: Date) => {
        return time.getTime() > (Date.now() - millisecondsPerDay)
    }
    // 默认是31天范围
    const onMoon = 1000 * 60 * 60 * 24 * days;
    // 用来判断该日期是否被禁用的函数
    const isSelectDisabled = (time) => {
        const firstSelectedDay = stateDay.firstSelectedDays as unknown as Date;
        if (isSpace && !firstSelectedDay) return disabledCurrentDate(time)
        if (firstSelectedDay) {
            // 当前时间往前推一天
            if (isSpace) return (
                (time.getTime() < firstSelectedDay.getTime() - onMoon ||
                time.getTime() > firstSelectedDay.getTime() + onMoon)
            );
            return (
                time.getTime() < firstSelectedDay.getTime() - onMoon ||
                time.getTime() > firstSelectedDay.getTime() + onMoon
            );
        }
        return false;
    };
    // 当日历所选的日期更改时触发。 仅适用于 daterange
    const selectCalendar = (date) => {
        const [minDate, maxDate] = date;
        if (minDate && !maxDate) {
            stateDay.firstSelectedDays = minDate; //记录选中的首个日期
        } else {
            stateDay.firstSelectedDays = null;
        }
    };

    return {
        defaultTime,
        isSelectDisabled,
        selectCalendar
    }
}

export {
    useTimeRange
}