import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
import { useState } from 'react';
import { Flex, VStack, ButtonGroup, Button, Spacer, RadioGroup, Stack, Radio } from '@chakra-ui/react';
import enUS from 'date-fns/locale/en-US';
import zonedTimeToUtc from 'date-fns-tz/zonedTimeToUtc';
import DatePicker from 'react-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] = useState(defaultDate);
  const [timezone, setTimezone] = useState(defaultTimezone);
  const handleDateTimeChange = (date, event) => {
    if (date) {
      const utcDateAsLocal = timezone === "UTC" ? zonedTimeToUtc(date, DEFAULT_TIMEZONE, { locale: enUS }) : date;
      const dateString = utcDateAsLocal.toUTCString();
      const formattedStartDate = timezone === "UTC" ? new Date(utcDateAsLocal) : date;
      setStartDate(formattedStartDate);
      onChange(date, dateString, timezone);
    }
  };
  return /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsx(
    DatePicker,
    __spreadProps(__spreadValues({}, props), {
      showTimeSelect: true,
      selected: startDate,
      onChange: handleDateTimeChange,
      locale: enUS,
      timeCaption: (_a2 = props.timeCaption) != null ? _a2 : `Time - ${timezone}`,
      fixedHeight: true,
      children: /* @__PURE__ */ jsx(Flex, { pl: 2, pb: 2, width: "100%", children: /* @__PURE__ */ jsxs(VStack, { align: "stretch", width: "100%", children: [
        showButtons && /* @__PURE__ */ jsxs(ButtonGroup, { display: "flex", px: 4, py: 1, children: [
          /* @__PURE__ */ jsx(
            Button,
            {
              size: "xs",
              variant: "link",
              textDecoration: "unset",
              onClick: () => setStartDate(new Date(Date.now())),
              children: "Today"
            }
          ),
          /* @__PURE__ */ jsx(Spacer, {}),
          /* @__PURE__ */ jsx(
            Button,
            {
              size: "xs",
              variant: "link",
              textDecoration: "unset",
              onClick: () => setStartDate(void 0),
              children: "Clear"
            }
          )
        ] }),
        showTimezoneOptions && /* @__PURE__ */ jsx(
          RadioGroup,
          {
            value: timezone,
            onChange: (nextTimezone) => {
              setTimezone(nextTimezone);
              setStartDate(
                nextTimezone === "UTC" && startDate ? zonedTimeToUtc(startDate, DEFAULT_TIMEZONE) : startDate
              );
            },
            children: /* @__PURE__ */ jsx(Stack, { spacing: 4, direction: "row", children: TIMEZONE_OPTIONS.map((timezone2) => /* @__PURE__ */ jsx(Radio, { value: timezone2, colorScheme: "brand", children: timezone2 })) })
          }
        )
      ] }) })
    })
  ) });
};

export { DateTimePicker as default };
//# sourceMappingURL=DateTimePicker.js.map
