"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-i18n/lib/format-message"));

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

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

  return _s = hours, _n = +_s, (_n === 1 ? _default_nb_number(hours) + " time" : _default_nb_number(hours) + " timer");
};

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

  return _s2 = minutes, _n2 = +_s2, (_n2 === 1 ? _default_nb_number(minutes) + " minutt" : _default_nb_number(minutes) + " minutter");
};

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

  return _s3 = seconds, _n3 = +_s3, (_n3 === 1 ? _default_nb_number(seconds) + " sekund" : _default_nb_number(seconds) + " sekunder");
};

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

exports.formatTimespan = formatTimespan;

function formatTime(time) {
  var secondDuration = arguments.length > 1 && arguments[1] !== void 0 ? 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);
}