import { useRef, useEffect, useCallback } from "react";
import debounce_1 from "../../../../../../lodash@4.17.21/node_modules/lodash/debounce.js";
import { scrollTo } from "./util.js";
import cs from "../_util/classNames.js";
import usePrevious from "../_util/hooks/usePrevious.js";
import { jsx } from "../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js";
function TimeColumn(props) {
  var prefixCls = props.prefixCls, list = props.list, value = props.value, onHandleSelect = props.onHandleSelect, unit = props.unit, popupVisible = props.popupVisible, scrollSticky = props.scrollSticky;
  var lis = useRef(/* @__PURE__ */ new Map());
  var wrapper = useRef();
  var ul = useRef();
  var listItemHeight = useRef(0);
  var prevPopupVisible = usePrevious(popupVisible);
  var prevScrollTop = useRef(wrapper.current && wrapper.current.scrollTop);
  useEffect(function() {
    var li = lis.current.get(value);
    if (li && popupVisible && prevPopupVisible) {
      scrollTo(wrapper.current, li.offsetTop, 150);
      prevScrollTop.current = li.offsetTop;
    }
  }, [value]);
  useEffect(function() {
    if (popupVisible && popupVisible !== prevPopupVisible) {
      var li = lis.current.get(value);
      if (li) {
        scrollTo(wrapper.current, li.offsetTop, 0);
        prevScrollTop.current = li.offsetTop;
      }
    }
  }, [popupVisible, prevPopupVisible]);
  useEffect(function() {
    if (list.length <= 1) {
      return;
    }
    listItemHeight.current = (ul.current.clientHeight - wrapper.current.clientHeight) / (list.length - 1);
  }, [list.length]);
  var onScrollList = useCallback(debounce_1(function() {
    var mathFunc = wrapper.current.scrollTop - prevScrollTop.current > 0 ? Math.ceil : Math.floor;
    var index = mathFunc(wrapper.current.scrollTop / listItemHeight.current);
    if (index !== value && list[index] && !list[index].disabled) {
      onHandleSelect(list[index].value, unit);
    }
  }, 100), [onHandleSelect]);
  return /* @__PURE__ */ jsx("div", {
    className: cs(prefixCls + "-list"),
    ref: wrapper,
    onWheel: scrollSticky && onScrollList,
    children: /* @__PURE__ */ jsx("ul", {
      ref: ul,
      children: list.map(function(item) {
        var _a;
        return /* @__PURE__ */ jsx("li", {
          className: cs(prefixCls + "-cell", (_a = {}, _a[prefixCls + "-cell-disabled"] = item.disabled, _a[prefixCls + "-cell-selected"] = item.selected, _a)),
          onClick: function() {
            return !item.disabled && onHandleSelect(item.value, unit);
          },
          ref: function(element) {
            lis.current.set(item.value, element);
          },
          children: /* @__PURE__ */ jsx("div", {
            className: prefixCls + "-cell-inner",
            children: item.label
          })
        }, item.value);
      })
    })
  });
}
export {
  TimeColumn as default
};
//# sourceMappingURL=time-column.js.map
