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

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

var _uuid;

function _load_uuid() {
  return _uuid = require('uuid');
}

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

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

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 Tmp = function (_Repository) {
  _inherits(Tmp, _Repository);

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

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

  _createClass(Tmp, [{
    key: 'getPath',
    value: function getPath() {
      return (_path || _load_path()).join(_get(Tmp.prototype.__proto__ || Object.getPrototypeOf(Tmp.prototype), 'getPath', this).call(this), (_constants || _load_constants()).BIT_TMP_DIRNAME);
    }
  }, {
    key: 'composePath',
    value: function composePath(p) {
      return (_path || _load_path()).join(this.getPath(), p);
    }
  }, {
    key: 'save',
    value: function save(data) {
      var _this2 = this;

      var ext = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '.js';

      return new Promise(function (resolve, reject) {
        var fileName = (0, (_uuid || _load_uuid()).v4)();
        var filePath = _this2.composePath(`${fileName}${ext}`);
        (_fsExtra || _load_fsExtra()).default.outputFile(filePath, data, function (err) {
          if (err) return reject(err);
          return resolve(filePath);
        });
      });
    }
  }, {
    key: 'saveSync',
    value: function saveSync(data) {
      var ext = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '.js';

      var fileName = (0, (_uuid || _load_uuid()).v4)();
      var filePath = this.composePath(`${fileName}${ext}`);
      (_fsExtra || _load_fsExtra()).default.outputFileSync(filePath, data);
      return filePath;
    }
  }, {
    key: 'remove',
    value: function remove(fileNameOrPath) {
      var _this3 = this;

      var ext = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '.js';

      return new Promise(function (resolve, reject) {
        var fileName = (_path || _load_path()).parse(fileNameOrPath).name;
        var filePath = _this3.composePath(`${fileName}${ext}`);
        (_fsExtra || _load_fsExtra()).default.remove(filePath, function (err) {
          if (err) return reject(err);
          return resolve();
        });
      });
    }
  }, {
    key: 'removeSync',
    value: function removeSync(fileNameOrPath) {
      var ext = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '.js';

      var fileName = (_path || _load_path()).parse(fileNameOrPath).name;
      var filePath = this.composePath(`${fileName}${ext}`);
      return (_fsExtra || _load_fsExtra()).default.removeSync(filePath);
    }
  }, {
    key: 'clear',
    value: function clear() {
      return (_fsExtra || _load_fsExtra()).default.emptyDir(this.getPath());
    }
  }, {
    key: 'clearSync',
    value: function clearSync() {
      return (_fsExtra || _load_fsExtra()).default.emptyDirSync(this.getPath());
    }
  }]);

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

exports.default = Tmp;