"use strict";

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

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

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

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

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

var _recordDataHelpers = require("../../helpers/recordDataHelpers");

var _SearchPanelContainer = _interopRequireDefault(require("../../containers/search/SearchPanelContainer"));

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

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

const messages = (0, _reactIntl.defineMessages)({
  title: {
    "id": "relatedMediaPanel.title",
    "defaultMessage": "Related Media"
  }
});

const getSearchDescriptor = (relatedCsid, updatedTimestamp) => _immutable.default.fromJS({
  recordType: 'media',
  searchQuery: {
    rel: relatedCsid,
    p: 0,
    size: 5
  },
  seqID: updatedTimestamp
});

const propTypes = {
  config: _propTypes.default.object,
  csid: _propTypes.default.string,
  recordData: _propTypes.default.instanceOf(_immutable.default.Map),
  recordType: _propTypes.default.string
};

class RelatedMediaPanel extends _react.Component {
  constructor(props) {
    super(props);
    this.handleSearchDescriptorChange = this.handleSearchDescriptorChange.bind(this);
    const {
      csid,
      recordData
    } = this.props;
    this.state = {
      searchDescriptor: getSearchDescriptor(csid, (0, _recordDataHelpers.getUpdatedTimestamp)(recordData))
    };
  }

  componentWillReceiveProps(nextProps) {
    const {
      csid,
      recordData
    } = this.props;
    const updatedTimestamp = (0, _recordDataHelpers.getUpdatedTimestamp)(recordData);
    const {
      csid: nextCsid,
      recordData: nextRecordData
    } = nextProps;
    const nextUpdatedTimestamp = (0, _recordDataHelpers.getUpdatedTimestamp)(nextRecordData);

    if (nextCsid !== csid || nextUpdatedTimestamp !== updatedTimestamp) {
      this.setState({
        searchDescriptor: getSearchDescriptor(nextCsid, nextUpdatedTimestamp)
      });
    }
  }

  handleSearchDescriptorChange(searchDescriptor) {
    this.setState({
      searchDescriptor
    });
  }

  render() {
    const {
      config,
      csid,
      recordType
    } = this.props;
    const {
      searchDescriptor
    } = this.state;

    if (!searchDescriptor.get('seqID')) {
      // Don't render until after the record has loaded.
      return null;
    }

    return _react.default.createElement(_SearchPanelContainer.default, {
      collapsed: true,
      columnSetName: "narrow",
      config: config,
      csid: csid,
      name: "relatedMediaPanel",
      searchDescriptor: searchDescriptor,
      recordType: recordType,
      title: _react.default.createElement(_reactIntl.FormattedMessage, messages.title),
      onSearchDescriptorChange: this.handleSearchDescriptorChange
    });
  }

}

exports.default = RelatedMediaPanel;
RelatedMediaPanel.propTypes = propTypes;