import { isVNode as _isVNode, mergeProps as _mergeProps, createVNode as _createVNode } from "vue";
import { cloneVNode, defineComponent, inject } from "vue";
import { dataTableContextKey } from "./interface";
import { getColumnClassName, getRowClassName } from "./utils";
import VirtualList from '../virtual-list';
import Header from "./TableHeader";

function _isSlot(s) {
  return typeof s === 'function' || Object.prototype.toString.call(s) === '[object Object]' && !_isVNode(s);
}

export default defineComponent({
  props: {
    showHeader: Boolean
  },

  setup(props, {
    slots
  }) {
    const {
      props: tableProps,
      leftFixedColumsRef,
      rightFixedColumsRef,
      fixedColumnLeftMapRef,
      fixedColumnRightMapRef
    } = inject(dataTableContextKey);
    const {
      rowProps,
      rowClass
    } = tableProps;
    return () => {
      const {
        prefixCls,
        columns,
        data,
        scrollX,
        maxHeight,
        virtualScroll
      } = tableProps;
      const scrollableY = !!maxHeight;
      const {
        showHeader
      } = props;
      const rows = data.map((row, index) => {
        const cls = [`${prefixCls}_tr`, getRowClassName(rowClass, row, index)];
        const props = rowProps === null || rowProps === void 0 ? void 0 : rowProps(row, index);
        return _createVNode("tr", _mergeProps({
          "class": cls,
          "key": row.key
        }, props), [columns.map((col, colIndex) => {
          const fixLast = col === leftFixedColumsRef.value[leftFixedColumsRef.value.length - 1],
                rightFirst = col === rightFixedColumsRef.value[0];
          const cls = [`${prefixCls}_td`, getColumnClassName(col, colIndex), col.fixed && `${prefixCls}_td-fix-${col.fixed}`, fixLast && `${prefixCls}_td-fix-left-last`, rightFirst && `${prefixCls}_td-fix-right-first`, col.align && `${prefixCls}_td-align-${col.align}`];
          const style = {
            width: col.width + 'px',
            left: fixedColumnLeftMapRef.value[col.key] + 'px',
            right: fixedColumnRightMapRef.value[col.key] + 'px'
          };
          return _createVNode("td", {
            "key": col.key,
            "class": cls,
            "style": style
          }, [slots[col.key] ? slots[col.key]({
            row,
            index
          }) : row[col.key]]);
        })]);
      });

      const itemsWrap = (_, {
        slots
      }) => _createVNode("table", {
        "class": `${prefixCls}_table`,
        "style": `width:${scrollX}px`
      }, [showHeader && _createVNode(Header, null, null), _createVNode("tbody", {
        "class": `${prefixCls}_tbody`
      }, { ...slots
      })]);

      let tableNode = _createVNode(itemsWrap, null, _isSlot(rows) ? rows : {
        default: () => [rows]
      });

      const style = [scrollableY && `maxHeight:${maxHeight}px; overflow-y:scroll;`];
      if (virtualScroll) style.push({
        overflowY: 'hidden'
      });
      const bodyProps = {
        class: `${prefixCls}_body`,
        style
      };

      if (virtualScroll) {
        tableNode = _createVNode(VirtualList, {
          "data": rows,
          "itemsWrap": itemsWrap,
          "height": maxHeight,
          "virtual": true,
          "itemKey": "key"
        }, {
          default: ({
            item
          }) => item
        });
        return cloneVNode(tableNode, bodyProps);
      }

      return _createVNode("div", bodyProps, [tableNode]);
    };
  }

});