'use strict';

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

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

var uriPattern = /\/cspace-services\/(.*?)\/(.*?)(\/items\/(.*?))?$/;

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

  var match = uriPattern.exec(uri);

  if (!match) {
    return undefined;
  }

  var servicePath = match[1];

  var csid = void 0;
  var vocabularyServicePath = void 0;

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

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

  if (!recordTypeConfig) {
    return undefined;
  }

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

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

    var vocabulary = vocabularyConfig ? vocabularyConfig.name : undefined;

    if (!vocabulary) {
      return undefined;
    }

    parts.push(vocabulary);
  }

  parts.push(csid);

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

exports.default = {};