'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _regenerator;

function _load_regenerator() {
  return _regenerator = _interopRequireDefault(require('babel-runtime/regenerator'));
}

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

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

var _fsExtra;

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

var _vinyl;

function _load_vinyl() {
  return _vinyl = _interopRequireDefault(require('vinyl'));
}

var _logger;

function _load_logger() {
  return _logger = _interopRequireDefault(require('../../../logger/logger'));
}

var _utils;

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

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

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

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

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

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

  _createClass(AbstractVinyl, [{
    key: 'updatePaths',


    // Update the base path and keep the relative value to be the same
    value: function updatePaths(_ref) {
      var newBase = _ref.newBase,
          newRelative = _ref.newRelative,
          newCwd = _ref.newCwd;

      var relative = newRelative || this.relative;
      if (newCwd) this.cwd = newCwd;
      this.base = newBase;
      this.path = (_path || _load_path()).default.join(this.base, relative);
    }
  }, {
    key: 'write',
    value: function () {
      var _ref2 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(writePath) {
        var force = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
        var filePath;
        return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                filePath = writePath || this.path;

                (_logger || _load_logger()).default.debug(`writing a file to the file-system at ${filePath}, force: ${force.toString()}`);

                if (!(!force && (_fsExtra || _load_fsExtra()).default.existsSync(filePath))) {
                  _context.next = 4;
                  break;
                }

                return _context.abrupt('return', null);

              case 4:
                _context.next = 6;
                return (_fsExtra || _load_fsExtra()).default.outputFile(filePath, (_utils || _load_utils()).eol.auto(this.contents, this.relative));

              case 6:
                return _context.abrupt('return', filePath);

              case 7:
              case 'end':
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function write(_x2) {
        return _ref2.apply(this, arguments);
      }

      return write;
    }()
  }, {
    key: 'toReadableString',
    value: function toReadableString() {
      return {
        relativePath: this.relative,
        content: this.contents.toString()
      };
    }
  }], [{
    key: 'loadFromParsedString',
    value: function loadFromParsedString(parsedString) {
      var contents = Buffer.isBuffer(parsedString._contents) ? parsedString._contents : Buffer.from(parsedString._contents);
      return {
        cwd: parsedString._cwd,
        path: parsedString.history[parsedString.history.length - 1],
        base: parsedString._base,
        contents
      };
    }
  }, {
    key: 'loadFromParsedStringArray',
    value: function loadFromParsedStringArray(arr) {
      if (!arr) return undefined;
      return arr.map(this.loadFromParsedString);
    }
  }]);

  return AbstractVinyl;
}((_vinyl || _load_vinyl()).default);

exports.default = AbstractVinyl;