'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

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

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _configHelpers = require('../../helpers/configHelpers');

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

var _VocabularyUsedByPanel = require('../../../styles/cspace-ui/VocabularyUsedByPanel.css');

var _VocabularyUsedByPanel2 = _interopRequireDefault(_VocabularyUsedByPanel);

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

var messages = (0, _reactIntl.defineMessages)({
  title: {
    'id': 'vocabularyUsedByPanel.title',
    'defaultMessage': 'Used By'
  },
  notUsed: {
    'id': 'vocabularyUsedByPanel.notUsed',
    'defaultMessage': 'No uses found.'
  }
});

var renderUses = function renderUses(uses, config, intl) {
  var usedByRecordTypes = uses ? Object.keys(uses) : null;

  if (!usedByRecordTypes || usedByRecordTypes.length === 0) {
    return _react2.default.createElement(
      'div',
      null,
      _react2.default.createElement(_reactIntl.FormattedMessage, messages.notUsed)
    );
  }

  var recordTypes = config.recordTypes;


  var formattedUses = {};

  usedByRecordTypes.forEach(function (recordType) {
    var formattedRecordName = intl.formatMessage(recordTypes[recordType].messages.record.name);

    var formattedFieldNames = uses[recordType].map(function (fieldConfig) {
      var _fieldConfig$messages = fieldConfig.messages,
          fullName = _fieldConfig$messages.fullName,
          name = _fieldConfig$messages.name;


      return intl.formatMessage(fullName || name);
    }).sort();

    formattedUses[formattedRecordName] = formattedFieldNames;
  });

  var items = Object.keys(formattedUses).sort().map(function (formattedRecordName, index) {
    return _react2.default.createElement(
      'li',
      { key: index },
      _react2.default.createElement(
        'div',
        null,
        formattedRecordName
      ),
      _react2.default.createElement(
        'ul',
        null,
        formattedUses[formattedRecordName].map(function (formattedFieldName, fieldIndex) {
          return _react2.default.createElement(
            'li',
            { key: fieldIndex },
            formattedFieldName
          );
        })
      )
    );
  });

  return _react2.default.createElement(
    'ul',
    null,
    items
  );
};

var propTypes = {
  config: _propTypes2.default.object,
  data: _propTypes2.default.instanceOf(_immutable2.default.Map)
};

var contextTypes = {
  intl: _reactIntl.intlShape
};

function VocabularyUsedByPanel(props, context) {
  var config = props.config,
      data = props.data;
  var intl = context.intl;


  if (!data) {
    return null;
  }

  var shortId = data.getIn(['document', 'ns2:vocabularies_common', 'shortIdentifier']);
  var uses = (0, _configHelpers.findVocabularyUses)(config, shortId);
  var title = _react2.default.createElement(
    'h3',
    null,
    _react2.default.createElement(_reactIntl.FormattedMessage, messages.title)
  );

  return _react2.default.createElement(
    _PanelContainer.ConnectedPanel,
    {
      className: _VocabularyUsedByPanel2.default.common,
      collapsible: true,
      collapsed: true,
      config: config,
      header: title,
      name: 'usedBy',
      recordType: 'vocabulary'
    },
    renderUses(uses, config, intl)
  );
}

VocabularyUsedByPanel.propTypes = propTypes;
VocabularyUsedByPanel.contextTypes = contextTypes;