"use strict";

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

function _utils() {
  const data = require("../../utils");

  _utils = function () {
    return data;
  };

  return data;
}

function _bitId() {
  const data = require("../../bit-id");

  _bitId = function () {
    return data;
  };

  return data;
}

class SpecsResults {
  constructor({
    tests,
    stats,
    pass,
    failures,
    specFile
  }) {
    this.tests = tests;
    this.stats = stats;
    this.pass = pass;
    this.failures = failures;
    this.specFile = (0, _utils().pathNormalizeToLinux)(specFile);
  }

  serialize() {
    return {
      tests: this.tests,
      stats: this.stats,
      pass: this.pass,
      failures: this.failures,
      specFile: this.specFile
    };
  }

  static deserialize(plainObject) {
    return new SpecsResults(plainObject);
  }

  static createFromRaw(rawResults) {
    const hasFailures = rawResults.failures && rawResults.failures.length;
    const pass = rawResults.pass || !hasFailures && rawResults.tests.every(test => test.pass);
    let failures;

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

    const start = rawResults.stats ? rawResults.stats.start : undefined;
    const end = rawResults.stats ? rawResults.stats.end : undefined;
    const duration = rawResults.stats && rawResults.stats.duration ? parseInt(rawResults.stats.duration) : calcDuration(end, start);
    const stats = {
      start,
      end,
      duration
    };
    const tests = rawResults.tests.map(result => {
      result.duration = parseInt(result.duration); // $FlowFixMe

      return result;
    });

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

        return failure;
      });
    }

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

}

exports.default = SpecsResults;