"use strict";

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

function _react() {
  const data = _interopRequireDefault(require("react"));

  _react = function _react() {
    return data;
  };

  return data;
}

function _ora() {
  const data = _interopRequireDefault(require("ora"));

  _ora = function _ora() {
    return data;
  };

  return data;
}

function _events() {
  const data = require("events");

  _events = function _events() {
    return data;
  };

  return data;
}

var _MemoryStream = _interopRequireDefault(require("./MemoryStream"));

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

class Logger extends _events().EventEmitter {
  constructor() {
    super();
    this.id = void 0;
    this.spinner = void 0;
    this.log = '';
    this.ws = null;
    this.ws = new _MemoryStream.default({
      onData: this.onChildProcessData.bind(this)
    });
    this.spinner = (0, _ora().default)({
      stream: this.ws
    });
  }

  success(info) {
    this.spinner.succeed(info);
  }

  error(info) {
    this.spinner.fail(info);
  }

  succeed(info) {
    this.spinner.succeed(info);
  }

  start(info) {
    this.spinner.start(info);
  }

  fail(info) {
    this.spinner.fail(info);
  }

  stopAndPersist(option) {
    this.spinner.stopAndPersist(option);
  }

  setId(id) {
    this.id = id;
  }

  clear() {
    this.log = '';
  }

  getLog() {
    return this.log;
  } // 主动添加日志


  appendLog(data = '') {
    if (!data) {
      return;
    }

    this.log = `${this.log}${data}\n`;
    this.emit('log', {
      data: `${data}\n`
    });
  }
  /**
   * 接受来自子进程的日志
   *  1. ora: spinner 进程
   *  2. execa: 子进程日志
   */


  onChildProcessData(chunk) {
    const data = chunk.toString();

    if (!data) {
      return;
    }

    this.log = `${this.log}${data}`;
    this.emit('log', {
      data
    });
  }

}

var _default = Logger;
exports.default = _default;