'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.refNameToUrl = exports.getVocabulary = exports.getRecordType = exports.refNameToCsid = undefined;

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

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

var refNameToCsid = exports.refNameToCsid = function refNameToCsid(refName) {
  if (!refName) {
    return null;
  }

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

  if (itemShortID) {
    return 'urn:cspace:name(' + itemShortID + ')';
  }

  var csid = (0, _cspaceRefname.getCsid)(refName);

  return csid || null;
};

var getRecordType = exports.getRecordType = function getRecordType(config, refName) {
  if (!refName) {
    return null;
  }

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

  return recordTypeConfig ? recordTypeConfig.name : null;
};

var getVocabulary = exports.getVocabulary = function getVocabulary(config, refName) {
  if (!refName) {
    return null;
  }

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

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

  return vocabularyConfig ? vocabularyConfig.name : null;
};

var refNameToUrl = exports.refNameToUrl = function refNameToUrl(config, refName) {
  if (!refName) {
    return null;
  }

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

  if (!recordTypeConfig) {
    return null;
  }

  var recordType = recordTypeConfig.name;

  if (recordTypeConfig.vocabularies) {
    var vocabularyShortID = (0, _cspaceRefname.getVocabularyShortID)(refName);
    var vocabularyConfig = (0, _configHelpers.getVocabularyConfigByShortID)(recordTypeConfig, vocabularyShortID);

    if (!vocabularyConfig) {
      return null;
    }

    var vocabulary = vocabularyConfig.name;

    var itemShortID = (0, _cspaceRefname.getItemShortID)(refName);
    var _csid = 'urn:cspace:name(' + itemShortID + ')';

    return '/record/' + recordType + '/' + vocabulary + '/' + _csid;
  }

  var csid = (0, _cspaceRefname.getCsid)(refName);

  return '/record/' + recordType + '/' + csid;
};