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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var helper = require('../_chunks/dep-33ee4bce.js');
var _typeof = require('@babel/runtime/helpers/typeof');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var VueCompositionAPI = require('@vue/composition-api');
var isFunction = require('lodash/isFunction');
var upperFirst = require('lodash/upperFirst');
var isString = require('lodash/isString');
var pick = require('lodash/pick');
var get = require('lodash/get');
var table_utils = require('./utils.js');
var table_hooks_useFixed = require('./hooks/useFixed.js');
var table_hooks_useClassName = require('./hooks/useClassName.js');
var table_ellipsis = require('./ellipsis.js');
var table_baseTableProps = require('./base-table-props.js');
require('@babel/runtime/helpers/slicedToArray');
require('../_common/js/log/log.js');
require('../_common/js/utils/getScrollbarWidth.js');
require('../utils/dom.js');
require('vue');
require('raf');
require('../utils/easing.js');
require('../config-provider/useConfig.js');
require('lodash/cloneDeep');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('../utils/render-tnode.js');
require('lodash/isObject');
require('../popup/index.js');
require('../popup/popup.js');
require('@popperjs/core');
require('../config.js');
require('../utils/classnames.js');
require('../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('lodash/camelCase');
require('../_common/js/utils/set-style.js');
require('../popup/props.js');
require('../popup/container.js');
require('../utils/map-props.js');
require('../utils/withInstall.js');
require('lodash/capitalize');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);
var upperFirst__default = /*#__PURE__*/_interopDefaultLegacy(upperFirst);
var isString__default = /*#__PURE__*/_interopDefaultLegacy(isString);
var pick__default = /*#__PURE__*/_interopDefaultLegacy(pick);
var get__default = /*#__PURE__*/_interopDefaultLegacy(get);

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__default["default"](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__default["default"](col.cell)) {
    return col.cell(VueCompositionAPI.h, params);
  }

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

  if (isString__default["default"](col.cell) && slots[col.cell]) {
    return slots[col.cell](params);
  }

  if (isFunction__default["default"](col.render)) {
    return col.render(VueCompositionAPI.h, _objectSpread(_objectSpread({}, params), {}, {
      type: "cell"
    }));
  }

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

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

      var customClasses = table_utils.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__default["default"](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 = VueCompositionAPI.ref(null);
    var isInit = VueCompositionAPI.ref(props.rowIndex === 0);

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

    VueCompositionAPI.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 = VueCompositionAPI.inject("tableContentRef");
        var rowHeightRef = VueCompositionAPI.inject("rowHeightRef");
        VueCompositionAPI.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);
          }
        });
      }
    });
    VueCompositionAPI.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__default["default"](col.ellipsis) ? col.ellipsis(h2, cellParams) : void 0;
      return h(table_ellipsis["default"], {
        "attrs": {
          "placement": placement,
          "attach": this.tableElm ? function () {
            return _this.tableElm;
          } : void 0,
          "popupContent": content && function () {
            return content;
          },
          "popupProps": _typeof__default["default"](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 = table_hooks_useFixed.getColumnFixedStyles(col, colIndex, rowAndColFixedPosition, this.tableColFixedClasses);
      var customClasses = isFunction__default["default"](col.className) ? col.className(_objectSpread(_objectSpread({}, params), {}, {
        type: "td"
      })) : col.className;
      var classes = [tdStyles.classes, customClasses, (_ref = {}, _defineProperty__default["default"](_ref, this.tdEllipsisClass, col.ellipsis), _defineProperty__default["default"](_ref, this.tableBaseClass.tdLastRow, rowIndex + cellSpans.rowspan === dataLength), _defineProperty__default["default"](_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.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 = VueCompositionAPI.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__default["default"](_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.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]);
  }
});

exports.ROW_LISTENERS = ROW_LISTENERS;
exports.TABLE_PROPS = TABLE_PROPS;
exports["default"] = TrElement;
exports.renderCell = renderCell;
//# sourceMappingURL=tr.js.map
