'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _getClassName2 = require('babel-plugin-react-css-modules/dist/browser/getClassName');

var _getClassName3 = _interopRequireDefault(_getClassName2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _omit = require('lodash/omit');

var _omit2 = _interopRequireDefault(_omit);

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

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 _styleModuleImportMap = {
  './dropdown-content.scss': {
    'content-wrapper': 'flive_dropdown-content_content-wrapper',
    'reverse': 'flive_dropdown-content_reverse',
    'primary': 'flive_dropdown-content_primary',
    'info': 'flive_dropdown-content_info',
    'warning': 'flive_dropdown-content_warning',
    'alert': 'flive_dropdown-content_alert',
    'launcher': 'flive_dropdown-content_launcher',
    'icon': 'flive_dropdown-content_icon',
    'dash-lagoon': 'flive_dropdown-content_dash-lagoon',
    'dash-night-blue': 'flive_dropdown-content_dash-night-blue',
    'full-width': 'flive_dropdown-content_full-width',
    'active': 'flive_dropdown-content_active',
    'content': 'flive_dropdown-content_content',
    'content-2-items-per-row': 'flive_dropdown-content_content-2-items-per-row'
  }
};

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

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

var Content = function (_Component) {
  _inherits(Content, _Component);

  function Content() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, Content);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Content.__proto__ || Object.getPrototypeOf(Content)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
      initial: true,
      height: null
    }, _this.computeHeight = function () {
      if (_this.props.maxItems < _this.props.children.length) {
        _this.setState({ height: _this.container.getBoundingClientRect().height, initial: false });
      }
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(Content, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      this.computeHeight();
    }
  }, {
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      if (nextProps.maxItems !== this.props.maxItems) {
        this.setState({ height: undefined, initial: true });
      }
    }
  }, {
    key: 'shouldComponentUpdate',
    value: function shouldComponentUpdate(nextProps, nextState) {
      return this.props.active !== nextProps.active || this.state.initial !== nextState.initial || this.state.height !== nextState.height || this.props.children !== nextProps.children;
    }
  }, {
    key: 'componentDidUpdate',
    value: function componentDidUpdate() {
      if (this.state.initial) {
        this.computeHeight();
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      var _props = this.props,
          active = _props.active,
          children = _props.children,
          reverse = _props.reverse,
          className = _props.className,
          maxItems = _props.maxItems,
          variant = _props.variant,
          closePanel = _props.closePanel,
          itemsPerRow = _props.itemsPerRow;

      var resultItemsPerRow = Math.min(itemsPerRow, 2);
      var childProps = { closePanel: closePanel };
      if (variant === 'launcher') {
        childProps.variant = 'launcher';
      }
      return _react2.default.createElement(
        'div',
        {
          className: (className ? className + ' ' : '') + (0, _getClassName3.default)((0, _classnames2.default)('content-wrapper', variant, { active: active, reverse: reverse }), _styleModuleImportMap)
        },
        _react2.default.createElement(
          'div',
          {
            ref: function ref(container) {
              _this2.container = container;
            },
            style: { height: this.state.height || 'auto' },
            className: (0, _getClassName3.default)((0, _classnames2.default)('content', _defineProperty({}, 'content-' + resultItemsPerRow + '-items-per-row', resultItemsPerRow > 1)), _styleModuleImportMap)
          },
          _react2.default.Children.map(children, function (child, i) {
            if (i < parseInt(maxItems, 10) && _this2.state.initial || !_this2.state.initial) {
              if (_react2.default.isValidElement(child)) {
                if (typeof child.type === 'function') {
                  return _react2.default.cloneElement(child, childProps);
                }
                return _react2.default.cloneElement(child, (0, _omit2.default)(childProps, ['closePanel']));
              }
            }
            return child;
          })
        )
      );
    }
  }]);

  return Content;
}(_react.Component);

Content.propTypes = {
  /** @ignore */
  className: _propTypes2.default.string,
  /** @ignore */
  children: _propTypes2.default.node,
  active: _propTypes2.default.bool,
  variant: _propTypes2.default.string,
  reverse: _propTypes2.default.bool,
  closePanel: _propTypes2.default.func,
  itemsPerRow: _propTypes2.default.oneOf([1, 2]),
  maxItems: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number])
};
Content.defaultProps = {
  className: undefined,
  children: [],
  active: false,
  variant: 'primary',
  reverse: false,
  closePanel: null,
  itemsPerRow: 1,
  maxItems: 100
};


Content.componentName = 'Content';

exports.default = Content;
module.exports = exports['default'];