"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");

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 _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 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 _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 Cell =
/*#__PURE__*/
function (_React$Component) {
  _inherits(Cell, _React$Component);

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

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

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

        return !isEqual;
      }

      return true;
    }
  }, {
    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,
          cell = _this$props.cell,
          value = _this$props.value,
          resizable = _this$props.resizable,
          colIndex = _this$props.colIndex,
          rowIndex = _this$props.rowIndex,
          record = _this$props.record,
          context = _this$props.context,
          align = _this$props.align,
          _this$props$style = _this$props.style,
          style = _this$props$style === void 0 ? {} : _this$props$style,
          Tag = _this$props.component,
          children = _this$props.children,
          title = _this$props.title,
          width = _this$props.width,
          innerStyle = _this$props.innerStyle,
          primaryKey = _this$props.primaryKey,
          __normalized = _this$props.__normalized,
          filterMode = _this$props.filterMode,
          filterMenuProps = _this$props.filterMenuProps,
          filters = _this$props.filters,
          sortable = _this$props.sortable,
          lock = _this$props.lock,
          pure = _this$props.pure,
          locale = _this$props.locale,
          expandedIndexSimulate = _this$props.expandedIndexSimulate,
          rtl = _this$props.rtl,
          columnId = _this$props.columnId,
          table = _this$props.table,
          isInHeader = _this$props.isInHeader,
          type = _this$props.type,
          others = _objectWithoutProperties(_this$props, ["prefix", "className", "cell", "value", "resizable", "colIndex", "rowIndex", "record", "context", "align", "style", "component", "children", "title", "width", "innerStyle", "primaryKey", "__normalized", "filterMode", "filterMenuProps", "filters", "sortable", "lock", "pure", "locale", "expandedIndexSimulate", "rtl", "columnId", "table", "isInHeader", "type"]);

      delete others.columnId;
      delete others.table;
      delete others.isInHeader;

      var tagStyle = _objectSpread({}, style);

      var cellProps = {
        value: value,
        index: rowIndex,
        record: record,
        context: context
      };
      var content = cell;

      if (_react["default"].isValidElement(content)) {
        content = _react["default"].cloneElement(content, cellProps);
      } else if (typeof content === 'function') {
        content = content(value, rowIndex, record, context);
      }

      if (align) {
        tagStyle.textAlign = align;

        if (rtl) {
          tagStyle.textAlign = align === 'left' ? 'right' : align === 'right' ? 'left' : align;
        }
      }

      var cls = (0, _classnames2["default"])((_classnames = {}, _defineProperty(_classnames, "".concat(prefix, "table-cell"), true), _defineProperty(_classnames, className, className), _classnames));
      return _react["default"].createElement(Tag, _extends({}, _util.obj.pickAttrs(others), {
        className: cls,
        style: tagStyle,
        role: "gridcell"
      }), _react["default"].createElement("div", {
        className: "".concat(prefix, "table-cell-wrapper"),
        style: innerStyle
      }, type === 'body' ? _react["default"].createElement(_react["default"].Fragment, null, children, children ? _react["default"].createElement("span", {
        style: {
          verticalAlign: 'middle'
        }
      }, content) : content) : _react["default"].createElement(_react["default"].Fragment, null, children ? _react["default"].createElement("span", {
        style: {
          verticalAlign: 'middle'
        }
      }, content) : content, children), table && table.AutoWidth && table.AutoWidth.renderCalculator(columnId, isInHeader, content, children)));
    }
  }]);

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

exports["default"] = Cell;

_defineProperty(Cell, "propTypes", {
  prefix: _propTypes["default"].string,
  pure: _propTypes["default"].bool,
  primaryKey: _propTypes["default"].string,
  className: _propTypes["default"].string,
  record: _propTypes["default"].any,
  value: _propTypes["default"].any,
  colIndex: _propTypes["default"].number,
  rowIndex: _propTypes["default"].number,
  title: _propTypes["default"].any,
  width: _propTypes["default"].oneOfType([_propTypes["default"].number, _propTypes["default"].string]),
  context: _propTypes["default"].any,
  cell: _propTypes["default"].oneOfType([_propTypes["default"].element, _propTypes["default"].node, _propTypes["default"].func]),
  align: _propTypes["default"].oneOf(['left', 'center', 'right']),
  component: _propTypes["default"].oneOf(['td', 'th', 'div']),
  children: _propTypes["default"].any,
  style: _propTypes["default"].object,
  innerStyle: _propTypes["default"].object,
  filterMode: _propTypes["default"].oneOf(['single', 'multiple']),
  filterMenuProps: _propTypes["default"].object,
  filters: _propTypes["default"].array,
  sortable: _propTypes["default"].bool,
  lock: _propTypes["default"].any,
  type: _propTypes["default"].oneOf(['header', 'body']),
  resizable: _propTypes["default"].bool,
  __normalized: _propTypes["default"].bool,
  columnId: _propTypes["default"].string,
  table: _propTypes["default"].object,
  isInHeader: _propTypes["default"].bool
});

_defineProperty(Cell, "defaultProps", {
  component: 'td',
  type: 'body',
  cell: function cell(value) {
    return value;
  },
  prefix: 'next-',
  columnId: null,
  table: null,
  isInHeader: false
});