function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

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 React from 'react';
import { findDOMNode } from 'react-dom';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { obj, dom } from '../../util';
import { fetchDataByPath } from '../util';

var noop = function noop() {};

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

  function Row() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, Row);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Row)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_this), "onClick", function (e) {
      var _this$props = _this.props,
          record = _this$props.record,
          rowIndex = _this$props.rowIndex;

      _this.props.onClick(record, rowIndex, e);
    });

    _defineProperty(_assertThisInitialized(_this), "onMouseEnter", function (e) {
      var _this$props2 = _this.props,
          record = _this$props2.record,
          rowIndex = _this$props2.rowIndex;

      _this.onRowHover(record, rowIndex, true, e);
    });

    _defineProperty(_assertThisInitialized(_this), "onMouseLeave", function (e) {
      var _this$props3 = _this.props,
          record = _this$props3.record,
          rowIndex = _this$props3.rowIndex;

      _this.onRowHover(record, rowIndex, false, e);
    });

    return _this;
  }

  _createClass(Row, [{
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(nextProps) {
      if (nextProps.pure) {
        var isEqual = obj.shallowEqual(this.props, nextProps);
        return !isEqual;
      }

      return true;
    }
  }, {
    key: "onRowHover",
    value: function onRowHover(record, index, isEnter, e) {
      var _this$props4 = this.props,
          onMouseEnter = _this$props4.onMouseEnter,
          onMouseLeave = _this$props4.onMouseLeave,
          currentRow = findDOMNode(this);

      if (isEnter) {
        onMouseEnter(record, index, e);
        currentRow && dom.addClass(currentRow, 'hovered');
      } else {
        onMouseLeave(record, index, e);
        currentRow && dom.removeClass(currentRow, 'hovered');
      }
    }
  }, {
    key: "renderCells",
    value: function renderCells(record, rowIndex) {
      var _this2 = this;

      var _this$props5 = this.props,
          Cell = _this$props5.Cell,
          columns = _this$props5.columns,
          getCellProps = _this$props5.getCellProps,
          cellRef = _this$props5.cellRef,
          prefix = _this$props5.prefix,
          primaryKey = _this$props5.primaryKey,
          pure = _this$props5.pure,
          locale = _this$props5.locale,
          table = _this$props5.table,
          rtl = _this$props5.rtl; // use params first, it's for list

      rowIndex = rowIndex !== undefined ? rowIndex : this.props.rowIndex;
      var lockType = this.context.lockType;
      return columns.map(function (child, colIndex) {
        var _classnames;

        /* eslint-disable no-unused-vars, prefer-const */
        var dataIndex = child.dataIndex,
            align = child.align,
            alignHeader = child.alignHeader,
            width = child.width,
            others = _objectWithoutProperties(child, ["dataIndex", "align", "alignHeader", "width"]);

        var value = fetchDataByPath(record, dataIndex);
        var attrs = getCellProps(rowIndex, colIndex, dataIndex, record) || {};

        if (_this2.context.notRenderCellIndex) {
          var matchCellIndex = _this2.context.notRenderCellIndex.map(function (cellIndex) {
            return cellIndex.toString();
          }).indexOf([rowIndex, colIndex].toString());

          if (matchCellIndex > -1) {
            _this2.context.notRenderCellIndex.splice(matchCellIndex, 1);

            return null;
          }
        }

        if (attrs.colSpan && attrs.colSpan > 1 || attrs.rowSpan && attrs.rowSpan > 1) {
          _this2._getNotRenderCellIndex(colIndex, rowIndex, attrs.colSpan || 1, attrs.rowSpan || 1);
        }

        var cellClass = attrs.className;
        var className = classnames((_classnames = {
          first: lockType !== 'right' && colIndex === 0,
          last: lockType !== 'left' && (colIndex === columns.length - 1 || colIndex + attrs.colSpan === columns.length)
        }, _defineProperty(_classnames, child.className, child.className), _defineProperty(_classnames, cellClass, cellClass), _classnames));
        return React.createElement(Cell, _extends({
          key: colIndex
        }, others, attrs, {
          ref: function ref(cell) {
            return cellRef(rowIndex, colIndex, cell);
          },
          prefix: prefix,
          pure: pure,
          primaryKey: primaryKey,
          record: record,
          className: className,
          value: value,
          colIndex: colIndex,
          rowIndex: rowIndex,
          align: align,
          locale: locale,
          rtl: rtl,
          width: width,
          table: table
        }));
      });
    }
  }, {
    key: "_getNotRenderCellIndex",
    value: function _getNotRenderCellIndex(colIndex, rowIndex, colSpan, rowSpan) {
      var maxColIndex = colSpan;
      var maxRowIndex = rowSpan;
      var notRenderCellIndex = [];

      for (var i = 0; i < maxColIndex; i++) {
        for (var j = 0; j < maxRowIndex; j++) {
          notRenderCellIndex.push([rowIndex + j, colIndex + i]);
        }
      }

      [].push.apply(this.context.notRenderCellIndex, notRenderCellIndex);
    }
  }, {
    key: "render",
    value: function render() {
      var _classnames2;

      /* eslint-disable no-unused-vars*/
      var _this$props6 = this.props,
          prefix = _this$props6.prefix,
          className = _this$props6.className,
          onClick = _this$props6.onClick,
          onMouseEnter = _this$props6.onMouseEnter,
          onMouseLeave = _this$props6.onMouseLeave,
          columns = _this$props6.columns,
          Cell = _this$props6.Cell,
          getCellProps = _this$props6.getCellProps,
          rowIndex = _this$props6.rowIndex,
          record = _this$props6.record,
          children = _this$props6.children,
          primaryKey = _this$props6.primaryKey,
          cellRef = _this$props6.cellRef,
          colGroup = _this$props6.colGroup,
          pure = _this$props6.pure,
          locale = _this$props6.locale,
          expandedIndexSimulate = _this$props6.expandedIndexSimulate,
          rtl = _this$props6.rtl,
          wrapper = _this$props6.wrapper,
          others = _objectWithoutProperties(_this$props6, ["prefix", "className", "onClick", "onMouseEnter", "onMouseLeave", "columns", "Cell", "getCellProps", "rowIndex", "record", "children", "primaryKey", "cellRef", "colGroup", "pure", "locale", "expandedIndexSimulate", "rtl", "wrapper"]);

      var cls = classnames((_classnames2 = {}, _defineProperty(_classnames2, "".concat(prefix, "table-row"), true), _defineProperty(_classnames2, className, className), _classnames2));
      var tr = React.createElement("tr", _extends({
        className: cls,
        role: "row"
      }, others, {
        onClick: this.onClick,
        onMouseEnter: this.onMouseEnter,
        onMouseLeave: this.onMouseLeave
      }), this.renderCells(record), children);
      return wrapper(tr);
    }
  }]);

  return Row;
}(React.Component);

_defineProperty(Row, "propTypes", {
  prefix: PropTypes.string,
  pure: PropTypes.bool,
  primaryKey: PropTypes.string,
  className: PropTypes.string,
  columns: PropTypes.array,
  record: PropTypes.any,
  Cell: PropTypes.func,
  rowIndex: PropTypes.number,
  getCellProps: PropTypes.func,
  onClick: PropTypes.func,
  onMouseEnter: PropTypes.func,
  onMouseLeave: PropTypes.func,
  children: PropTypes.any,
  cellRef: PropTypes.func,
  colGroup: PropTypes.object,
  locale: PropTypes.object,
  wrapper: PropTypes.func,
  table: PropTypes.object
});

_defineProperty(Row, "defaultProps", {
  prefix: 'next-',
  primaryKey: 'id',
  columns: [],
  record: {},
  getCellProps: noop,
  onClick: noop,
  onMouseEnter: noop,
  onMouseLeave: noop,
  cellRef: noop,
  colGroup: {},
  wrapper: function wrapper(row) {
    return row;
  },
  table: null
});

_defineProperty(Row, "contextTypes", {
  notRenderCellIndex: PropTypes.array,
  lockType: PropTypes.oneOf(['left', 'right'])
});

export { Row as default };