'use strict';

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

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _ramda;

function _load_ramda() {
  return _ramda = _interopRequireDefault(require('ramda'));
}

var _bitId;

function _load_bitId() {
  return _bitId = require('../../../bit-id');
}

var _bitMap;

function _load_bitMap() {
  return _bitMap = _interopRequireDefault(require('../../bit-map'));
}

var _constants;

function _load_constants() {
  return _constants = require('../../../constants');
}

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

/**
 * a dependency component may have multiple files that are required from the parent component, each
 * one of the files has its RelativePath instance.
 *
 * For example:
 * main component: "foo" => foo.js => `const isString = require('../utils/is-string'); const isArray = require('../utils/is-array');
 * dependency: "utils" => utils/is-string.js, utils/is-array.js
 * In this example, the component "foo" has one dependency "utils" with two RelativePaths.
 * one for utils/is-string.js file and the second for utils/is-array.js file
 */
var Dependency = function () {
  function Dependency(id, relativePaths) {
    _classCallCheck(this, Dependency);

    this.id = id;
    this.relativePaths = relativePaths;
  }

  _createClass(Dependency, null, [{
    key: 'stripOriginallySharedDir',
    value: function stripOriginallySharedDir(dependency, bitMap, originallySharedDir) {
      var pathWithoutSharedDir = function pathWithoutSharedDir(pathStr, sharedDir) {
        if (!sharedDir) return pathStr;
        var partToRemove = `${sharedDir}/`;
        return pathStr.replace(partToRemove, '');
      };
      var depFromBitMap = bitMap.getComponentIfExist(dependency.id);
      dependency.relativePaths.forEach(function (relativePath) {
        relativePath.sourceRelativePath = pathWithoutSharedDir(relativePath.sourceRelativePath, originallySharedDir);
        if (depFromBitMap && depFromBitMap.origin === (_constants || _load_constants()).COMPONENT_ORIGINS.IMPORTED) {
          relativePath.destinationRelativePath = pathWithoutSharedDir(relativePath.destinationRelativePath, depFromBitMap.originallySharedDir);
        }
      });
    }
  }, {
    key: 'getClone',
    value: function getClone(dependency) {
      return {
        id: dependency.id,
        relativePaths: (_ramda || _load_ramda()).default.clone(dependency.relativePaths)
      };
    }
  }]);

  return Dependency;
}();

exports.default = Dependency;