'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
require('../../../utils/index.cjs');
var Filler = require('./Filler.cjs');
var Item = require('./Item.cjs');
var ScrollBar = require('./ScrollBar.cjs');
var useHeights = require('./hooks/useHeights.cjs');
var useScrollTo = require('./hooks/useScrollTo.cjs');
var useFrameWheel = require('./hooks/useFrameWheel.cjs');
var useMobileTouchMove = require('./hooks/useMobileTouchMove.cjs');
var useOriginScroll = require('./hooks/useOriginScroll.cjs');
var vueTypes = require('../../../utils/vue/vueTypes.cjs');
var supportsPassive = require('../../../utils/event/supportsPassive.cjs');
var _class = require('../../../utils/dom/class.cjs');

const EMPTY_DATA = [];
const ScrollStyle = {
  overflowY: "auto",
  overflowAnchor: "none"
};
function renderChildren(list, startIndex, endIndex, setNodeRef, renderFunc, {
  getKey
}) {
  return list.slice(startIndex, endIndex + 1).map((item, index) => {
    const eleIndex = startIndex + index;
    const node = renderFunc(item, eleIndex, {
      // style: status === 'MEASURE_START' ? { visibility: 'hidden' } : {},
    });
    const key = getKey(item);
    return vue.createVNode(Item.default, {
      "key": key,
      "setRef": (ele) => setNodeRef(item, ele)
    }, {
      default: () => [node]
    });
  });
}
const List = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "List",
  inheritAttrs: false,
  props: {
    prefixCls: String,
    data: vueTypes.default.array,
    height: Number,
    itemHeight: Number,
    /** If not match virtual scroll condition, Set List still use height of container. */
    fullHeight: {
      type: Boolean,
      default: void 0
    },
    itemKey: {
      type: [String, Number, Function],
      required: true
    },
    component: {
      type: [String, Object]
    },
    /** Set `false` will always use real scroll instead of virtual one */
    virtual: {
      type: Boolean,
      default: void 0
    },
    children: Function,
    onScroll: Function,
    onMousedown: Function,
    onMouseenter: Function,
    onVisibleChange: Function
  },
  setup(props, {
    expose
  }) {
    const useVirtual = vue.computed(() => {
      const {
        height,
        itemHeight,
        virtual
      } = props;
      return !!(virtual !== false && height && itemHeight);
    });
    const inVirtual = vue.computed(() => {
      const {
        height,
        itemHeight,
        data: data2
      } = props;
      return useVirtual.value && data2 && itemHeight * data2.length > height;
    });
    const state = vue.reactive({
      scrollTop: 0,
      scrollMoving: false
    });
    const data = vue.computed(() => {
      return props.data || EMPTY_DATA;
    });
    const mergedData = vue.shallowRef([]);
    vue.watch(data, () => {
      mergedData.value = vue.toRaw(data.value).slice();
    }, {
      immediate: true
    });
    const itemKey = vue.shallowRef((_item) => void 0);
    vue.watch(() => props.itemKey, (val) => {
      if (typeof val === "function")
        itemKey.value = val;
      else
        itemKey.value = (item) => item == null ? void 0 : item[val];
    }, {
      immediate: true
    });
    const componentRef = vue.shallowRef();
    const fillerInnerRef = vue.shallowRef();
    const scrollBarRef = vue.shallowRef();
    const getKey = (item) => {
      return itemKey.value(item);
    };
    const sharedConfig = {
      getKey
    };
    function syncScrollTop(newTop) {
      let value;
      if (typeof newTop === "function")
        value = newTop(state.scrollTop);
      else
        value = newTop;
      const alignedTop = keepInRange(value);
      if (componentRef.value)
        componentRef.value.scrollTop = alignedTop;
      state.scrollTop = alignedTop;
    }
    const [setInstance, collectHeight, heights, updatedMark] = useHeights.default(mergedData, getKey, null, null);
    const calRes = vue.reactive({
      scrollHeight: void 0,
      start: 0,
      end: 0,
      offset: void 0
    });
    const offsetHeight = vue.shallowRef(0);
    vue.onMounted(() => {
      vue.nextTick(() => {
        var _a;
        offsetHeight.value = ((_a = fillerInnerRef.value) == null ? void 0 : _a.offsetHeight) || 0;
      });
    });
    vue.onUpdated(() => {
      vue.nextTick(() => {
        var _a;
        offsetHeight.value = ((_a = fillerInnerRef.value) == null ? void 0 : _a.offsetHeight) || 0;
      });
    });
    vue.watch([useVirtual, mergedData], () => {
      if (!useVirtual.value) {
        Object.assign(calRes, {
          scrollHeight: void 0,
          start: 0,
          end: mergedData.value.length - 1,
          offset: void 0
        });
      }
    }, {
      immediate: true
    });
    vue.watch([useVirtual, mergedData, offsetHeight, inVirtual], () => {
      if (useVirtual.value && !inVirtual.value) {
        Object.assign(calRes, {
          scrollHeight: offsetHeight.value,
          start: 0,
          end: mergedData.value.length - 1,
          offset: void 0
        });
      }
      if (componentRef.value)
        state.scrollTop = componentRef.value.scrollTop;
    }, {
      immediate: true
    });
    vue.watch([inVirtual, useVirtual, () => state.scrollTop, mergedData, updatedMark, () => props.height, offsetHeight], () => {
      if (!useVirtual.value || !inVirtual.value)
        return;
      let itemTop = 0;
      let startIndex;
      let startOffset;
      let endIndex;
      const dataLen = mergedData.value.length;
      const data2 = mergedData.value;
      const scrollTop = state.scrollTop;
      const {
        itemHeight,
        height
      } = props;
      const scrollTopHeight = scrollTop + height;
      for (let i = 0; i < dataLen; i += 1) {
        const item = data2[i];
        const key = getKey(item);
        let cacheHeight = heights.get(key);
        if (cacheHeight === void 0)
          cacheHeight = itemHeight;
        const currentItemBottom = itemTop + cacheHeight;
        if (startIndex === void 0 && currentItemBottom >= scrollTop) {
          startIndex = i;
          startOffset = itemTop;
        }
        if (endIndex === void 0 && currentItemBottom > scrollTopHeight)
          endIndex = i;
        itemTop = currentItemBottom;
      }
      if (startIndex === void 0) {
        startIndex = 0;
        startOffset = 0;
        endIndex = Math.ceil(height / itemHeight);
      }
      if (endIndex === void 0)
        endIndex = dataLen - 1;
      endIndex = Math.min(endIndex + 1, dataLen);
      Object.assign(calRes, {
        scrollHeight: itemTop,
        start: startIndex,
        end: endIndex,
        offset: startOffset
      });
    }, {
      immediate: true
    });
    const maxScrollHeight = vue.computed(() => calRes.scrollHeight - props.height);
    function keepInRange(newScrollTop) {
      let newTop = newScrollTop;
      if (!Number.isNaN(maxScrollHeight.value))
        newTop = Math.min(newTop, maxScrollHeight.value);
      newTop = Math.max(newTop, 0);
      return newTop;
    }
    const isScrollAtTop = vue.computed(() => state.scrollTop <= 0);
    const isScrollAtBottom = vue.computed(() => state.scrollTop >= maxScrollHeight.value);
    const originScroll = useOriginScroll.default(isScrollAtTop, isScrollAtBottom);
    function onScrollBar(newScrollTop) {
      const newTop = newScrollTop;
      syncScrollTop(newTop);
    }
    function onFallbackScroll(e) {
      var _a;
      const {
        scrollTop: newScrollTop
      } = e.currentTarget;
      if (newScrollTop !== state.scrollTop)
        syncScrollTop(newScrollTop);
      (_a = props.onScroll) == null ? void 0 : _a.call(props, e);
    }
    const [onRawWheel, onFireFoxScroll] = useFrameWheel.default(useVirtual, isScrollAtTop, isScrollAtBottom, (offsetY) => {
      syncScrollTop((top) => {
        const newTop = top + offsetY;
        return newTop;
      });
    });
    useMobileTouchMove.default(useVirtual, componentRef, (deltaY, smoothOffset) => {
      if (originScroll(deltaY, smoothOffset))
        return false;
      onRawWheel({
        preventDefault() {
        },
        deltaY
      });
      return true;
    });
    function onMozMousePixelScroll(e) {
      if (useVirtual.value)
        e.preventDefault();
    }
    const removeEventListener = () => {
      if (componentRef.value) {
        componentRef.value.removeEventListener("wheel", onRawWheel, supportsPassive.supportsPassive ? {
          passive: false
        } : false);
        componentRef.value.removeEventListener("DOMMouseScroll", onFireFoxScroll);
        componentRef.value.removeEventListener("MozMousePixelScroll", onMozMousePixelScroll);
      }
    };
    vue.watchEffect(() => {
      vue.nextTick(() => {
        if (componentRef.value) {
          removeEventListener();
          componentRef.value.addEventListener("wheel", onRawWheel, supportsPassive.supportsPassive ? {
            passive: false
          } : false);
          componentRef.value.addEventListener("DOMMouseScroll", onFireFoxScroll);
          componentRef.value.addEventListener("MozMousePixelScroll", onMozMousePixelScroll);
        }
      });
    });
    vue.onBeforeUnmount(() => {
      removeEventListener();
    });
    const scrollTo = useScrollTo.default(componentRef, mergedData, heights, props, getKey, collectHeight, syncScrollTop, () => {
      var _a;
      (_a = scrollBarRef.value) == null ? void 0 : _a.delayHidden();
    });
    expose({
      scrollTo
    });
    const componentStyle = vue.computed(() => {
      let cs = null;
      if (props.height) {
        cs = {
          [props.fullHeight ? "height" : "maxHeight"]: `${props.height}px`,
          ...ScrollStyle
        };
        if (useVirtual.value) {
          cs.overflowY = "hidden";
          if (state.scrollMoving)
            cs.pointerEvents = "none";
        }
      }
      return cs;
    });
    vue.watch([() => calRes.start, () => calRes.end, mergedData], () => {
      if (props.onVisibleChange) {
        const renderList = mergedData.value.slice(calRes.start, calRes.end + 1);
        props.onVisibleChange(renderList, mergedData.value);
      }
    }, {
      flush: "post"
    });
    const delayHideScrollBar = () => {
      var _a;
      (_a = scrollBarRef.value) == null ? void 0 : _a.delayHidden();
    };
    return {
      state,
      mergedData,
      componentStyle,
      onFallbackScroll,
      onScrollBar,
      componentRef,
      useVirtual,
      calRes,
      collectHeight,
      setInstance,
      sharedConfig,
      scrollBarRef,
      fillerInnerRef,
      delayHideScrollBar
    };
  },
  render() {
    const {
      prefixCls = "rc-virtual-list",
      height,
      itemHeight,
      fullHeight,
      data,
      itemKey,
      virtual,
      component: Component = "div",
      onScroll,
      children = this.$slots.default,
      style,
      class: className,
      ...restProps
    } = {
      ...this.$props,
      ...this.$attrs
    };
    const mergedClassName = _class.classNames(prefixCls, className);
    const {
      scrollTop
    } = this.state;
    const {
      scrollHeight,
      offset,
      start,
      end
    } = this.calRes;
    const {
      componentStyle,
      onFallbackScroll,
      onScrollBar,
      useVirtual,
      collectHeight,
      sharedConfig,
      setInstance,
      mergedData,
      delayHideScrollBar
    } = this;
    return vue.createVNode("div", {
      "style": {
        ...style,
        position: "relative"
      },
      "class": mergedClassName,
      ...restProps
    }, [vue.createVNode(Component, {
      "class": `${prefixCls}-holder`,
      "style": componentStyle,
      "ref": "componentRef",
      "onScroll": onFallbackScroll,
      "onMouseenter": delayHideScrollBar
    }, {
      default: () => [vue.createVNode(Filler.default, {
        "prefixCls": prefixCls,
        "height": scrollHeight,
        "offset": offset,
        "onInnerResize": collectHeight,
        "ref": "fillerInnerRef"
      }, {
        default: () => renderChildren(mergedData, start, end, setInstance, children, sharedConfig)
      })]
    }), useVirtual && vue.createVNode(ScrollBar.default, {
      "ref": "scrollBarRef",
      "prefixCls": prefixCls,
      "scrollTop": scrollTop,
      "height": height,
      "scrollHeight": scrollHeight,
      "count": mergedData.length,
      "onScroll": onScrollBar,
      "onStartMove": () => {
        this.state.scrollMoving = true;
      },
      "onStopMove": () => {
        this.state.scrollMoving = false;
      }
    }, null)]);
  }
});

exports.default = List;
//# sourceMappingURL=List.cjs.map
