'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 _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _2 = require('./');

var _3 = _interopRequireDefault(_2);

var _status = require('../status');

var _status2 = _interopRequireDefault(_status);

var _helpers = require('./helpers');

var _step_arguments = require('../step_arguments');

var _assertionErrorFormatter = require('assertion-error-formatter');

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

var getStepLineToKeywordMap = _helpers.GherkinDocumentParser.getStepLineToKeywordMap,
    getScenarioLineToDescriptionMap = _helpers.GherkinDocumentParser.getScenarioLineToDescriptionMap;
var getScenarioDescription = _helpers.PickleParser.getScenarioDescription,
    getStepLineToPickledStepMap = _helpers.PickleParser.getStepLineToPickledStepMap,
    getStepKeyword = _helpers.PickleParser.getStepKeyword;

var JsonFormatter = function (_Formatter) {
  (0, _inherits3.default)(JsonFormatter, _Formatter);

  function JsonFormatter(options) {
    (0, _classCallCheck3.default)(this, JsonFormatter);

    var _this = (0, _possibleConstructorReturn3.default)(this, (JsonFormatter.__proto__ || Object.getPrototypeOf(JsonFormatter)).call(this, options));

    options.eventBroadcaster.on('test-run-finished', _this.onTestRunFinished.bind(_this));
    return _this;
  }

  (0, _createClass3.default)(JsonFormatter, [{
    key: 'convertNameToId',
    value: function convertNameToId(obj) {
      return obj.name.replace(/ /g, '-').toLowerCase();
    }
  }, {
    key: 'formatDataTable',
    value: function formatDataTable(dataTable) {
      return {
        rows: dataTable.rows.map(function (row) {
          return { cells: _lodash2.default.map(row.cells, 'value') };
        })
      };
    }
  }, {
    key: 'formatDocString',
    value: function formatDocString(docString) {
      return {
        content: docString.content,
        line: docString.location.line
      };
    }
  }, {
    key: 'formatStepArguments',
    value: function formatStepArguments(stepArguments) {
      var iterator = (0, _step_arguments.buildStepArgumentIterator)({
        dataTable: this.formatDataTable.bind(this),
        docString: this.formatDocString.bind(this)
      });
      return _lodash2.default.map(stepArguments, iterator);
    }
  }, {
    key: 'onTestRunFinished',
    value: function onTestRunFinished() {
      var _this2 = this;

      var groupedTestCases = {};
      _lodash2.default.each(this.eventDataCollector.testCaseMap, function (testCase) {
        var uri = testCase.sourceLocation.uri;

        if (!groupedTestCases[uri]) {
          groupedTestCases[uri] = [];
        }
        groupedTestCases[uri].push(testCase);
      });
      var features = _lodash2.default.map(groupedTestCases, function (group, uri) {
        var gherkinDocument = _this2.eventDataCollector.gherkinDocumentMap[uri];
        var featureData = _this2.getFeatureData(gherkinDocument.feature, uri);
        var stepLineToKeywordMap = getStepLineToKeywordMap(gherkinDocument);
        var scenarioLineToDescriptionMap = getScenarioLineToDescriptionMap(gherkinDocument);
        featureData.elements = group.map(function (testCase) {
          var _eventDataCollector$g = _this2.eventDataCollector.getTestCaseData(testCase.sourceLocation),
              pickle = _eventDataCollector$g.pickle;

          var scenarioData = _this2.getScenarioData({
            featureId: featureData.id,
            pickle: pickle,
            scenarioLineToDescriptionMap: scenarioLineToDescriptionMap
          });
          var stepLineToPickledStepMap = getStepLineToPickledStepMap(pickle);
          var isBeforeHook = true;
          scenarioData.steps = testCase.steps.map(function (testStep) {
            isBeforeHook = isBeforeHook && !testStep.sourceLocation;
            return _this2.getStepData({
              isBeforeHook: isBeforeHook,
              stepLineToKeywordMap: stepLineToKeywordMap,
              stepLineToPickledStepMap: stepLineToPickledStepMap,
              testStep: testStep
            });
          });
          return scenarioData;
        });
        return featureData;
      });
      this.log(JSON.stringify(features, null, 2));
    }
  }, {
    key: 'getFeatureData',
    value: function getFeatureData(feature, uri) {
      return {
        description: feature.description,
        keyword: feature.keyword,
        name: feature.name,
        line: feature.location.line,
        id: this.convertNameToId(feature),
        tags: this.getTags(feature),
        uri: uri
      };
    }
  }, {
    key: 'getScenarioData',
    value: function getScenarioData(_ref) {
      var featureId = _ref.featureId,
          pickle = _ref.pickle,
          scenarioLineToDescriptionMap = _ref.scenarioLineToDescriptionMap;

      var description = getScenarioDescription({
        pickle: pickle,
        scenarioLineToDescriptionMap: scenarioLineToDescriptionMap
      });
      return {
        description: description,
        id: featureId + ';' + this.convertNameToId(pickle),
        keyword: 'Scenario',
        line: pickle.locations[0].line,
        name: pickle.name,
        tags: this.getTags(pickle),
        type: 'scenario'
      };
    }
  }, {
    key: 'getStepData',
    value: function getStepData(_ref2) {
      var isBeforeHook = _ref2.isBeforeHook,
          stepLineToKeywordMap = _ref2.stepLineToKeywordMap,
          stepLineToPickledStepMap = _ref2.stepLineToPickledStepMap,
          testStep = _ref2.testStep;

      var data = {};
      if (testStep.sourceLocation) {
        var line = testStep.sourceLocation.line;

        var pickleStep = stepLineToPickledStepMap[line];
        data.arguments = this.formatStepArguments(pickleStep.arguments);
        data.keyword = getStepKeyword({ pickleStep: pickleStep, stepLineToKeywordMap: stepLineToKeywordMap });
        data.line = line;
        data.name = pickleStep.text;
      } else {
        data.keyword = isBeforeHook ? 'Before' : 'After';
        data.hidden = true;
      }
      if (testStep.actionLocation) {
        data.match = { location: (0, _helpers.formatLocation)(testStep.actionLocation) };
      }
      if (testStep.result) {
        var _testStep$result = testStep.result,
            exception = _testStep$result.exception,
            status = _testStep$result.status;

        data.result = { status: status };
        if (testStep.result.duration) {
          data.result.duration = testStep.result.duration * 1000000;
        }
        if (status === _status2.default.FAILED && exception) {
          data.result.error_message = (0, _assertionErrorFormatter.format)(exception);
        }
      }
      if (_lodash2.default.size(testStep.attachments) > 0) {
        data.embeddings = testStep.attachments.map(function (attachment) {
          return {
            data: attachment.data,
            mime_type: attachment.media.type
          };
        });
      }
      return data;
    }
  }, {
    key: 'getTags',
    value: function getTags(obj) {
      return _lodash2.default.map(obj.tags, function (tagData) {
        return {
          name: tagData.name,
          line: tagData.location.line
        };
      });
    }
  }]);
  return JsonFormatter;
}(_3.default);

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