'use strict';

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

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

const uriPattern = /^\/(.*?)\/(.*?)(\/items\/(.*?))?$/;

const serviceUriToLocation = exports.serviceUriToLocation = (config, uri) => {
  if (!uri) {
    return undefined;
  }

  const match = uriPattern.exec(uri);

  if (!match) {
    return undefined;
  }

  const servicePath = match[1];

  let csid;
  let vocabularyServicePath;

  if (match[3]) {
    csid = match[4];
    vocabularyServicePath = match[2];
  } else {
    csid = match[2];
  }

  const recordTypeConfig = (0, _configHelpers.getRecordTypeConfigByServicePath)(config, servicePath);

  if (!recordTypeConfig) {
    return undefined;
  }

  const recordType = recordTypeConfig.name;
  const parts = ['/record', recordType];

  if (vocabularyServicePath) {
    const vocabularyConfig = (0, _configHelpers.getVocabularyConfigByServicePath)(recordTypeConfig, vocabularyServicePath);

    const vocabulary = vocabularyConfig ? vocabularyConfig.name : undefined;

    if (!vocabulary) {
      return undefined;
    }

    parts.push(vocabulary);
  }

  parts.push(csid);

  return {
    pathname: parts.join('/')
  };
};

exports.default = {};