"use strict";

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

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

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

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

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

var _get = _interopRequireDefault(require("lodash/get"));

var _TitleBar = _interopRequireDefault(require("../sections/TitleBar"));

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

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

var _WorkflowStateIcon = _interopRequireDefault(require("./WorkflowStateIcon"));

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

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)({
  authority: {
    "id": "recordTitleBar.authority",
    "defaultMessage": "{recordType} - {vocabulary}"
  }
});
const propTypes = {
  csid: _propTypes.default.string,
  data: _propTypes.default.instanceOf(_immutable.default.Map),
  recordType: _propTypes.default.string,
  vocabulary: _propTypes.default.string,
  searchName: _propTypes.default.string,
  searchDescriptor: _propTypes.default.instanceOf(_immutable.default.Map),
  // eslint-disable-next-line react/forbid-prop-types
  originSearchPageState: _propTypes.default.object,
  onDocked: _propTypes.default.func
};
const contextTypes = {
  config: _propTypes.default.shape({
    recordTypes: _propTypes.default.object
  }),
  intl: _reactIntl.intlShape
};

function RecordTitleBar(props, context) {
  const {
    csid,
    data,
    recordType,
    vocabulary,
    searchName,
    searchDescriptor,
    originSearchPageState,
    onDocked,
    ...remainingProps
  } = props;
  const {
    config,
    intl
  } = context;
  const recordTypeConfig = config.recordTypes[recordType];

  if (!recordTypeConfig) {
    return null;
  }

  const title = recordTypeConfig.title(data, {
    config,
    intl
  });
  let aside;

  if (vocabulary) {
    const values = {
      recordType: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, recordTypeConfig.messages.record.name),
      vocabulary: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, recordTypeConfig.vocabularies[vocabulary].messages.name)
    };
    aside = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.authority, {
      values: values
    }));
  } else {
    aside = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, recordTypeConfig.messages.record.name);
  }

  let nav;
  const isAudit = recordType === 'audit';

  if (searchDescriptor && !isAudit) {
    nav = /*#__PURE__*/_react.default.createElement(_SearchResultTraverserContainer.default, {
      config: config,
      csid: csid,
      searchName: searchName,
      searchDescriptor: searchDescriptor,
      originSearchPageState: originSearchPageState
    });
  }

  const serviceType = (0, _get.default)(recordTypeConfig, ['serviceConfig', 'serviceType']);

  const workflowStateIcon = /*#__PURE__*/_react.default.createElement(_WorkflowStateIcon.default, {
    value: (0, _recordDataHelpers.getWorkflowState)(data)
  });

  return /*#__PURE__*/_react.default.createElement(_TitleBar.default, _extends({
    serviceType: serviceType,
    title: title,
    aside: aside,
    icon: workflowStateIcon,
    nav: nav,
    onDocked: onDocked
  }, remainingProps));
}

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