import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";

/**
 * TableTbody.jsx
 *
 * @Author: apple
 * @Date: 2018/3/8 16:34
 */
import React from 'react';
import classNames from 'classnames';
import TableCell from './TableCell';
import Tr from './Tr';
import { isFunction } from '../util/isType';
import listenCssRender from '../util/listenCssRender';
/**
 * @ignore
 * */

var TableTbody =
/*#__PURE__*/
function (_React$Component) {
  _inherits(TableTbody, _React$Component);

  function TableTbody(props, context) {
    var _this;

    _classCallCheck(this, TableTbody);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(TableTbody).call(this, props, context));

    _defineProperty(_assertThisInitialized(_this), "getTableTbody", function (el) {
      var tableTbodyRef = _this.props.tableTbodyRef;
      _this.tableTbody = el;
      tableTbodyRef && tableTbodyRef(el);
    });

    _defineProperty(_assertThisInitialized(_this), "action", function (at, data, srcData, pid, id) {
      pid = pid || "root";

      if (JSON.stringify(srcData) === "{}") {
        data[pid] = {};
        data[pid][id] = {};
      } else {
        for (var i in srcData) {
          if (i == pid && at === 'add') {
            data[pid] = {};
            data[pid][id] = {};
          }

          if (i == id && at === 'delete') {
            delete data[id];
            delete srcData[id];
          }

          if (JSON.stringify(srcData[i]) !== "{}") _this.action(at, data[i], srcData[i], pid, id);
        }
      }
    });

    _defineProperty(_assertThisInitialized(_this), "renderData", function (data, level, dis, pid) {
      var mine = _assertThisInitialized(_this);

      var _this$props = _this.props,
          expandRowIds = _this$props.expandRowIds,
          renderColumns = _this$props.renderColumns,
          selfPrefixCls = _this$props.selfPrefixCls,
          stripe = _this$props.stripe,
          rowHeightArray = _this$props.rowHeightArray,
          position = _this$props.position,
          rules = _this$props.rules,
          pubsubEventString = _this$props.pubsubEventString,
          expandOpt = _this$props.expandOpt,
          children = _this$props.children,
          checkbox = _this$props.checkbox,
          hover = _this$props.hover,
          mainDataSource = _this$props.mainDataSource;
      var extended = 0;
      return data.map(function (elm, i) {
        var result = [];
        var expandRow = expandRowIds.filter(function (item) {
          return item === i;
        })[0];
        var colSpan = renderColumns.length;
        var rowIndex = i + extended + 1;
        var classStr = classNames(_defineProperty({}, "".concat(selfPrefixCls, "-cell-stripe"), stripe && !(expandRow === 0 || expandRow) && !!(rowIndex && !(rowIndex % 2))));
        var trStyle = {
          height: rowHeightArray[i],
          expandRowHeight: null
        };

        if (expandRow) {
          trStyle.height = rowHeightArray[2 * i];
          trStyle.expandRowHeight = rowHeightArray[2 * i + 1];
        }

        if (level === 0) dis = false;
        if (JSON.stringify(mine.state.showID).indexOf(pid) > -1) dis = false;
        var trProps = {
          key: i,
          id: i,
          data: position !== 'main' ? mainDataSource[i] : elm,
          classStr: classStr,
          elm: elm,
          rules: rules,
          trStyle: trStyle,
          renderColumns: renderColumns,
          position: position,
          selfPrefixCls: selfPrefixCls,
          colSpan: colSpan,
          pubsubEventString: pubsubEventString,
          expandOpt: expandOpt,
          children: children,
          checkbox: checkbox,
          hover: hover,
          level: level,
          handleExpandRowClick: mine.handleExpandRowClick,
          dis: dis,
          pid: pid
        };
        if (!dis) result.push(React.createElement(Tr, trProps));

        if (elm.children) {
          level++;
          dis = true;
          if (JSON.stringify(mine.state.showID).indexOf(elm.id) > -1) dis = false;
          if (!dis) result.push(_this.renderData(elm.children, level, dis, elm.id));
          level--;
        }

        return result;
      });
    });

    _this.state = {
      showID: {}
    };
    _this.handleExpandRowClick = _this.handleExpandRowClick.bind(_assertThisInitialized(_this));
    return _this;
  }

  _createClass(TableTbody, [{
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(nextProps, nextState) {
      var _this$props2 = this.props,
          data = _this$props2.data,
          renderColumns = _this$props2.renderColumns;
      var nextData = nextProps.data,
          nextRenderColumns = nextProps.renderColumns;
      var showID = this.state.showID;
      var nextShowID = nextState.showID;
      return data !== nextData || renderColumns !== nextRenderColumns || JSON.stringify(showID) !== JSON.stringify(nextShowID);
    }
  }, {
    key: "handleExpandRowClick",
    value: function handleExpandRowClick(type, id, pid) {
      pid = pid || "root";
      var showID = this.state.showID;
      var data = JSON.parse(JSON.stringify(showID));

      if (type === 'right-arrow') {
        this.action("add", data, JSON.parse(JSON.stringify(showID)), pid, id);
      } else if (type === 'down-arrow') {
        this.action("delete", data, JSON.parse(JSON.stringify(showID)), pid, id);
      }

      this.setState({
        showID: data
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
          tbodyClassName = _this$props3.tbodyClassName,
          data = _this$props3.data,
          renderColumns = _this$props3.renderColumns,
          position = _this$props3.position,
          noDataRender = _this$props3.noDataRender;
      return React.createElement("tbody", {
        className: tbodyClassName,
        ref: this.getTableTbody
      }, data.length !== 0 ? this.renderData(data, 0) : position === 'main' && React.createElement("tr", null, React.createElement(TableCell, {
        className: "text-center",
        colSpan: renderColumns.length,
        style: {
          padding: '55px',
          color: '#999'
        },
        content: isFunction(noDataRender) ? noDataRender() : noDataRender
      })));
    }
  }]);

  return TableTbody;
}(React.Component);

_defineProperty(TableTbody, "propTypes", {});

_defineProperty(TableTbody, "defaultProps", {});

export default TableTbody;