function _extends() {
  _extends = Object.assign ? Object.assign.bind() : function(target) {
    for (var i = 1; i < arguments.length; i++) {
      var source = arguments[i];
      for (var key in source) {
        if (Object.prototype.hasOwnProperty.call(source, key)) {
          target[key] = source[key];
        }
      }
    }
    return target;
  };
  return _extends.apply(this, arguments);
}
import React, { forwardRef, memo, useCallback, useContext, useEffect, useImperativeHandle, useMemo, useRef } from "react";
import { useSpring, animated } from "@react-spring/web";
import { useDrag } from "@use-gesture/react";
import classNames from "classnames";
import { useSetState, useUpdateEffect } from "../hooks";
import { isObject, range } from "../utils";
import { deepClone } from "../utils/deep-clone";
import { rubberbandIfOutOfBounds } from "../utils/rubberband";
import ConfigProviderContext from "../config-provider/ConfigProviderContext";
function isOptionDisabled(option) {
  return isObject(option) && option.disabled;
}
const DEFAULT_DURATION = 200;
const MOMENTUM_LIMIT_DISTANCE = 15;
const PickerColumn = /* @__PURE__ */ memo(/* @__PURE__ */ forwardRef((props, ref) => {
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace("picker-column", prefixCls);
  const {
    itemHeight,
    visibleItemCount,
    defaultIndex,
    initialOptions
  } = props;
  const root = useRef(null);
  const wrapper = useRef(null);
  const indexRef = useRef(defaultIndex);
  const optionsRef = useRef(deepClone(initialOptions));
  const [state, updateState] = useSetState({
    offset: 0,
    duration: 0,
    options: deepClone(initialOptions)
  });
  const count = useMemo(() => optionsRef.current.length, [optionsRef.current.length]);
  const baseOffset = useMemo(() => {
    return itemHeight * (+visibleItemCount - 1) / 2;
  }, [itemHeight, visibleItemCount]);
  const draggingRef = useRef(false);
  const [{
    y
  }, api] = useSpring(() => ({
    from: {
      y: defaultIndex * -itemHeight + baseOffset
    },
    config: {
      tension: 400,
      mass: 0.8
    }
  }));
  const adjustIndex = (index) => {
    index = range(index, 0, count);
    for (let i = index; i < count; i += 1) {
      if (!isOptionDisabled(optionsRef.current[i]))
        return i;
    }
    return null;
  };
  const setIndex = (index, emitChange) => {
    index = adjustIndex(index) || 0;
    const offset = index * -itemHeight;
    api.start({
      y: offset + baseOffset
    });
    if (index !== indexRef.current) {
      indexRef.current = index;
      if (emitChange && props.onChange) {
        setTimeout(() => {
          props.onChange(index);
        }, 0);
      }
    }
    updateState({
      offset
    });
  };
  const setOptions = (options) => {
    if (JSON.stringify(options) !== JSON.stringify(optionsRef.current)) {
      optionsRef.current = options;
      setIndex(props.defaultIndex);
    }
  };
  const onClickItem = (index) => {
    if (draggingRef.current || props.readonly) {
      return;
    }
    updateState({
      duration: DEFAULT_DURATION
    });
    setIndex(index, true);
  };
  function isOption(option) {
    return isObject(option) && props.textKey in option;
  }
  const getOptionText = (option) => {
    if (isOption(option)) {
      return option[props.textKey];
    }
    return option;
  };
  const getIndexByOffset = (offset) => range(Math.round(-offset / props.itemHeight), 0, count - 1);
  const bind = useDrag((dragState) => {
    if (props.readonly) {
      return;
    }
    draggingRef.current = true;
    if (dragState.tap) {
      updateState({
        duration: 0,
        offset: dragState.offset[1]
      });
    } else if (dragState.last) {
      const allowMomentum = Math.abs(dragState.velocity[1] * dragState.direction[1] * 50) > MOMENTUM_LIMIT_DISTANCE;
      const position = dragState.offset[1] + dragState.velocity[1] * dragState.direction[1] * 50 - baseOffset;
      const targetIndex = getIndexByOffset(position);
      updateState({
        duration: allowMomentum ? +props.swipeDuration : DEFAULT_DURATION,
        offset: position + baseOffset
      });
      setIndex(targetIndex, true);
    } else {
      const position = dragState.offset[1];
      const max = baseOffset;
      const min = -(count * itemHeight) + baseOffset;
      const offset = rubberbandIfOutOfBounds(position, min, max, itemHeight * 50, 0.2);
      api.start({
        y: offset
      });
    }
  }, {
    axis: "y",
    from: () => [0, y.get()],
    filterTaps: true,
    pointer: {
      touch: true
    }
  });
  const renderOptions = useMemo(() => {
    const optionStyle = {
      height: `${props.itemHeight}px`
    };
    return optionsRef.current.map((option, index) => {
      const text = getOptionText(option);
      const disabled = isOptionDisabled(option);
      const data = {
        role: "button",
        key: index,
        style: optionStyle,
        tabIndex: disabled ? -1 : 0,
        className: classNames(bem("item", {
          disabled,
          selected: index === indexRef.current
        })),
        onClick: () => {
          draggingRef.current = false;
          onClickItem(index);
        }
      };
      const childData = {
        className: "rc-ellipsis",
        children: text
      };
      return /* @__PURE__ */ React.createElement("li", data, props.optionRender ? props.optionRender(option) : /* @__PURE__ */ React.createElement("div", childData));
    });
  }, [optionsRef.current, indexRef.current, onClickItem, props.optionRender]);
  const setValue = (value) => {
    const options = optionsRef.current;
    for (let i = 0; i < options.length; i += 1) {
      if (getOptionText(options[i]) === value) {
        return setIndex(i);
      }
    }
    return null;
  };
  const getValue = useCallback(() => optionsRef.current[indexRef.current], [indexRef.current, optionsRef.current]);
  useEffect(() => {
    setIndex(defaultIndex);
  }, [defaultIndex]);
  useUpdateEffect(() => {
    setOptions(deepClone(initialOptions));
  }, [initialOptions]);
  useImperativeHandle(ref, () => ({
    state,
    getIndex: () => indexRef.current,
    getOptions: () => optionsRef.current,
    setIndex,
    getValue,
    setValue,
    setOptions
  }));
  return /* @__PURE__ */ React.createElement("div", _extends({
    ref: root,
    className: classNames(bem(), props.className)
  }, bind()), /* @__PURE__ */ React.createElement(animated.ul, {
    ref: wrapper,
    className: classNames(bem("wrapper")),
    style: {
      y,
      transitionDuration: `${state.duration}ms`,
      transitionProperty: state.duration ? "all" : "none"
    },
    "aria-hidden": true
  }, renderOptions));
}), (prev, next) => {
  if (prev.index !== next.index)
    return false;
  if (prev.textKey !== next.textKey) {
    return false;
  }
  if (JSON.stringify(prev.initialOptions) !== JSON.stringify(next.initialOptions)) {
    return false;
  }
  return true;
});
PickerColumn.displayName = "PickerColumn";
var stdin_default = PickerColumn;
export {
  stdin_default as default
};
