"use client";
'use strict';

var jsxRuntime = require('react/jsx-runtime');
var reactIcons_esm = require('../../node_modules/.pnpm/@radix-ui_react-icons@1.3.2_react@19.1.1/node_modules/@radix-ui/react-icons/dist/react-icons.esm.cjs');
var index = require('../../node_modules/.pnpm/class-variance-authority@0.7.1/node_modules/class-variance-authority/dist/index.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 inputField = require('../input-field/input-field.cjs');
var popover = require('../popover/popover.cjs');
var calendar = require('./calendar.cjs');
var calendar_module = require('./calendar.module.css.cjs');

function RangePicker({ dateFormat = 'DD/MM/YYYY', inputFieldsProps = {}, calendarProps, onSelect = () => { }, value, defaultValue = {
    to: new Date(),
    from: new Date()
}, pickerGroupClassName, children, showCalendarIcon = true, footer, timeZone, popoverProps }) {
    const [showCalendar, setShowCalendar] = React.useState(false);
    const [currentRangeField, setCurrentRangeField] = React.useState('from');
    const [internalValue, setInternalValue] = React.useState(value ?? defaultValue);
    const [currentMonth, setCurrentMonth] = React.useState(internalValue?.from);
    const selectedRange = value ?? internalValue;
    const startDate = selectedRange.from
        ? dayjs_min.default(selectedRange.from).format(dateFormat)
        : '';
    const endDate = selectedRange.to
        ? dayjs_min.default(selectedRange.to).format(dateFormat)
        : '';
    // Ensures two months are visible even when
    // current month is the last allowed month (endMonth).
    const computedDefaultMonth = React.useMemo(() => {
        let month = currentMonth;
        if (calendarProps?.endMonth) {
            const endMonth = dayjs_min.default(calendarProps.endMonth);
            const fromMonth = dayjs_min.default(currentMonth);
            if (fromMonth.isSame(endMonth, 'month')) {
                month = endMonth.subtract(1, 'month').toDate();
            }
        }
        return month;
    }, [currentMonth, calendarProps?.endMonth]);
    const onTriggerClick = React.useCallback((e) => {
        const field = e.currentTarget.dataset.rangeField;
        if (field === 'start') {
            setCurrentRangeField('from');
        }
        else {
            setCurrentRangeField('to');
        }
        if (showCalendar) {
            e.preventDefault();
            e.stopPropagation();
        }
    }, [showCalendar]);
    // Handle date selection with custom logic
    const handleSelect = (_, selectedDay) => {
        let newRange = { ...selectedRange };
        let newCurrentRangeField = currentRangeField;
        if (currentRangeField === 'from') {
            // If selecting start date and it's after the current end date
            if (selectedRange?.to &&
                dayjs_min.default(selectedDay).isAfter(dayjs_min.default(selectedRange.to))) {
                newRange = { from: selectedDay };
                newCurrentRangeField = 'to';
            }
            else {
                newRange.from = selectedDay;
                if (!selectedRange?.to)
                    newCurrentRangeField = 'to';
            }
        }
        else {
            // If selecting end date and it's before the current start date
            if (selectedRange?.from &&
                dayjs_min.default(selectedDay).isBefore(dayjs_min.default(selectedRange.from))) {
                newRange = { from: selectedDay };
                newCurrentRangeField = 'to';
            }
            else
                newRange.to = selectedDay;
        }
        if (newCurrentRangeField !== currentRangeField)
            setCurrentRangeField(newCurrentRangeField);
        setInternalValue(newRange);
        onSelect(newRange);
    };
    const defaultTrigger = (jsxRuntime.jsxs(flex.Flex, { gap: 'medium', className: pickerGroupClassName, children: [jsxRuntime.jsx(inputField.InputField, { size: 'small', placeholder: 'Select start date', trailingIcon: showCalendarIcon ? jsxRuntime.jsx(reactIcons_esm.CalendarIcon, {}) : undefined, className: calendar_module.default.datePickerInput, ...(inputFieldsProps.startDate ?? {}), value: startDate, readOnly: true, "data-range-field": 'start', "data-active": showCalendar && currentRangeField === 'from', onClick: onTriggerClick }), jsxRuntime.jsx(inputField.InputField, { size: 'small', placeholder: 'Select end date', trailingIcon: showCalendarIcon ? jsxRuntime.jsx(reactIcons_esm.CalendarIcon, {}) : undefined, className: calendar_module.default.datePickerInput, ...(inputFieldsProps.endDate ?? {}), value: endDate, readOnly: true, "data-range-field": 'end', "data-active": showCalendar && currentRangeField === 'to', onClick: onTriggerClick })] }));
    const trigger = typeof children === 'function'
        ? children({ startDate, endDate })
        : children || defaultTrigger;
    return (jsxRuntime.jsxs(popover.Popover, { open: showCalendar, onOpenChange: setShowCalendar, children: [jsxRuntime.jsx(popover.Popover.Trigger, { asChild: true, children: trigger }), jsxRuntime.jsxs(popover.Popover.Content, { ...popoverProps, className: index.cx(calendar_module.default.calendarPopover, popoverProps?.className), side: popoverProps?.side ?? 'top', children: [jsxRuntime.jsx(calendar.Calendar, { showOutsideDays: false, numberOfMonths: 2, defaultMonth: selectedRange.from, required: true, ...calendarProps, timeZone: timeZone, mode: 'range', month: computedDefaultMonth, selected: selectedRange, onSelect: handleSelect, onMonthChange: setCurrentMonth }), footer && (jsxRuntime.jsx(flex.Flex, { align: 'center', justify: 'center', className: calendar_module.default.calendarFooter, children: footer }))] })] }));
}

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