"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.refNameToUrl = exports.refNameToCsid = exports.getVocabulary = exports.getRecordType = void 0;
var _cspaceRefname = require("cspace-refname");
var _configHelpers = require("./configHelpers");
const refNameToCsid = refName => {
  if (!refName) {
    return null;
  }
  const itemShortID = (0, _cspaceRefname.getItemShortID)(refName);
  if (itemShortID) {
    return `urn:cspace:name(${itemShortID})`;
  }
  const csid = (0, _cspaceRefname.getCsid)(refName);
  return csid || null;
};
exports.refNameToCsid = refNameToCsid;
const getRecordType = (config, refName) => {
  if (!refName) {
    return null;
  }
  const servicePath = (0, _cspaceRefname.getServicePath)(refName);
  const recordTypeConfig = (0, _configHelpers.getRecordTypeConfigByServicePath)(config, servicePath);
  return recordTypeConfig ? recordTypeConfig.name : null;
};
exports.getRecordType = getRecordType;
const getVocabulary = (config, refName) => {
  if (!refName) {
    return null;
  }
  const servicePath = (0, _cspaceRefname.getServicePath)(refName);
  const recordTypeConfig = (0, _configHelpers.getRecordTypeConfigByServicePath)(config, servicePath);
  const vocabularyShortID = (0, _cspaceRefname.getVocabularyShortID)(refName);
  const vocabularyConfig = (0, _configHelpers.getVocabularyConfigByShortID)(recordTypeConfig, vocabularyShortID);
  return vocabularyConfig ? vocabularyConfig.name : null;
};
exports.getVocabulary = getVocabulary;
const refNameToUrl = (config, refName, csid) => {
  if (!refName) {
    return null;
  }
  const servicePath = (0, _cspaceRefname.getServicePath)(refName);
  const recordTypeConfig = (0, _configHelpers.getRecordTypeConfigByServicePath)(config, servicePath);
  if (!recordTypeConfig) {
    return null;
  }
  const recordType = recordTypeConfig.name;
  if (recordTypeConfig.vocabularies) {
    const vocabularyShortID = (0, _cspaceRefname.getVocabularyShortID)(refName);
    const vocabularyConfig = (0, _configHelpers.getVocabularyConfigByShortID)(recordTypeConfig, vocabularyShortID);
    if (!vocabularyConfig) {
      return null;
    }
    const vocabulary = vocabularyConfig.name;
    const itemShortID = (0, _cspaceRefname.getItemShortID)(refName);
    const csidOrURN = csid || `urn:cspace:name(${itemShortID})`;
    return `/record/${recordType}/${vocabulary}/${csidOrURN}`;
  }
  return `/record/${recordType}/${csid || (0, _cspaceRefname.getCsid)(refName)}`;
};
exports.refNameToUrl = refNameToUrl;