import { jsx } from 'react/jsx-runtime';
import { useContext } from 'react';
import { DatePickerContext } from './DatePicker.js';
import { cn } from '../../../utils/cn.js';

const DatePickerPresets = ({ presets, className, setTempRange, onPresetSelect, }) => {
    const context = useContext(DatePickerContext);
    if (!context) {
        throw new Error('DatePickerPresets must be used within a DatePicker');
    }
    const { theme, selectedValue } = context;
    // Check if a preset is currently active (compare against committed selectedValue)
    const isPresetActive = (preset) => {
        if (!selectedValue || typeof selectedValue !== 'object' || !('start' in selectedValue)) {
            return false;
        }
        const presetRange = preset.getValue();
        const currentRange = selectedValue;
        // Compare dates (ignoring time)
        const isSameDate = (d1, d2) => {
            if (!d1 || !d2)
                return d1 === d2;
            return (d1.getFullYear() === d2.getFullYear() &&
                d1.getMonth() === d2.getMonth() &&
                d1.getDate() === d2.getDate());
        };
        return (isSameDate(currentRange.start, presetRange.start) &&
            isSameDate(currentRange.end, presetRange.end));
    };
    const handlePresetClick = (preset) => {
        const range = preset.getValue();
        // Update tempRange to show visual selection on calendar
        // The range is only committed when Apply button is clicked
        setTempRange === null || setTempRange === void 0 ? void 0 : setTempRange(range);
        // Notify parent to adjust calendar view
        onPresetSelect === null || onPresetSelect === void 0 ? void 0 : onPresetSelect(range);
    };
    return (jsx("div", { className: cn(theme.presetsContainerStyle, className), children: presets.map((preset, index) => (jsx("button", { type: "button", onClick: () => handlePresetClick(preset), className: theme.presetButtonStyle, "data-active": isPresetActive(preset), children: preset.label }, index))) }));
};

export { DatePickerPresets };
