'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 _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 _ramda;

function _load_ramda() {
  return _ramda = _interopRequireDefault(require('ramda'));
}

var _vinylFile;

function _load_vinylFile() {
  return _vinylFile = _interopRequireDefault(require('vinyl-file'));
}

var _abstractVinyl;

function _load_abstractVinyl() {
  return _abstractVinyl = _interopRequireDefault(require('./abstract-vinyl'));
}

var _fileSourceNotFound;

function _load_fileSourceNotFound() {
  return _fileSourceNotFound = _interopRequireDefault(require('../exceptions/file-source-not-found'));
}

var _logger;

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

var _objects;

function _load_objects() {
  return _objects = require('../../../scope/objects');
}

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

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

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

  _createClass(SourceFile, [{
    key: 'clone',
    value: function clone() {
      return new SourceFile(this);
    }
  }], [{
    key: 'load',
    value: function load(filePath, distTarget) {
      var base = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : consumerPath;
      var // TODO: change params order to fix lint error
      consumerPath = arguments[3];
      var extendedProps = arguments[4];

      try {
        var file = new SourceFile((_vinylFile || _load_vinylFile()).default.readSync(filePath, { base, cwd: consumerPath }));
        var addToFile = function addToFile(value, key) {
          return file[key] = value;
        }; /* eslint-disable-line no-return-assign */
        (_ramda || _load_ramda()).default.forEachObjIndexed(addToFile, extendedProps);
        return file;
      } catch (err) {
        (_logger || _load_logger()).default.error(`failed loading file ${filePath}. Error: ${err}`);
        if (err.code === 'ENOENT' && err.path) {
          throw new (_fileSourceNotFound || _load_fileSourceNotFound()).default(err.path);
        }
        throw err;
      }
    }
    // TODO: remove this distFilePath?

  }, {
    key: 'loadFromParsedString',
    value: function loadFromParsedString(parsedString) {
      if (!parsedString) return null;
      var opts = _get(SourceFile.__proto__ || Object.getPrototypeOf(SourceFile), 'loadFromParsedString', this).call(this, parsedString);
      return new SourceFile(opts);
    }
  }, {
    key: 'loadFromParsedStringArray',
    value: function loadFromParsedStringArray(arr) {
      if (!arr) return null;
      return arr.map(this.loadFromParsedString);
    }
  }, {
    key: 'loadFromSourceFileModel',
    value: function () {
      var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(file, repository) {
        var content;
        return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.next = 2;
                return file.file.load(repository);

              case 2:
                content = _context.sent;
                return _context.abrupt('return', new SourceFile({ base: '.', path: file.relativePath, contents: content.contents, test: file.test }));

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

      function loadFromSourceFileModel(_x2, _x3) {
        return _ref.apply(this, arguments);
      }

      return loadFromSourceFileModel;
    }()
  }]);

  return SourceFile;
}((_abstractVinyl || _load_abstractVinyl()).default);

exports.default = SourceFile;