"use strict";

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

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

function _bluebird() {
  const data = require("bluebird");

  _bluebird = function () {
    return data;
  };

  return data;
}

function _ramda() {
  const data = _interopRequireDefault(require("ramda"));

  _ramda = function () {
    return data;
  };

  return data;
}

function _vinylFile() {
  const data = _interopRequireDefault(require("vinyl-file"));

  _vinylFile = function () {
    return data;
  };

  return data;
}

function _abstractVinyl() {
  const data = _interopRequireDefault(require("./abstract-vinyl"));

  _abstractVinyl = function () {
    return data;
  };

  return data;
}

function _fileSourceNotFound() {
  const data = _interopRequireDefault(require("../exceptions/file-source-not-found"));

  _fileSourceNotFound = function () {
    return data;
  };

  return data;
}

function _logger() {
  const data = _interopRequireDefault(require("../../../logger/logger"));

  _logger = function () {
    return data;
  };

  return data;
}

class SourceFile extends _abstractVinyl().default {
  // TODO: remove this distFilePath?
  static load(filePath, distTarget, base = consumerPath, // TODO: change params order to fix lint error
  consumerPath, extendedProps) {
    try {
      const file = new SourceFile(_vinylFile().default.readSync(filePath, {
        base,
        cwd: consumerPath
      }));

      const addToFile = (value, key) => file[key] = value;
      /* eslint-disable-line no-return-assign */


      _ramda().default.forEachObjIndexed(addToFile, extendedProps);

      return file;
    } catch (err) {
      _logger().default.errorAndAddBreadCrumb('source-file.load', 'failed loading file {filePath}. Error: {message}', {
        filePath,
        message: err.message
      }, err);

      if (err.code === 'ENOENT' && err.path) {
        throw new (_fileSourceNotFound().default)(err.path);
      }

      throw err;
    }
  }

  static loadFromParsedString(parsedString) {
    if (!parsedString) return null;
    const opts = super.loadFromParsedString(parsedString);
    return new SourceFile(opts);
  }

  static loadFromParsedStringArray(arr) {
    if (!arr) return null;
    return arr.map(this.loadFromParsedString);
  }

  static loadFromSourceFileModel(file, repository) {
    return (0, _bluebird().coroutine)(function* () {
      // $FlowFixMe
      const content = yield file.file.load(repository);
      return new SourceFile({
        base: '.',
        path: file.relativePath,
        contents: content.contents,
        test: file.test
      });
    })();
  }

  clone() {
    return new SourceFile(this);
  }

}

exports.default = SourceFile;