'use strict';

var jsxRuntime = require('../../node_modules/.pnpm/react@18.2.0/node_modules/react/jsx-runtime.cjs');
var reactIcons_esm = require('../../node_modules/.pnpm/@radix-ui_react-icons@1.3.0_react@18.2.0/node_modules/@radix-ui/react-icons/dist/react-icons.esm.cjs');
var dayjs_min = require('../../node_modules/.pnpm/dayjs@1.11.11/node_modules/dayjs/dayjs.min.cjs');
var React = require('react');
var flex = require('../flex/flex.cjs');
var popover = require('../popover/popover.cjs');
var calendar = require('./calendar.cjs');
var calendar_module = require('./calendar.module.css.cjs');
var inputField = require('../input-field/input-field.cjs');

function RangePicker({ side = "top", dateFormat = "DD/MM/YYYY", inputFieldsProps = {}, calendarProps, onSelect = () => { }, value = {
    to: new Date(),
    from: new Date(),
}, pickerGroupClassName, children, showCalendarIcon = true, footer, }) {
    const [showCalendar, setShowCalendar] = React.useState(false);
    const [currentRangeField, setCurrentRangeField] = React.useState("from");
    const [selectedRange, setSelectedRange] = React.useState(value);
    const startDate = selectedRange.from ? dayjs_min.default(selectedRange.from).format(dateFormat) : '';
    const endDate = selectedRange.to ? dayjs_min.default(selectedRange.to).format(dateFormat) : '';
    // 1st click will select the start date.
    // 2nd click will select the end date.
    // 3rd click will select the start date again.
    const handleSelect = (range, selectedDay) => {
        // TODO: Remove custom logic and reuse the default logic from react-day-picker
        let newRange;
        if (currentRangeField === "from") {
            // First click - set from date and prepare for to date selection
            newRange = { from: selectedDay };
            setCurrentRangeField("to");
        }
        else {
            // Second click - setting to date
            const from = selectedRange.from;
            if (dayjs_min.default(selectedDay).isBefore(dayjs_min.default(from))) {
                // If selected date is before current from date, start new range
                newRange = { from: selectedDay };
                setCurrentRangeField("to");
            }
            else {
                // Set the to date
                newRange = { from, to: selectedDay };
                setCurrentRangeField("from");
            }
        }
        setSelectedRange(newRange);
        // Return the range with +1 day for the end date in the callback
        const callbackRange = {
            from: newRange.from,
            to: newRange.to ? dayjs_min.default(newRange.to).add(1, 'day').toDate() : undefined
        };
        onSelect(callbackRange);
    };
    function onOpenChange(open) {
        setShowCalendar(Boolean(open));
    }
    const defaultTrigger = (jsxRuntime.jsxRuntimeExports.jsxs(flex.Flex, { gap: "medium", className: pickerGroupClassName, children: [jsxRuntime.jsxRuntimeExports.jsx(inputField.InputField, { size: 'small', ...(inputFieldsProps.startDate ?? {}), value: startDate, trailingIcon: showCalendarIcon ? jsxRuntime.jsxRuntimeExports.jsx(reactIcons_esm.CalendarIcon, {}) : undefined, readOnly: true }), jsxRuntime.jsxRuntimeExports.jsx(inputField.InputField, { size: 'small', ...(inputFieldsProps.endDate ?? {}), value: endDate, trailingIcon: showCalendarIcon ? jsxRuntime.jsxRuntimeExports.jsx(reactIcons_esm.CalendarIcon, {}) : undefined, readOnly: true })] }));
    const trigger = typeof children === 'function'
        ? children({ startDate, endDate })
        : children || defaultTrigger;
    return (jsxRuntime.jsxRuntimeExports.jsxs(popover.Popover, { open: showCalendar, onOpenChange: onOpenChange, children: [jsxRuntime.jsxRuntimeExports.jsx(popover.Popover.Trigger, { asChild: true, children: trigger }), jsxRuntime.jsxRuntimeExports.jsxs(popover.Popover.Content, { side: side, className: calendar_module.default.calendarPopover, children: [jsxRuntime.jsxRuntimeExports.jsx(calendar.Calendar, { showOutsideDays: false, numberOfMonths: 2, defaultMonth: selectedRange.from, required: true, ...calendarProps, mode: "range", selected: selectedRange, onSelect: handleSelect }), footer && (jsxRuntime.jsxRuntimeExports.jsx(flex.Flex, { align: "center", justify: "center", className: calendar_module.default.calendarFooter, children: footer }))] })] }));
}

exports.RangePicker = RangePicker;
//# sourceMappingURL=range-picker.cjs.map
