import { defineComponent, inject, createVNode } from 'vue';
import VirtualList from '../../virtual-list/virtualList';
import { provideKey } from '../const';
import Colgroup from './colgroup';
import Tr from './tr';

var virtualTable = defineComponent({
  props: {
    columns: {
      type: Array,
      required: true
    }
  },
  setup(props) {
    const {
      showData,
      rootProps,
      layout,
      prefixCls,
      bodyWrapperRef,
      bodyWrapperClass,
      bodyStyle,
      syncPosition,
      scrollbarRef,
      noFixedColumn
    } = inject(provideKey);
    const renderDefault = _ref => {
      let {
        source,
        index
      } = _ref;
      return createVNode(Tr, {
        "expanded": false,
        "row": source,
        "rowIndex": index,
        "columns": props.columns
      }, null);
    };
    const renderItemList = itemVNodes => {
      return [createVNode(Colgroup, {
        columns: props.columns
      }), createVNode('tbody', {}, itemVNodes.length ? itemVNodes : [createVNode(Tr, {
        "columns": props.columns
      }, null)])];
    };
    const onScroll = e => {
      if (layout.isScrollX.value || layout.isScrollY.value) {
        syncPosition(e);
      }
    };
    return () => {
      var _rootProps$virtualScr, _rootProps$virtualScr2;
      return createVNode(VirtualList, {
        "ref": el => {
          if (el) {
            scrollbarRef.value = el.scrollRef;
            bodyWrapperRef.value = el.$el;
          }
        },
        "shadow": {
          x: noFixedColumn.value,
          y: true
        },
        "shadowStyle": {
          zIndex: 3
        },
        "verticalRatioStyle": {
          zIndex: 3
        },
        "horizontalRatioStyle": {
          zIndex: 3
        },
        "onScroll": onScroll,
        "dataSources": showData.value,
        "dataKey": rootProps.rowKey,
        "estimateSize": (_rootProps$virtualScr = rootProps.virtualScrollOption.estimateSize) !== null && _rootProps$virtualScr !== void 0 ? _rootProps$virtualScr : 54,
        "keeps": (_rootProps$virtualScr2 = rootProps.virtualScrollOption.keeps) !== null && _rootProps$virtualScr2 !== void 0 ? _rootProps$virtualScr2 : 20,
        "class": bodyWrapperClass.value,
        "maxHeight": layout.bodyHeight.value,
        "wrapTag": 'table',
        "wrapClass": `${prefixCls}-body`,
        "wrapStyle": bodyStyle.value,
        "always": rootProps.alwaysScrollbar,
        "renderItemList": renderItemList
      }, {
        default: renderDefault
      });
    };
  }
});

export { virtualTable as default };
