'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 _path;

function _load_path() {
  return _path = require('../../../utils/path');
}

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, manipulateDirData, originallySharedDir) {
      var pathWithoutSharedDir = function pathWithoutSharedDir(pathStr, sharedDir) {
        if (!sharedDir) return pathStr;
        var partToRemove = `${sharedDir}/`;
        return pathStr.replace(partToRemove, '');
      };
      var depManipulateDir = manipulateDirData.find(function (manipulateDirItem) {
        return manipulateDirItem.id.isEqual(dependency.id);
      });
      dependency.relativePaths.forEach(function (relativePath) {
        if (relativePath.isCustomResolveUsed) return; // don't strip sharedDir when custom resolved is used
        relativePath.sourceRelativePath = pathWithoutSharedDir(relativePath.sourceRelativePath, originallySharedDir);
        if (depManipulateDir) {
          relativePath.destinationRelativePath = pathWithoutSharedDir(relativePath.destinationRelativePath, depManipulateDir.originallySharedDir);
        }
      });
    }
  }, {
    key: 'addWrapDir',
    value: function addWrapDir(dependency, manipulateDirData, componentWrapDir) {
      var pathWithWrapDir = function pathWithWrapDir(pathStr, wrapDir) {
        if (!wrapDir) return pathStr;
        return (0, (_path || _load_path()).pathJoinLinux)(wrapDir, pathStr);
      };

      var depManipulateDir = manipulateDirData.find(function (manipulateDirItem) {
        return manipulateDirItem.id.isEqual(dependency.id);
      });
      dependency.relativePaths.forEach(function (relativePath) {
        if (relativePath.isCustomResolveUsed) return; // don't add wrapDir when custom resolved is used
        relativePath.sourceRelativePath = pathWithWrapDir(relativePath.sourceRelativePath, componentWrapDir);
        if (depManipulateDir && depManipulateDir.wrapDir) {
          relativePath.destinationRelativePath = pathWithWrapDir(relativePath.destinationRelativePath, depManipulateDir.wrapDir);
        }
      });
    }
  }, {
    key: 'getClone',
    value: function getClone(dependency) {
      return {
        id: dependency.id,
        relativePaths: (_ramda || _load_ramda()).default.clone(dependency.relativePaths)
      };
    }
  }]);

  return Dependency;
}();

exports.default = Dependency;