'use strict';

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

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var View = function () {
  function View(name, dependencies) {
    _classCallCheck(this, View);

    this.name = name;
    this.dependencies = dependencies;

    var notification = atom.notifications.addInfo('Installing ' + name + ' dependencies', {
      detail: 'Installing ' + dependencies.map(function (i) {
        return i.name;
      }).join(', '),
      dismissable: true
    });
    var progress = document.createElement('progress');
    this.dispose = function () {
      notification.dismiss();
    };
    this.advance = function () {
      progress.value++;
    };
    progress.max = dependencies.length;
    progress.style.width = '100%';
    try {
      var notificationView = atom.views.getView(notification);
      var notificationContent = notificationView.querySelector('.detail-content');
      if (notificationContent) {
        notificationContent.appendChild(progress);
      }
    } catch (_) {/* Notifications package is disabled */}
  }

  _createClass(View, [{
    key: 'complete',
    value: function complete(errors) {
      this.dispose();
      if (!errors.size) {
        atom.notifications.addSuccess('Installed ' + this.name + ' dependencies', {
          detail: 'Installed ' + this.dependencies.map(function (i) {
            return i.name;
          }).join(', ')
        });
        return;
      }
      var packages = [];
      var _iteratorNormalCompletion = true;
      var _didIteratorError = false;
      var _iteratorError = undefined;

      try {
        for (var _iterator = errors[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
          var _step$value = _slicedToArray(_step.value, 2);

          var packageName = _step$value[0];
          var error = _step$value[1];

          packages.push('  • ' + packageName);
          console.error('[Package-Deps] Unable to install', packageName, ', Error:', error && error.stack || error);
        }
      } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
      } finally {
        try {
          if (!_iteratorNormalCompletion && _iterator.return) {
            _iterator.return();
          }
        } finally {
          if (_didIteratorError) {
            throw _iteratorError;
          }
        }
      }

      atom.notifications.addWarning('Failed to install ' + this.name + ' dependencies', {
        detail: 'These packages were not installed, check your console\nfor more info.\n' + packages.join('\n'),
        dismissable: true
      });
    }
  }]);

  return View;
}();

exports.default = View;