import _objectSpread from "@babel/runtime/helpers/esm/objectSpread";
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";

/**
 * Tr.jsx
 *
 * @Author: apple
 * @Date: 2018/4/17 18:09
 */
import React from 'react';
import classNames from 'classnames';
import TableCell from './TableCell';
import ChevronRight from '../icons/internals/ChevronRight';
import ExpandMore from '../icons/internals/ExpandMore';
import { isFunction } from '../util/isType';
import pubsub from '../util/observer';
import { addClass, deleteClass } from '../util/operateClass';

var Tr =
/*#__PURE__*/
function (_React$PureComponent) {
  _inherits(Tr, _React$PureComponent);

  function Tr(props, context) {
    var _this;

    _classCallCheck(this, Tr);

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

    _defineProperty(_assertThisInitialized(_this), "handleExpandClick", function (e, type) {
      var _this$pubsubEvent = _this.pubsubEvent,
          expandRow = _this$pubsubEvent.expandRow,
          shrinkRow = _this$pubsubEvent.shrinkRow;
      var position = _this.props.position;

      if (position === 'left' || position === 'main') {
        if (type === 'right-arrow') {
          pubsub.publish(expandRow);
        }

        if (type === 'down-arrow') {
          pubsub.publish(shrinkRow);
        }
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleExpandRowClick", function (e, type, id, pid) {
      var position = _this.props.position;

      if (position === 'left' || position === 'main') {
        if (type === 'right-arrow') {
          _this.setState({
            arrowType: 'down-arrow',
            displayExpandSonRow: true
          });
        }

        if (type === 'down-arrow') {
          _this.setState({
            arrowType: 'right-arrow',
            displayExpandSonRow: false
          });
        }
      }

      _this.props.handleExpandRowClick(type, id, pid);
    });

    _defineProperty(_assertThisInitialized(_this), "handleMouseEnter", function () {
      var hover = _this.props.hover;
      var mouseEnterRow = _this.pubsubEvent.mouseEnterRow;
      hover && pubsub.publish(mouseEnterRow);
    });

    _defineProperty(_assertThisInitialized(_this), "handleMouseLeave", function () {
      var hover = _this.props.hover;
      var mouseLeaveRow = _this.pubsubEvent.mouseLeaveRow;
      hover && pubsub.publish(mouseLeaveRow);
    });

    var _this$props = _this.props,
        _id = _this$props.id,
        pubsubEventString = _this$props.pubsubEventString; // 事件随机key值，确保在整个项目中独一无二

    _this.pubsubEvent = {
      expandRow: "".concat(_id, "-").concat(pubsubEventString.token, "-expand-main"),
      shrinkRow: "".concat(_id, "-").concat(pubsubEventString.token, "-shrink-main"),
      fixExpandRow: "".concat(_id, "-").concat(pubsubEventString.token, "-expand-fix"),
      fixShrinkRow: "".concat(_id, "-").concat(pubsubEventString.token, "-shrink-fix"),
      mouseEnterRow: "".concat(_id, "-").concat(pubsubEventString.token, "-mouseEnterRow"),
      mouseLeaveRow: "".concat(_id, "-").concat(pubsubEventString.token, "-mouseLeaveRow")
    };
    _this.state = {
      displayExpandRow: false,
      displayExpandSonRow: false
    };
    return _this;
  }

  _createClass(Tr, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this2 = this;

      var self = this;
      var position = this.props.position;
      var _this$pubsubEvent2 = this.pubsubEvent,
          expandRow = _this$pubsubEvent2.expandRow,
          shrinkRow = _this$pubsubEvent2.shrinkRow,
          fixExpandRow = _this$pubsubEvent2.fixExpandRow,
          fixShrinkRow = _this$pubsubEvent2.fixShrinkRow,
          mouseEnterRow = _this$pubsubEvent2.mouseEnterRow,
          mouseLeaveRow = _this$pubsubEvent2.mouseLeaveRow; // 需要针对是否是主表格来决定如何监听

      if (position === 'main') {
        // 订阅打开展开栏事件
        pubsub.subscribe(expandRow, function () {
          self.setState({
            displayExpandRow: true,
            arrowType: 'down-arrow'
          }, function () {// pubsub.publish(fixExpandRow, this.expandTr.getBoundingClientRect().height);
          });
        }); // 订阅收缩展开栏事件

        pubsub.subscribe(shrinkRow, function () {
          self.setState({
            displayExpandRow: false,
            arrowType: 'right-arrow'
          }, function () {
            pubsub.publish(fixShrinkRow);
          });
        });
      } else {
        // 订阅固定列打开展开栏事件
        pubsub.subscribe(fixExpandRow, function (event, height) {
          self.setState({
            displayExpandRow: true,
            arrowType: 'down-arrow'
          });
          _this2.expandTr.style.height = "".concat(height, "px");
        }); // 订阅固定列收缩展开栏事件

        pubsub.subscribe(fixShrinkRow, function () {
          self.setState({
            displayExpandRow: false,
            arrowType: 'right-arrow'
          });
        });
      }

      pubsub.subscribe(mouseEnterRow, function () {
        addClass(_this2.normalTr, 'ne-table-is-hover');
      });
      pubsub.subscribe(mouseLeaveRow, function () {
        deleteClass(_this2.normalTr, 'ne-table-is-hover');
      });
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      for (var item in this.pubsubEvent) {
        pubsub.unsubscribe(item);
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this3 = this;

      var mine = this;
      var _this$props2 = this.props,
          id = _this$props2.id,
          level = _this$props2.level,
          elm = _this$props2.elm,
          classStr = _this$props2.classStr,
          renderColumns = _this$props2.renderColumns,
          data = _this$props2.data,
          rules = _this$props2.rules,
          colSpan = _this$props2.colSpan,
          selfPrefixCls = _this$props2.selfPrefixCls,
          position = _this$props2.position,
          dis = _this$props2.dis,
          expandOpt = _this$props2.expandOpt,
          children = _this$props2.children,
          pid = _this$props2.pid;
      var _this$state = this.state,
          displayExpandRow = _this$state.displayExpandRow,
          displayExpandSonRow = _this$state.displayExpandSonRow;
      var _this$pubsubEvent3 = this.pubsubEvent,
          expandRow = _this$pubsubEvent3.expandRow,
          shrinkRow = _this$pubsubEvent3.shrinkRow;
      var displayStyle = {};

      if (dis) {
        displayStyle.display = 'none';
      }

      var trClass = classStr;
      var flag = false;
      var indexFlag = 0;
      return [React.createElement("tr", {
        key: "tr-".concat(id),
        "data-row": id,
        className: trClass,
        style: displayStyle,
        onMouseEnter: this.handleMouseEnter,
        onMouseLeave: this.handleMouseLeave,
        ref: function ref(normalTr) {
          return _this3.normalTr = normalTr;
        }
      }, isFunction(elm) ? elm(rules.length) : rules.map(function (e, index) {
        if (e !== 'checkbox' && e !== 'expandBtn') {
          flag = true;
          indexFlag += 1;
        }

        var render = renderColumns[index].render;

        if (e === 'expandBtn' && expandOpt) {
          var type = 'right-arrow';

          if (displayExpandRow) {
            type = 'down-arrow';
          }

          var Component = displayExpandRow ? ExpandMore : ChevronRight;

          render = function render() {
            return React.createElement(Component, {
              onClick: function onClick(e) {
                return mine.handleExpandClick(e, type);
              }
            });
          };
        }

        if (e === 'expandBtn' && children && elm.children) {
          var _type = 'right-arrow';

          if (displayExpandSonRow) {
            _type = 'down-arrow';
          }

          var _Component = displayExpandSonRow ? ExpandMore : ChevronRight;

          render = function render() {
            return React.createElement(_Component, {
              onClick: function onClick(e) {
                return mine.handleExpandRowClick(e, _type, elm.id, pid);
              }
            });
          };
        }

        var renderData = data;

        if (elm._tableExpandRow) {
          renderData = _objectSpread({}, data);
          delete renderData._tableExpandRow;
        }

        return React.createElement(TableCell, {
          key: "tr-".concat(id, "-td-").concat(e),
          content: elm[e],
          className: classNames(renderColumns[index].className, {
            'text-center': renderColumns[index].textCenter
          }),
          level: flag && indexFlag < 2 ? level : '',
          render: isFunction(render) ? function () {
            return render(id, renderData, e, {
              publish: function publish(event) {
                return pubsub.publish(event);
              },
              shrinkEvent: shrinkRow,
              expandEvent: expandRow
            });
          } : null
        });
      })), // 展开栏
      elm._tableExpandRow && displayExpandRow && React.createElement("tr", {
        key: "tr-".concat(id, "-expand"),
        "data-row": "".concat(id, "-expand"),
        ref: function ref(expandTr) {
          return _this3.expandTr = expandTr;
        }
      }, React.createElement(TableCell, {
        colSpan: "".concat(colSpan),
        style: displayStyle,
        content: elm._tableExpandRow(),
        className: classNames("".concat(selfPrefixCls, "-body-expand-row-").concat(position)),
        key: "tr-".concat(id, "-td-expand")
      }))];
    }
  }]);

  return Tr;
}(React.PureComponent);

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

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

export default Tr;