"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = VocabularyUsedByPanel;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _reactIntl = require("react-intl");
var _immutable = _interopRequireDefault(require("immutable"));
var _configHelpers = require("../../helpers/configHelpers");
var _formatHelpers = require("../../helpers/formatHelpers");
var _PanelContainer = require("../../containers/layout/PanelContainer");
var _VocabularyUsedByPanel = _interopRequireDefault(require("../../../styles/cspace-ui/VocabularyUsedByPanel.css"));
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 renderUses = (uses, config, intl) => {
  const usedByRecordTypes = uses ? Object.keys(uses) : null;
  if (!usedByRecordTypes || usedByRecordTypes.length === 0) {
    return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.notUsed));
  }
  const {
    recordTypes
  } = config;
  const formattedUses = {};
  usedByRecordTypes.forEach(recordType => {
    const formattedRecordName = intl.formatMessage(recordTypes[recordType].messages.record.name);
    const formattedFieldNames = uses[recordType].map(fieldConfig => (0, _formatHelpers.formatExtensionFieldName)(intl, fieldConfig)).sort();
    formattedUses[formattedRecordName] = formattedFieldNames;
  });
  const items = Object.keys(formattedUses).sort().map((formattedRecordName, index) =>
  /*#__PURE__*/
  // FIXME: Use a different key
  // eslint-disable-next-line react/no-array-index-key
  _react.default.createElement("li", {
    key: index
  }, /*#__PURE__*/_react.default.createElement("div", null, formattedRecordName), /*#__PURE__*/_react.default.createElement("ul", null, formattedUses[formattedRecordName].map((formattedFieldName, fieldIndex) =>
  /*#__PURE__*/
  // FIXME: Use a different key
  // eslint-disable-next-line react/no-array-index-key
  _react.default.createElement("li", {
    key: fieldIndex
  }, formattedFieldName)))));
  return /*#__PURE__*/_react.default.createElement("ul", null, items);
};
const propTypes = {
  config: _propTypes.default.shape({
    recordTypes: _propTypes.default.object
  }),
  data: _propTypes.default.instanceOf(_immutable.default.Map)
};
const contextTypes = {
  intl: _reactIntl.intlShape
};
function VocabularyUsedByPanel(props, context) {
  const {
    config,
    data
  } = props;
  const {
    intl
  } = context;
  if (!data) {
    return null;
  }
  const shortId = data.getIn(['document', 'ns2:vocabularies_common', 'shortIdentifier']);
  const uses = (0, _configHelpers.findVocabularyUses)(config, shortId);
  const title = /*#__PURE__*/_react.default.createElement("h3", null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.title));
  return /*#__PURE__*/_react.default.createElement(_PanelContainer.ConnectedPanel, {
    className: _VocabularyUsedByPanel.default.common,
    collapsible: true,
    collapsed: true,
    config: config,
    header: title,
    name: "usedBy",
    recordType: "vocabulary"
  }, renderUses(uses, config, intl));
}
VocabularyUsedByPanel.propTypes = propTypes;
VocabularyUsedByPanel.contextTypes = contextTypes;