"use strict";

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

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

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

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

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

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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 _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 _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 ExpandedRow =
/*#__PURE__*/
function (_React$Component) {
  _inherits(ExpandedRow, _React$Component);

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

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

  _createClass(ExpandedRow, [{
    key: "renderExpandedRow",
    value: function renderExpandedRow(record, index, colSpan) {
      var _this$context = this.context,
          expandedRowRender = _this$context.expandedRowRender,
          expandedRowIndent = _this$context.expandedRowIndent,
          openRowKeys = _this$context.openRowKeys,
          lockType = _this$context.lockType;
      var _this$props = this.props,
          columns = _this$props.columns,
          cellRef = _this$props.cellRef;

      if (expandedRowRender) {
        var _this$props2 = this.props,
            primaryKey = _this$props2.primaryKey,
            prefix = _this$props2.prefix,
            leftIndent = expandedRowIndent[0],
            rightIndent = expandedRowIndent[1],
            totalIndent = leftIndent + rightIndent,
            renderCols = function renderCols(number) {
          var ret = [];

          var _loop = function _loop(i) {
            ret.push(_react["default"].createElement("td", {
              key: i,
              ref: function ref(cell) {
                return cellRef(index, i, cell);
              }
            }, "\xA0"));
          };

          for (var i = 0; i < number; i++) {
            _loop(i);
          }

          return ret;
        };

        var content;

        if (totalIndent > colSpan && !lockType) {
          _util.log.warning("It's not allowed expandedRowIndent is more than the number of columns.");
        }

        if (leftIndent < columns.length && lockType === 'left') {
          _util.log.warning('expandedRowIndent left is less than the number of left lock columns.');
        }

        if (rightIndent < columns.length && lockType === 'right') {
          _util.log.warning('expandedRowIndent right is less than the number of right lock columns.');
        }

        if (lockType) {
          return openRowKeys.indexOf(record[primaryKey]) > -1 ? _react["default"].createElement("tr", {
            className: "".concat(prefix, "table-expanded-row"),
            key: "expanded-".concat(index)
          }, _react["default"].createElement("td", {
            colSpan: colSpan,
            ref: function ref(cell) {
              return cellRef(index, 0, cell);
            }
          }, "\xA0")) : null;
        }

        content = expandedRowRender(record, index);

        if (!_react["default"].isValidElement(content)) {
          content = _react["default"].createElement("div", {
            className: "".concat(prefix, "table-cell-wrapper")
          }, content);
        }

        return openRowKeys.indexOf(record[primaryKey]) > -1 ? _react["default"].createElement("tr", {
          className: "".concat(prefix, "table-expanded-row"),
          key: "expanded-".concat(record[primaryKey] || index)
        }, renderCols(leftIndent), _react["default"].createElement("td", {
          colSpan: colSpan - totalIndent
        }, content), renderCols(rightIndent)) : null;
      } else {
        return null;
      }
    }
  }, {
    key: "render",
    value: function render() {
      /* eslint-disable no-unused-vars*/
      var _this$props3 = this.props,
          record = _this$props3.record,
          rowIndex = _this$props3.rowIndex,
          columns = _this$props3.columns,
          others = _objectWithoutProperties(_this$props3, ["record", "rowIndex", "columns"]);

      var expandedIndexSimulate = this.context.expandedIndexSimulate;

      if (record.__expanded) {
        var expandedIndex = expandedIndexSimulate ? (rowIndex - 1) / 2 : rowIndex;
        return this.renderExpandedRow(record, expandedIndex, columns.length);
      }

      var newRowIndex = expandedIndexSimulate ? rowIndex / 2 : rowIndex;
      return _react["default"].createElement(_row["default"], _extends({}, others, {
        record: record,
        columns: columns,
        rowIndex: newRowIndex
      }));
    }
  }]);

  return ExpandedRow;
}(_react["default"].Component);

exports["default"] = ExpandedRow;

_defineProperty(ExpandedRow, "propTypes", _objectSpread({}, _row["default"].propTypes));

_defineProperty(ExpandedRow, "defaultProps", _objectSpread({}, _row["default"].defaultProps));

_defineProperty(ExpandedRow, "contextTypes", {
  openRowKeys: _propTypes["default"].array,
  expandedRowRender: _propTypes["default"].func,
  expandedRowIndent: _propTypes["default"].array,
  expandedIndexSimulate: _propTypes["default"].bool,
  lockType: _propTypes["default"].oneOf(['left', 'right'])
});