"use strict";

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

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

function _fsExtra() {
  const data = _interopRequireDefault(require("fs-extra"));

  _fsExtra = function () {
    return data;
  };

  return data;
}

function _createSymlinkOrCopy() {
  const data = _interopRequireDefault(require("../utils/fs/create-symlink-or-copy"));

  _createSymlinkOrCopy = function () {
    return data;
  };

  return data;
}

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

  _bitId = function () {
    return data;
  };

  return data;
}

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

  _generalError = function () {
    return data;
  };

  return data;
}

class Symlink {
  // current existing path
  // new symlink path
  constructor(src, dest, componentId) {
    this.src = src;
    this.dest = dest;
    this.componentId = componentId;
  }

  write() {
    this._throwForMissingDistOutsideComponent();

    return (0, _createSymlinkOrCopy().default)(this.src, this.dest, this.componentId ? this.componentId.toString() : null);
  }

  static makeInstance(src, dest, componentId) {
    return new Symlink(src, dest, componentId);
  }

  _throwForMissingDistOutsideComponent() {
    if (!this.forDistOutsideComponentsDir) return;

    const srcExists = _fsExtra().default.existsSync(this.src);

    if (!srcExists) {
      const componentId = this.componentId ? this.componentId.toString() : '';
      throw new (_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".`);
    }
  }

}

exports.default = Symlink;