'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 _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };

var _path;

function _load_path() {
  return _path = _interopRequireWildcard(require('path'));
}

var _repository;

function _load_repository() {
  return _repository = _interopRequireDefault(require('../repository'));
}

var _constants;

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

var _bitId;

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

var _utils;

function _load_utils() {
  return _utils = require('../../utils');
}

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var Source = function (_Repository) {
  _inherits(Source, _Repository);

  function Source() {
    _classCallCheck(this, Source);

    return _possibleConstructorReturn(this, (Source.__proto__ || Object.getPrototypeOf(Source)).apply(this, arguments));
  }

  _createClass(Source, [{
    key: 'getPath',
    value: function getPath() {
      return (_path || _load_path()).join(_get(Source.prototype.__proto__ || Object.getPrototypeOf(Source.prototype), 'getPath', this).call(this), (_constants || _load_constants()).BIT_SOURCES_DIRNAME);
    }
  }, {
    key: 'listVersions',
    value: function listVersions(bitId) {
      return (0, (_utils || _load_utils()).listDirectories)(this.composeVersionsPath(bitId.name, bitId.box)).map(function (version) {
        return version;
      });
    }
  }, {
    key: 'resolveVersion',
    value: function resolveVersion(id) {
      return id.getVersion().resolve(this.listVersions(id));
    }
  }, {
    key: 'isBoxEmpty',
    value: function isBoxEmpty(box) {
      return (0, (_utils || _load_utils()).empty)((0, (_utils || _load_utils()).listDirectories)(this.composeBoxPath(box)));
    }
  }, {
    key: 'composeBoxPath',
    value: function composeBoxPath(box) {
      return (_path || _load_path()).join(this.getPath(), box);
    }
  }, {
    key: 'clean',
    value: function clean(bitId) {
      bitId.version = this.resolveVersion(bitId);
      (0, (_utils || _load_utils()).rmDir)(this.composeSourcePath(bitId));

      if ((0, (_utils || _load_utils()).empty)(this.listVersions(bitId).length)) {
        (0, (_utils || _load_utils()).rmDir)(this.composeVersionsPath(bitId.name, bitId.box));
      }

      if (this.isBoxEmpty(bitId.box)) {
        (0, (_utils || _load_utils()).rmDir)(this.composeBoxPath(bitId.box));
      }
    }
  }, {
    key: 'composeVersionsPath',
    value: function composeVersionsPath(name, box) {
      return (_path || _load_path()).join(this.getPath(), box, name);
    }
  }, {
    key: 'composeSourcePath',
    value: function composeSourcePath(_ref) {
      var name = _ref.name,
          _ref$box = _ref.box,
          box = _ref$box === undefined ? 'global' : _ref$box,
          version = _ref.version;

      return (_path || _load_path()).join(this.getPath(), box, name, version);
    }
  }]);

  return Source;
}((_repository || _load_repository()).default);

exports.default = Source;