"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const index$1 = require("../../../../../node_modules/@vueuse/core/index.js");
const types = require("../../../../utils/types.js");
const shared_esmBundler = require("../../../../../../@vue/shared/dist/shared.esm-bundler.js");
const index = require("../../../../hooks/use-namespace/index.js");
const useCache = require("../hooks/use-cache.js");
const defaults = require("../defaults.js");
const useWheel = require("../hooks/use-wheel.js");
const props = require("../props.js");
const utils = require("../utils.js");
const scrollbar = require("../components/scrollbar.js");
const Vue = require("vue");
const index$2 = require("../../../../../node_modules/@vueuse/shared/index.js");
const createList = ({ name, getOffset, getItemSize, getItemOffset, getEstimatedTotalSize, getStartIndexForOffset, getStopIndexForStartIndex, initCache, clearCache, validateProps }) => {
  return Vue.defineComponent({
    name: name ?? "ElVirtualList",
    props: props.virtualizedListProps,
    emits: [defaults.ITEM_RENDER_EVT, defaults.SCROLL_EVT],
    setup(props2, { emit, expose }) {
      validateProps(props2);
      const instance = Vue.getCurrentInstance();
      const ns = index.useNamespace("vl");
      const dynamicSizeCache = Vue.ref(initCache(props2, instance));
      const getItemStyleCache = useCache.useCache();
      const windowRef = Vue.ref();
      const innerRef = Vue.ref();
      const scrollbarRef = Vue.ref();
      const states = Vue.ref({
        isScrolling: false,
        scrollDir: "forward",
        scrollOffset: types.isNumber(props2.initScrollOffset) ? props2.initScrollOffset : 0,
        updateRequested: false,
        isScrollbarDragging: false,
        scrollbarAlwaysOn: props2.scrollbarAlwaysOn
      });
      const itemsToRender = Vue.computed(() => {
        const { total, cache } = props2;
        const { isScrolling, scrollDir, scrollOffset } = Vue.unref(states);
        if (total === 0) return [
          0,
          0,
          0,
          0
        ];
        const startIndex = getStartIndexForOffset(props2, scrollOffset, Vue.unref(dynamicSizeCache));
        const stopIndex = getStopIndexForStartIndex(props2, startIndex, scrollOffset, Vue.unref(dynamicSizeCache));
        const cacheBackward = !isScrolling || scrollDir === defaults.BACKWARD ? Math.max(1, cache) : 1;
        const cacheForward = !isScrolling || scrollDir === defaults.FORWARD ? Math.max(1, cache) : 1;
        return [
          Math.max(0, startIndex - cacheBackward),
          Math.max(0, Math.min(total - 1, stopIndex + cacheForward)),
          startIndex,
          stopIndex
        ];
      });
      const estimatedTotalSize = Vue.computed(() => getEstimatedTotalSize(props2, Vue.unref(dynamicSizeCache)));
      const _isHorizontal = Vue.computed(() => utils.isHorizontal(props2.layout));
      const windowStyle = Vue.computed(() => [
        {
          position: "relative",
          [`overflow-${_isHorizontal.value ? "x" : "y"}`]: "scroll",
          WebkitOverflowScrolling: "touch",
          willChange: "transform"
        },
        {
          direction: props2.direction,
          height: types.isNumber(props2.height) ? `${props2.height}px` : props2.height,
          width: types.isNumber(props2.width) ? `${props2.width}px` : props2.width
        },
        props2.style
      ]);
      const innerStyle = Vue.computed(() => {
        const size = Vue.unref(estimatedTotalSize);
        const horizontal = Vue.unref(_isHorizontal);
        return {
          height: horizontal ? "100%" : `${size}px`,
          pointerEvents: Vue.unref(states).isScrolling ? "none" : void 0,
          width: horizontal ? `${size}px` : "100%",
          margin: 0,
          boxSizing: "border-box"
        };
      });
      const clientSize = Vue.computed(() => _isHorizontal.value ? props2.width : props2.height);
      const { onWheel } = useWheel.default({
        atStartEdge: Vue.computed(() => states.value.scrollOffset <= 0),
        atEndEdge: Vue.computed(() => states.value.scrollOffset >= estimatedTotalSize.value),
        layout: Vue.computed(() => props2.layout)
      }, (offset) => {
        var _a, _b;
        (_b = (_a = scrollbarRef.value).onMouseUp) == null ? void 0 : _b.call(_a);
        scrollTo(Math.min(states.value.scrollOffset + offset, estimatedTotalSize.value - clientSize.value));
      });
      index$1.useEventListener(windowRef, "wheel", onWheel, { passive: false });
      const emitEvents = () => {
        const { total } = props2;
        if (total > 0) {
          const [cacheStart, cacheEnd, visibleStart, visibleEnd] = Vue.unref(itemsToRender);
          emit(defaults.ITEM_RENDER_EVT, cacheStart, cacheEnd, visibleStart, visibleEnd);
        }
        const { scrollDir, scrollOffset, updateRequested } = Vue.unref(states);
        emit(defaults.SCROLL_EVT, scrollDir, scrollOffset, updateRequested);
      };
      const scrollVertically = (e) => {
        const { clientHeight, scrollHeight, scrollTop } = e.currentTarget;
        const _states = Vue.unref(states);
        if (_states.scrollOffset === scrollTop) return;
        const scrollOffset = Math.max(0, Math.min(scrollTop, scrollHeight - clientHeight));
        states.value = {
          ..._states,
          isScrolling: true,
          scrollDir: utils.getScrollDir(_states.scrollOffset, scrollOffset),
          scrollOffset,
          updateRequested: false
        };
        Vue.nextTick(resetIsScrolling);
      };
      const scrollHorizontally = (e) => {
        const { clientWidth, scrollLeft, scrollWidth } = e.currentTarget;
        const _states = Vue.unref(states);
        if (_states.scrollOffset === scrollLeft) return;
        const { direction } = props2;
        let scrollOffset = scrollLeft;
        if (direction === defaults.RTL) switch (utils.getRTLOffsetType()) {
          case defaults.RTL_OFFSET_NAG:
            scrollOffset = -scrollLeft;
            break;
          case defaults.RTL_OFFSET_POS_DESC:
            scrollOffset = scrollWidth - clientWidth - scrollLeft;
            break;
        }
        scrollOffset = Math.max(0, Math.min(scrollOffset, scrollWidth - clientWidth));
        states.value = {
          ..._states,
          isScrolling: true,
          scrollDir: utils.getScrollDir(_states.scrollOffset, scrollOffset),
          scrollOffset,
          updateRequested: false
        };
        Vue.nextTick(resetIsScrolling);
      };
      const onScroll = (e) => {
        Vue.unref(_isHorizontal) ? scrollHorizontally(e) : scrollVertically(e);
        emitEvents();
      };
      const onScrollbarScroll = (distanceToGo, totalSteps) => {
        const offset = (estimatedTotalSize.value - clientSize.value) / totalSteps * distanceToGo;
        scrollTo(Math.min(estimatedTotalSize.value - clientSize.value, offset));
      };
      const scrollTo = (offset) => {
        offset = Math.max(offset, 0);
        if (offset === Vue.unref(states).scrollOffset) return;
        states.value = {
          ...Vue.unref(states),
          scrollOffset: offset,
          scrollDir: utils.getScrollDir(Vue.unref(states).scrollOffset, offset),
          updateRequested: true
        };
        Vue.nextTick(resetIsScrolling);
      };
      const scrollToItem = (idx, alignment = defaults.AUTO_ALIGNMENT) => {
        const { scrollOffset } = Vue.unref(states);
        idx = Math.max(0, Math.min(idx, props2.total - 1));
        scrollTo(getOffset(props2, idx, alignment, scrollOffset, Vue.unref(dynamicSizeCache)));
      };
      const getItemStyle = (idx) => {
        const { direction, itemSize, layout } = props2;
        const itemStyleCache = getItemStyleCache.value(clearCache && itemSize, clearCache && layout, clearCache && direction);
        let style;
        if (shared_esmBundler.hasOwn(itemStyleCache, String(idx))) style = itemStyleCache[idx];
        else {
          const offset = getItemOffset(props2, idx, Vue.unref(dynamicSizeCache));
          const size = getItemSize(props2, idx, Vue.unref(dynamicSizeCache));
          const horizontal = Vue.unref(_isHorizontal);
          const isRtl = direction === defaults.RTL;
          const offsetHorizontal = horizontal ? offset : 0;
          itemStyleCache[idx] = style = {
            position: "absolute",
            left: isRtl ? void 0 : `${offsetHorizontal}px`,
            right: isRtl ? `${offsetHorizontal}px` : void 0,
            top: !horizontal ? `${offset}px` : 0,
            height: !horizontal ? `${size}px` : "100%",
            width: horizontal ? `${size}px` : "100%"
          };
        }
        return style;
      };
      const resetIsScrolling = () => {
        states.value.isScrolling = false;
        Vue.nextTick(() => {
          getItemStyleCache.value(-1, null, null);
        });
      };
      const resetScrollTop = () => {
        const window = windowRef.value;
        if (window) window.scrollTop = 0;
      };
      Vue.onMounted(() => {
        if (!index$2.isClient) return;
        const { initScrollOffset } = props2;
        const windowElement = Vue.unref(windowRef);
        if (types.isNumber(initScrollOffset) && windowElement) if (Vue.unref(_isHorizontal)) windowElement.scrollLeft = initScrollOffset;
        else windowElement.scrollTop = initScrollOffset;
        emitEvents();
      });
      Vue.onUpdated(() => {
        const { direction, layout } = props2;
        const { scrollOffset, updateRequested } = Vue.unref(states);
        const windowElement = Vue.unref(windowRef);
        if (updateRequested && windowElement) if (layout === defaults.HORIZONTAL) if (direction === defaults.RTL) switch (utils.getRTLOffsetType()) {
          case defaults.RTL_OFFSET_NAG:
            windowElement.scrollLeft = -scrollOffset;
            break;
          case defaults.RTL_OFFSET_POS_ASC:
            windowElement.scrollLeft = scrollOffset;
            break;
          default: {
            const { clientWidth, scrollWidth } = windowElement;
            windowElement.scrollLeft = scrollWidth - clientWidth - scrollOffset;
            break;
          }
        }
        else windowElement.scrollLeft = scrollOffset;
        else windowElement.scrollTop = scrollOffset;
      });
      Vue.onActivated(() => {
        Vue.unref(windowRef).scrollTop = Vue.unref(states).scrollOffset;
      });
      const api = {
        ns,
        clientSize,
        estimatedTotalSize,
        windowStyle,
        windowRef,
        innerRef,
        innerStyle,
        itemsToRender,
        scrollbarRef,
        states,
        getItemStyle,
        onScroll,
        onScrollbarScroll,
        onWheel,
        scrollTo,
        scrollToItem,
        resetScrollTop
      };
      expose({
        windowRef,
        innerRef,
        getItemStyleCache,
        scrollTo,
        scrollToItem,
        resetScrollTop,
        states
      });
      return api;
    },
    render(ctx) {
      var _a;
      const { $slots, className, clientSize, containerElement, data, getItemStyle, innerElement, itemsToRender, innerStyle, layout, total, onScroll, onScrollbarScroll, states, useIsScrolling, windowStyle, ns } = ctx;
      const [start, end] = itemsToRender;
      const Container = Vue.resolveDynamicComponent(containerElement);
      const Inner = Vue.resolveDynamicComponent(innerElement);
      const children = [];
      if (total > 0) for (let i = start; i <= end; i++) children.push(Vue.h(Vue.Fragment, { key: i }, (_a = $slots.default) == null ? void 0 : _a.call($slots, {
        data,
        index: i,
        isScrolling: useIsScrolling ? states.isScrolling : void 0,
        style: getItemStyle(i)
      })));
      const InnerNode = [Vue.h(Inner, Vue.mergeProps(ctx.innerProps, {
        style: innerStyle,
        ref: "innerRef"
      }), !shared_esmBundler.isString(Inner) ? { default: () => children } : children)];
      const scrollbar$1 = Vue.h(scrollbar.default, {
        ref: "scrollbarRef",
        clientSize,
        layout,
        onScroll: onScrollbarScroll,
        ratio: clientSize * 100 / this.estimatedTotalSize,
        scrollFrom: states.scrollOffset / (this.estimatedTotalSize - clientSize),
        total,
        alwaysOn: states.scrollbarAlwaysOn
      });
      const listContainer = Vue.h(Container, {
        class: [ns.e("window"), className],
        style: windowStyle,
        onScroll,
        ref: "windowRef",
        key: 0
      }, !shared_esmBundler.isString(Container) ? { default: () => [InnerNode] } : [InnerNode]);
      return Vue.h("div", {
        key: 0,
        class: [ns.e("wrapper"), states.scrollbarAlwaysOn ? "always-on" : ""]
      }, [listContainer, scrollbar$1]);
    }
  });
};
exports.default = createList;
