"use strict";

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

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

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

  _bluebird = function () {
    return data;
  };

  return data;
}

function _defineProperty2() {
  const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

  _defineProperty2 = function () {
    return data;
  };

  return data;
}

function _path() {
  const data = _interopRequireDefault(require("path"));

  _path = function () {
    return data;
  };

  return data;
}

function _fsExtra() {
  const data = _interopRequireDefault(require("fs-extra"));

  _fsExtra = function () {
    return data;
  };

  return data;
}

function _vinyl() {
  const data = _interopRequireDefault(require("vinyl"));

  _vinyl = function () {
    return data;
  };

  return data;
}

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

  _logger = function () {
    return data;
  };

  return data;
}

function _utils() {
  const data = require("../../../utils");

  _utils = function () {
    return data;
  };

  return data;
}

function _source() {
  const data = _interopRequireDefault(require("../../../scope/models/source"));

  _source = function () {
    return data;
  };

  return data;
}

class AbstractVinyl extends _vinyl().default {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2().default)(this, "override", true);
    (0, _defineProperty2().default)(this, "verbose", false);
  }

  static fromVinyl(vinyl) {
    if (vinyl instanceof AbstractVinyl) return vinyl;
    return new AbstractVinyl(vinyl);
  } // Update the base path and keep the relative value to be the same


  updatePaths({
    newBase,
    newRelative,
    newCwd
  }) {
    const relative = newRelative || this.relative;
    const base = newBase || this.base;
    if (newCwd) this.cwd = newCwd;
    this.base = base;
    this.path = _path().default.join(this.base, relative);
  }

  write(writePath, override = this.override, verbose = this.verbose) {
    var _this = this;

    return (0, _bluebird().coroutine)(function* () {
      const filePath = writePath || _this.path;

      const msg = _verboseMsg(filePath, override);

      if (verbose) {
        console.log(msg); // eslint-disable-line no-console
      }

      _logger().default.debug(msg);

      if (!override && _fsExtra().default.existsSync(filePath)) return null;
      yield _fsExtra().default.outputFile(filePath, _utils().eol.auto(_this.contents));
      return filePath;
    })();
  }

  toReadableString() {
    return {
      relativePath: this.relative,
      content: this.contents.toString()
    };
  }

  static loadFromParsedString(parsedString) {
    const 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
    };
  }

  static loadFromParsedStringArray(arr) {
    if (!arr) return undefined;
    return arr.map(this.loadFromParsedString);
  }
  /**
   * before saving component files in the model, their EOL should be converted to Linux format so
   * then when working on the same components in Windows and Linux they won't appear as modified
   */


  toSourceAsLinuxEOL() {
    // $FlowFixMe
    return _source().default.from(_utils().eol.lf(this.contents));
  }

  _getStatIfFileExists() {
    var _this2 = this;

    return (0, _bluebird().coroutine)(function* () {
      try {
        return yield _fsExtra().default.lstat(_this2.path);
      } catch (err) {
        return null; // probably file does not exist
      }
    })();
  }

}
/**
 * Generate message for the logs and for output in case of verbose
 * this function is exported for testing purposes
 */


exports.default = AbstractVinyl;

function _verboseMsg(filePath, force) {
  const msg = `writing a file to the file-system at ${filePath}, force: ${force.toString()}`;
  return msg;
}