'use strict';

var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var react = require('@chakra-ui/react');
var enUS = require('date-fns/locale/en-US');
var zonedTimeToUtc = require('date-fns-tz/zonedTimeToUtc');
var DatePicker = require('react-datepicker');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var enUS__default = /*#__PURE__*/_interopDefaultLegacy(enUS);
var zonedTimeToUtc__default = /*#__PURE__*/_interopDefaultLegacy(zonedTimeToUtc);
var DatePicker__default = /*#__PURE__*/_interopDefaultLegacy(DatePicker);

var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
const DEFAULT_TIMEZONE = "Europe/Berlin";
const TIMEZONE_OPTIONS = ["Local", "UTC"];
const DateTimePicker = (_a) => {
  var _b = _a, {
    onChange,
    showTimezoneOptions = true,
    showButtons = true,
    showTimeSelect = true,
    showWeekNumbers = false,
    defaultDate,
    defaultTimezone = "Local"
  } = _b, props = __objRest(_b, [
    "onChange",
    "showTimezoneOptions",
    "showButtons",
    "showTimeSelect",
    "showWeekNumbers",
    "defaultDate",
    "defaultTimezone"
  ]);
  var _a2;
  const [startDate, setStartDate] = React.useState(defaultDate);
  const [timezone, setTimezone] = React.useState(defaultTimezone);
  const handleDateTimeChange = (date, event) => {
    if (date) {
      const utcDateAsLocal = timezone === "UTC" ? zonedTimeToUtc__default["default"](date, DEFAULT_TIMEZONE, { locale: enUS__default["default"] }) : date;
      const dateString = utcDateAsLocal.toUTCString();
      const formattedStartDate = timezone === "UTC" ? new Date(utcDateAsLocal) : date;
      setStartDate(formattedStartDate);
      onChange(date, dateString, timezone);
    }
  };
  return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: /* @__PURE__ */ jsxRuntime.jsx(
    DatePicker__default["default"],
    __spreadProps(__spreadValues({}, props), {
      showTimeSelect: true,
      selected: startDate,
      onChange: handleDateTimeChange,
      locale: enUS__default["default"],
      timeCaption: (_a2 = props.timeCaption) != null ? _a2 : `Time - ${timezone}`,
      fixedHeight: true,
      children: /* @__PURE__ */ jsxRuntime.jsx(react.Flex, { pl: 2, pb: 2, width: "100%", children: /* @__PURE__ */ jsxRuntime.jsxs(react.VStack, { align: "stretch", width: "100%", children: [
        showButtons && /* @__PURE__ */ jsxRuntime.jsxs(react.ButtonGroup, { display: "flex", px: 4, py: 1, children: [
          /* @__PURE__ */ jsxRuntime.jsx(
            react.Button,
            {
              size: "xs",
              variant: "link",
              textDecoration: "unset",
              onClick: () => setStartDate(new Date(Date.now())),
              children: "Today"
            }
          ),
          /* @__PURE__ */ jsxRuntime.jsx(react.Spacer, {}),
          /* @__PURE__ */ jsxRuntime.jsx(
            react.Button,
            {
              size: "xs",
              variant: "link",
              textDecoration: "unset",
              onClick: () => setStartDate(void 0),
              children: "Clear"
            }
          )
        ] }),
        showTimezoneOptions && /* @__PURE__ */ jsxRuntime.jsx(
          react.RadioGroup,
          {
            value: timezone,
            onChange: (nextTimezone) => {
              setTimezone(nextTimezone);
              setStartDate(
                nextTimezone === "UTC" && startDate ? zonedTimeToUtc__default["default"](startDate, DEFAULT_TIMEZONE) : startDate
              );
            },
            children: /* @__PURE__ */ jsxRuntime.jsx(react.Stack, { spacing: 4, direction: "row", children: TIMEZONE_OPTIONS.map((timezone2) => /* @__PURE__ */ jsxRuntime.jsx(react.Radio, { value: timezone2, colorScheme: "brand", children: timezone2 })) })
          }
        )
      ] }) })
    })
  ) });
};

module.exports = DateTimePicker;
//# sourceMappingURL=DateTimePicker.cjs.map
