'use strict';

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

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _gherkin_document_parser = require('./gherkin_document_parser');

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

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

var EventDataCollector = function () {
  function EventDataCollector(eventBroadcaster) {
    (0, _classCallCheck3.default)(this, EventDataCollector);

    eventBroadcaster.on('gherkin-document', this.storeGherkinDocument.bind(this)).on('pickle-accepted', this.storePickle.bind(this)).on('test-case-prepared', this.storeTestCase.bind(this)).on('test-step-attachment', this.storeTestStepAttachment.bind(this)).on('test-step-finished', this.storeTestStepResult.bind(this)).on('test-case-finished', this.storeTestCaseResult.bind(this));
    this.gherkinDocumentMap = {}; // uri to gherkinDocument
    this.pickleMap = {}; // uri:line to {pickle, uri}
    this.testCaseMap = {}; // uri:line to {sourceLocation, steps, result}
  }

  (0, _createClass3.default)(EventDataCollector, [{
    key: 'getTestCaseKey',
    value: function getTestCaseKey(_ref) {
      var uri = _ref.uri,
          line = _ref.line;

      return uri + ':' + line;
    }
  }, {
    key: 'getTestCaseData',
    value: function getTestCaseData(sourceLocation) {
      return {
        gherkinDocument: this.gherkinDocumentMap[sourceLocation.uri],
        pickle: this.pickleMap[this.getTestCaseKey(sourceLocation)],
        testCase: this.testCaseMap[this.getTestCaseKey(sourceLocation)]
      };
    }
  }, {
    key: 'getTestStepData',
    value: function getTestStepData(_ref2) {
      var sourceLocation = _ref2.testCase.sourceLocation,
          index = _ref2.index;

      var _getTestCaseData = this.getTestCaseData(sourceLocation),
          gherkinDocument = _getTestCaseData.gherkinDocument,
          pickle = _getTestCaseData.pickle,
          testCase = _getTestCaseData.testCase;

      var result = { testStep: testCase.steps[index] };
      if (result.testStep.sourceLocation) {
        var line = result.testStep.sourceLocation.line;

        result.gherkinKeyword = (0, _gherkin_document_parser.getStepLineToKeywordMap)(gherkinDocument)[line];
        result.pickleStep = (0, _pickle_parser.getStepLineToPickledStepMap)(pickle)[line];
      }
      return result;
    }
  }, {
    key: 'storeGherkinDocument',
    value: function storeGherkinDocument(_ref3) {
      var document = _ref3.document,
          uri = _ref3.uri;

      this.gherkinDocumentMap[uri] = document;
    }
  }, {
    key: 'storePickle',
    value: function storePickle(_ref4) {
      var pickle = _ref4.pickle,
          uri = _ref4.uri;

      this.pickleMap[uri + ':' + pickle.locations[0].line] = pickle;
    }
  }, {
    key: 'storeTestCase',
    value: function storeTestCase(_ref5) {
      var sourceLocation = _ref5.sourceLocation,
          steps = _ref5.steps;

      var key = this.getTestCaseKey(sourceLocation);
      this.testCaseMap[key] = { sourceLocation: sourceLocation, steps: steps };
    }
  }, {
    key: 'storeTestStepAttachment',
    value: function storeTestStepAttachment(_ref6) {
      var index = _ref6.index,
          testCase = _ref6.testCase,
          data = _ref6.data,
          media = _ref6.media;

      var key = this.getTestCaseKey(testCase.sourceLocation);
      var step = this.testCaseMap[key].steps[index];
      if (!step.attachments) {
        step.attachments = [];
      }
      step.attachments.push({ data: data, media: media });
    }
  }, {
    key: 'storeTestStepResult',
    value: function storeTestStepResult(_ref7) {
      var index = _ref7.index,
          testCase = _ref7.testCase,
          result = _ref7.result;

      var key = this.getTestCaseKey(testCase.sourceLocation);
      this.testCaseMap[key].steps[index].result = result;
    }
  }, {
    key: 'storeTestCaseResult',
    value: function storeTestCaseResult(_ref8) {
      var sourceLocation = _ref8.sourceLocation,
          result = _ref8.result;

      var key = this.getTestCaseKey(sourceLocation);
      this.testCaseMap[key].result = result;
    }
  }]);
  return EventDataCollector;
}();

exports.default = EventDataCollector;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 7828 characters]