"use strict";

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

// MediaPlayer utils.js

/**
 * Create a time object (hour, minute, second) from an amount of seconds
 *
 * @param  {Number|String} value A duration of time represented in seconds
 *
 * @returns {Object}       An object with keys {hour, minute, second} representing the duration
 *                        seconds provided as an argument.
 * @private
 */
var parseTime = function parseTime(value) {
  value = parseFloat(value);
  var time = {};
  var hour = Math.floor(value / (60 * 60));
  time.minute = Math.floor(value / 60 % 60);
  time.second = Math.floor(value % 60);

  if (hour) {
    time.hour = hour;
  }

  return time;
};
/**
 * Generate a time usable by <time datetime />
 *
 * @param  {Number|String} seconds A duration of time represented in seconds
 *
 * @returns {String}      String formatted for use in a `datetime` field of a `<time>` tag.
 * @private
 */


var secondsToPeriod = function secondsToPeriod(seconds) {
  return 'P' + seconds + 'S';
};
/**
 * Make a human-readable time
 *
 * @param  {Number|String} seconds A duration of time represented in seconds
 * @param {DurationFmt} durfmt An instance of a {@link i18n/ilib/lib/DurationFmt.DurationFmt} object
 *                             from iLib confugured to display time used by the {@Link VideoPlayer}
 *                             component.
 * @param  {Object} config Additional configuration object that includes `includeHour`.
 *
 * @returns {String}      Formatted duration string
 * @private
 */


exports.secondsToPeriod = secondsToPeriod;

var secondsToTime = function secondsToTime(seconds, durfmt, config) {
  var includeHour = config && config.includeHour;

  if (durfmt && typeof durfmt.format === 'function') {
    var parsedTime = parseTime(seconds);
    var timeString = durfmt.format(parsedTime).toString();

    if (includeHour && !parsedTime.hour) {
      return '00:' + timeString;
    } else {
      return timeString;
    }
  }

  return includeHour ? '00:00:00' : '00:00';
};

exports.secondsToTime = secondsToTime;