/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

import { h as helper } from '../_chunks/dep-1ec74b8b.js';
import _typeof from '@babel/runtime/helpers/typeof';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { h, defineComponent, computed, ref, onMounted, inject, nextTick, onBeforeUnmount } from '@vue/composition-api';
import isFunction from 'lodash/isFunction';
import upperFirst from 'lodash/upperFirst';
import isString from 'lodash/isString';
import pick from 'lodash/pick';
import get from 'lodash/get';
import { formatRowAttributes, formatRowClassNames } from './utils.js';
import { getRowFixedStyles, getColumnFixedStyles } from './hooks/useFixed.js';
import useClassName from './hooks/useClassName.js';
import TEllipsis from './ellipsis.js';
import baseTableProps from './base-table-props.js';
import '@babel/runtime/helpers/slicedToArray';
import '../_common/js/log/index.js';
import '../_common/js/log/log.js';
import '../_common/js/utils/getScrollbarWidth.js';
import '../utils/dom.js';
import 'vue';
import 'raf';
import '../utils/easing.js';
import '../config-provider/useConfig.js';
import 'lodash/cloneDeep';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '../utils/render-tnode.js';
import 'lodash/isObject';
import '../popup/index.js';
import '../popup/popup.js';
import '@popperjs/core';
import '../config.js';
import '../utils/classnames.js';
import '../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import 'lodash/camelCase';
import '../_common/js/utils/set-style.js';
import '../popup/props.js';
import '../popup/container.js';
import '../utils/map-props.js';
import '../utils/withInstall.js';
import 'lodash/capitalize';
import './style/index.js';
import '../popup/type.js';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var TABLE_PROPS = ["rowKey", "rowClassName", "columns", "fixedRows", "footData", "rowAttributes", "rowspanAndColspan", "onCellClick", "onRowClick", "onRowDblclick", "onRowMouseover", "onRowMousedown", "onRowMouseenter", "onRowMouseleave", "onRowMouseup"];
var ROW_LISTENERS = ["click", "dblclick", "mouseover", "mousedown", "mouseenter", "mouseleave", "mouseup"];
function renderCell(params, slots) {
  var col = params.col,
      row = params.row;

  if (isFunction(col.cell)) {
    return col.cell(h, params);
  }

  if (slots[col.colKey]) {
    return slots[col.colKey](params);
  }

  if (isString(col.cell) && slots[col.cell]) {
    return slots[col.cell](params);
  }

  if (isFunction(col.render)) {
    return col.render(h, _objectSpread(_objectSpread({}, params), {}, {
      type: "cell"
    }));
  }

  return get(row, col.colKey);
}
var TrElement = defineComponent({
  name: "TR",
  props: _objectSpread(_objectSpread({
    row: Object,
    rowIndex: Number,
    dataLength: Number,
    rowAndColFixedPosition: Map,
    skipSpansMap: Map,
    onTrRowspanOrColspan: Function
  }, pick(baseTableProps, TABLE_PROPS)), {}, {
    scrollType: String,
    rowHeight: Number,
    trs: Map,
    bufferSize: Number,
    isVirtual: Boolean,
    tableElm: {}
  }),
  setup: function setup(props, context) {
    var _useClassName = useClassName(),
        tdEllipsisClass = _useClassName.tdEllipsisClass,
        tableBaseClass = _useClassName.tableBaseClass,
        tableColFixedClasses = _useClassName.tableColFixedClasses,
        tableRowFixedClasses = _useClassName.tableRowFixedClasses,
        tdAlignClasses = _useClassName.tdAlignClasses;

    var trStyles = computed(function () {
      return getRowFixedStyles(get(props.row, props.rowKey || "id"), props.rowIndex, props.dataLength, props.fixedRows, props.rowAndColFixedPosition, tableRowFixedClasses);
    });
    var trAttributes = computed(function () {
      return formatRowAttributes(props.rowAttributes, {
        row: props.row,
        rowIndex: props.rowIndex,
        type: "body"
      });
    });
    var classes = computed(function () {
      var _trStyles$value;

      var customClasses = formatRowClassNames(props.rowClassName, {
        row: props.row,
        rowIndex: props.rowIndex,
        type: "body"
      }, props.rowKey || "id");
      return [(_trStyles$value = trStyles.value) === null || _trStyles$value === void 0 ? void 0 : _trStyles$value.classes, customClasses];
    });

    var getTrListeners = function getTrListeners(row, rowIndex) {
      var trListeners = {};
      ROW_LISTENERS.forEach(function (eventName) {
        trListeners[eventName] = function (e) {
          var _props$;

          var p = {
            e: e,
            row: row,
            index: rowIndex
          };
          (_props$ = props["onRow".concat(upperFirst(eventName))]) === null || _props$ === void 0 ? void 0 : _props$.call(props, p);
          context.emit("row-".concat(eventName), p);
        };
      });
      return trListeners;
    };

    var observe = function observe(element, root, callback, marginBottom) {
      if (!window || !window.IntersectionObserver) {
        callback();
        return;
      }

      try {
        var io = new window.IntersectionObserver(function (entries) {
          var entry = entries[0];

          if (entry.isIntersecting) {
            callback();
            io.unobserve(element);
          }
        }, {
          rootMargin: "0px 0px ".concat(marginBottom, "px 0px"),
          root: root
        });
        io.observe(element);
        return io;
      } catch (e) {
        console.error(e);
        callback();
      }
    };

    var tr = ref(null);
    var isInit = ref(props.rowIndex === 0);

    var init = function init() {
      !isInit.value && requestAnimationFrame(function () {
        isInit.value = true;
      });
    };

    onMounted(function () {
      var rowIndex = props.rowIndex,
          rowHeight = props.rowHeight,
          bufferSize = props.bufferSize,
          trs = props.trs,
          rowData = props.row,
          scrollType = props.scrollType,
          isVirtual = props.isVirtual;

      if (scrollType === "virtual") {
        if (isVirtual) {
          var $index = rowData.$index;
          trs.set($index, tr.value);
          context.emit("onRowMounted");
        }
      } else if (scrollType === "lazy") {
        var tableContentRef = inject("tableContentRef");
        var rowHeightRef = inject("rowHeightRef");
        nextTick(function () {
          if (rowHeight === void 0) {
            if (rowIndex === 0) {
              var offsetHeight = tr.value.offsetHeight;
              rowHeightRef.value = offsetHeight;
            } else {
              var height = rowHeightRef.value;
              observe(tr.value, tableContentRef.value, init, height * bufferSize);
            }
          } else {
            observe(tr.value, tableContentRef.value, init, rowHeight * bufferSize);
          }
        });
      }
    });
    onBeforeUnmount(function () {
      if (props.isVirtual) {
        var trs = props.trs,
            row = props.row;
        var $index = row.$index;
        trs["delete"]($index);
      }
    });
    return {
      tableColFixedClasses: tableColFixedClasses,
      tSlots: context.slots,
      tdEllipsisClass: tdEllipsisClass,
      tableBaseClass: tableBaseClass,
      tdAlignClasses: tdAlignClasses,
      trStyles: trStyles,
      classes: classes,
      trAttributes: trAttributes,
      getTrListeners: getTrListeners,
      tr: tr,
      isInit: isInit
    };
  },
  methods: {
    renderEllipsisCell: function renderEllipsisCell(h2, cellParams, params) {
      var _this = this;

      var h = this.$createElement;
      var cellNode = params.cellNode;
      var col = cellParams.col,
          colIndex = cellParams.colIndex;
      var placement = colIndex < 2 ? "top-left" : "top-right";
      var content = isFunction(col.ellipsis) ? col.ellipsis(h2, cellParams) : void 0;
      return h(TEllipsis, {
        "attrs": {
          "placement": placement,
          "attach": this.tableElm ? function () {
            return _this.tableElm;
          } : void 0,
          "popupContent": content && function () {
            return content;
          },
          "popupProps": _typeof(col.ellipsis) === "object" ? col.ellipsis : void 0
        }
      }, [cellNode]);
    },
    renderTd: function renderTd(h2, params, extra) {
      var _ref,
          _this2 = this;

      var h = this.$createElement;
      var col = params.col,
          colIndex = params.colIndex,
          rowIndex = params.rowIndex;
      var cellSpans = extra.cellSpans,
          dataLength = extra.dataLength,
          rowAndColFixedPosition = extra.rowAndColFixedPosition;
      var cellNode = renderCell(params, this.tSlots);
      var tdStyles = getColumnFixedStyles(col, colIndex, rowAndColFixedPosition, this.tableColFixedClasses);
      var customClasses = isFunction(col.className) ? col.className(_objectSpread(_objectSpread({}, params), {}, {
        type: "td"
      })) : col.className;
      var classes = [tdStyles.classes, customClasses, (_ref = {}, _defineProperty(_ref, this.tdEllipsisClass, col.ellipsis), _defineProperty(_ref, this.tableBaseClass.tdLastRow, rowIndex + cellSpans.rowspan === dataLength), _defineProperty(_ref, this.tdAlignClasses[col.align], col.align && col.align !== "left"), _ref)];

      var onClick = function onClick(e) {
        var _this2$onCellClick;

        var p = _objectSpread(_objectSpread({}, params), {}, {
          e: e
        });

        (_this2$onCellClick = _this2.onCellClick) === null || _this2$onCellClick === void 0 ? void 0 : _this2$onCellClick.call(_this2, p);

        _this2.$emit("cell-click", p);
      };

      return h("td", helper([{
        "class": classes,
        "style": tdStyles.style
      }, {
        "attrs": _objectSpread(_objectSpread({}, col.attrs), cellSpans)
      }, {
        "on": {
          "click": onClick
        }
      }]), [col.ellipsis ? this.renderEllipsisCell(h2, params, {
        cellNode: cellNode
      }) : cellNode]);
    }
  },
  render: function render(h2) {
    var _this$columns,
        _this3 = this,
        _this$trStyles;

    var h = arguments[0];
    var row = this.row,
        rowIndex = this.rowIndex,
        dataLength = this.dataLength,
        rowAndColFixedPosition = this.rowAndColFixedPosition,
        scrollType = this.scrollType,
        isInit = this.isInit;
    var hasHolder = scrollType === "lazy" && !isInit;
    var rowHeightRef = inject("rowHeightRef");
    var columVNodeList = (_this$columns = this.columns) === null || _this$columns === void 0 ? void 0 : _this$columns.map(function (col, colIndex) {
      var _this3$skipSpansMap;

      var cellSpans = {};
      var params = {
        row: row,
        col: col,
        rowIndex: rowIndex,
        colIndex: colIndex
      };

      if (isFunction(_this3.rowspanAndColspan)) {
        var _this3$onTrRowspanOrC;

        var o = _this3.rowspanAndColspan(params);

        (o === null || o === void 0 ? void 0 : o.rowspan) > 1 && (cellSpans.rowspan = o.rowspan);
        (o === null || o === void 0 ? void 0 : o.colspan) > 1 && (cellSpans.colspan = o.colspan);
        (_this3$onTrRowspanOrC = _this3.onTrRowspanOrColspan) === null || _this3$onTrRowspanOrC === void 0 ? void 0 : _this3$onTrRowspanOrC.call(_this3, params, cellSpans);
      }

      var skipped = (_this3$skipSpansMap = _this3.skipSpansMap) === null || _this3$skipSpansMap === void 0 ? void 0 : _this3$skipSpansMap.get([rowIndex, colIndex].join());
      if (skipped) return null;
      return _this3.renderTd(h2, params, {
        dataLength: dataLength,
        rowAndColFixedPosition: rowAndColFixedPosition,
        columnLength: _this3.columns.length,
        cellSpans: cellSpans
      });
    });
    return h("tr", helper([{
      "ref": "tr"
    }, {
      "attrs": this.trAttributes
    }, {
      "style": (_this$trStyles = this.trStyles) === null || _this$trStyles === void 0 ? void 0 : _this$trStyles.style,
      "class": this.classes
    }, {
      "on": this.getTrListeners(row, rowIndex)
    }]), [hasHolder ? [h("td", {
      "style": {
        height: "".concat(rowHeightRef.value, "px"),
        border: "none"
      }
    })] : columVNodeList]);
  }
});

export { ROW_LISTENERS, TABLE_PROPS, TrElement as default, renderCell };
//# sourceMappingURL=tr.js.map
