var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  default: () => stdin_default
});
module.exports = __toCommonJS(stdin_exports);
var import_react = __toESM(require("react"));
var import_web = require("@react-spring/web");
var import_react2 = require("@use-gesture/react");
var import_classnames = __toESM(require("classnames"));
var import_hooks = require("../hooks");
var import_utils = require("../utils");
var import_deep_clone = require("../utils/deep-clone");
var import_rubberband = require("../utils/rubberband");
var import_ConfigProviderContext = __toESM(require("../config-provider/ConfigProviderContext"));
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);
}
function isOptionDisabled(option) {
  return (0, import_utils.isObject)(option) && option.disabled;
}
const DEFAULT_DURATION = 200;
const MOMENTUM_LIMIT_DISTANCE = 15;
const PickerColumn = /* @__PURE__ */ (0, import_react.memo)(/* @__PURE__ */ (0, import_react.forwardRef)((props, ref) => {
  const {
    prefixCls,
    createNamespace
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const [bem] = createNamespace("picker-column", prefixCls);
  const {
    itemHeight,
    visibleItemCount,
    defaultIndex,
    initialOptions
  } = props;
  const root = (0, import_react.useRef)(null);
  const wrapper = (0, import_react.useRef)(null);
  const indexRef = (0, import_react.useRef)(defaultIndex);
  const optionsRef = (0, import_react.useRef)((0, import_deep_clone.deepClone)(initialOptions));
  const [state, updateState] = (0, import_hooks.useSetState)({
    offset: 0,
    duration: 0,
    options: (0, import_deep_clone.deepClone)(initialOptions)
  });
  const count = (0, import_react.useMemo)(() => optionsRef.current.length, [optionsRef.current.length]);
  const baseOffset = (0, import_react.useMemo)(() => {
    return itemHeight * (+visibleItemCount - 1) / 2;
  }, [itemHeight, visibleItemCount]);
  const draggingRef = (0, import_react.useRef)(false);
  const [{
    y
  }, api] = (0, import_web.useSpring)(() => ({
    from: {
      y: defaultIndex * -itemHeight + baseOffset
    },
    config: {
      tension: 400,
      mass: 0.8
    }
  }));
  const adjustIndex = (index) => {
    index = (0, import_utils.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 (0, import_utils.isObject)(option) && props.textKey in option;
  }
  const getOptionText = (option) => {
    if (isOption(option)) {
      return option[props.textKey];
    }
    return option;
  };
  const getIndexByOffset = (offset) => (0, import_utils.range)(Math.round(-offset / props.itemHeight), 0, count - 1);
  const bind = (0, import_react2.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 = (0, import_rubberband.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 = (0, import_react.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: (0, import_classnames.default)(bem("item", {
          disabled,
          selected: index === indexRef.current
        })),
        onClick: () => {
          draggingRef.current = false;
          onClickItem(index);
        }
      };
      const childData = {
        className: "rc-ellipsis",
        children: text
      };
      return /* @__PURE__ */ import_react.default.createElement("li", data, props.optionRender ? props.optionRender(option) : /* @__PURE__ */ import_react.default.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 = (0, import_react.useCallback)(() => optionsRef.current[indexRef.current], [indexRef.current, optionsRef.current]);
  (0, import_react.useEffect)(() => {
    setIndex(defaultIndex);
  }, [defaultIndex]);
  (0, import_hooks.useUpdateEffect)(() => {
    setOptions((0, import_deep_clone.deepClone)(initialOptions));
  }, [initialOptions]);
  (0, import_react.useImperativeHandle)(ref, () => ({
    state,
    getIndex: () => indexRef.current,
    getOptions: () => optionsRef.current,
    setIndex,
    getValue,
    setValue,
    setOptions
  }));
  return /* @__PURE__ */ import_react.default.createElement("div", _extends({
    ref: root,
    className: (0, import_classnames.default)(bem(), props.className)
  }, bind()), /* @__PURE__ */ import_react.default.createElement(import_web.animated.ul, {
    ref: wrapper,
    className: (0, import_classnames.default)(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;
