// Generated by CoffeeScript 1.12.7
(function() {
  var Stopwatch, duration, hrtime;

  hrtime = require('./hrtime');

  duration = require('./duration');

  Stopwatch = (function() {
    var hrtimeDiffToNanos;

    hrtimeDiffToNanos = function(diff) {
      return diff[0] * 1e9 + diff[1];
    };

    function Stopwatch(hrtime1) {
      this.hrtime = hrtime1;
      this.reset();
    }

    Stopwatch.prototype.reset = function() {
      this.accumulator = 0;
      this.lastTime = null;
      return this;
    };

    Stopwatch.prototype.start = function() {
      if (this.lastTime == null) {
        this.lastTime = this.hrtime();
      }
      return this;
    };

    Stopwatch.prototype.stop = function() {
      if (this.lastTime != null) {
        this.accumulator += hrtimeDiffToNanos(this.hrtime(this.lastTime));
        this.lastTime = null;
      }
      return this;
    };

    Stopwatch.prototype.duration = function() {
      return duration["new"](this.elapsedNanos());
    };

    Stopwatch.prototype.format = function() {
      return this.duration().format();
    };

    Stopwatch.prototype.toString = function() {
      return this.format();
    };

    Stopwatch.prototype.elapsedNanos = function() {
      if (this.lastTime != null) {
        return this.accumulator + hrtimeDiffToNanos(this.hrtime(this.lastTime));
      } else {
        return this.accumulator;
      }
    };

    Stopwatch.prototype.isRunning = function() {
      return this.lastTime != null;
    };

    return Stopwatch;

  })();

  module.exports = {
    "new": function() {
      return new Stopwatch(hrtime);
    },
    newWithCustomClock: function(clock) {
      return new Stopwatch(clock);
    }
  };

}).call(this);
