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

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

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

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

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

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

var _MediaViewerContainer2 = _interopRequireDefault(_MediaViewerContainer);

var _csidHelpers = require('../../helpers/csidHelpers');

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

const messages = (0, _reactIntl.defineMessages)({
  titleWithCount: {
    'id': 'mediaViewerPanel.titleWithCount',
    'defaultMessage': '{title}: {totalItems, number}'
  }
});

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

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

class MediaViewerPanel extends _react.Component {
  componentDidMount() {
    this.search();
  }

  componentDidUpdate(prevProps) {
    const prevSearchDescriptor = prevProps.searchDescriptor;
    const searchDescriptor = this.props.searchDescriptor;


    if (!_immutable2.default.is(prevSearchDescriptor, searchDescriptor)) {
      this.search();
    }
  }

  search() {
    var _props = this.props;
    const 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);
    }
  }

  renderHeader() {
    var _props2 = this.props;
    const config = _props2.config,
          listType = _props2.listType,
          ownBlobCsid = _props2.ownBlobCsid,
          searchResult = _props2.searchResult,
          title = _props2.title;


    const listTypeConfig = config.listTypes[listType];

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

    let headerContent;

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

      // ownBlobCsid might be a new record identifier (e.g. '/blob'), if there is no blob. Only
      // increment the count if it's an actual csid.

      if ((0, _csidHelpers.isCsid)(ownBlobCsid)) {
        count += 1;
      }

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

    return _react2.default.createElement(
      'h3',
      null,
      headerContent
    );
  }

  render() {
    var _props3 = this.props;
    const 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: (0, _csidHelpers.isCsid)(ownBlobCsid) ? ownBlobCsid : undefined,
        recordType: recordType,
        searchName: name,
        searchDescriptor: searchDescriptor
      })
    );
  }
}

exports.default = MediaViewerPanel;
MediaViewerPanel.propTypes = propTypes;
MediaViewerPanel.defaultProps = defaultProps;