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

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

const formatFieldName = (fieldConfig, intl) => {
  const formattedParentName = fieldConfig.extensionParentConfig ? formatFieldName(fieldConfig.extensionParentConfig, intl) : null;

  var _fieldConfig$messages = fieldConfig.messages;
  const fullName = _fieldConfig$messages.fullName,
        name = _fieldConfig$messages.name;


  const formattedName = intl.formatMessage(fullName || name);

  return [formattedParentName, formattedName].filter(part => !!part).join(' - ');
};

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

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

  const recordTypes = config.recordTypes;


  const formattedUses = {};

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

    const formattedFieldNames = uses[recordType].map(fieldConfig => formatFieldName(fieldConfig, intl)).sort();

    formattedUses[formattedRecordName] = formattedFieldNames;
  });

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

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

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

const contextTypes = {
  intl: _reactIntl.intlShape
};

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


  if (!data) {
    return null;
  }

  const shortId = data.getIn(['document', 'ns2:vocabularies_common', 'shortIdentifier']);
  const uses = (0, _configHelpers.findVocabularyUses)(config, shortId);
  const 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;