'use strict';

var vue = require('vue');

function useVirtualized() {
  const actualHeightContainerEl = vue.ref(null);
  const translateContainerEl = vue.ref(null);
  const scrollContainerEl = vue.ref(null);
  const isMultiple = vue.ref(false);
  const saveDATA = vue.ref([]);
  const RenderedItemsCache = {};
  const getDom = (props) => {
    const getElements = (className) => {
      actualHeightContainerEl.value = document.querySelector(`${className} .el-scrollbar__view`);
      translateContainerEl.value = document.querySelector(`${className} .el-table__body`);
      scrollContainerEl.value = document.querySelector(`${className} .el-scrollbar__wrap`);
    };
    isMultiple.value = props.multiple;
    if (props.multiple) {
      getElements(".f_select_table_multiple");
    } else {
      getElements(".f_select_table_radio");
    }
  };
  const getItemHeightFromCache = (index) => {
    const val = RenderedItemsCache[index];
    return val === void 0 ? 50 : val;
  };
  const updateActualHeight = () => {
    let actualHeight = 0;
    saveDATA.value.forEach((_, i) => {
      actualHeight += getItemHeightFromCache(i);
    });
    actualHeightContainerEl.value.style.height = actualHeight + "px";
  };
  const updateOffset = (offset) => {
    if (translateContainerEl.value && translateContainerEl.value.style) {
      translateContainerEl.value.style.transform = `translateY(${offset}px)`;
    }
  };
  const updateRenderedItemCache = (index) => {
    const shouldUpdate = Object.keys(RenderedItemsCache).length < saveDATA.value.length;
    if (!shouldUpdate) return;
    vue.nextTick(() => {
      const Items = Array.from(
        document.querySelectorAll(
          `${isMultiple.value ? `.f_select_table_multiple` : `.f_select_table_radio`} .el-table__row`
        )
      );
      Items.forEach((el) => {
        if (!RenderedItemsCache[index]) {
          RenderedItemsCache[index] = el.offsetHeight;
        }
        index++;
      });
      updateActualHeight();
    });
  };
  return {
    scrollContainerEl,
    updateRenderedItemCache,
    updateOffset,
    getDom,
    getItemHeightFromCache,
    saveDATA
  };
}

exports.useVirtualized = useVirtualized;
