'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getTestCases = exports.getTestCasesFromFilesystem = exports.getExpandedArgv = undefined;

var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray');

var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);

var _bluebird = require('bluebird');

var _bluebird2 = _interopRequireDefault(_bluebird);

var getExpandedArgv = exports.getExpandedArgv = function () {
  var _ref2 = (0, _bluebird.coroutine)(function* (_ref) {
    var argv = _ref.argv,
        cwd = _ref.cwd;

    var _ArgvParser$parse = _argv_parser2.default.parse(argv),
        options = _ArgvParser$parse.options;

    var fullArgv = argv;
    var profileArgv = yield new _profile_loader2.default(cwd).getArgv(options.profile);
    if (profileArgv.length > 0) {
      fullArgv = _lodash2.default.concat(argv.slice(0, 2), profileArgv, argv.slice(2));
    }
    return fullArgv;
  });

  return function getExpandedArgv(_x) {
    return _ref2.apply(this, arguments);
  };
}();

var getTestCasesFromFilesystem = exports.getTestCasesFromFilesystem = function () {
  var _ref4 = (0, _bluebird.coroutine)(function* (_ref3) {
    var cwd = _ref3.cwd,
        eventBroadcaster = _ref3.eventBroadcaster,
        featureDefaultLanguage = _ref3.featureDefaultLanguage,
        featurePaths = _ref3.featurePaths,
        order = _ref3.order,
        pickleFilter = _ref3.pickleFilter;

    var result = [];
    yield _bluebird2.default.each(featurePaths, function () {
      var _ref5 = (0, _bluebird.coroutine)(function* (featurePath) {
        var source = yield _fs2.default.readFile(featurePath, 'utf8');
        result = result.concat((yield getTestCases({
          eventBroadcaster: eventBroadcaster,
          language: featureDefaultLanguage,
          source: source,
          pickleFilter: pickleFilter,
          uri: _path2.default.relative(cwd, featurePath)
        })));
      });

      return function (_x3) {
        return _ref5.apply(this, arguments);
      };
    }());
    orderTestCases(result, order);
    return result;
  });

  return function getTestCasesFromFilesystem(_x2) {
    return _ref4.apply(this, arguments);
  };
}();

var getTestCases = exports.getTestCases = function () {
  var _ref7 = (0, _bluebird.coroutine)(function* (_ref6) {
    var eventBroadcaster = _ref6.eventBroadcaster,
        language = _ref6.language,
        pickleFilter = _ref6.pickleFilter,
        source = _ref6.source,
        uri = _ref6.uri;

    var result = [];
    var events = _gherkin2.default.generateEvents(source, uri, {}, language);
    events.forEach(function (event) {
      eventBroadcaster.emit(event.type, _lodash2.default.omit(event, 'type'));
      if (event.type === 'pickle') {
        var pickle = event.pickle;

        if (pickleFilter.matches({ pickle: pickle, uri: uri })) {
          eventBroadcaster.emit('pickle-accepted', { pickle: pickle, uri: uri });
          result.push({ pickle: pickle, uri: uri });
        } else {
          eventBroadcaster.emit('pickle-rejected', { pickle: pickle, uri: uri });
        }
      }
      if (event.type === 'attachment') {
        throw new Error('Parse error in \'' + uri + '\': ' + event.data);
      }
    });
    return result;
  });

  return function getTestCases(_x4) {
    return _ref7.apply(this, arguments);
  };
}();

// Orders the testCases in place - morphs input


exports.orderTestCases = orderTestCases;

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _argv_parser = require('./argv_parser');

var _argv_parser2 = _interopRequireDefault(_argv_parser);

var _fs = require('mz/fs');

var _fs2 = _interopRequireDefault(_fs);

var _gherkin = require('gherkin');

var _gherkin2 = _interopRequireDefault(_gherkin);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _profile_loader = require('./profile_loader');

var _profile_loader2 = _interopRequireDefault(_profile_loader);

var _knuthShuffleSeeded = require('knuth-shuffle-seeded');

var _knuthShuffleSeeded2 = _interopRequireDefault(_knuthShuffleSeeded);

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

function orderTestCases(testCases, order) {
  var _order$split = order.split(':'),
      _order$split2 = (0, _slicedToArray3.default)(_order$split, 2),
      type = _order$split2[0],
      seed = _order$split2[1];

  switch (type) {
    case 'defined':
      break;
    case 'random':
      if (!seed) {
        seed = Math.floor(Math.random() * 1000 * 1000).toString();
        console.warn('Random order using seed: ' + seed);
      }
      (0, _knuthShuffleSeeded2.default)(testCases, seed);
      break;
    default:
      throw new Error('Unrecgonized order type. Should be `defined` or `random`');
  }
}
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 7444 characters]