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

exports.default = RecordTitleBar;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

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

var _get = require('lodash/get');

var _get2 = _interopRequireDefault(_get);

var _TitleBar = require('../sections/TitleBar');

var _TitleBar2 = _interopRequireDefault(_TitleBar);

var _SearchResultTraverserContainer = require('../../containers/search/SearchResultTraverserContainer');

var _SearchResultTraverserContainer2 = _interopRequireDefault(_SearchResultTraverserContainer);

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

var _WorkflowStateIcon = require('./WorkflowStateIcon');

var _WorkflowStateIcon2 = _interopRequireDefault(_WorkflowStateIcon);

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

var messages = (0, _reactIntl.defineMessages)({
  authority: {
    'id': 'recordTitleBar.authority',
    'defaultMessage': '{recordType} - {vocabulary}'
  }
});

var propTypes = {
  csid: _propTypes2.default.string,
  data: _propTypes2.default.instanceOf(_immutable2.default.Map),
  recordType: _propTypes2.default.string,
  vocabulary: _propTypes2.default.string,
  searchName: _propTypes2.default.string,
  searchDescriptor: _propTypes2.default.instanceOf(_immutable2.default.Map),
  onDocked: _propTypes2.default.func
};

var contextTypes = {
  config: _propTypes2.default.object,
  intl: _reactIntl.intlShape
};

function RecordTitleBar(props, context) {
  var csid = props.csid,
      data = props.data,
      recordType = props.recordType,
      vocabulary = props.vocabulary,
      searchName = props.searchName,
      searchDescriptor = props.searchDescriptor,
      onDocked = props.onDocked,
      remainingProps = _objectWithoutProperties(props, ['csid', 'data', 'recordType', 'vocabulary', 'searchName', 'searchDescriptor', 'onDocked']);

  var config = context.config,
      intl = context.intl;


  var recordTypeConfig = config.recordTypes[recordType];

  if (!recordTypeConfig) {
    return null;
  }

  var title = recordTypeConfig.title(data, { config: config, intl: intl });

  var aside = void 0;

  if (vocabulary) {
    var values = {
      recordType: _react2.default.createElement(_reactIntl.FormattedMessage, recordTypeConfig.messages.record.name),
      vocabulary: _react2.default.createElement(_reactIntl.FormattedMessage, recordTypeConfig.vocabularies[vocabulary].messages.name)
    };

    aside = _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.authority, { values: values }));
  } else {
    aside = _react2.default.createElement(_reactIntl.FormattedMessage, recordTypeConfig.messages.record.name);
  }

  var nav = void 0;

  if (searchDescriptor) {
    nav = _react2.default.createElement(_SearchResultTraverserContainer2.default, {
      config: config,
      csid: csid,
      searchName: searchName,
      searchDescriptor: searchDescriptor
    });
  }

  var serviceType = (0, _get2.default)(recordTypeConfig, ['serviceConfig', 'serviceType']);
  var workflowStateIcon = _react2.default.createElement(_WorkflowStateIcon2.default, { value: (0, _recordDataHelpers.getWorkflowState)(data) });

  return _react2.default.createElement(_TitleBar2.default, _extends({
    serviceType: serviceType,
    title: title,
    aside: aside,
    icon: workflowStateIcon,
    nav: nav,
    onDocked: onDocked
  }, remainingProps));
}

RecordTitleBar.propTypes = propTypes;
RecordTitleBar.contextTypes = contextTypes;