import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React, { Component } from 'react';
import { connect } from 'mini-store';
import TableRow from './TableRow';
import { remove } from './utils';

var ExpandableTable = /*#__PURE__*/function (_Component) {
  _inherits(ExpandableTable, _Component);

  var _super = _createSuper(ExpandableTable);

  function ExpandableTable(props) {
    var _this;

    _classCallCheck(this, ExpandableTable);

    _this = _super.call(this, props);

    _defineProperty(_assertThisInitialized(_this), "handleExpandChange", function (expanded, record, event, rowKey) {
      var destroy = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;

      if (event) {
        event.preventDefault();
        event.stopPropagation();
      }

      var _this$props = _this.props,
          onExpandedRowsChange = _this$props.onExpandedRowsChange,
          onExpand = _this$props.onExpand,
          _this$props$uncontrol = _this$props.uncontrolled,
          uncontrolled = _this$props$uncontrol === void 0 ? false : _this$props$uncontrol;

      var _this$store$getState = _this.store.getState(),
          expandedRowKeys = _this$store$getState.expandedRowKeys;

      if (expanded) {
        // row was expaned
        expandedRowKeys = [].concat(_toConsumableArray(expandedRowKeys), [rowKey]);
      } else {
        // row was collapse
        var expandedRowIndex = expandedRowKeys.indexOf(rowKey);

        if (expandedRowIndex !== -1) {
          expandedRowKeys = remove(expandedRowKeys, rowKey);
        }
      }

      if (!_this.props.expandedRowKeys || uncontrolled) {
        _this.store.setState({
          expandedRowKeys: expandedRowKeys
        });
      }

      onExpandedRowsChange(expandedRowKeys);

      if (!destroy) {
        onExpand(expanded, record);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "renderExpandIndentCell", function (rows, fixed) {
      var _this$props2 = _this.props,
          prefixCls = _this$props2.prefixCls,
          expandIconAsCell = _this$props2.expandIconAsCell;

      if (!expandIconAsCell || fixed === 'right' || !rows.length) {
        return;
      }

      var iconColumn = {
        key: 'rc-table-expand-icon-cell',
        className: "".concat(prefixCls, "-expand-icon-th"),
        title: '',
        rowSpan: rows.length
      };
      rows[0].unshift(_objectSpread(_objectSpread({}, iconColumn), {}, {
        column: iconColumn
      }));
    });

    _defineProperty(_assertThisInitialized(_this), "renderRows", function (renderRows, rows, record, index, indent, fixed, parentKey, ancestorKeys) {
      var _this$props3 = _this.props,
          expandedRowClassName = _this$props3.expandedRowClassName,
          expandedRowRender = _this$props3.expandedRowRender,
          childrenColumnName = _this$props3.childrenColumnName;
      var childrenData = record[childrenColumnName];
      var nextAncestorKeys = [].concat(_toConsumableArray(ancestorKeys), [parentKey]);
      var nextIndent = indent + 1;

      if (expandedRowRender) {
        rows.push(_this.renderExpandedRow(record, index, expandedRowRender, expandedRowClassName(record, index, indent), nextAncestorKeys, nextIndent, fixed));
      }

      if (childrenData) {
        renderRows(childrenData, nextIndent, rows, nextAncestorKeys);
      }
    });

    var data = props.data,
        _childrenColumnName = props.childrenColumnName,
        defaultExpandAllRows = props.defaultExpandAllRows,
        _expandedRowKeys = props.expandedRowKeys,
        defaultExpandedRowKeys = props.defaultExpandedRowKeys,
        getRowKey = props.getRowKey;
    _this.preExpandedRowKeys = _expandedRowKeys;
    var finnalExpandedRowKeys = [];

    var _rows = _toConsumableArray(data);

    if (defaultExpandAllRows) {
      for (var i = 0; i < _rows.length; i++) {
        var row = _rows[i];
        finnalExpandedRowKeys.push(getRowKey(row, i));
        _rows = _rows.concat(row[_childrenColumnName] || []);
      }
    } else {
      finnalExpandedRowKeys = _expandedRowKeys || defaultExpandedRowKeys;
    }

    _this.columnManager = props.columnManager;
    _this.store = props.store;

    _this.store.setState({
      expandedRowsHeight: {},
      expandedRowKeys: finnalExpandedRowKeys
    });

    return _this;
  }

  _createClass(ExpandableTable, [{
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      if ('expandedRowKeys' in this.props && this.preExpandedRowKeys !== this.props.expandedRowKeys) {
        // modified by njq.niu@hand-china.com
        this.preExpandedRowKeys = this.props.expandedRowKeys;
        this.store.setState({
          expandedRowKeys: this.props.expandedRowKeys
        });
      }
    }
  }, {
    key: "renderExpandedRow",
    value: function renderExpandedRow(record, index, _render, className, ancestorKeys, indent, fixed) {
      var _this2 = this;

      var _this$props4 = this.props,
          prefixCls = _this$props4.prefixCls,
          expandIconAsCell = _this$props4.expandIconAsCell,
          indentSize = _this$props4.indentSize;
      var colCount;

      if (fixed === 'left') {
        colCount = this.columnManager.leftLeafColumns().length;
      } else if (fixed === 'right') {
        colCount = this.columnManager.rightLeafColumns().length;
      } else {
        colCount = this.columnManager.leafColumns().length;
      }

      var columns = [{
        key: 'extra-row',
        render: function render() {
          var _this2$store$getState = _this2.store.getState(),
              expandedRowKeys = _this2$store$getState.expandedRowKeys;

          var expanded = !!~expandedRowKeys.indexOf(parentKey);
          return {
            props: {
              colSpan: colCount
            },
            children: fixed !== 'right' ? _render(record, index, indent, expanded) : '&nbsp;'
          };
        }
      }];

      if (expandIconAsCell && fixed !== 'right') {
        columns.unshift({
          key: 'expand-icon-placeholder',
          render: function render() {
            return null;
          }
        });
      }

      var parentKey = ancestorKeys[ancestorKeys.length - 1];
      var rowKey = "".concat(parentKey, "-extra-row");
      var components = {
        body: {
          row: 'tr',
          cell: 'td'
        }
      };
      return /*#__PURE__*/React.createElement(TableRow, {
        key: rowKey,
        columns: columns,
        className: className,
        rowKey: rowKey,
        ancestorKeys: ancestorKeys,
        prefixCls: "".concat(prefixCls, "-expanded-row"),
        indentSize: indentSize,
        indent: indent,
        fixed: fixed,
        components: components,
        expandedRow: true
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props5 = this.props,
          data = _this$props5.data,
          childrenColumnName = _this$props5.childrenColumnName,
          children = _this$props5.children;
      var needIndentSpaced = data.some(function (record) {
        return record[childrenColumnName];
      });
      return children({
        props: this.props,
        needIndentSpaced: needIndentSpaced,
        renderRows: this.renderRows,
        handleExpandChange: this.handleExpandChange,
        renderExpandIndentCell: this.renderExpandIndentCell
      });
    }
  }]);

  return ExpandableTable;
}(Component);

_defineProperty(ExpandableTable, "defaultProps", {
  expandIconAsCell: false,
  expandedRowClassName: function expandedRowClassName() {
    return '';
  },
  expandIconColumnIndex: 0,
  defaultExpandAllRows: false,
  defaultExpandedRowKeys: [],
  childrenColumnName: 'children',
  indentSize: 15,
  onExpand: function onExpand() {},
  onExpandedRowsChange: function onExpandedRowsChange() {}
});

export default connect()(ExpandableTable);
//# sourceMappingURL=ExpandableTable.js.map
