"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames2 = _interopRequireDefault(require("classnames"));

var _util = require("../../util");

var _row = _interopRequireDefault(require("../base/row"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

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 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(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

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 _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

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

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

var GroupListRow =
/*#__PURE__*/
function (_Row) {
  _inherits(GroupListRow, _Row);

  function GroupListRow() {
    _classCallCheck(this, GroupListRow);

    return _possibleConstructorReturn(this, _getPrototypeOf(GroupListRow).apply(this, arguments));
  }

  _createClass(GroupListRow, [{
    key: "render",
    value: function render() {
      var _classnames;

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

      var cls = (0, _classnames2["default"])((_classnames = {}, _defineProperty(_classnames, "".concat(prefix, "table-row"), true), _defineProperty(_classnames, className, className), _classnames)); // clear notRenderCellIndex, incase of cached data

      this.context.notRenderCellIndex = [];
      return _react["default"].createElement("table", _extends({
        className: cls,
        role: "row"
      }, _util.obj.pickAttrs(others), {
        onClick: this.onClick,
        onMouseEnter: this.onMouseEnter,
        onMouseLeave: this.onMouseLeave
      }), colGroup, _react["default"].createElement("tbody", null, this.renderContent('header'), this.renderChildren(), this.renderContent('footer')));
    }
  }, {
    key: "isChildrenSelection",
    value: function isChildrenSelection() {
      return this.context.listHeader && this.context.listHeader.hasChildrenSelection;
    }
  }, {
    key: "isFirstLevelDataWhenNoChildren",
    value: function isFirstLevelDataWhenNoChildren() {
      return this.context.listHeader && this.context.listHeader.useFirstLevelDataWhenNoChildren;
    }
  }, {
    key: "isSelection",
    value: function isSelection() {
      return this.context.listHeader && this.context.listHeader.hasSelection;
    }
  }, {
    key: "renderChildren",
    value: function renderChildren() {
      var _this = this;

      var _this$props2 = this.props,
          record = _this$props2.record,
          primaryKey = _this$props2.primaryKey;
      var children = record.children;
      var toRenderList = children;

      if (this.isFirstLevelDataWhenNoChildren()) {
        _util.log.warning("useFirstLevelDataWhenNoChildren is deprecated, change your dataSource structure, make sure there is 'children' in your dataSource.");

        toRenderList = children || [record];
      }

      if (toRenderList) {
        return toRenderList.map(function (child, index) {
          var cells = _this.renderCells(child, index);

          if (_this.isChildrenSelection()) {
            if (!child[primaryKey]) {
              _util.log.warning('record.children/recored should contains primaryKey when childrenSelection is true.');
            }

            return _react["default"].createElement("tr", {
              key: child[primaryKey]
            }, cells);
          }

          if (_this.context.rowSelection) {
            cells.shift();
            cells[0] = _react["default"].cloneElement(cells[0], _objectSpread({
              colSpan: 2
            }, cells[0].props));
          }

          return _react["default"].createElement("tr", {
            key: index
          }, cells);
        });
      }

      return null;
    }
  }, {
    key: "renderContent",
    value: function renderContent(type) {
      var _this$props3 = this.props,
          columns = _this$props3.columns,
          prefix = _this$props3.prefix,
          record = _this$props3.record,
          rowIndex = _this$props3.rowIndex;
      var cameType = type.charAt(0).toUpperCase() + type.substr(1);
      var list = this.context["list".concat(cameType)];
      var listNode;

      if (list) {
        if (_react["default"].isValidElement(list.cell)) {
          listNode = _react["default"].cloneElement(list.cell, {
            record: record,
            index: rowIndex
          });
        } else if (typeof list.cell === 'function') {
          listNode = list.cell(record, rowIndex);
        }

        if (listNode) {
          var cells = this.renderCells(record);

          if (type === 'header' && this.context.rowSelection && this.isSelection()) {
            cells = cells.slice(0, 1);
            cells.push(_react["default"].createElement("td", {
              colSpan: columns.length - 1,
              key: "listNode"
            }, _react["default"].createElement("div", {
              className: "".concat(prefix, "table-cell-wrapper")
            }, listNode)));
            listNode = _react["default"].createElement("tr", {
              className: "".concat(prefix, "table-group-").concat(type)
            }, cells);
          } else {
            listNode = _react["default"].createElement("tr", {
              className: "".concat(prefix, "table-group-").concat(type)
            }, _react["default"].createElement("td", {
              colSpan: columns.length
            }, _react["default"].createElement("div", {
              className: "".concat(prefix, "table-cell-wrapper")
            }, listNode)));
          }
        }
      }

      return listNode;
    }
  }]);

  return GroupListRow;
}(_row["default"]);

exports["default"] = GroupListRow;

_defineProperty(GroupListRow, "contextTypes", {
  listHeader: _propTypes["default"].any,
  listFooter: _propTypes["default"].any,
  rowSelection: _propTypes["default"].object,
  notRenderCellIndex: _propTypes["default"].array,
  lockType: _propTypes["default"].oneOf(['left', 'right'])
});