/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

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

var _typeof = require('@babel/runtime/helpers/typeof');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
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');
var table_hooks_useLazyLoad = require('./hooks/useLazyLoad.js');
var table_hooks_useRowspanAndColspan = require('./hooks/useRowspanAndColspan.js');
require('lodash/isObject');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/debounce');
require('../_common/js/log/log.js');
require('../_common/js/utils/getScrollbarWidth.js');
require('../_common/js/utils/helper.js');
require('@babel/runtime/helpers/toConsumableArray');
require('@babel/runtime/helpers/objectWithoutProperties');
require('lodash/isNull');
require('lodash/isUndefined');
require('lodash/isNumber');
require('lodash/isArray');
require('../utils/dom.js');
require('../_chunks/dep-7f0559c3.js');
require('../utils/easing.js');
require('../hooks/useConfig.js');
require('../config-provider/useConfig.js');
require('lodash/cloneDeep');
require('../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('dayjs');
require('../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../tooltip/index.js');
require('../utils/withInstall.js');
require('../tooltip/tooltip.js');
require('lodash/omit');
require('../popup/props.js');
require('../popup/index.js');
require('../popup/popup.js');
require('@popperjs/core');
require('../hooks/useVModel.js');
require('../_common/js/utils/set-style.js');
require('../popup/container.js');
require('../hooks/useResizeObserver.js');
require('../hooks/tnode.js');
require('../tooltip/util.js');
require('../tooltip/props.js');
require('../_common/js/utils/observe.js');

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; }
function _isSlot(s) {
  return typeof s === 'function' || Object.prototype.toString.call(s) === '[object Object]' && !vue.isVNode(s);
}
var TABLE_PROPS = ["rowKey", "rowClassName", "columns", "fixedRows", "footData", "rowAttributes", "rowspanAndColspan", "scroll", "cellEmptyContent", "pagination", "attach", "onCellClick", "onRowClick", "onRowDblclick", "onRowMouseover", "onRowMousedown", "onRowMouseenter", "onRowMouseleave", "onRowMouseup"];
var ROW_LISTENERS = ["click", "dblclick", "mouseover", "mousedown", "mouseenter", "mouseleave", "mouseup"];
function renderCell(params, slots, extra) {
  var col = params.col,
    row = params.row,
    rowIndex = params.rowIndex;
  if (col.colKey === "serial-number") {
    var _ref = (extra === null || extra === void 0 ? void 0 : extra.pagination) || {},
      current = _ref.current,
      pageSize = _ref.pageSize,
      defaultCurrent = _ref.defaultCurrent,
      defaultPageSize = _ref.defaultPageSize;
    var tCurrent = current || defaultCurrent;
    var tPageSize = pageSize || defaultPageSize;
    if (tPageSize && tCurrent) return tPageSize * (tCurrent - 1) + rowIndex + 1;
    return rowIndex + 1;
  }
  if (isFunction__default["default"](col.cell)) return col.cell(vue.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(vue.h, _objectSpread(_objectSpread({}, params), {}, {
    type: "cell"
  }));
  var r = get__default["default"](row, col.colKey);
  if (![void 0, "", null].includes(r)) return r;
  if (extra !== null && extra !== void 0 && extra.cellEmptyContent) return isFunction__default["default"](extra.cellEmptyContent) ? extra.cellEmptyContent(vue.h, params) : extra.cellEmptyContent;
  if (slots.cellEmptyContent) return slots.cellEmptyContent(params);
  return r;
}
var TrElement = vue.defineComponent({
  name: "TR",
  props: _objectSpread(_objectSpread({
    row: Object,
    rowIndex: Number,
    dataLength: Number,
    ellipsisOverlayClassName: String,
    classPrefix: String,
    rowAndColFixedPosition: Map,
    skipSpansMap: Map,
    virtualConfig: Object
  }, pick__default["default"](table_baseTableProps["default"], TABLE_PROPS)), {}, {
    tableElm: {},
    tableContentElm: {}
  }),
  emits: ["row-mounted"],
  setup: function setup(props, context) {
    var _toRefs = vue.toRefs(props),
      tableContentElm = _toRefs.tableContentElm;
    var trRef = vue.ref(null);
    var _useClassName = table_hooks_useClassName["default"](),
      tdEllipsisClass = _useClassName.tdEllipsisClass,
      tableBaseClass = _useClassName.tableBaseClass,
      tableColFixedClasses = _useClassName.tableColFixedClasses,
      tableRowFixedClasses = _useClassName.tableRowFixedClasses,
      tdAlignClasses = _useClassName.tdAlignClasses,
      tableDraggableClasses = _useClassName.tableDraggableClasses;
    var trStyles = vue.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 = vue.computed(function () {
      return table_utils.formatRowAttributes(props.rowAttributes, {
        row: props.row,
        rowIndex: props.rowIndex,
        type: "body"
      }) || {};
    });
    var classes = vue.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].filter(function (v) {
        return v;
      });
    });
    var _useLazyLoad = table_hooks_useLazyLoad["default"](tableContentElm, trRef, vue.reactive(_objectSpread(_objectSpread({}, props.scroll), {}, {
        rowIndex: props.rowIndex
      }))),
      hasLazyLoadHolder = _useLazyLoad.hasLazyLoadHolder,
      tRowHeight = _useLazyLoad.tRowHeight;
    var getTrListeners = function getTrListeners(row, rowIndex) {
      var trListeners = {};
      ROW_LISTENERS.forEach(function (eventName) {
        trListeners["on".concat(upperFirst__default["default"](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);
        };
      });
      return trListeners;
    };
    vue.watch([trRef], function () {
      var _props$virtualConfig;
      if ((_props$virtualConfig = props.virtualConfig) !== null && _props$virtualConfig !== void 0 && _props$virtualConfig.isVirtualScroll.value) {
        context.emit("row-mounted", {
          ref: trRef,
          data: props.row
        });
      }
    });
    return {
      trRef: trRef,
      tableColFixedClasses: tableColFixedClasses,
      tdEllipsisClass: tdEllipsisClass,
      tableBaseClass: tableBaseClass,
      tdAlignClasses: tdAlignClasses,
      tableDraggableClasses: tableDraggableClasses,
      trStyles: trStyles,
      classes: classes,
      trAttributes: trAttributes,
      tRowHeight: tRowHeight,
      hasLazyLoadHolder: hasLazyLoadHolder,
      getTrListeners: getTrListeners
    };
  },
  methods: {
    renderEllipsisCell: function renderEllipsisCell(cellParams, params) {
      var cellNode = params.cellNode;
      var col = cellParams.col,
        colIndex = cellParams.colIndex;
      var content = isFunction__default["default"](col.ellipsis) ? col.ellipsis(vue.h, cellParams) : void 0;
      if (_typeof__default["default"](col.ellipsis) === "object" && isFunction__default["default"](col.ellipsis.content)) content = col.ellipsis.content(vue.h, cellParams);
      var tooltipProps = {};
      if (_typeof__default["default"](col.ellipsis) === "object") tooltipProps = "props" in col.ellipsis ? col.ellipsis.props : col.ellipsis || void 0;
      var tableElement = this.tableElm;
      var placement = colIndex === 0 ? "top-left" : "top";
      placement = colIndex === this.columns.length - 1 ? "top-right" : placement;
      return vue.createVNode(table_ellipsis["default"], {
        "placement": placement,
        "attach": tableElement ? function () {
          return tableElement;
        } : void 0,
        "tooltipContent": content && function () {
          return content;
        },
        "tooltipProps": tooltipProps,
        "overlayClassName": this.ellipsisOverlayClassName,
        "classPrefix": this.classPrefix
      }, _isSlot(cellNode) ? cellNode : {
        "default": function _default() {
          return [cellNode];
        }
      });
    },
    renderTd: function renderTd(params, extra) {
      var _ref2,
        _this = this;
      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.$slots, {
        cellEmptyContent: extra.cellEmptyContent,
        pagination: this.pagination
      });
      var tdStyles = table_hooks_useFixed.getColumnFixedStyles(col, colIndex, rowAndColFixedPosition, this.tableColFixedClasses);
      var customClasses = table_utils.formatClassNames(col.className, _objectSpread(_objectSpread({}, params), {}, {
        type: "td"
      }));
      var classes = [tdStyles.classes, customClasses, (_ref2 = {}, _defineProperty__default["default"](_ref2, this.tdEllipsisClass, col.ellipsis), _defineProperty__default["default"](_ref2, this.tableBaseClass.tdLastRow, rowIndex + cellSpans.rowspan === dataLength), _defineProperty__default["default"](_ref2, this.tableBaseClass.tdFirstCol, colIndex === 0 && this.rowspanAndColspan), _defineProperty__default["default"](_ref2, this.tdAlignClasses[col.align], col.align && col.align !== "left"), _defineProperty__default["default"](_ref2, this.tableDraggableClasses.handle, col.colKey === "drag"), _ref2)];
      var onClick = function onClick(e) {
        var _this$onCellClick;
        var p = _objectSpread(_objectSpread({}, params), {}, {
          e: e
        });
        if (col.stopPropagation) e.stopPropagation();
        (_this$onCellClick = _this.onCellClick) === null || _this$onCellClick === void 0 ? void 0 : _this$onCellClick.call(_this, p);
      };
      var normalAttrs = isFunction__default["default"](col.attrs) ? col.attrs(_objectSpread(_objectSpread({}, params), {}, {
        type: "td"
      })) : col.attrs;
      var attrs = _objectSpread(_objectSpread({}, normalAttrs), cellSpans);
      return vue.createVNode("td", vue.mergeProps({
        "key": col.colKey || colIndex,
        "class": classes,
        "style": tdStyles.style
      }, attrs, {
        "onClick": onClick
      }), [col.ellipsis ? this.renderEllipsisCell(params, {
        cellNode: cellNode
      }) : cellNode]);
    }
  },
  render: function render() {
    var _this$columns,
      _this2 = this,
      _this$trStyles;
    var row = this.row,
      rowIndex = this.rowIndex,
      dataLength = this.dataLength,
      rowAndColFixedPosition = this.rowAndColFixedPosition;
    var columVNodeList = (_this$columns = this.columns) === null || _this$columns === void 0 ? void 0 : _this$columns.map(function (col, colIndex) {
      var cellSpans = {};
      var params = {
        row: row,
        col: col,
        rowIndex: rowIndex,
        colIndex: colIndex
      };
      var spanState = null;
      if (_this2.skipSpansMap.size) {
        var _spanState, _spanState2;
        var cellKey = table_hooks_useRowspanAndColspan.getCellKey(row, _this2.rowKey, col.colKey, colIndex);
        spanState = _this2.skipSpansMap.get(cellKey) || {};
        ((_spanState = spanState) === null || _spanState === void 0 ? void 0 : _spanState.rowspan) > 1 && (cellSpans.rowspan = spanState.rowspan);
        ((_spanState2 = spanState) === null || _spanState2 === void 0 ? void 0 : _spanState2.colspan) > 1 && (cellSpans.colspan = spanState.colspan);
        if (spanState.skipped) return null;
      }
      return _this2.renderTd(params, {
        dataLength: dataLength,
        rowAndColFixedPosition: rowAndColFixedPosition,
        columnLength: _this2.columns.length,
        cellSpans: cellSpans,
        cellEmptyContent: _this2.cellEmptyContent
      });
    });
    return vue.createVNode("tr", vue.mergeProps({
      "ref": "trRef"
    }, this.trAttributes, {
      "style": (_this$trStyles = this.trStyles) === null || _this$trStyles === void 0 ? void 0 : _this$trStyles.style,
      "class": this.classes
    }, this.getTrListeners(row, rowIndex)), [this.hasLazyLoadHolder ? [vue.createVNode("td", {
      "style": {
        height: "".concat(this.tRowHeight, "px"),
        border: "none"
      }
    }, null)] : columVNodeList]);
  }
});

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