"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _formatMessage = _interopRequireDefault(require("@instructure/quiz-common/lib/format-message"));

var _default_en_number = Intl.NumberFormat("en", { style: "decimal" }).format;
var minuteDuration = 60;
var hourDuration = 3600;

var formatHours = function formatHours(hours) {
  var _s, _n, _i, _v;

  return _s = hours, _n = +_s, (_i = Math.floor(Math.abs(+_s)), _v = (_s + '.').split('.')[1].length, _i === 1 && _v === 0 ? _default_en_number(hours) + " hour" : _default_en_number(hours) + " hours");
};

var formatMinutes = function formatMinutes(minutes) {
  var _s2, _n2, _i2, _v2;

  return _s2 = minutes, _n2 = +_s2, (_i2 = Math.floor(Math.abs(+_s2)), _v2 = (_s2 + '.').split('.')[1].length, _i2 === 1 && _v2 === 0 ? _default_en_number(minutes) + " minute" : _default_en_number(minutes) + " minutes");
};

var formatSeconds = function formatSeconds(seconds) {
  var _s3, _n3, _i3, _v3;

  return _s3 = seconds, _n3 = +_s3, (_i3 = Math.floor(Math.abs(+_s3)), _v3 = (_s3 + '.').split('.')[1].length, _i3 === 1 && _v3 === 0 ? _default_en_number(seconds) + " second" : _default_en_number(seconds) + " seconds");
};

var formatTimespan = function formatTimespan(startTime, endTime) {
  return formatTime(endTime - startTime, 1000);
};

exports.formatTimespan = formatTimespan;

function formatTime(time) {
  var secondDuration = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  var totalSeconds = Math.floor(time / secondDuration);
  var parts = [];
  var hours = Math.floor(totalSeconds / hourDuration);

  if (hours !== 0) {
    parts.push(formatHours(hours));
  }

  var minutes = Math.floor(totalSeconds / minuteDuration) % minuteDuration;

  if (minutes !== 0) {
    parts.push(formatMinutes(minutes));
  }

  var seconds = totalSeconds % minuteDuration;

  if (seconds !== 0 || parts.length === 0) {
    parts.push(formatSeconds(seconds));
  }

  var separator = " ";
  return parts.join(separator);
}