import * as _vue from "vue";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import { inject } from 'vue';
import PropTypes from '../../_util/vue-types';
import classNames from '../../_util/classNames';
import ColGroup from './ColGroup';
import TableHeader from './TableHeader';
import TableRow from './TableRow';
import ExpandableRow from './ExpandableRow';
import { connect } from '../../_util/store';

function noop() {}

var BaseTable = {
  name: 'BaseTable',
  inheritAttrs: false,
  props: {
    fixed: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
    columns: PropTypes.array.isRequired,
    tableClassName: PropTypes.string.isRequired,
    hasHead: PropTypes.bool.isRequired,
    hasBody: PropTypes.bool.isRequired,
    store: PropTypes.object.isRequired,
    expander: PropTypes.object.isRequired,
    getRowKey: PropTypes.func,
    isAnyColumnsFixed: PropTypes.bool
  },
  setup: function setup() {
    return {
      table: inject('table', {})
    };
  },
  methods: {
    getColumns: function getColumns(cols) {
      var _this$$props = this.$props,
          _this$$props$columns = _this$$props.columns,
          columns = _this$$props$columns === void 0 ? [] : _this$$props$columns,
          fixed = _this$$props.fixed;
      var table = this.table;
      var prefixCls = table.$props.prefixCls;
      return (cols || columns).map(function (column) {
        return _objectSpread(_objectSpread({}, column), {}, {
          className: !!column.fixed && !fixed ? classNames("".concat(prefixCls, "-fixed-columns-in-body"), column.className, column.class) : classNames(column.className, column.class)
        });
      });
    },
    handleRowHover: function handleRowHover(isHover, key) {
      this.store.setState({
        currentHoverKey: isHover ? key : null
      });
    },
    renderRows: function renderRows(renderData, indent) {
      var _this = this;

      var ancestorKeys = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];

      var _this$table$$attrs$th = _objectSpread(_objectSpread(_objectSpread({}, this.table.$attrs), this.table.$props), this.table.$data),
          columnManager = _this$table$$attrs$th.columnManager,
          components = _this$table$$attrs$th.sComponents,
          prefixCls = _this$table$$attrs$th.prefixCls,
          childrenColumnName = _this$table$$attrs$th.childrenColumnName,
          rowClassName = _this$table$$attrs$th.rowClassName,
          _this$table$$attrs$th2 = _this$table$$attrs$th.customRow,
          customRow = _this$table$$attrs$th2 === void 0 ? noop : _this$table$$attrs$th2,
          _this$table$$attrs$th3 = _this$table$$attrs$th.onRowClick,
          onRowClick = _this$table$$attrs$th3 === void 0 ? noop : _this$table$$attrs$th3,
          _this$table$$attrs$th4 = _this$table$$attrs$th.onRowDoubleClick,
          onRowDoubleClick = _this$table$$attrs$th4 === void 0 ? noop : _this$table$$attrs$th4,
          _this$table$$attrs$th5 = _this$table$$attrs$th.onRowContextMenu,
          onRowContextMenu = _this$table$$attrs$th5 === void 0 ? noop : _this$table$$attrs$th5,
          _this$table$$attrs$th6 = _this$table$$attrs$th.onRowMouseEnter,
          onRowMouseEnter = _this$table$$attrs$th6 === void 0 ? noop : _this$table$$attrs$th6,
          _this$table$$attrs$th7 = _this$table$$attrs$th.onRowMouseLeave,
          onRowMouseLeave = _this$table$$attrs$th7 === void 0 ? noop : _this$table$$attrs$th7,
          rowRef = _this$table$$attrs$th.rowRef;

      var getRowKey = this.getRowKey,
          fixed = this.fixed,
          expander = this.expander,
          isAnyColumnsFixed = this.isAnyColumnsFixed;
      var rows = [];

      var _loop = function _loop(i) {
        var record = renderData[i];
        var key = getRowKey(record, i);
        var className = typeof rowClassName === 'string' ? rowClassName : rowClassName(record, i, indent);
        var onHoverProps = {};

        if (columnManager.isAnyColumnsFixed()) {
          onHoverProps.onHover = _this.handleRowHover;
        }

        var leafColumns = void 0;

        if (fixed === 'left') {
          leafColumns = columnManager.leftLeafColumns();
        } else if (fixed === 'right') {
          leafColumns = columnManager.rightLeafColumns();
        } else {
          leafColumns = _this.getColumns(columnManager.leafColumns());
        }

        var rowPrefixCls = "".concat(prefixCls, "-row");

        var expandableRowProps = _objectSpread(_objectSpread({}, expander.props), {}, {
          fixed: fixed,
          index: i,
          prefixCls: rowPrefixCls,
          record: record,
          rowKey: key,
          needIndentSpaced: expander.needIndentSpaced,
          key: key,
          onRowClick: onRowClick,
          onExpandedChange: expander.handleExpandChange
        });

        var row = _vue.createVNode(ExpandableRow, expandableRowProps, {
          default: function _default(expandableRow) {
            var tableRowProps = _objectSpread(_objectSpread({
              fixed: fixed,
              indent: indent,
              record: record,
              index: i,
              prefixCls: rowPrefixCls,
              childrenColumnName: childrenColumnName,
              columns: leafColumns,
              rowKey: key,
              ancestorKeys: ancestorKeys,
              components: components,
              isAnyColumnsFixed: isAnyColumnsFixed,
              customRow: customRow,
              onRowDoubleClick: onRowDoubleClick,
              onRowContextMenu: onRowContextMenu,
              onRowMouseEnter: onRowMouseEnter,
              onRowMouseLeave: onRowMouseLeave
            }, onHoverProps), {}, {
              class: className,
              ref: rowRef(record, i, indent)
            }, expandableRow);

            return _vue.createVNode(TableRow, tableRowProps, null);
          }
        });

        rows.push(row);
        expander.renderRows(_this.renderRows, rows, record, i, indent, fixed, key, ancestorKeys);
      };

      for (var i = 0; i < renderData.length; i += 1) {
        _loop(i);
      }

      return rows;
    }
  },
  render: function render() {
    var _this2 = this;

    var _this$table = this.table,
        components = _this$table.sComponents,
        prefixCls = _this$table.prefixCls,
        scroll = _this$table.scroll,
        data = _this$table.data;
    var _this$$props2 = this.$props,
        expander = _this$$props2.expander,
        tableClassName = _this$$props2.tableClassName,
        hasHead = _this$$props2.hasHead,
        hasBody = _this$$props2.hasBody,
        fixed = _this$$props2.fixed,
        isAnyColumnsFixed = _this$$props2.isAnyColumnsFixed;
    var tableStyle = {};

    if (!fixed && scroll.x) {
      // 当有固定列时，width auto 会导致 body table 的宽度撑不开，从而固定列无法对齐
      // 详情见：https://github.com/ant-design/ant-design/issues/22160
      var tableWidthScrollX = isAnyColumnsFixed ? 'max-content' : 'auto'; // not set width, then use content fixed width

      tableStyle.width = scroll.x === true ? tableWidthScrollX : scroll.x;
      tableStyle.width = typeof tableStyle.width === 'number' ? "".concat(tableStyle.width, "px") : tableStyle.width;
    }

    var Table = hasBody ? components.table : 'table';
    var BodyWrapper = components.body.wrapper;
    var body;

    if (hasBody) {
      body = _vue.createVNode(BodyWrapper, {
        "class": "".concat(prefixCls, "-tbody")
      }, {
        default: function _default() {
          return [_this2.renderRows(data, 0)];
        }
      });
    }

    var columns = this.getColumns();
    return _vue.createVNode(Table, {
      "class": tableClassName,
      "style": tableStyle,
      "key": "table"
    }, {
      default: function _default() {
        return [_vue.createVNode(ColGroup, {
          "columns": columns,
          "fixed": fixed
        }, null), hasHead && _vue.createVNode(TableHeader, {
          "expander": expander,
          "columns": columns,
          "fixed": fixed
        }, null), body];
      }
    });
  }
};
export default connect()(BaseTable);