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

function _load_fsExtra() {
  return _fsExtra = _interopRequireDefault(require('fs-extra'));
}

var _createSymlinkOrCopy;

function _load_createSymlinkOrCopy() {
  return _createSymlinkOrCopy = _interopRequireDefault(require('../utils/fs/create-symlink-or-copy'));
}

var _bitId;

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

var _generalError;

function _load_generalError() {
  return _generalError = _interopRequireDefault(require('../error/general-error'));
}

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"); } }

var Symlink = function () {
  // new symlink path
  function Symlink(src, dest, componentId) {
    _classCallCheck(this, Symlink);

    this.src = src;
    this.dest = dest;
    this.componentId = componentId;
  } // current existing path


  _createClass(Symlink, [{
    key: 'write',
    value: function write() {
      this._throwForMissingDistOutsideComponent();
      return (0, (_createSymlinkOrCopy || _load_createSymlinkOrCopy()).default)(this.src, this.dest, this.componentId ? this.componentId.toString() : null);
    }
  }, {
    key: '_throwForMissingDistOutsideComponent',
    value: function _throwForMissingDistOutsideComponent() {
      if (!this.forDistOutsideComponentsDir) return;
      var srcExists = (_fsExtra || _load_fsExtra()).default.existsSync(this.src);
      if (!srcExists) {
        var componentId = this.componentId ? this.componentId.toString() : '';
        throw new (_generalError || _load_generalError()).default(`unable to link ${componentId}, the file ${this.src} is missing from the filesystem.
it happens when the "dist" directory is set to be outside the components directory, either by changing this settings later or by cloning the project without the dist directory
to rebuild the "dist" directory for all components, please run "bit import --merge".`);
      }
    }
  }], [{
    key: 'makeInstance',
    value: function makeInstance(src, dest, componentId) {
      return new Symlink(src, dest, componentId);
    }
  }]);

  return Symlink;
}();

exports.default = Symlink;