import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _typeof from "@babel/runtime/helpers/esm/typeof";
var _excluded = ["colSpan", "rowSpan", "style", "class"];
import { createVNode as _createVNode } from "vue";
import classNames from '../../_util/classNames';
import { flattenChildren, isValidElement, parseStyleText } from '../../_util/props-util';
import { defineComponent, isVNode, renderSlot } from 'vue';
import { getPathValue, validateValue } from '../utils/valueUtil';
import { useInjectSlots } from '../../table/context';
import { INTERNAL_COL_DEFINE } from '../utils/legacyUtil';

function isRenderCell(data) {
  return data && _typeof(data) === 'object' && !Array.isArray(data) && !isValidElement(data);
}

export default defineComponent({
  name: 'Cell',
  props: ['prefixCls', 'record', 'index', 'dataIndex', 'customRender', 'component', 'colSpan', 'rowSpan', 'fixLeft', 'fixRight', 'firstFixLeft', 'lastFixLeft', 'firstFixRight', 'lastFixRight', 'appendNode', 'additionalProps', 'ellipsis', 'align', 'rowType', 'isSticky', 'column', 'cellType', 'transformCellText'],
  slots: ['appendNode'],
  setup: function setup(props, _ref) {
    var slots = _ref.slots;
    var contextSlots = useInjectSlots();
    return function () {
      var _slots$appendNode, _slots$default, _classNames;

      var prefixCls = props.prefixCls,
          record = props.record,
          index = props.index,
          dataIndex = props.dataIndex,
          customRender = props.customRender,
          _props$component = props.component,
          Component = _props$component === void 0 ? 'td' : _props$component,
          colSpan = props.colSpan,
          rowSpan = props.rowSpan,
          fixLeft = props.fixLeft,
          fixRight = props.fixRight,
          firstFixLeft = props.firstFixLeft,
          lastFixLeft = props.lastFixLeft,
          firstFixRight = props.firstFixRight,
          lastFixRight = props.lastFixRight,
          _props$appendNode = props.appendNode,
          appendNode = _props$appendNode === void 0 ? (_slots$appendNode = slots.appendNode) === null || _slots$appendNode === void 0 ? void 0 : _slots$appendNode.call(slots) : _props$appendNode,
          _props$additionalProp = props.additionalProps,
          additionalProps = _props$additionalProp === void 0 ? {} : _props$additionalProp,
          ellipsis = props.ellipsis,
          align = props.align,
          rowType = props.rowType,
          isSticky = props.isSticky,
          _props$column = props.column,
          column = _props$column === void 0 ? {} : _props$column,
          cellType = props.cellType;
      var cellPrefixCls = "".concat(prefixCls, "-cell"); // ==================== Child Node ====================

      var cellProps;
      var childNode;
      var children = (_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots);

      if (validateValue(children) || cellType === 'header') {
        childNode = children;
      } else {
        var _column$slots;

        var value = getPathValue(record, dataIndex); // Customize render node

        childNode = value;

        if (customRender) {
          var renderData = customRender({
            text: value,
            value: value,
            record: record,
            index: index,
            column: column.__originColumn__
          });

          if (isRenderCell(renderData)) {
            childNode = renderData.children;
            cellProps = renderData.props;
          } else {
            childNode = renderData;
          }
        }

        if (!(INTERNAL_COL_DEFINE in column) && cellType === 'body' && contextSlots.value.bodyCell && !((_column$slots = column.slots) !== null && _column$slots !== void 0 && _column$slots.customRender)) {
          var child = renderSlot(contextSlots.value, 'bodyCell', {
            text: value,
            value: value,
            record: record,
            index: index,
            column: column.__originColumn__
          }, function () {
            return [childNode === undefined ? value : childNode];
          });
          childNode = flattenChildren(child);
        }
        /** maybe we should @deprecated */


        if (props.transformCellText) {
          childNode = props.transformCellText({
            text: childNode,
            record: record,
            index: index,
            column: column.__originColumn__
          });
        }
      } // Not crash if final `childNode` is not validate ReactNode


      if (_typeof(childNode) === 'object' && !Array.isArray(childNode) && !isValidElement(childNode)) {
        childNode = null;
      }

      if (ellipsis && (lastFixLeft || firstFixRight)) {
        var _childNode = function () {
          return childNode;
        }();

        childNode = _createVNode("span", {
          "class": "".concat(cellPrefixCls, "-content")
        }, [childNode]);
      }

      if (Array.isArray(childNode) && childNode.length === 1) {
        childNode = childNode[0];
      }

      var _ref2 = cellProps || {},
          cellColSpan = _ref2.colSpan,
          cellRowSpan = _ref2.rowSpan,
          cellStyle = _ref2.style,
          cellClassName = _ref2.class,
          restCellProps = _objectWithoutProperties(_ref2, _excluded);

      var mergedColSpan = cellColSpan !== undefined ? cellColSpan : colSpan;
      var mergedRowSpan = cellRowSpan !== undefined ? cellRowSpan : rowSpan;

      if (mergedColSpan === 0 || mergedRowSpan === 0) {
        return null;
      } // ====================== Fixed =======================


      var fixedStyle = {};
      var isFixLeft = typeof fixLeft === 'number';
      var isFixRight = typeof fixRight === 'number';

      if (isFixLeft) {
        fixedStyle.position = 'sticky';
        fixedStyle.left = "".concat(fixLeft, "px");
      }

      if (isFixRight) {
        fixedStyle.position = 'sticky';
        fixedStyle.right = "".concat(fixRight, "px");
      } // ====================== Align =======================


      var alignStyle = {};

      if (align) {
        alignStyle.textAlign = align;
      } // ====================== Render ======================


      var title;
      var ellipsisConfig = ellipsis === true ? {
        showTitle: true
      } : ellipsis;

      if (ellipsisConfig && (ellipsisConfig.showTitle || rowType === 'header')) {
        if (typeof childNode === 'string' || typeof childNode === 'number') {
          title = childNode.toString();
        } else if (isVNode(childNode) && typeof childNode.children === 'string') {
          title = childNode.children;
        }
      }

      var componentProps = _objectSpread(_objectSpread(_objectSpread({
        title: title
      }, restCellProps), additionalProps), {}, {
        colSpan: mergedColSpan && mergedColSpan !== 1 ? mergedColSpan : null,
        rowSpan: mergedRowSpan && mergedRowSpan !== 1 ? mergedRowSpan : null,
        class: classNames(cellPrefixCls, (_classNames = {}, _defineProperty(_classNames, "".concat(cellPrefixCls, "-fix-left"), isFixLeft), _defineProperty(_classNames, "".concat(cellPrefixCls, "-fix-left-first"), firstFixLeft), _defineProperty(_classNames, "".concat(cellPrefixCls, "-fix-left-last"), lastFixLeft), _defineProperty(_classNames, "".concat(cellPrefixCls, "-fix-right"), isFixRight), _defineProperty(_classNames, "".concat(cellPrefixCls, "-fix-right-first"), firstFixRight), _defineProperty(_classNames, "".concat(cellPrefixCls, "-fix-right-last"), lastFixRight), _defineProperty(_classNames, "".concat(cellPrefixCls, "-ellipsis"), ellipsis), _defineProperty(_classNames, "".concat(cellPrefixCls, "-with-append"), appendNode), _defineProperty(_classNames, "".concat(cellPrefixCls, "-fix-sticky"), (isFixLeft || isFixRight) && isSticky), _classNames), additionalProps.class, cellClassName),
        style: _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, parseStyleText(additionalProps.style)), alignStyle), fixedStyle), cellStyle)
      });

      return _createVNode(Component, componentProps, {
        default: function _default() {
          return [appendNode, childNode];
        }
      });
    };
  }
});