'use strict';

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

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _location_helpers = require('../location_helpers');

var _pickle_parser = require('../pickle_parser');

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

function buildEmptyMapping(stepDefinitions) {
  var mapping = {};
  stepDefinitions.forEach(function (stepDefinition) {
    var location = (0, _location_helpers.formatLocation)(stepDefinition);
    mapping[location] = {
      line: stepDefinition.line,
      pattern: stepDefinition.pattern,
      matches: [],
      uri: stepDefinition.uri
    };
  });
  return mapping;
}

function buildMapping(_ref) {
  var stepDefinitions = _ref.stepDefinitions,
      eventDataCollector = _ref.eventDataCollector;

  var mapping = buildEmptyMapping(stepDefinitions);
  _lodash2.default.each(eventDataCollector.testCaseMap, function (testCase) {
    var _eventDataCollector$g = eventDataCollector.getTestCaseData(testCase.sourceLocation),
        pickle = _eventDataCollector$g.pickle;

    var stepLineToPickledStepMap = (0, _pickle_parser.getStepLineToPickledStepMap)(pickle);
    testCase.steps.forEach(function (testStep) {
      var actionLocation = testStep.actionLocation,
          sourceLocation = testStep.sourceLocation,
          duration = testStep.result.duration;

      if (actionLocation && sourceLocation) {
        var location = (0, _location_helpers.formatLocation)(actionLocation);
        var match = {
          line: sourceLocation.line,
          text: stepLineToPickledStepMap[sourceLocation.line].text,
          uri: sourceLocation.uri
        };
        if (isFinite(duration)) {
          match.duration = duration;
        }
        if (mapping[location]) {
          mapping[location].matches.push(match);
        }
      }
    });
  });
  return mapping;
}

function invertNumber(key) {
  return function (obj) {
    var value = obj[key];
    if (isFinite(value)) {
      return -1 * value;
    }
    return 1;
  };
}

function buildResult(mapping) {
  return _lodash2.default.chain(mapping).map(function (_ref2) {
    var line = _ref2.line,
        matches = _ref2.matches,
        pattern = _ref2.pattern,
        uri = _ref2.uri;

    var sortedMatches = _lodash2.default.sortBy(matches, [invertNumber('duration'), 'text']);
    var result = { line: line, matches: sortedMatches, pattern: pattern, uri: uri };
    var meanDuration = _lodash2.default.meanBy(matches, 'duration');
    if (isFinite(meanDuration)) {
      result.meanDuration = meanDuration;
    }
    return result;
  }).sortBy(invertNumber('meanDuration')).value();
}

function getUsage(_ref3) {
  var stepDefinitions = _ref3.stepDefinitions,
      eventDataCollector = _ref3.eventDataCollector;

  var mapping = buildMapping({ stepDefinitions: stepDefinitions, eventDataCollector: eventDataCollector });
  return buildResult(mapping);
}
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 6860 characters]