'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 _printTools = require('../utils/print-tools');

var _socket = require('./helpers/socket');

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

class Sync {
  constructor(context) {
    this.session = context.session;
    this.Socket = context.Socket;
  }

  run([cmd]) {
    const localSockets = this.Socket.listLocal();
    const socketsCount = localSockets.length;

    if (socketsCount === 0) {
      (0, _printTools.warning)('No Sockets were found in your config.');
      process.exit();
    }

    const pluralize = socketsCount === 1 ? 'socket' : 'sockets';

    (0, _printTools.echo)();
    (0, _printTools.echo)(4)(`Updating ${_chalk2.default.cyan(socketsCount)} ${pluralize}:`);
    (0, _printTools.echo)();

    const socketPromises = localSockets.map(socketName => this.Socket.get(socketName));
    const configs = {};

    _bluebird2.default.each(socketPromises, socket => (0, _socket.askForConfigOptionsToSync)(socket).then(config => {
      configs[socket.name] = config;
    })).then(sockets => sockets.map(socket => (0, _socket.updateSocket)(socket, configs[socket.name]))).then(updates => {
      _bluebird2.default.all(updates).then(resp => {
        (0, _printTools.echo)();
      });
    });
  }
}
exports.default = Sync;
module.exports = exports['default'];
//# sourceMappingURL=sync.js.map