'use strict';

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

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _utils;

function _load_utils() {
  return _utils = require('../../utils');
}

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var SpecsResults = function () {
  function SpecsResults(_ref) {
    var tests = _ref.tests,
        stats = _ref.stats,
        pass = _ref.pass,
        failures = _ref.failures,
        specFile = _ref.specFile;

    _classCallCheck(this, SpecsResults);

    this.tests = tests;
    this.stats = stats;
    this.pass = pass;
    this.failures = failures;
    this.specFile = (0, (_utils || _load_utils()).pathNormalizeToLinux)(specFile);
  }

  _createClass(SpecsResults, [{
    key: 'serialize',
    value: function serialize() {
      return {
        tests: this.tests,
        stats: this.stats,
        pass: this.pass,
        failures: this.failures,
        specFile: this.specFile
      };
    }
  }], [{
    key: 'deserialize',
    value: function deserialize(plainObject) {
      return new SpecsResults(plainObject);
    }
  }, {
    key: 'createFromRaw',
    value: function createFromRaw(rawResults) {
      var hasFailures = rawResults.failures && rawResults.failures.length;
      var pass = rawResults.pass || !hasFailures && rawResults.tests.every(function (test) {
        return test.pass;
      });
      var failures = void 0;

      var calcDuration = function calcDuration(endDateString, startDateString) {
        if (!endDateString || !startDateString) return undefined;
        return new Date(endDateString) - new Date(startDateString);
      };

      var start = rawResults.stats ? rawResults.stats.start : undefined;
      var end = rawResults.stats ? rawResults.stats.end : undefined;
      var duration = rawResults.stats && rawResults.stats.duration ? parseInt(rawResults.stats.duration) : calcDuration(end, start);
      var stats = {
        start,
        end,
        duration
      };

      var tests = rawResults.tests.map(function (result) {
        result.duration = parseInt(result.duration);
        // $FlowFixMe
        return result;
      });

      if (hasFailures) {
        failures = rawResults.failures.map(function (failure) {
          failure.duration = failure.duration ? parseInt(failure.duration) : undefined;
          // $FlowFixMe
          return failure;
        });
      }

      return new SpecsResults({ tests, stats, pass, failures, specFile: rawResults.specPath });
    }
  }]);

  return SpecsResults;
}();

exports.default = SpecsResults;