'use strict';

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

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

var _cspaceRefname = require('cspace-refname');

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

exports.default = function () {
  return {
    listTypes: {
      common: {
        listNodeName: 'ns2:abstract-common-list',
        itemNodeName: 'list-item',
        messages: (0, _reactIntl.defineMessages)({
          resultCount: {
            'id': 'list.common.resultCount',
            'defaultMessage': '{totalItems, plural, =0 {No records} one {1 record} other {{startNum}\u2013{endNum} of {totalItems} records}} found'
          },
          searching: {
            'id': 'list.common.searching',
            'defaultMessage': 'Finding records...'
          }
        }),
        getItemLocation: function getItemLocation(item, _ref) {
          var config = _ref.config,
              searchDescriptor = _ref.searchDescriptor;

          var docType = item.get('docType');
          var refName = item.get('refName');
          var csid = item.get('csid');

          var recordTypeConfig = void 0;

          if (docType) {
            // This is a search on multiple record types, which will contain the record type of each
            // item in the result.

            recordTypeConfig = (0, _configHelpers.getRecordTypeConfigByServiceObjectName)(config, docType);
          } else {
            // This is a search on a single record type, so the record type of all items is in the
            // search descriptor.

            recordTypeConfig = config.recordTypes[searchDescriptor.recordType];
          }

          if (recordTypeConfig) {
            if (recordTypeConfig.serviceConfig.serviceType === 'authority') {
              var vocabularyShortID = (0, _cspaceRefname.getVocabularyShortID)(refName);

              var vocabularyConfig = (0, _configHelpers.getVocabularyConfigByShortID)(recordTypeConfig, vocabularyShortID);

              if (vocabularyConfig) {
                return '/record/' + recordTypeConfig.name + '/' + vocabularyConfig.name + '/' + csid;
              }
            } else {
              return '/record/' + recordTypeConfig.name + '/' + csid;
            }
          }

          return null;
        }
      },
      authRef: {
        listNodeName: 'ns3:authority-ref-list',
        itemNodeName: 'authority-ref-item',
        // NB: This is a list of terms, not records: a record may appear multiple times in the list,
        // if multiple synonyms are used by the record. The messages reflect this.
        messages: (0, _reactIntl.defineMessages)({
          resultCount: {
            'id': 'list.authRef.resultCount',
            'defaultMessage': '{totalItems, plural, =0 {No terms} one {1 term} other {{startNum}\u2013{endNum} of {totalItems} terms}} found'
          },
          searching: {
            'id': 'list.authRef.searching',
            'defaultMessage': 'Finding terms...'
          }
        }),
        getItemLocation: function getItemLocation(item, _ref2) {
          var config = _ref2.config;

          var refName = item.get('refName');
          var servicePath = (0, _cspaceRefname.getServicePath)(refName);
          var recordTypeConfig = (0, _configHelpers.getRecordTypeConfigByServicePath)(config, servicePath);

          if (recordTypeConfig) {
            var vocabularyShortID = (0, _cspaceRefname.getVocabularyShortID)(refName);

            var vocabularyConfig = (0, _configHelpers.getVocabularyConfigByShortID)(recordTypeConfig, vocabularyShortID);

            if (vocabularyConfig) {
              var itemShortID = (0, _cspaceRefname.getItemShortID)(refName);

              if (itemShortID) {
                return '/record/' + recordTypeConfig.name + '/' + vocabularyConfig.name + '/urn:cspace:name(' + itemShortID + ')';
              }
            }
          }

          return null;
        }
      },
      refDoc: {
        listNodeName: 'ns3:authority-ref-doc-list',
        itemNodeName: 'authority-ref-doc-item',
        // NB: This is a list of uses, not records: a record may appear multiple times in the list,
        // if it uses the authority item multiple times in different fields (or different instances
        // of a repeating field). The messages reflect this.
        messages: (0, _reactIntl.defineMessages)({
          resultCount: {
            'id': 'list.refDoc.resultCount',
            'defaultMessage': '{totalItems, plural, =0 {No uses} one {1 use} other {{startNum}\u2013{endNum} of {totalItems} uses}} found'
          },
          searching: {
            'id': 'list.refDoc.searching',
            'defaultMessage': 'Finding uses...'
          }
        }),
        getItemLocation: function getItemLocation(item, _ref3) {
          var config = _ref3.config;

          var refName = item.get('refName');
          var csid = item.get('docId');
          var servicePath = (0, _cspaceRefname.getServicePath)(refName);
          var recordTypeConfig = (0, _configHelpers.getRecordTypeConfigByServicePath)(config, servicePath);

          if (recordTypeConfig) {
            if (recordTypeConfig.serviceConfig.serviceType === 'authority') {
              var vocabularyShortID = (0, _cspaceRefname.getVocabularyShortID)(refName);

              var vocabularyConfig = (0, _configHelpers.getVocabularyConfigByShortID)(recordTypeConfig, vocabularyShortID);

              if (vocabularyConfig) {
                return '/record/' + recordTypeConfig.name + '/' + vocabularyConfig.name + '/' + csid;
              }
            } else {
              return '/record/' + recordTypeConfig.name + '/' + csid;
            }
          }

          return null;
        }
      }
    }
  };
};