"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); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

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),
  originSearchPageState: _propTypes.default.object,
  onDocked: _propTypes.default.func
};
const contextTypes = {
  config: _propTypes.default.object,
  intl: _reactIntl.intlShape
};

function RecordTitleBar(props, context) {
  const {
    csid,
    data,
    recordType,
    vocabulary,
    searchName,
    searchDescriptor,
    originSearchPageState,
    onDocked
  } = props,
        remainingProps = _objectWithoutProperties(props, ["csid", "data", "recordType", "vocabulary", "searchName", "searchDescriptor", "originSearchPageState", "onDocked"]);

  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: _react.default.createElement(_reactIntl.FormattedMessage, recordTypeConfig.messages.record.name),
      vocabulary: _react.default.createElement(_reactIntl.FormattedMessage, recordTypeConfig.vocabularies[vocabulary].messages.name)
    };
    aside = _react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.authority, {
      values: values
    }));
  } else {
    aside = _react.default.createElement(_reactIntl.FormattedMessage, recordTypeConfig.messages.record.name);
  }

  let nav;

  if (searchDescriptor) {
    nav = _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 = _react.default.createElement(_WorkflowStateIcon.default, {
    value: (0, _recordDataHelpers.getWorkflowState)(data)
  });

  return _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;