'use strict';

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

var _regenerator;

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

var _chokidar;

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

var _ramda;

function _load_ramda() {
  return _ramda = _interopRequireDefault(require('ramda'));
}

var _consumer;

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

var _index;

function _load_index() {
  return _index = require('../index');
}

var _componentsList;

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

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"); }); }; } // todo: fix according to the build changes. buildInlineAll no longer exists

/**
 * 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 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(verbose) {
    var consumer, consumerPath, componentsList, bitMapComponentsValues, addConsumerPath, 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;
            consumerPath = consumer.getPath();
            componentsList = new (_componentsList || _load_componentsList()).default(consumer);
            _context.next = 7;
            return componentsList.idsFromBitMap();

          case 7:
            bitMapComponentsValues = _context.sent;

            addConsumerPath = function addConsumerPath(x) {
              return `${consumerPath}/${x.path}`;
            };

            bitMapComponentsPaths = (_ramda || _load_ramda()).default.map(addConsumerPath, bitMapComponentsValues);
            watcher = (_chokidar || _load_chokidar()).default.watch(bitMapComponentsPaths, {
              ignoreInitial: true,
              ignored: '**/dist/**'
            });


            console.log('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, (_index || _load_index()).buildAll)().then(function (buildResult) {
                console.log(buildResult); // eslint-disable-line no-console
              }).catch(function (err) {
                log(err); // eslint-disable-line
              });
            });

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

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

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

  return watchAll;
}();