"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const ahooks = require("ahooks");
const React = require("react");
const index = require("../../icons/index.cjs");
;/* empty css                                                                                                                                                                                        */
;/* empty css                                                                                                                                                                                         */
;/* empty css                                                                                                                                                                                                 */
;/* empty css                                                                                                                                                                                            */
;/* empty css                                                                                                                                                                                            */
const index$1 = require("../../primitive/Typography/index.cjs");
const dayjs = require("../../utils/dayjs.cjs");
const helpers = require("../TimeRangePicker/helpers.cjs");
const TimeScollerPicker = require("./TimeScollerPicker.cjs");
const useDisclosure = require("../../node_modules/.pnpm/@mantine_hooks@7.15.2_react@18.3.1/node_modules/@mantine/hooks/esm/use-disclosure/use-disclosure.cjs");
const useUncontrolled = require("../../node_modules/.pnpm/@mantine_hooks@7.15.2_react@18.3.1/node_modules/@mantine/hooks/esm/use-uncontrolled/use-uncontrolled.cjs");
const Popover = 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/Popover/Popover.cjs");
const TextInput = require("../../primitive/TextInput/TextInput.cjs");
const Loader = 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/Loader/Loader.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 Stack = 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/Stack/Stack.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 DatePicker = require("../../node_modules/.pnpm/@mantine_dates@7.15.2_@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hoo_pt67admfm6wboa3vchveewq6zq/node_modules/@mantine/dates/esm/components/DatePicker/DatePicker.cjs");
const Divider = 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/Divider/Divider.cjs");
const TimeInput = require("../../node_modules/.pnpm/@mantine_dates@7.15.2_@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hoo_pt67admfm6wboa3vchveewq6zq/node_modules/@mantine/dates/esm/components/TimeInput/TimeInput.cjs");
const Flex = 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/Flex/Flex.cjs");
const DateTimePicker = ({
  placeholder = "Select time",
  format = helpers.DEFAULT_TIME_FORMAT,
  defaultValue,
  value,
  startDate = dayjs.dayjs().subtract(10, "year").toDate(),
  endDate = dayjs.dayjs().add(10, "year").toDate(),
  onChange,
  disable = false,
  utcOffset = dayjs.dayjs().utcOffset(),
  withinPortal = true,
  sx,
  loading = false,
  size
}) => {
  const [opened, { close, open }] = useDisclosure.useDisclosure(false);
  const [currentValue, setCurrentValue] = useUncontrolled.useUncontrolled({
    value: value ? dayjs.dayjs(value) : void 0,
    defaultValue: defaultValue ? dayjs.dayjs(defaultValue) : dayjs.dayjs(),
    onChange: (v) => {
      onChange == null ? void 0 : onChange(v.toDate());
    }
  });
  const [currentValueChangedBy, setCurrentValueChangedBy] = React.useState(null);
  const updateCurrentValue = ahooks.useMemoizedFn((val, from) => {
    let next = val;
    if (!next.isValid()) {
      return;
    }
    if ((currentValue == null ? void 0 : currentValue.unix()) === next.unix()) {
      return;
    }
    if (startDate && next.isBefore(startDate)) {
      next = dayjs.dayjs(startDate);
    } else if (endDate && next.isAfter(endDate)) {
      next = dayjs.dayjs(endDate);
    }
    setCurrentValue(next);
    setCurrentValueChangedBy(from);
    setTimeout(() => {
      setCurrentValueChangedBy(null);
    }, 20);
  });
  const inputStr = currentValue.format(format);
  const utcStr = React.useMemo(() => {
    const h = Math.floor(utcOffset / 60);
    const m = utcOffset % 60;
    return `UTC${h >= 0 ? "+" : "-"}${Math.abs(h)}:${m < 10 ? "0" : ""}${m}`;
  }, [utcOffset]);
  const calendarChange = ahooks.useMemoizedFn((v) => {
    let next = currentValue;
    next = next.utcOffset(utcOffset).year(v.getFullYear()).month(v.getMonth()).date(v.getDate());
    updateCurrentValue(next, "calendar");
  });
  const timeInputChange = ahooks.useMemoizedFn((e) => {
    const originVal = e.currentTarget.value;
    const v = dayjs.dayjs(originVal, "HH:mm:ss").toDate();
    let next = currentValue;
    next = next.utcOffset(utcOffset).hour(v.getHours()).minute(v.getMinutes()).second(v.getSeconds());
    updateCurrentValue(next, "timeInput");
  });
  const timeScrollPickerChange = ahooks.useMemoizedFn((v) => {
    const [h, m, s] = v;
    let next = currentValue;
    next = next.utcOffset(utcOffset).hour(h).minute(m).second(s);
    updateCurrentValue(next, "timeScroller");
  });
  return /* @__PURE__ */ jsxRuntime.jsxs(
    Popover.Popover,
    {
      position: "bottom-end",
      opened,
      withinPortal,
      shadow: "md",
      closeOnClickOutside: true,
      onChange: (opened2) => {
        if (opened2) {
          open();
        } else {
          close();
        }
      },
      children: [
        /* @__PURE__ */ jsxRuntime.jsx(Popover.Popover.Target, { children: /* @__PURE__ */ jsxRuntime.jsx(
          TextInput.TextInput,
          {
            readOnly: true,
            disabled: disable,
            placeholder,
            value: inputStr,
            rightSection: loading ? /* @__PURE__ */ jsxRuntime.jsx(Loader.Loader, { size: "xs" }) : /* @__PURE__ */ jsxRuntime.jsx(Box.Box, { component: index.IconClock, size: 18, c: "carbon.6" }),
            sx,
            size,
            onClick: open
          }
        ) }),
        /* @__PURE__ */ jsxRuntime.jsx(Popover.Popover.Dropdown, { children: /* @__PURE__ */ jsxRuntime.jsxs(Stack.Stack, { children: [
          /* @__PURE__ */ jsxRuntime.jsxs(Group.Group, { align: "flex-start", children: [
            /* @__PURE__ */ jsxRuntime.jsx(
              DatePicker.DatePicker,
              {
                minDate: startDate,
                maxDate: endDate,
                value: currentValue.toDate(),
                onChange: calendarChange,
                withCellSpacing: false,
                size: "md"
              }
            ),
            /* @__PURE__ */ jsxRuntime.jsx(Divider.Divider, { orientation: "vertical", mt: -12, mb: -16 }),
            /* @__PURE__ */ jsxRuntime.jsxs(Stack.Stack, { justify: "flex-start", children: [
              /* @__PURE__ */ jsxRuntime.jsx(
                TimeInput.TimeInput,
                {
                  withSeconds: true,
                  value: currentValue.format("HH:mm:ss"),
                  onChange: timeInputChange,
                  size: "sm",
                  w: 112,
                  styles: {
                    input: {
                      height: 30
                    }
                  }
                }
              ),
              /* @__PURE__ */ jsxRuntime.jsxs(
                Box.Box,
                {
                  pos: "relative",
                  fz: 14,
                  fw: 400,
                  h: 224,
                  sx: (theme) => ({ color: theme.colors.carbon[8], overflow: "hidden", zIndex: 9999 }),
                  children: [
                    /* @__PURE__ */ jsxRuntime.jsxs(Flex.Flex, { mah: "100%", gap: 8, children: [
                      /* @__PURE__ */ jsxRuntime.jsx(
                        Box.Box,
                        {
                          bg: "carbon.3",
                          pos: "absolute",
                          top: 0,
                          left: 0,
                          h: 32,
                          w: 32,
                          sx: (theme) => ({ zIndex: -1, borderRadius: theme.defaultRadius, pointerEvents: "none" })
                        }
                      ),
                      /* @__PURE__ */ jsxRuntime.jsx(
                        Box.Box,
                        {
                          bg: "carbon.3",
                          pos: "absolute",
                          top: 0,
                          left: 40,
                          h: 32,
                          w: 32,
                          sx: (theme) => ({ zIndex: -1, borderRadius: theme.defaultRadius, pointerEvents: "none" })
                        }
                      ),
                      /* @__PURE__ */ jsxRuntime.jsx(
                        Box.Box,
                        {
                          bg: "carbon.3",
                          pos: "absolute",
                          top: 0,
                          left: 80,
                          h: 32,
                          w: 32,
                          sx: (theme) => ({ zIndex: -1, borderRadius: theme.defaultRadius, pointerEvents: "none" })
                        }
                      )
                    ] }),
                    /* @__PURE__ */ jsxRuntime.jsx(
                      TimeScollerPicker.TimeScollerPicker,
                      {
                        currentValue,
                        currentValueChangedBy,
                        onChange: timeScrollPickerChange,
                        start: startDate,
                        end: endDate,
                        utcOffset
                      }
                    )
                  ]
                }
              )
            ] })
          ] }),
          /* @__PURE__ */ jsxRuntime.jsx(Divider.Divider, { mx: -16 }),
          /* @__PURE__ */ jsxRuntime.jsx(Group.Group, { children: /* @__PURE__ */ jsxRuntime.jsxs(index$1.Typography, { size: "sm", children: [
            "Use",
            " ",
            /* @__PURE__ */ jsxRuntime.jsx(index$1.Typography, { fw: 600, component: "span", children: utcStr }),
            " ",
            "from your local time zone"
          ] }) })
        ] }) })
      ]
    }
  );
};
const TimePicker = ({
  value,
  onChange,
  defaultValue,
  disable = false,
  sx,
  size,
  minTime,
  maxTime,
  withinPortal,
  withArrow,
  position,
  shadow,
  ...rest
}) => {
  const [currentValue, setCurrentValue] = useUncontrolled.useUncontrolled({
    value: value ? dayjs.dayjs(value, "HH:mm:ss") : void 0,
    defaultValue: defaultValue ? dayjs.dayjs(defaultValue, "HH:mm:ss") : dayjs.dayjs(),
    onChange: (v) => {
      onChange == null ? void 0 : onChange(v.format("HH:mm:ss"));
    }
  });
  const [currentValueChangedBy, setCurrentValueChangedBy] = React.useState(null);
  const startDate = React.useMemo(() => dayjs.dayjs(minTime, "HH:mm:ss"), [minTime]);
  const endDate = React.useMemo(() => dayjs.dayjs(maxTime, "HH:mm:ss"), [maxTime]);
  const updateCurrentValue = ahooks.useMemoizedFn((val, from) => {
    let next = val;
    if (!next.isValid()) {
      return;
    }
    if ((currentValue == null ? void 0 : currentValue.unix()) === next.unix()) {
      return;
    }
    if (startDate && next.isBefore(startDate)) {
      next = dayjs.dayjs(startDate);
    } else if (endDate && next.isAfter(endDate)) {
      next = dayjs.dayjs(endDate);
    }
    setCurrentValue(next);
    setCurrentValueChangedBy(from);
    setTimeout(() => {
      setCurrentValueChangedBy(null);
    }, 20);
  });
  const timeInputChange = ahooks.useMemoizedFn((e) => {
    const originVal = e.currentTarget.value;
    const v = dayjs.dayjs(originVal, "HH:mm:ss").toDate();
    let next = currentValue;
    next = next.hour(v.getHours()).minute(v.getMinutes()).second(v.getSeconds());
    updateCurrentValue(next, "timeInput");
  });
  const timeScrollPickerChange = ahooks.useMemoizedFn((v) => {
    const [h, m, s] = v;
    let next = currentValue;
    next = next.hour(h).minute(m).second(s);
    updateCurrentValue(next, "timeScroller");
  });
  return /* @__PURE__ */ jsxRuntime.jsxs(Popover.Popover, { withinPortal, withArrow, position, shadow, children: [
    /* @__PURE__ */ jsxRuntime.jsx(Popover.Popover.Target, { children: /* @__PURE__ */ jsxRuntime.jsx(
      TimeInput.TimeInput,
      {
        ...rest,
        disabled: disable,
        withSeconds: true,
        value: currentValue.format("HH:mm:ss"),
        onChange: timeInputChange,
        size,
        sx,
        rightSection: /* @__PURE__ */ jsxRuntime.jsx(index.IconClock, { size: 16, c: "carbon.7" })
      }
    ) }),
    /* @__PURE__ */ jsxRuntime.jsx(Popover.Popover.Dropdown, { children: /* @__PURE__ */ jsxRuntime.jsxs(
      Box.Box,
      {
        pos: "relative",
        fz: 14,
        fw: 400,
        h: 224,
        sx: (theme) => ({ color: theme.colors.carbon[8], overflow: "hidden", zIndex: 9999 }),
        children: [
          /* @__PURE__ */ jsxRuntime.jsxs(Flex.Flex, { mah: "100%", gap: 8, children: [
            /* @__PURE__ */ jsxRuntime.jsx(
              Box.Box,
              {
                bg: "carbon.3",
                pos: "absolute",
                top: 0,
                left: 0,
                h: 32,
                w: 32,
                sx: (theme) => ({ zIndex: -1, borderRadius: theme.defaultRadius, pointerEvents: "none" })
              }
            ),
            /* @__PURE__ */ jsxRuntime.jsx(
              Box.Box,
              {
                bg: "carbon.3",
                pos: "absolute",
                top: 0,
                left: 40,
                h: 32,
                w: 32,
                sx: (theme) => ({ zIndex: -1, borderRadius: theme.defaultRadius, pointerEvents: "none" })
              }
            ),
            /* @__PURE__ */ jsxRuntime.jsx(
              Box.Box,
              {
                bg: "carbon.3",
                pos: "absolute",
                top: 0,
                left: 80,
                h: 32,
                w: 32,
                sx: (theme) => ({ zIndex: -1, borderRadius: theme.defaultRadius, pointerEvents: "none" })
              }
            )
          ] }),
          /* @__PURE__ */ jsxRuntime.jsx(
            TimeScollerPicker.TimeScollerPicker,
            {
              currentValue,
              currentValueChangedBy,
              onChange: timeScrollPickerChange,
              start: startDate.toDate(),
              end: endDate.toDate()
            }
          )
        ]
      }
    ) })
  ] });
};
exports.DateTimePicker = DateTimePicker;
exports.TimePicker = TimePicker;
