import { useControlledValue } from '../helpers/useControlledValue.js';
import { rangeIncludesDate } from '../utils/rangeIncludesDate.js';
import { addToRange } from '../utils/addToRange.js';
import { rangeContainsModifiers } from '../utils/rangeContainsModifiers.js';

/**
 * Hook to manage range selection in the DayPicker component.
 *
 * @template T - The type of DayPicker props.
 * @param props - The DayPicker props.
 * @param dateLib - The date utility library instance.
 * @returns An object containing the selected range, a function to select a
 *   range, and a function to check if a date is within the range.
 */
function useRange(props, dateLib) {
    const { disabled, excludeDisabled, resetOnSelect, selected: initiallySelected, required, onSelect, } = props;
    const [internallySelected, setSelected] = useControlledValue(initiallySelected, onSelect ? initiallySelected : undefined);
    const selected = !onSelect ? internallySelected : initiallySelected;
    const isSelected = (date) => selected && rangeIncludesDate(selected, date, false, dateLib);
    const select = (triggerDate, modifiers, e) => {
        const { min, max } = props;
        let newRange;
        if (triggerDate) {
            const selectedFrom = selected?.from;
            const selectedTo = selected?.to;
            const hasFullRange = !!selectedFrom && !!selectedTo;
            const isClickingSingleDayRange = !!selectedFrom &&
                !!selectedTo &&
                dateLib.isSameDay(selectedFrom, selectedTo) &&
                dateLib.isSameDay(triggerDate, selectedFrom);
            if (resetOnSelect && (hasFullRange || !selected?.from)) {
                if (!required && isClickingSingleDayRange) {
                    newRange = undefined;
                }
                else {
                    newRange = { from: triggerDate, to: undefined };
                }
            }
            else {
                newRange = addToRange(triggerDate, selected, min, max, required, dateLib);
            }
        }
        if (excludeDisabled && disabled && newRange?.from && newRange.to) {
            if (rangeContainsModifiers({ from: newRange.from, to: newRange.to }, disabled, dateLib)) {
                // if a disabled days is found, the range is reset
                newRange.from = triggerDate;
                newRange.to = undefined;
            }
        }
        if (!onSelect) {
            setSelected(newRange);
        }
        onSelect?.(newRange, triggerDate, modifiers, e);
        return newRange;
    };
    return {
        selected,
        select,
        isSelected,
    };
}

export { useRange };
//# sourceMappingURL=useRange.js.map
