'use strict';

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

var _chalk = require('chalk');

var _chalk2 = _interopRequireDefault(_chalk);

var _bluebird = require('bluebird');

var _bluebird2 = _interopRequireDefault(_bluebird);

var _debug = require('../utils/debug');

var _debug2 = _interopRequireDefault(_debug);

var _spinner = require('./helpers/spinner');

var _printTools = require('../utils/print-tools');

var _dateUtils = require('../utils/date-utils');

var _print = require('./helpers/print');

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

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new _bluebird2.default(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 _bluebird2.default.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

const { debug } = (0, _debug2.default)('cmd-socket-sourcemaps');

class SocketSourceMapsCmd {
  constructor(context) {
    this.context = context;
    this.session = context.session;
    this.Socket = context.Socket;
    this.registry = new context.Registry();
    this.firstRun = true;

    this.mainSpinner = new _spinner.GlobalSpinner((0, _printTools.p)(3)(`${_chalk2.default.grey('waiting...')}`));
  }

  run([cmd]) {
    var _this = this;

    return _asyncToGenerator(function* () {
      _this.cmd = cmd;

      const msg = (0, _printTools.p)(2)(`${_chalk2.default.magenta('getting sockets:')} ${(0, _dateUtils.currentTime)()}`);
      const spinner = new _spinner.SimpleSpinner(msg).start();
      _this.socketList = yield _this.Socket.flatList();
      spinner.stop();

      const configs = {};

      _bluebird2.default.all(_this.socketList.map(function (socket) {
        return _this.buildSocketSourceMap(socket, configs[socket.name]);
      })).then(function () {
        (0, _printTools.echo)();
      });
    })();
  }

  buildSocketSourceMap(socket, config) {
    var _this2 = this;

    return _asyncToGenerator(function* () {
      const deployTimer = new _dateUtils.Timer();
      const msg = (0, _printTools.p)(1)(`${_chalk2.default.magenta('building source map:')} ${(0, _dateUtils.currentTime)()} ${_chalk2.default.cyan(socket.name)}`);
      const spinner = new _spinner.SimpleSpinner(msg).start();

      // Let's compile
      return socket.compile({ withSourceMaps: true }).then(function (resp) {
        spinner.stop();
        return SocketSourceMapsCmd.printCompileSuccessful(socket.name, deployTimer);
      }).catch(function (err) {
        spinner.stop();
        (0, _print.printCompileError)(err, socket.name);

        if (_this2.cmd.bail) {
          SocketSourceMapsCmd.bail();
        }
      });
    })();
  }

  getSocketToUpdate(fileName) {
    return this.localSockets.find(socket => socket.isSocketFile(fileName));
  }

  static bail() {
    (0, _printTools.echo)();
    process.exit(1);
  }

  static printCompileSuccessful(socketName, deployTimer) {
    debug('printUpdateSuccessful', socketName);

    const duration = _chalk2.default.dim(deployTimer.getDuration());
    const socketNameStr = `${_chalk2.default.cyan(socketName)}`;
    const status = _chalk2.default.grey('source map built:');
    (0, _printTools.echo)(6)(`${status} ${(0, _dateUtils.currentTime)()} ${socketNameStr} ${duration}`);
    //
    // if (updateStatus.status === 'ok') {
    //   const status = format.grey('socket synced:');
    //   echo(6)(`${status} ${currentTime()} ${socketNameStr} ${duration}`);
    // } else if (updateStatus.status === 'stopped') {
    //   const status = format.grey('socket in sync:');
    //   echo(5)(`${status} ${currentTime()} ${socketNameStr} ${duration}`);
    // } else if (updateStatus.status === 'error') {
    //   const errDetail = format.red(updateStatus.message.error);
    //   const status = format.red('socket not synced:');
    //   echo(2)(`${status} ${currentTime()} ${socketNameStr} ${duration} ${errDetail}`);
    // } else {
    //   const status = format.red('socket not synced:');
    //   echo(2)(`${status} ${currentTime()} ${socketNameStr} ${duration}`);
    // }
  }
}
exports.default = SocketSourceMapsCmd;
module.exports = exports['default'];
//# sourceMappingURL=socket-sourcemaps.js.map