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

var _SearchPanel = require('../../../styles/cspace-ui/SearchPanel.css');

var _SearchPanel2 = _interopRequireDefault(_SearchPanel);

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,
          prevSearchResult = prevProps.searchResult;
    var _props = this.props;
    const searchDescriptor = _props.searchDescriptor,
          searchResult = _props.searchResult;


    if (!_immutable2.default.is(prevSearchDescriptor, searchDescriptor) ||
    // If the search result was cleared from the store, redo the search.
    typeof searchResult === 'undefined' && prevSearchResult) {
      this.search();
    }
  }

  search() {
    var _props2 = this.props;
    const columnSetName = _props2.columnSetName,
          config = _props2.config,
          listType = _props2.listType,
          name = _props2.name,
          search = _props2.search,
          searchDescriptor = _props2.searchDescriptor;


    if (search) {
      search(config, name, searchDescriptor, listType, columnSetName);
    }
  }

  renderHeader() {
    var _props3 = this.props;
    const config = _props3.config,
          listType = _props3.listType,
          ownBlobCsid = _props3.ownBlobCsid,
          searchResult = _props3.searchResult,
          title = _props3.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 _props4 = this.props;
    const collapsed = _props4.collapsed,
          color = _props4.color,
          columnSetName = _props4.columnSetName,
          config = _props4.config,
          listType = _props4.listType,
          name = _props4.name,
          ownBlobCsid = _props4.ownBlobCsid,
          recordType = _props4.recordType,
          searchDescriptor = _props4.searchDescriptor;


    return _react2.default.createElement(
      _PanelContainer.ConnectedPanel,
      {
        className: _SearchPanel2.default.common,
        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;