"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

function _path() {
  const data = _interopRequireDefault(require("path"));

  _path = function () {
    return data;
  };

  return data;
}

function _generalError() {
  const data = _interopRequireDefault(require("../../error/general-error"));

  _generalError = function () {
    return data;
  };

  return data;
}

function _constants() {
  const data = require("../../constants");

  _constants = function () {
    return data;
  };

  return data;
}

function _bitId() {
  const data = _interopRequireDefault(require("../../bit-id/bit-id"));

  _bitId = function () {
    return data;
  };

  return data;
}

function getNodeModulesPathOfComponent(bindingPrefix, id, allowNonScope = false) {
  if (!id.scope && !allowNonScope) {
    throw new (_generalError().default)(`Failed creating a path in node_modules for ${id.toString()}, as it does not have a scope yet`);
  }

  if (!bindingPrefix) bindingPrefix = _constants().DEFAULT_BINDINGS_PREFIX; // Temp fix to support old components before the migration has been running

  bindingPrefix = bindingPrefix === 'bit' ? '@bit' : bindingPrefix;
  const allSlashes = new RegExp('/', 'g');
  const name = id.name.replace(allSlashes, _constants().NODE_PATH_COMPONENT_SEPARATOR);
  const partsToJoin = id.scope ? [id.scope, name] : [name];
  return _path().default.join('node_modules', bindingPrefix, partsToJoin.join(_constants().NODE_PATH_COMPONENT_SEPARATOR));
}