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; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

import T from './Table';
import { getOptionProps, getKey, getPropsData, getSlot } from '../_util/props-util';
var Table = {
  name: 'ATable',
  Column: T.Column,
  ColumnGroup: T.ColumnGroup,
  props: T.props,
  inheritAttrs: false,
  methods: {
    normalize: function normalize() {
      var _this = this;

      var elements = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
      var columns = [];
      elements.forEach(function (element) {
        var _element$props, _element$props2, _element$type;

        if (!element) {
          return;
        }

        var key = getKey(element);
        var style = ((_element$props = element.props) === null || _element$props === void 0 ? void 0 : _element$props.style) || {};
        var cls = ((_element$props2 = element.props) === null || _element$props2 === void 0 ? void 0 : _element$props2.class) || '';
        var props = getPropsData(element);

        var _ref = element.children || {},
            children = _ref.default,
            restSlots = _objectWithoutProperties(_ref, ["default"]);

        var column = _objectSpread(_objectSpread(_objectSpread({}, restSlots), props), {}, {
          style: style,
          class: cls
        });

        if (key) {
          column.key = key;
        }

        if ((_element$type = element.type) === null || _element$type === void 0 ? void 0 : _element$type.__ANT_TABLE_COLUMN_GROUP) {
          column.children = _this.normalize(typeof children === 'function' ? children() : children);
        } else {
          var _element$children;

          var customRender = (_element$children = element.children) === null || _element$children === void 0 ? void 0 : _element$children.default;
          column.customRender = column.customRender || customRender;
        }

        columns.push(column);
      });
      return columns;
    },
    updateColumns: function updateColumns() {
      var _this2 = this;

      var cols = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
      var columns = [];
      var $slots = this.$slots;
      cols.forEach(function (col) {
        var _col$slots = col.slots,
            slots = _col$slots === void 0 ? {} : _col$slots,
            restProps = _objectWithoutProperties(col, ["slots"]);

        var column = _objectSpread({}, restProps);

        Object.keys(slots).forEach(function (key) {
          var name = slots[key];

          if (column[key] === undefined && $slots[name]) {
            column[key] = $slots[name];
          }
        }); // if (slotScopeName && $scopedSlots[slotScopeName]) {
        //   column.customRender = column.customRender || $scopedSlots[slotScopeName]
        // }

        if (col.children) {
          column.children = _this2.updateColumns(column.children);
        }

        columns.push(column);
      });
      return columns;
    }
  },
  render: function render() {
    var normalize = this.normalize,
        $slots = this.$slots;

    var props = _objectSpread(_objectSpread({}, getOptionProps(this)), this.$attrs);

    var columns = props.columns ? this.updateColumns(props.columns) : normalize(getSlot(this));
    var title = props.title,
        footer = props.footer;
    var slotTitle = $slots.title,
        slotFooter = $slots.footer,
        _$slots$expandedRowRe = $slots.expandedRowRender,
        expandedRowRender = _$slots$expandedRowRe === void 0 ? props.expandedRowRender : _$slots$expandedRowRe;
    title = title || slotTitle;
    footer = footer || slotFooter;

    var tProps = _objectSpread(_objectSpread({}, props), {}, {
      columns: columns,
      title: title,
      footer: footer,
      expandedRowRender: expandedRowRender
    });

    return _vue.createVNode(T, _vue.mergeProps(tProps, {
      "ref": "table"
    }), null);
  }
};
/* istanbul ignore next */

Table.install = function (app) {
  app.component(Table.name, Table);
  app.component(Table.Column.name, Table.Column);
  app.component(Table.ColumnGroup.name, Table.ColumnGroup);
};

export default Table;