'use strict';

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

var _chalk = require('chalk');

var _chalk2 = _interopRequireDefault(_chalk);

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

var _debug2 = _interopRequireDefault(_debug);

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

var _hosting = require('../utils/hosting');

var _hosting2 = _interopRequireDefault(_hosting);

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

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

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

class HostingSync {
  constructor(context) {
    debug('HostingSync.constructor');
    this.context = context;
    this.session = context.session;
    this.Socket = context.Socket;
  }

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

    return _asyncToGenerator(function* () {
      debug(`HostingSync.run ${hostingName}`);
      _this.name = hostingName;
      if (!hostingName || typeof hostingName !== 'string') return (0, _printTools.error)('Hosting name is a required parameter!');

      let hosting = null;
      if (cmd.socket) {
        _this.socket = yield _this.Socket.get(cmd.socket);
      } else {
        hosting = yield _hosting2.default.get(hostingName, _this.socket);
      }

      if (!hosting.existLocally) {
        if (_this.socket) {
          (0, _printTools.error)(4)(`There is no "${_this.name}" hosting in the "${_this.socket.name}" socket!`);
        } else {
          (0, _printTools.error)(4)(`There is no "${_this.name}" hosting in project!`);
        }
        (0, _printTools.echo)();
        process.exit();
      }

      // if (!hosting.existRemotely) {
      //   echo();
      //   echon('Syncing hosting...');
      //
      //   const updateStatus = await this.socket.update();
      //   if (updateStatus.status === 'ok') {
      //     echo(format.green('Done'));
      //   } else {
      //     error(updateStatus.message);
      //   }
      //
      //   hosting = await this.socket.getHosting(hostingName);
      // }

      return hosting.existRemotely && _this.syncHosting(hosting);
    })();
  }

  syncHosting(hosting) {
    var _this2 = this;

    return _asyncToGenerator(function* () {
      debug(`Syncing ${hosting.name} (${_this2.session.project.instance})`);

      if (!hosting.name) return;
      (0, _printTools.echo)();
      (0, _printTools.echo)(8)(`Syncing hosting files for ${_chalk2.default.blue(hosting.name)}`);
      (0, _printTools.echo)(8)(`${_chalk2.default.dim(hosting.getURL())}`);
      (0, _printTools.echo)();

      return hosting.syncFiles();
    })();
  }
}

exports.default = HostingSync;
module.exports = exports['default'];
//# sourceMappingURL=hosting-sync.js.map