"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.2.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 customParseFormat = require('../../node_modules/.pnpm/dayjs@1.11.11/node_modules/dayjs/plugin/customParseFormat.cjs');
var React = require('react');
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');

dayjs_min.default.extend(customParseFormat.default);
function DatePicker({ dateFormat = 'DD/MM/YYYY', inputFieldProps, calendarProps, value = new Date(), onSelect = () => { }, children, showCalendarIcon = true, timeZone, popoverProps }) {
    const [showCalendar, setShowCalendar] = React.useState(false);
    const [selectedDate, setSelectedDate] = React.useState(value);
    const [error, setError] = React.useState();
    const formattedDate = dayjs_min.default(selectedDate).format(dateFormat);
    const isDropdownOpenRef = React.useRef(false);
    const inputFieldRef = React.useRef(null);
    const contentRef = React.useRef(null);
    const isInputFieldFocused = React.useRef(false);
    const selectedDateRef = React.useRef(selectedDate);
    React.useEffect(() => {
        selectedDateRef.current = selectedDate;
    }, [selectedDate]);
    const isElementOutside = React.useCallback((el) => {
        return (!isDropdownOpenRef.current && // Month and Year dropdown from Date picker
            !inputFieldRef.current?.contains(el) && // InputField
            !contentRef.current?.contains(el));
    }, []);
    const handleMouseDown = React.useCallback((event) => {
        const el = event.target;
        if (el && isElementOutside(el))
            removeEventListeners();
    }, [isElementOutside]);
    function registerEventListeners() {
        isInputFieldFocused.current = true;
        document.addEventListener('mouseup', handleMouseDown);
    }
    function removeEventListeners(skipUpdate = false) {
        isInputFieldFocused.current = false;
        setShowCalendar(false);
        const updatedVal = dayjs_min.default(selectedDateRef.current).format(dateFormat);
        if (inputFieldRef.current)
            inputFieldRef.current.value = updatedVal;
        if (!error && !skipUpdate)
            onSelect(dayjs_min.default(updatedVal).toDate());
        document.removeEventListener('mouseup', handleMouseDown);
    }
    const handleSelect = (day) => {
        setSelectedDate(day);
        onSelect(day);
        setError(undefined);
        removeEventListeners(true);
    };
    function onDropdownOpen() {
        isDropdownOpenRef.current = true;
    }
    function onOpenChange(open) {
        if (!isDropdownOpenRef.current &&
            !(isInputFieldFocused.current && showCalendar)) {
            setShowCalendar(Boolean(open));
        }
        isDropdownOpenRef.current = false;
    }
    function handleInputFocus() {
        if (isInputFieldFocused.current)
            return;
        if (!showCalendar)
            setShowCalendar(true);
    }
    function handleInputBlur(event) {
        if (isInputFieldFocused.current) {
            const el = event.relatedTarget;
            if (el && isElementOutside(el))
                removeEventListeners();
        }
        else {
            registerEventListeners();
            setTimeout(() => inputFieldRef.current?.select());
        }
    }
    function handleKeyUp(event) {
        if (event.code === 'Enter' && inputFieldRef.current) {
            inputFieldRef.current.blur();
            removeEventListeners();
        }
    }
    function handleInputChange(event) {
        const { value } = event.target;
        const format = value.includes('/')
            ? 'DD/MM/YYYY'
            : value.includes('-')
                ? 'DD-MM-YYYY'
                : undefined;
        const date = dayjs_min.default(value, format);
        const isValidDate = date.isValid();
        const isAfter = calendarProps?.startMonth !== undefined
            ? dayjs_min.default(date).isSameOrAfter(calendarProps.startMonth)
            : true;
        const isBefore = calendarProps?.endMonth !== undefined
            ? dayjs_min.default(date).isSameOrBefore(calendarProps.endMonth)
            : true;
        const isValid = isValidDate && isAfter && isBefore && dayjs_min.default(date).isSameOrBefore(dayjs_min.default());
        if (isValid) {
            setSelectedDate(date.toDate());
            setError(undefined);
        }
        else {
            setError('Invalid date');
        }
    }
    const defaultTrigger = (jsxRuntime.jsx(inputField.InputField, { size: 'small', placeholder: 'Select date', error: error, className: calendar_module.default.datePickerInput, trailingIcon: showCalendarIcon ? jsxRuntime.jsx(reactIcons_esm.CalendarIcon, {}) : undefined, ...inputFieldProps, ref: inputFieldRef, value: formattedDate, onChange: handleInputChange, onFocus: handleInputFocus, onBlur: handleInputBlur, onKeyUp: handleKeyUp }));
    const trigger = typeof children === 'function' ? (children({ selectedDate: formattedDate })) : children ? (jsxRuntime.jsx("div", { children: children })) : (jsxRuntime.jsx("div", { children: defaultTrigger }));
    return (jsxRuntime.jsxs(popover.Popover, { open: showCalendar, onOpenChange: onOpenChange, children: [jsxRuntime.jsx(popover.Popover.Trigger, { asChild: true, children: trigger }), jsxRuntime.jsx(popover.Popover.Content, { ref: contentRef, ...popoverProps, className: index.cx(calendar_module.default.calendarPopover, popoverProps?.className), side: popoverProps?.side ?? 'top', children: jsxRuntime.jsx(calendar.Calendar, { required: true, ...calendarProps, timeZone: timeZone, onDropdownOpen: onDropdownOpen, mode: 'single', selected: selectedDate, month: selectedDate, onSelect: handleSelect, onMonthChange: setSelectedDate }) })] }));
}

exports.DatePicker = DatePicker;
//# sourceMappingURL=date-picker.cjs.map
