'use strict';

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

var vue = require('vue');
require('../../../../utils/index.cjs');
var valueUtil = require('../utils/valueUtil.cjs');
var index = require('../Cell/index.cjs');
var TableContext = require('../context/TableContext.cjs');
var BodyContext = require('../context/BodyContext.cjs');
var ExpandedRow = require('./ExpandedRow.cjs');
var _class = require('../../../../utils/dom/class.cjs');

var BodyRow = /* @__PURE__ */ vue.defineComponent({
  name: "BodyRow",
  inheritAttrs: false,
  props: ["record", "index", "renderIndex", "recordKey", "expandedKeys", "rowComponent", "cellComponent", "customRow", "rowExpandable", "indent", "rowKey", "getRowKey", "childrenColumnName"],
  setup(props, {
    attrs
  }) {
    const tableContext = TableContext.useInjectTable();
    const bodyContext = BodyContext.useInjectBody();
    const expandRended = vue.shallowRef(false);
    const expanded = vue.computed(() => props.expandedKeys && props.expandedKeys.has(props.recordKey));
    vue.watchEffect(() => {
      if (expanded.value)
        expandRended.value = true;
    });
    const rowSupportExpand = vue.computed(() => bodyContext.expandableType === "row" && (!props.rowExpandable || props.rowExpandable(props.record)));
    const nestExpandable = vue.computed(() => bodyContext.expandableType === "nest");
    const hasNestChildren = vue.computed(() => props.childrenColumnName && props.record && props.record[props.childrenColumnName]);
    const mergedExpandable = vue.computed(() => rowSupportExpand.value || nestExpandable.value);
    const onInternalTriggerExpand = (record, event) => {
      bodyContext.onTriggerExpand(record, event);
    };
    const additionalProps = vue.computed(() => {
      var _a;
      return ((_a = props.customRow) == null ? void 0 : _a.call(props, props.record, props.index)) || {};
    });
    const onClick = (event, ...args) => {
      var _a, _b;
      if (bodyContext.expandRowByClick && mergedExpandable.value)
        onInternalTriggerExpand(props.record, event);
      (_b = (_a = additionalProps.value) == null ? void 0 : _a.onClick) == null ? void 0 : _b.call(_a, event, ...args);
    };
    const computeRowClassName = vue.computed(() => {
      const {
        record,
        index,
        indent
      } = props;
      const {
        rowClassName
      } = bodyContext;
      if (typeof rowClassName === "string")
        return rowClassName;
      else if (typeof rowClassName === "function")
        return rowClassName(record, index, indent);
      return "";
    });
    const columnsKey = vue.computed(() => valueUtil.getColumnsKey(bodyContext.flattenColumns));
    return () => {
      const {
        class: className,
        style
      } = attrs;
      const {
        record,
        index: index$1,
        rowKey,
        indent = 0,
        rowComponent: RowComponent,
        cellComponent
      } = props;
      const {
        prefixCls,
        fixedInfoList,
        transformCellText
      } = tableContext;
      const {
        flattenColumns,
        expandedRowClassName,
        indentSize,
        expandIcon,
        expandedRowRender,
        expandIconColumnIndex
      } = bodyContext;
      const baseRowNode = vue.createVNode(RowComponent, {
        ...additionalProps.value,
        "data-row-key": rowKey,
        "class": _class.classNames(className, `${prefixCls}-row`, `${prefixCls}-row-level-${indent}`, computeRowClassName.value, additionalProps.value.class),
        "style": [style, additionalProps.value.style],
        "onClick": onClick
      }, {
        default: () => [flattenColumns.map((column, colIndex) => {
          const {
            customRender,
            dataIndex,
            className: columnClassName
          } = column;
          const key = columnsKey[colIndex];
          const fixedInfo = fixedInfoList[colIndex];
          let additionalCellProps;
          if (column.customCell)
            additionalCellProps = column.customCell(record, index$1, column);
          const appendNode = colIndex === (expandIconColumnIndex || 0) && nestExpandable.value ? vue.createVNode(vue.Fragment, null, [vue.createVNode("span", {
            "style": {
              paddingLeft: `${indentSize * indent}px`
            },
            "class": `${prefixCls}-row-indent indent-level-${indent}`
          }, null), expandIcon({
            prefixCls,
            expanded: expanded.value,
            expandable: hasNestChildren.value,
            record,
            onExpand: onInternalTriggerExpand
          })]) : null;
          return vue.createVNode(index.default, {
            "cellType": "body",
            "class": columnClassName,
            "ellipsis": column.ellipsis,
            "align": column.align,
            "component": cellComponent,
            "prefixCls": prefixCls,
            "key": key,
            "record": record,
            "index": index$1,
            "renderIndex": props.renderIndex,
            "dataIndex": dataIndex,
            "customRender": customRender,
            ...fixedInfo,
            "additionalProps": additionalCellProps,
            "column": column,
            "transformCellText": transformCellText,
            "appendNode": appendNode
          }, null);
        })]
      });
      let expandRowNode;
      if (rowSupportExpand.value && (expandRended.value || expanded.value)) {
        const expandContent = expandedRowRender({
          record,
          index: index$1,
          indent: indent + 1,
          expanded: expanded.value
        });
        const computedExpandedRowClassName = expandedRowClassName && expandedRowClassName(record, index$1, indent);
        expandRowNode = vue.createVNode(ExpandedRow.default, {
          "expanded": expanded.value,
          "class": _class.classNames(`${prefixCls}-expanded-row`, `${prefixCls}-expanded-row-level-${indent + 1}`, computedExpandedRowClassName),
          "prefixCls": prefixCls,
          "component": RowComponent,
          "cellComponent": cellComponent,
          "colSpan": flattenColumns.length,
          "isEmpty": false
        }, {
          default: () => [expandContent]
        });
      }
      return vue.createVNode(vue.Fragment, null, [baseRowNode, expandRowNode]);
    };
  }
});

exports.default = BodyRow;
//# sourceMappingURL=BodyRow.cjs.map
