"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const React = require("react");
const index$1 = require("../../icons/index.cjs");
;/* empty css                                                                                                                                                                                        */
;/* empty css                                                                                                                                                                                         */
;/* empty css                                                                                                                                                                                                 */
;/* empty css                                                                                                                                                                                            */
;/* empty css                                                                                                                                                                                            */
const index$2 = require("../../primitive/Typography/index.cjs");
const index = require("../../primitive/Button/index.cjs");
const AbsoluteTimeRangePicker = require("./AbsoluteTimeRangePicker.cjs");
const helpers = require("./helpers.cjs");
const useHover = require("../../node_modules/.pnpm/@mantine_hooks@7.15.2_react@18.3.1/node_modules/@mantine/hooks/esm/use-hover/use-hover.cjs");
const Menu = require("../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/components/Menu/Menu.cjs");
const Tooltip = require("../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/components/Tooltip/Tooltip.cjs");
const ActionIcon = require("../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/components/ActionIcon/ActionIcon.cjs");
const Group = require("../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/components/Group/Group.cjs");
const Box = require("../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/core/Box/Box.cjs");
const Text = require("../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/components/Text/Text.cjs");
const TimeRangePicker = ({
  value,
  minDateTime,
  maxDateTime,
  maxDuration,
  disableAbsoluteRanges = false,
  onChange,
  quickRanges = helpers.DEFAULT_QUICK_RANGES,
  loading,
  placeholder,
  badgePlaceholder,
  clearable,
  timezone,
  sx,
  dateInputFormat,
  datePickerProps,
  localization,
  relativeFormatter,
  absoluteFormatter
}) => {
  const [opened, setOpened] = React.useState(false);
  const [customMode, setCustomMode] = React.useState(false);
  const isEmptyValue = !value;
  const isRelativeRange = (value == null ? void 0 : value.type) === "relative";
  const isFuture = isRelativeRange && (value == null ? void 0 : value.isFuture);
  const { hovered, ref: targetRef } = useHover.useHover();
  const timeRangeValue = isEmptyValue ? void 0 : helpers.toTimeRangeValue(value);
  const duration = isEmptyValue ? void 0 : timeRangeValue[1] - timeRangeValue[0];
  const selectedRelativeItem = React.useMemo(() => {
    if (!value || value.type === "absolute") {
      return;
    }
    return quickRanges.find((it) => it === value.value);
  }, [quickRanges, value]);
  const formattedAbsDateTime = React.useMemo(() => {
    if (!timeRangeValue) {
      return "";
    }
    if (absoluteFormatter) {
      return absoluteFormatter(value);
    }
    return `${helpers.timeFormatter(timeRangeValue[0], timezone ?? null, "MMM D, YYYY HH:mm")} - ${helpers.timeFormatter(
      timeRangeValue[1],
      timezone ?? null,
      "MMM D, YYYY HH:mm"
    )}`;
  }, [value]);
  const formattedRelativeDateTime = React.useMemo(() => {
    if (!value || value.type === "absolute") {
      return "";
    }
    if (relativeFormatter) {
      return relativeFormatter(value);
    }
    return `${isFuture ? "Next" : "Past"} ${helpers.formatDuration(duration)}`;
  }, [value]);
  return /* @__PURE__ */ jsxRuntime.jsxs(
    Menu.Menu,
    {
      shadow: "md",
      width: customMode ? "auto" : disableAbsoluteRanges ? 200 : 280,
      position: "bottom-end",
      opened,
      onOpen: () => {
        setOpened(true);
        setCustomMode(false);
      },
      onClose: () => setOpened(false),
      children: [
        /* @__PURE__ */ jsxRuntime.jsx(Menu.Menu.Target, { children: /* @__PURE__ */ jsxRuntime.jsx(Tooltip.Tooltip, { label: formattedAbsDateTime, disabled: isRelativeRange || isEmptyValue, withArrow: true, children: /* @__PURE__ */ jsxRuntime.jsx(
          index.Button,
          {
            ref: targetRef,
            variant: "default",
            bg: "carbon.0",
            styles: (theme) => ({
              root: {
                paddingLeft: "12px",
                paddingRight: "12px",
                borderColor: opened ? theme.colors.carbon[9] : theme.colors.carbon[4],
                "&:hover": {
                  backgroundColor: theme.colors.carbon[0],
                  borderColor: opened ? theme.colors.carbon[9] : theme.colors.carbon[5]
                },
                "&:active": { transform: "none" }
              },
              inner: {
                width: "100%"
              },
              label: {
                display: "flex",
                justifyContent: "space-between",
                width: "100%",
                fontWeight: 400
              }
            }),
            w: disableAbsoluteRanges ? 200 : 280,
            sx,
            loading,
            rightSection: clearable && !!value && hovered ? /* @__PURE__ */ jsxRuntime.jsx(
              ActionIcon.ActionIcon,
              {
                size: "xs",
                onClick: (e) => {
                  e.stopPropagation();
                  onChange == null ? void 0 : onChange();
                },
                children: /* @__PURE__ */ jsxRuntime.jsx(index$1.IconX, { size: 16, color: "var(--mantine-color-carbon-7)!important" })
              }
            ) : /* @__PURE__ */ jsxRuntime.jsx(index$1.IconChevronSelectorVertical, { size: 16, color: "var(--mantine-color-carbon-7)!important" }),
            children: /* @__PURE__ */ jsxRuntime.jsxs(Group.Group, { w: "100%", gap: 0, children: [
              /* @__PURE__ */ jsxRuntime.jsx(Box.Box, { sx: { flex: "none" }, children: /* @__PURE__ */ jsxRuntime.jsx(DurationBadge, { children: isEmptyValue ? badgePlaceholder || "All" : helpers.formatDuration(duration, true) }) }),
              /* @__PURE__ */ jsxRuntime.jsx(
                Text.Text,
                {
                  px: 8,
                  sx: {
                    flex: "1 1",
                    overflow: "hidden",
                    whiteSpace: "nowrap",
                    textOverflow: "ellipsis",
                    textAlign: "left"
                  },
                  c: isEmptyValue ? "dimmed" : "carbon.8",
                  children: isEmptyValue ? placeholder || "Time Range" : isRelativeRange ? formattedRelativeDateTime : formattedAbsDateTime
                }
              )
            ] })
          }
        ) }) }),
        /* @__PURE__ */ jsxRuntime.jsx(Menu.Menu.Dropdown, { children: customMode ? /* @__PURE__ */ jsxRuntime.jsx(
          AbsoluteTimeRangePicker.default,
          {
            value: timeRangeValue,
            minDateTime: minDateTime == null ? void 0 : minDateTime(),
            maxDateTime: maxDateTime == null ? void 0 : maxDateTime(),
            maxDuration,
            onChange: (v) => {
              onChange == null ? void 0 : onChange(v);
              setOpened(false);
            },
            onCancel: () => setOpened(false),
            onReturnClick: () => setCustomMode(false),
            dateInputFormat,
            datePickerProps,
            localization
          }
        ) : /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
          !disableAbsoluteRanges && /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
            /* @__PURE__ */ jsxRuntime.jsx(
              Menu.Menu.Item,
              {
                rightSection: /* @__PURE__ */ jsxRuntime.jsx(index$1.IconChevronRight, { size: 16 }),
                closeMenuOnClick: false,
                onClick: () => setCustomMode(true),
                children: /* @__PURE__ */ jsxRuntime.jsx(index$2.Typography, { variant: "body-lg", children: (localization == null ? void 0 : localization.entry) || "Custom" })
              }
            ),
            /* @__PURE__ */ jsxRuntime.jsx(Menu.Menu.Divider, {})
          ] }),
          /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: quickRanges.map((item) => {
            const isNumber = typeof item === "number";
            const seconds = isNumber ? item : item.value;
            const isFuture2 = isNumber ? false : item.isFuture;
            let label = isNumber ? `Past ${helpers.formatDuration(seconds)}` : item.label;
            if (!label) {
              label = isFuture2 ? `Next ${helpers.formatDuration(seconds)}` : `Past ${helpers.formatDuration(seconds)}`;
            }
            return /* @__PURE__ */ jsxRuntime.jsx(
              Menu.Menu.Item,
              {
                sx: (theme) => ({
                  background: seconds === selectedRelativeItem ? theme.colors.carbon[3] : ""
                }),
                onClick: () => onChange == null ? void 0 : onChange({ type: "relative", value: seconds, isFuture: isFuture2 }),
                children: /* @__PURE__ */ jsxRuntime.jsx(Text.Text, { children: label })
              },
              seconds
            );
          }) })
        ] }) })
      ]
    }
  );
};
const DurationBadge = ({ children }) => {
  return /* @__PURE__ */ jsxRuntime.jsx(
    Box.Box,
    {
      display: "inline-block",
      w: 35,
      py: 3,
      bg: "carbon.3",
      c: "carbon.8",
      fz: 10,
      lh: "14px",
      ta: "center",
      sx: { borderRadius: 8 },
      children
    }
  );
};
exports.DEFAULT_QUICK_RANGES = helpers.DEFAULT_QUICK_RANGES;
exports.DEFAULT_TIME_FORMAT = helpers.DEFAULT_TIME_FORMAT;
exports.DEFAULT_TIME_FORMAT_WITH_TIMEZONE = helpers.DEFAULT_TIME_FORMAT_WITH_TIMEZONE;
exports.DEFAULT_TIME_RANGE = helpers.DEFAULT_TIME_RANGE;
exports.addOffsetUTC = helpers.addOffsetUTC;
exports.formatDuration = helpers.formatDuration;
exports.fromTimeRangeValue = helpers.fromTimeRangeValue;
exports.getUTCString = helpers.getUTCString;
exports.timeFormatter = helpers.timeFormatter;
exports.toTimeRangeValue = helpers.toTimeRangeValue;
exports.toURLTimeRange = helpers.toURLTimeRange;
exports.urlToTimeRange = helpers.urlToTimeRange;
exports.urlToTimeRangeValue = helpers.urlToTimeRangeValue;
exports.TimeRangePicker = TimeRangePicker;
