"use strict";
// The require scope
var __webpack_require__ = {};
/************************************************************************/ // webpack/runtime/compat_get_default_export
(()=>{
    // getDefaultExport function for compatibility with non-ESM modules
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
// webpack/runtime/define_property_getters
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
// webpack/runtime/has_own_property
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
// webpack/runtime/make_namespace_object
(()=>{
    // define __esModule on exports
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
/************************************************************************/ var __webpack_exports__ = {};
// ESM COMPAT FLAG
__webpack_require__.r(__webpack_exports__);
// EXPORTS
__webpack_require__.d(__webpack_exports__, {
    default: ()=>/* binding */ DateTimeInput_rslib_entry_
});
const external_react_namespaceObject = require("react");
const external_date_fns_namespaceObject = require("date-fns");
const external_date_fns_tz_namespaceObject = require("date-fns-tz");
const react_namespaceObject = require("@chakra-ui/react");
const icons_namespaceObject = require("@chakra-ui/icons");
const RadioGroup_js_namespaceObject = require("../RadioGroup/RadioGroup.js");
var RadioGroup_js_default = /*#__PURE__*/ __webpack_require__.n(RadioGroup_js_namespaceObject);
const external_Calendar_js_namespaceObject = require("./Calendar.js");
var external_Calendar_js_default = /*#__PURE__*/ __webpack_require__.n(external_Calendar_js_namespaceObject);
const PopoverContent_js_namespaceObject = require("../../Popover/PopoverContent.js");
var PopoverContent_js_default = /*#__PURE__*/ __webpack_require__.n(PopoverContent_js_namespaceObject);
const Popover_js_namespaceObject = require("../../Popover/Popover.js");
var Popover_js_default = /*#__PURE__*/ __webpack_require__.n(Popover_js_namespaceObject);
const Button_js_namespaceObject = require("../../Button/Button.js");
var Button_js_default = /*#__PURE__*/ __webpack_require__.n(Button_js_namespaceObject);
/**
 * 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, react_namespaceObject.useDisclosure)();
    const containerRef = (0, external_react_namespaceObject.useRef)(null);
    const [mode, setMode] = (0, external_react_namespaceObject.useState)('local');
    const [unixVal, setUnixVal] = (0, external_react_namespaceObject.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, external_react_namespaceObject.useState)(value ?? Number(new Date()));
    const date = (0, external_react_namespaceObject.useMemo)(()=>value && isFinite(value) ? new Date(value) : new Date(), [
        value
    ]);
    const selectedTimeZone = 'local' === mode ? Intl.DateTimeFormat().resolvedOptions().timeZone : 'UTC';
    const dateTimeLocalized = (0, external_date_fns_tz_namespaceObject.utcToZonedTime)(date, selectedTimeZone);
    return(// https://github.com/chakra-ui/chakra-ui/issues/4733
    // @ts-ignore
    /*#__PURE__*/ external_react_namespaceObject.createElement(Popover_js_default(), {
        trigger: null,
        isOpen: isOpen,
        onClose: onClose,
        closeOnBlur: true
    }, /*#__PURE__*/ external_react_namespaceObject.createElement(react_namespaceObject.PopoverTrigger, null, /*#__PURE__*/ external_react_namespaceObject.createElement(react_namespaceObject.InputGroup, null, isOpen ? /*#__PURE__*/ external_react_namespaceObject.createElement(react_namespaceObject.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__*/ external_react_namespaceObject.createElement(react_namespaceObject.Input, {
        onClick: ()=>{
            setUnixVal('');
            if (value) setDefaultMonth(value); // set default month on open
            onOpen();
        },
        readOnly: true,
        value: value ? `${(0, external_date_fns_namespaceObject.format)(dateTimeLocalized, 'dd.MM.yyyy HH:mm:ss')} (${'local' === mode ? 'Local' : 'UTC'})` : ''
    }), /*#__PURE__*/ external_react_namespaceObject.createElement(react_namespaceObject.InputRightElement, null, /*#__PURE__*/ external_react_namespaceObject.createElement(icons_namespaceObject.CalendarIcon, {
        color: "gray.500"
    })))), /*#__PURE__*/ external_react_namespaceObject.createElement(PopoverContent_js_default(), {
        ref: containerRef
    }, /*#__PURE__*/ external_react_namespaceObject.createElement(react_namespaceObject.Flex, {
        gap: 2,
        flexDirection: "column",
        p: 2
    }, /*#__PURE__*/ external_react_namespaceObject.createElement(external_Calendar_js_default(), {
        key: `${defaultMonth}`,
        defaultMonth: new Date(defaultMonth),
        style: {
            maxWidth: '100%'
        },
        mode: "single",
        selected: date,
        onSelect: (e)=>{
            if (e) {
                const updatedDate = (0, external_date_fns_namespaceObject.setYear)((0, external_date_fns_namespaceObject.setMonth)((0, external_date_fns_namespaceObject.setDate)(dateTimeLocalized, e.getDate()), e.getMonth()), e.getFullYear());
                onChange(1000 * (0, external_date_fns_namespaceObject.getUnixTime)(updatedDate));
            }
        }
    }), /*#__PURE__*/ external_react_namespaceObject.createElement(react_namespaceObject.Flex, {
        gap: 2,
        alignItems: "center"
    }, /*#__PURE__*/ external_react_namespaceObject.createElement(react_namespaceObject.Input, {
        type: "time",
        size: "sm",
        value: (0, external_date_fns_namespaceObject.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, external_date_fns_tz_namespaceObject.utcToZonedTime)(date, selectedTimeZone);
            const updatedTime = (0, external_date_fns_namespaceObject.setMinutes)((0, external_date_fns_namespaceObject.setHours)(zonedTime, hours), minutes);
            const resultTime = (0, external_date_fns_tz_namespaceObject.zonedTimeToUtc)(updatedTime, selectedTimeZone);
            const updated = 1000 * (0, external_date_fns_namespaceObject.getUnixTime)(resultTime);
            onChange(updated);
        }
    }), /*#__PURE__*/ external_react_namespaceObject.createElement(Button_js_default(), {
        size: "sm",
        onClick: ()=>{
            const newVal = Number(new Date());
            setDefaultMonth(newVal);
            onChange(newVal);
        }
    }, "Now")), /*#__PURE__*/ external_react_namespaceObject.createElement(react_namespaceObject.Box, null, /*#__PURE__*/ external_react_namespaceObject.createElement(RadioGroup_js_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;
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
