import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE_date_fns__ from "date-fns";
import * as __WEBPACK_EXTERNAL_MODULE_date_fns_tz__ from "date-fns-tz";
import * as __WEBPACK_EXTERNAL_MODULE__chakra_ui_react__ from "@chakra-ui/react";
import * as __WEBPACK_EXTERNAL_MODULE__chakra_ui_icons__ from "@chakra-ui/icons";
import * as __WEBPACK_EXTERNAL_MODULE__RadioGroup_RadioGroup_mjs__ from "../RadioGroup/RadioGroup.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__Calendar_mjs__ from "./Calendar.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__Popover_PopoverContent_mjs__ from "../../Popover/PopoverContent.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__Popover_Popover_mjs__ from "../../Popover/Popover.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__Button_Button_mjs__ from "../../Button/Button.mjs";
/**
 * Checks if a given number is a Unix timestamp and whether it's in seconds or milliseconds.
 *
 * @param {number} timestamp - The number to check as a Unix timestamp.
 * @returns {boolean} - True if unix timestamp is in ms.
 */ function isUnixTimestampMs(timestamp) {
    const numberOfDigits = timestamp.toString().length;
    return numberOfDigits >= 12;
}
const DateTimeInput = ({ onChange, value })=>{
    const { isOpen, onOpen, onClose } = (0, __WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.useDisclosure)();
    const containerRef = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)(null);
    const [mode, setMode] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)('local');
    const [unixVal, setUnixVal] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)('');
    // determines what month to show when the calendar is open
    // this value is used when Now button is used or unix timestamp is provided
    const [defaultMonth, setDefaultMonth] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(value ?? Number(new Date()));
    const date = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>value && isFinite(value) ? new Date(value) : new Date(), [
        value
    ]);
    const selectedTimeZone = 'local' === mode ? Intl.DateTimeFormat().resolvedOptions().timeZone : 'UTC';
    const dateTimeLocalized = (0, __WEBPACK_EXTERNAL_MODULE_date_fns_tz__.utcToZonedTime)(date, selectedTimeZone);
    return(// https://github.com/chakra-ui/chakra-ui/issues/4733
    // @ts-ignore
    /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__.createElement(__WEBPACK_EXTERNAL_MODULE__Popover_Popover_mjs__["default"], {
        trigger: null,
        isOpen: isOpen,
        onClose: onClose,
        closeOnBlur: true
    }, /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.PopoverTrigger, null, /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.InputGroup, null, isOpen ? /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Input, {
        type: "number",
        placeholder: "Enter unix timestamp",
        onChange: (e)=>setUnixVal(e.target.value),
        value: unixVal,
        onKeyDown: (e)=>{
            if ('Enter' === e.key) {
                e.preventDefault();
                const possibleTimestamp = Number(unixVal);
                if (Number.isFinite(possibleTimestamp)) isUnixTimestampMs(possibleTimestamp) ? onChange(possibleTimestamp) : onChange(1000 * possibleTimestamp);
                onClose();
            }
        },
        onBlur: (event)=>{
            if (!containerRef.current?.contains(event.relatedTarget)) onClose();
        }
    }) : /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Input, {
        onClick: ()=>{
            setUnixVal('');
            if (value) setDefaultMonth(value); // set default month on open
            onOpen();
        },
        readOnly: true,
        value: value ? `${(0, __WEBPACK_EXTERNAL_MODULE_date_fns__.format)(dateTimeLocalized, 'dd.MM.yyyy HH:mm:ss')} (${'local' === mode ? 'Local' : 'UTC'})` : ''
    }), /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.InputRightElement, null, /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_icons__.CalendarIcon, {
        color: "gray.500"
    })))), /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__.createElement(__WEBPACK_EXTERNAL_MODULE__Popover_PopoverContent_mjs__["default"], {
        ref: containerRef
    }, /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Flex, {
        gap: 2,
        flexDirection: "column",
        p: 2
    }, /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__.createElement(__WEBPACK_EXTERNAL_MODULE__Calendar_mjs__["default"], {
        key: `${defaultMonth}`,
        defaultMonth: new Date(defaultMonth),
        style: {
            maxWidth: '100%'
        },
        mode: "single",
        selected: date,
        onSelect: (e)=>{
            if (e) {
                const updatedDate = (0, __WEBPACK_EXTERNAL_MODULE_date_fns__.setYear)((0, __WEBPACK_EXTERNAL_MODULE_date_fns__.setMonth)((0, __WEBPACK_EXTERNAL_MODULE_date_fns__.setDate)(dateTimeLocalized, e.getDate()), e.getMonth()), e.getFullYear());
                onChange(1000 * (0, __WEBPACK_EXTERNAL_MODULE_date_fns__.getUnixTime)(updatedDate));
            }
        }
    }), /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Flex, {
        gap: 2,
        alignItems: "center"
    }, /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Input, {
        type: "time",
        size: "sm",
        value: (0, __WEBPACK_EXTERNAL_MODULE_date_fns__.format)(dateTimeLocalized, 'HH:mm'),
        onChange: (e)=>{
            const [hours, minutes] = e.target.value.split(':').map(Number);
            // https://github.com/marnusw/date-fns-tz/issues/174
            const zonedTime = (0, __WEBPACK_EXTERNAL_MODULE_date_fns_tz__.utcToZonedTime)(date, selectedTimeZone);
            const updatedTime = (0, __WEBPACK_EXTERNAL_MODULE_date_fns__.setMinutes)((0, __WEBPACK_EXTERNAL_MODULE_date_fns__.setHours)(zonedTime, hours), minutes);
            const resultTime = (0, __WEBPACK_EXTERNAL_MODULE_date_fns_tz__.zonedTimeToUtc)(updatedTime, selectedTimeZone);
            const updated = 1000 * (0, __WEBPACK_EXTERNAL_MODULE_date_fns__.getUnixTime)(resultTime);
            onChange(updated);
        }
    }), /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__.createElement(__WEBPACK_EXTERNAL_MODULE__Button_Button_mjs__["default"], {
        size: "sm",
        onClick: ()=>{
            const newVal = Number(new Date());
            setDefaultMonth(newVal);
            onChange(newVal);
        }
    }, "Now")), /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__.createElement(__WEBPACK_EXTERNAL_MODULE__chakra_ui_react__.Box, null, /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__.createElement(__WEBPACK_EXTERNAL_MODULE__RadioGroup_RadioGroup_mjs__["default"], {
        size: "sm",
        name: "mode",
        value: mode,
        onChange: (value)=>{
            setMode(value);
        },
        options: [
            {
                label: 'Local',
                value: 'local'
            },
            {
                label: 'UTC',
                value: 'utc'
            }
        ]
    }))))));
};
/* ESM default export */ const DateTimeInput_rslib_entry_ = DateTimeInput;
export { DateTimeInput_rslib_entry_ as default };
