'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

var _reactRouter = require('react-router');

var _reactAddonsShallowCompare = require('react-addons-shallow-compare');

var _reactAddonsShallowCompare2 = _interopRequireDefault(_reactAddonsShallowCompare);

var _throttle = require('lodash/throttle');

var _throttle2 = _interopRequireDefault(_throttle);

var _TimeString = require('../internal/TimeString');

var _TimeString2 = _interopRequireDefault(_TimeString);

var _Icon = require('../Icon');

var _Icon2 = _interopRequireDefault(_Icon);

var _Badge = require('../Badge');

var _Badge2 = _interopRequireDefault(_Badge);

var _OptClass = require('../internal/OptClass');

var _OptClass2 = _interopRequireDefault(_OptClass);

var _style = require('./style.scss');

var _style2 = _interopRequireDefault(_style);

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

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 ActivityFeedItem = function (_React$Component) {
  _inherits(ActivityFeedItem, _React$Component);

  function ActivityFeedItem(props) {
    _classCallCheck(this, ActivityFeedItem);

    var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(ActivityFeedItem).call(this, props));

    _this.generateLinkType = function (name) {
      var link = void 0;
      var re = '^(http|https)://';
      if (_this.props.profileUrl.match(re)) {
        link = _react2.default.createElement(
          'a',
          { href: _this.props.profileUrl, target: _this.props.profileUrlTarget },
          name
        );
      } else {
        link = _react2.default.createElement(
          _reactRouter.Link,
          { to: _this.props.profileUrl },
          name
        );
      }
      return link;
    };

    _this.generateProfileName = function () {
      var name = _this.props.name ? _this.props.name : null;
      if (!_this.props.profileUrl) {
        return name;
      }

      return _this.generateLinkType(name);
    };

    _this.generateActions = function () {
      var actions = _this.props.actions.map(function (action, index) {
        return _react2.default.createElement(_Icon2.default, { name: action.icon, onClick: action.callback, fill: '#3c97d3', height: '16', width: '16', key: index });
      });
      return actions;
    };

    _this.updateHeight = function () {
      var node = _reactDom2.default.findDOMNode(_this);
      var nodeHeight = node.getBoundingClientRect().height;
      var margin = parseInt(window.getComputedStyle(node)['margin-bottom']);
      var totalHeight = nodeHeight + margin;
      _this.props.onSetHeight(totalHeight);
    };

    _this.componentDidMount = function () {
      window.addEventListener('resize', _this.throttle);
      _this.updateHeight();
    };

    _this.componentWillUnmount = function () {
      window.removeEventListener('resize', _this.throttle);
    };

    _this.shouldComponentUpdate = function (nextProps, nextState) {
      return (0, _reactAddonsShallowCompare2.default)(_this, nextProps, nextState);
    };

    _this.render = function () {
      var badgeClasses = (0, _OptClass2.default)(_style2.default, 'indicator');

      return _react2.default.createElement(
        'li',
        null,
        _react2.default.createElement(_Badge2.default, {
          icon: _this.props.badge.icon,
          text: _this.props.badge.text,
          theme: _this.props.badge.theme,
          optClass: badgeClasses
        }),
        _react2.default.createElement(
          'div',
          { className: _style2.default['item-wrapper'] },
          _this.props.time ? _react2.default.createElement(
            'time',
            null,
            (0, _TimeString2.default)(_this.props.time)
          ) : null,
          _react2.default.createElement(
            'div',
            { className: _style2.default['title-wrapper'] },
            _react2.default.createElement(
              'h3',
              null,
              _this.generateProfileName(),
              ' ',
              _this.props.title ? _this.props.title : null
            ),
            _this.props.actions ? _react2.default.createElement(
              'div',
              { className: _style2.default['action-wrapper'] },
              _this.generateActions()
            ) : null
          ),
          _this.props.text ? _react2.default.createElement(
            'p',
            null,
            _this.props.text
          ) : null
        )
      );
    };

    _this.throttle = (0, _throttle2.default)(_this.updateHeight, 200);
    return _this;
  }

  return ActivityFeedItem;
}(_react2.default.Component);

ActivityFeedItem.propTypes = {
  /**
   * The name that appears in the
   */
  name: _react2.default.PropTypes.string,
  /**
   * A URL that you want to use to redirect when the name is clicked.
   */
  profileUrl: _react2.default.PropTypes.string,
  /**
   * A title to display next to the name.
   */
  title: _react2.default.PropTypes.string,
  /**
   * An array of actions that allow you to present an icon (on hover),
   * and a callback function (when clicked)
   */
  actions: _react2.default.PropTypes.array,
  /**
   * The text that you want to display below the title.
   */
  text: _react2.default.PropTypes.string,
  /**
   * A timestamp that you want to provide to the feed item.
   */
  time: _react2.default.PropTypes.string,
  /**
   * The badge configuration for the activity feed item.
   */
  badge: _react2.default.PropTypes.object,
  /**
   * Callback to send height to parent.
   */
  onSetHeight: _react2.default.PropTypes.func
};
exports.default = ActivityFeedItem;