'use strict';

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

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

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 _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactIntl = require('react-intl');

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _isEqual = require('lodash/isEqual');

var _isEqual2 = _interopRequireDefault(_isEqual);

var _PanelContainer = require('../../containers/layout/PanelContainer');

var _MediaViewerContainer = require('../../containers/media/MediaViewerContainer');

var _MediaViewerContainer2 = _interopRequireDefault(_MediaViewerContainer);

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 messages = (0, _reactIntl.defineMessages)({
  titleWithCount: {
    'id': 'mediaViewerPanel.titleWithCount',
    'defaultMessage': '{title}: {totalItems, number}'
  }
});

var propTypes = {
  collapsed: _react.PropTypes.bool,
  color: _react.PropTypes.string,
  columnSetName: _react.PropTypes.string,
  config: _react.PropTypes.object,
  name: _react.PropTypes.string,
  ownBlobCsid: _react.PropTypes.string,
  recordType: _react.PropTypes.string,
  searchDescriptor: _react.PropTypes.object,
  searchResult: _react.PropTypes.instanceOf(_immutable2.default.Map),
  listType: _react.PropTypes.string,
  title: _react.PropTypes.node,
  search: _react.PropTypes.func
};

var defaultProps = {
  columnSetName: 'default',
  listType: 'common'
};

var contextTypes = {
  router: _react.PropTypes.object
};

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

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

    return _possibleConstructorReturn(this, (MediaViewerPanel.__proto__ || Object.getPrototypeOf(MediaViewerPanel)).apply(this, arguments));
  }

  _createClass(MediaViewerPanel, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      this.search();
    }
  }, {
    key: 'componentDidUpdate',
    value: function componentDidUpdate(prevProps) {
      var prevSearchDescriptor = prevProps.searchDescriptor;
      var searchDescriptor = this.props.searchDescriptor;


      if (!(0, _isEqual2.default)(prevSearchDescriptor, searchDescriptor)) {
        this.search();
      }
    }
  }, {
    key: 'search',
    value: function search() {
      var _props = this.props,
          columnSetName = _props.columnSetName,
          config = _props.config,
          listType = _props.listType,
          name = _props.name,
          search = _props.search,
          searchDescriptor = _props.searchDescriptor;


      if (search) {
        search(config, name, searchDescriptor, listType, columnSetName);
      }
    }
  }, {
    key: 'renderHeader',
    value: function renderHeader() {
      var _props2 = this.props,
          config = _props2.config,
          listType = _props2.listType,
          ownBlobCsid = _props2.ownBlobCsid,
          searchResult = _props2.searchResult,
          title = _props2.title;


      var listTypeConfig = config.listTypes[listType];

      var totalItems = searchResult ? searchResult.getIn([listTypeConfig.listNodeName, 'totalItems']) : null;

      var headerContent = void 0;

      if (typeof totalItems !== 'undefined' && totalItems !== null) {
        var count = parseInt(totalItems, 10);

        if (ownBlobCsid) {
          count += 1;
        }

        headerContent = _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.titleWithCount, { values: { title: title, totalItems: count } }));
      } else {
        headerContent = title;
      }

      return _react2.default.createElement(
        'h3',
        null,
        headerContent
      );
    }
  }, {
    key: 'render',
    value: function render() {
      var _props3 = this.props,
          collapsed = _props3.collapsed,
          color = _props3.color,
          columnSetName = _props3.columnSetName,
          config = _props3.config,
          listType = _props3.listType,
          name = _props3.name,
          ownBlobCsid = _props3.ownBlobCsid,
          recordType = _props3.recordType,
          searchDescriptor = _props3.searchDescriptor;


      return _react2.default.createElement(
        _PanelContainer.ConnectedPanel,
        {
          collapsible: true,
          collapsed: collapsed,
          color: color,
          config: config,
          header: this.renderHeader(),
          name: name,
          recordType: recordType
        },
        _react2.default.createElement(_MediaViewerContainer2.default, {
          columnSetName: columnSetName,
          config: config,
          listType: listType,
          ownBlobCsid: ownBlobCsid,
          recordType: recordType,
          searchName: name,
          searchDescriptor: searchDescriptor
        })
      );
    }
  }]);

  return MediaViewerPanel;
}(_react.Component);

exports.default = MediaViewerPanel;


MediaViewerPanel.propTypes = propTypes;
MediaViewerPanel.defaultProps = defaultProps;
MediaViewerPanel.contextTypes = contextTypes;