"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

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

var _immutable = _interopRequireDefault(require("immutable"));

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

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

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

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

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _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; }; return _extends.apply(this, arguments); }

const messages = (0, _reactIntl.defineMessages)({
  titleWithCount: {
    "id": "mediaViewerPanel.titleWithCount",
    "defaultMessage": "{title}: {totalItems, number}"
  }
});
const propTypes = {
  collapsed: _propTypes.default.bool,
  color: _propTypes.default.string,
  columnSetName: _propTypes.default.string,
  config: _propTypes.default.shape({
    listTypes: _propTypes.default.object
  }),
  name: _propTypes.default.string,
  ownBlobCsid: _propTypes.default.string,
  recordType: _propTypes.default.string,
  searchDescriptor: _propTypes.default.instanceOf(_immutable.default.Map),
  searchResult: _propTypes.default.instanceOf(_immutable.default.Map),
  listType: _propTypes.default.string,
  title: _propTypes.default.node,
  search: _propTypes.default.func
};
const defaultProps = {
  columnSetName: 'default',
  listType: 'common'
};

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

  componentDidUpdate(prevProps) {
    const {
      searchDescriptor: prevSearchDescriptor,
      searchResult: prevSearchResult
    } = prevProps;
    const {
      searchDescriptor,
      searchResult
    } = this.props;

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

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

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

  renderHeader() {
    const {
      config,
      listType,
      ownBlobCsid,
      searchResult,
      title
    } = this.props;
    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 = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.titleWithCount, {
        values: {
          title,
          totalItems: count
        }
      }));
    } else {
      headerContent = title;
    }

    return /*#__PURE__*/_react.default.createElement("h3", null, headerContent);
  }

  render() {
    const {
      collapsed,
      color,
      columnSetName,
      config,
      listType,
      name,
      ownBlobCsid,
      recordType,
      searchDescriptor
    } = this.props;
    return /*#__PURE__*/_react.default.createElement(_PanelContainer.ConnectedPanel, {
      className: _SearchPanel.default.common,
      collapsible: true,
      collapsed: collapsed,
      color: color,
      config: config,
      header: this.renderHeader(),
      name: name,
      recordType: recordType
    }, /*#__PURE__*/_react.default.createElement(_MediaViewerContainer.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;