"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "DatePicker", {
    enumerable: true,
    get: function() {
        return DatePicker;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _popover = require("../popover");
const _utils = require("../utils");
const _calendarIcon = require("./sections/calendarIcon");
const _calendar = require("./calendar");
const _datefns = require("date-fns");
const _arrowIcon = require("./sections/arrowIcon");
const _i18n = require("../i18n");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const DatePicker = (props)=>{
    const { triggerClassName, className, initDate, trigger, onDateUpdate, numberOfMonths = 2, ...rest } = props;
    const [date, setDate] = _react.useState(initDate || {
        from: (0, _datefns.subDays)(new Date(), 30),
        to: new Date()
    });
    const t = (0, _i18n.useTranslation)();
    const [open, setOpen] = _react.useState(false);
    _react.useEffect(()=>{
        if (initDate) {
            setDate(initDate);
        }
    }, [
        initDate
    ]);
    const dateText = _react.useMemo(()=>{
        if (typeof date?.from !== 'undefined') {
            return `${(0, _datefns.format)(date.from, "yyyy/MM/dd")}${` - ${(0, _datefns.format)(date.to || date.from, "yyyy/MM/dd")}`}`;
        }
        return t("ui.calendar.selecet-date");
    }, [
        date,
        t
    ]);
    const triggerView = _react.useMemo(()=>{
        return trigger || /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
            id: "orderly-data-picker-trigger",
            className: (0, _utils.cn)("orderly-cursor-pointer orderly-flex orderly-gap-2 orderly-items-center orderly-h-[24px] orderly-text-3xs orderly-text-base-contrast-54 orderly-justify-start orderly-text-left orderly-px-2 orderly-py-2 orderly-bg-base-600 orderly-fill-base-contrast-54 hover:orderly-text-base-contrast-80", triggerClassName),
            children: [
                /*#__PURE__*/ (0, _jsxruntime.jsx)(_calendarIcon.CalendarIcon, {
                    fillOpacity: 1,
                    fill: "current",
                    className: "orderly-fill-base-contrast-54 hover:orderly-fill-white/80"
                }),
                dateText,
                /*#__PURE__*/ (0, _jsxruntime.jsx)(_arrowIcon.ArrowDown, {
                    fillOpacity: 1,
                    className: "orderly-fill-base-contrast-54 hover:orderly-fill-white/80"
                })
            ]
        });
    }, [
        trigger,
        triggerClassName,
        date
    ]);
    return /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
        className: (0, _utils.cn)("orderly-grid orderly-gap-2", className),
        children: /*#__PURE__*/ (0, _jsxruntime.jsxs)(_popover.Popover, {
            open: open,
            onOpenChange: (open)=>{
                if (!open) {}
                setOpen(open);
            },
            children: [
                /*#__PURE__*/ (0, _jsxruntime.jsx)(_popover.PopoverTrigger, {
                    asChild: true,
                    children: triggerView
                }),
                /*#__PURE__*/ (0, _jsxruntime.jsx)(_popover.PopoverContent, {
                    className: "orderly-w-auto orderly-p-0",
                    align: "start",
                    children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_calendar.Calendar, {
                        initialFocus: true,
                        mode: "range",
                        defaultMonth: date?.from,
                        // @ts-ignore
                        selected: date,
                        // @ts-ignore
                        onSelect: (date)=>{
                            onDateUpdate?.(date);
                            // @ts-ignore
                            setDate(date);
                        },
                        fromYear: 2024,
                        numberOfMonths: numberOfMonths,
                        ...rest
                    })
                })
            ]
        })
    });
};

//# sourceMappingURL=datePiker.js.map