'use strict';

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

var _regenerator;

function _load_regenerator() {
  return _regenerator = _interopRequireDefault(require('babel-runtime/regenerator'));
}

var _bluebird;

function _load_bluebird() {
  return _bluebird = require('bluebird');
}

var _chokidar;

function _load_chokidar() {
  return _chokidar = _interopRequireDefault(require('chokidar'));
}

var _chalk;

function _load_chalk() {
  return _chalk = _interopRequireDefault(require('chalk'));
}

var _consumer;

function _load_consumer() {
  return _consumer = require('../../../consumer');
}

var _build;

function _load_build() {
  return _build = require('../lib/build');
}

var _componentsList;

function _load_componentsList() {
  return _componentsList = _interopRequireDefault(require('../../../consumer/component/components-list'));
}

var _loader;

function _load_loader() {
  return _loader = _interopRequireDefault(require('../../../cli/loader'));
}

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

/**
 * Watch all components specified in bit.map.
 * Run buildAll for each change in the watched paths
 *
 * @export
 * @param {boolean} verbose - showing verbose output for inspection
 * @returns
 */
exports.default = function () {
  var _ref = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(verbose) {
    var consumer, componentsList, bitMapComponentsPaths, watcher;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return (0, (_consumer || _load_consumer()).loadConsumer)();

          case 2:
            consumer = _context.sent;
            componentsList = new (_componentsList || _load_componentsList()).default(consumer);
            bitMapComponentsPaths = componentsList.getPathsForAllFilesOfAllComponents(undefined, true);
            watcher = (_chokidar || _load_chokidar()).default.watch(bitMapComponentsPaths, {
              ignoreInitial: true,
              ignored: '**/dist/**'
            });


            console.log((_chalk || _load_chalk()).default.yellow('Starting watch for changes')); // eslint-disable-line no-console

            if (verbose) {
              // Print all watched paths
              bitMapComponentsPaths.forEach(function (path) {
                return console.log(`Watching ${path}`);
              }); // eslint-disable-line no-console
            }

            watcher.on('change', function (p) {
              var log = console.log.bind(console); // eslint-disable-line no-console
              log(`File ${p} has been changed, calling build`);
              // TODO: Make sure the log for build is printed to console
              (0, (_build || _load_build()).buildAll)(false, false).then(function (buildResult) {
                console.log(buildResult); // eslint-disable-line no-console
                (_loader || _load_loader()).default.stop();
                console.log((_chalk || _load_chalk()).default.yellow('watching for changes')); // eslint-disable-line no-console
              }).catch(function (err) {
                log(err); // eslint-disable-line
              });
            });

            return _context.abrupt('return', new Promise(function () {}));

          case 10:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

  function watchAll(_x) {
    return _ref.apply(this, arguments);
  }

  return watchAll;
}();