"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_da_number = Intl.NumberFormat("da", { style: "decimal" }).format;
var minuteDuration = 60;
var hourDuration = 3600;

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

  return _s = hours, _n = +_s, (_t = +('' + _s).replace(/^[^.]*.?|0+$/g, ''), _i = Math.floor(Math.abs(+_s)), _n === 1 || _t !== 0 && (_i === 0 || _i === 1) ? _default_da_number(hours) + " time" : _default_da_number(hours) + " timer");
};

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

  return _s2 = minutes, _n2 = +_s2, (_t2 = +('' + _s2).replace(/^[^.]*.?|0+$/g, ''), _i2 = Math.floor(Math.abs(+_s2)), _n2 === 1 || _t2 !== 0 && (_i2 === 0 || _i2 === 1) ? _default_da_number(minutes) + " minut" : _default_da_number(minutes) + " minutter");
};

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

  return _s3 = seconds, _n3 = +_s3, (_t3 = +('' + _s3).replace(/^[^.]*.?|0+$/g, ''), _i3 = Math.floor(Math.abs(+_s3)), _n3 === 1 || _t3 !== 0 && (_i3 === 0 || _i3 === 1) ? _default_da_number(seconds) + " sekund" : _default_da_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] !== 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);
}