"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const ahooks = require("ahooks");
const lodashEs = require("lodash-es");
const React = require("react");
;/* empty css                                                                                                                                                                                        */
;/* empty css                                                                                                                                                                                         */
;/* empty css                                                                                                                                                                                                 */
;/* empty css                                                                                                                                                                                            */
;/* empty css                                                                                                                                                                                            */
const dayjs = require("../../utils/dayjs.cjs");
const constant = require("./constant.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 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 ScrollArea = 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/ScrollArea/ScrollArea.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 createStyles = require("../../node_modules/.pnpm/@mantine_emotion@7.15.2_@emotion_cache@11.14.0_@emotion_react@11.14.0_@types_react@18.3.18_re_vflj3eioazr6wkrxpatinf66nq/node_modules/@mantine/emotion/esm/create-styles.cjs");
const useStyles = createStyles.createStyles(() => ({
  bold: {
    fontWeight: 600
  },
  cell: {
    textAlign: "center",
    cursor: "pointer",
    scrollSnapAlign: "start",
    userSelect: "none"
  },
  cellPlaceholder: {
    visibility: "hidden"
  }
}));
const getTimeRange = ({
  curr,
  type,
  start,
  end,
  utcOffset = dayjs.dayjs().utcOffset()
}) => {
  const map = {
    year: {
      min: dayjs.dayjs().year(),
      max: dayjs.dayjs().year() + 100
    },
    month: {
      min: 1,
      max: 12
    },
    day: {
      min: 1,
      max: curr.daysInMonth()
    },
    hour: {
      min: 0,
      max: 23
    },
    minute: {
      min: 0,
      max: 59
    },
    second: {
      min: 0,
      max: 59
    }
  };
  let { min, max } = map[type];
  const s1 = start ? dayjs.dayjs(start).utcOffset(utcOffset) : null;
  const s2 = end ? dayjs.dayjs(end).utcOffset(utcOffset) : null;
  switch (type) {
    case "year": {
      if (s1) {
        min = s1.year();
      }
      if (s2) {
        max = s2.year();
      }
      break;
    }
    case "month": {
      if (s1 && curr.isSame(s1, "year")) {
        min = s1.month() + 1;
      }
      if (s2 && curr.isSame(s2, "year")) {
        max = s2.month() + 1;
      }
      break;
    }
    case "day": {
      if (s1 && curr.isSame(s1, "month")) {
        min = s1.date();
      }
      if (s2 && curr.isSame(s2, "month")) {
        max = s2.date();
      }
      break;
    }
    case "hour": {
      if (s1 && curr.isSame(s1, "day")) {
        min = s1.hour();
      }
      if (s2 && curr.isSame(s2, "day")) {
        max = s2.hour();
      }
      break;
    }
    case "minute": {
      if (s1 && curr.isSame(s1, "hour")) {
        min = s1.minute();
      }
      if (s2 && curr.isSame(s2, "hour")) {
        max = s2.minute();
      }
      break;
    }
    case "second": {
      if (s1 && curr.isSame(s1, "minute")) {
        min = s1.second();
      }
      if (s2 && curr.isSame(s2, "minute")) {
        max = s2.second();
      }
      break;
    }
    default: {
      const exhaustiveCheck = "should not reach here";
      throw new Error(exhaustiveCheck);
    }
  }
  return { min, max };
};
const default2DigitRender = (val) => lodashEs.padStart(String(val), 2, "0");
function TimePickerScrollerColumn({
  min,
  max,
  curr,
  render = default2DigitRender,
  onChange,
  name,
  currentValueChangedBy
}) {
  const { classes, cx: clsx } = useStyles();
  const ref = React.useRef(null);
  const numbers = React.useMemo(() => lodashEs.range(min, max + 1), [min, max]);
  const timeoutRef = React.useRef();
  const isArtificialScroll = React.useRef(false);
  const [val, setVal] = useUncontrolled.useUncontrolled({
    value: curr,
    onChange
  });
  const adjustScrollTop = ahooks.useMemoizedFn((value) => {
    if (currentValueChangedBy === "timeScroller") return;
    const i = numbers.findIndex((i2) => i2 === value);
    if (i !== -1 && ref.current) {
      isArtificialScroll.current = true;
      ref.current.scrollTop = i * constant.CellHeight;
      window.requestAnimationFrame(() => {
        setTimeout(() => {
          isArtificialScroll.current = false;
        }, 100);
      });
    }
  });
  const onScroll = ahooks.useMemoizedFn((position) => {
    if (isArtificialScroll.current) return;
    if (currentValueChangedBy) return;
    clearTimeout(timeoutRef.current);
    const i = position.y / constant.CellHeight;
    if (i === Math.floor(i)) {
      const val2 = i >= numbers.length ? numbers.at(-1) : numbers[i];
      if (typeof val2 !== "undefined") {
        setVal(val2);
      }
    } else {
      timeoutRef.current = window.setTimeout(() => {
        const k = Math.round(i);
        const val2 = k >= numbers.length ? numbers.at(-1) : numbers[k];
        if (typeof val2 !== "undefined") {
          setVal(val2);
        }
      }, 300);
    }
  });
  const handleClickCell = ahooks.useMemoizedFn((e, i) => {
    var _a;
    e.stopPropagation();
    e.preventDefault();
    (_a = ref.current) == null ? void 0 : _a.scrollTo({ top: i * constant.CellHeight, behavior: "smooth" });
  });
  React.useEffect(() => {
    adjustScrollTop(val);
  }, [val]);
  return /* @__PURE__ */ jsxRuntime.jsxs(
    ScrollArea.ScrollArea,
    {
      viewportRef: ref,
      type: "never",
      styles: {
        viewport: {
          scrollSnapPointsY: `repeat(${constant.CellHeight}px)`,
          scrollSnapType: "y mandatory"
        }
      },
      onScrollPositionChange: onScroll,
      children: [
        numbers.map((i, index) => /* @__PURE__ */ jsxRuntime.jsx(
          "div",
          {
            className: clsx(classes.cell, i === curr && classes.bold),
            onClick: (e) => handleClickCell(e, index),
            style: constant.CellStyle,
            children: render ? render(i) : i
          },
          i
        )),
        lodashEs.range(6).map((i) => /* @__PURE__ */ jsxRuntime.jsx(Box.Box, { className: clsx(classes.cell, classes.cellPlaceholder), style: constant.CellStyle }, i))
      ]
    }
  );
}
function TimeScollerPicker({
  currentValue,
  currentValueChangedBy,
  start,
  end,
  utcOffset,
  onChange
}) {
  const options = React.useMemo(
    () => ({
      hour: getTimeRange({ curr: currentValue, start, end, utcOffset, type: "hour" }),
      minute: getTimeRange({ curr: currentValue, start, end, utcOffset, type: "minute" }),
      second: getTimeRange({ curr: currentValue, start, end, utcOffset, type: "second" })
    }),
    [currentValue, start, end, utcOffset]
  );
  const hourValue = React.useMemo(() => {
    return typeof utcOffset === "number" ? currentValue.utcOffset(utcOffset).hour() : currentValue.hour();
  }, [currentValue, utcOffset]);
  const minuteValue = React.useMemo(() => {
    return typeof utcOffset === "number" ? currentValue.utcOffset(utcOffset).minute() : currentValue.minute();
  }, [currentValue, utcOffset]);
  const secondValue = React.useMemo(() => {
    return typeof utcOffset === "number" ? currentValue.utcOffset(utcOffset).second() : currentValue.second();
  }, [currentValue, utcOffset]);
  const onHourChange = ahooks.useMemoizedFn((v) => {
    onChange == null ? void 0 : onChange([v, minuteValue, secondValue]);
  });
  const onMinuteChange = ahooks.useMemoizedFn((v) => {
    onChange == null ? void 0 : onChange([hourValue, v, secondValue]);
  });
  const onSecondChange = ahooks.useMemoizedFn((v) => {
    onChange == null ? void 0 : onChange([hourValue, minuteValue, v]);
  });
  return /* @__PURE__ */ jsxRuntime.jsxs(Flex.Flex, { mah: "100%", gap: 8, children: [
    /* @__PURE__ */ jsxRuntime.jsx(
      TimePickerScrollerColumn,
      {
        name: "hour",
        min: options.hour.min,
        max: options.hour.max,
        curr: hourValue,
        onChange: onHourChange,
        currentValueChangedBy
      }
    ),
    /* @__PURE__ */ jsxRuntime.jsx(
      TimePickerScrollerColumn,
      {
        name: "minute",
        min: options.minute.min,
        max: options.minute.max,
        curr: minuteValue,
        onChange: onMinuteChange,
        currentValueChangedBy
      }
    ),
    /* @__PURE__ */ jsxRuntime.jsx(
      TimePickerScrollerColumn,
      {
        name: "second",
        min: options.second.min,
        max: options.second.max,
        curr: secondValue,
        onChange: onSecondChange,
        currentValueChangedBy
      }
    )
  ] });
}
exports.TimeScollerPicker = TimeScollerPicker;
