"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.withLeadingZero = exports.isValidTime = exports.isDecimal = exports.formatHours = void 0;
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
/**
 * @function formatHours
 * @description
 * Formats a decimal number representing hours into a formatted string (HH:MM).
 * The input can be a number, string, or undefined. The function handles various formats
 * and can optionally add a leading zero to the hours component.
 *
 * @param {number | string | undefined} num - The input representing the hours, which can be in decimal format, a time string, or undefined.
 * @param {boolean} [withLeadingZeroHours=false] - Whether to add a leading zero to the hours part of the output.
 * @param {boolean} [trimZeroMinutes=false] - Whether to remove minutes if they are zero.
 *
 * @returns {string} - A formatted time string in HH:MM format.
 *
 * @example
 * formatHours(1.5) // "1:30"
 * formatHours("3.5", true) // "03:30"
 */
var formatHours = exports.formatHours = function formatHours(num) {
  var withLeadingZeroHours = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  var trimZeroMinutes = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  if (num === 0 || num === "0") {
    return trimZeroMinutes ? "0" : withLeadingZeroHours ? "00:00" : "0:00";
  }
  if (!num) {
    return "";
  }
  if (typeof num === "string" && num.indexOf(":") >= 0) {
    // eslint-disable-next-line prefer-const
    var _num$split = num.split(":"),
      _num$split2 = _slicedToArray(_num$split, 2),
      _hours = _num$split2[0],
      _minutes = _num$split2[1];
    if (_minutes && _minutes.length === 1 && Number(_minutes) < 10) {
      _minutes = "".concat(Number(_minutes), "0");
    }
    if (_hours && _minutes) {
      if (trimZeroMinutes && _minutes === "00") {
        return withLeadingZeroHours ? withLeadingZero(_hours) : String(Number(_hours));
      }
      return withLeadingZeroHours ? "".concat(withLeadingZero(_hours), ":").concat(_minutes) : "".concat(_hours, ":").concat(_minutes);
    } else if (_hours && !_minutes) {
      return withLeadingZeroHours ? "".concat(withLeadingZero(_hours), ":00") : "".concat(_hours, ":00");
    } else if (!_hours && _minutes) {
      return withLeadingZeroHours ? "00:".concat(_minutes) : "0:".concat(_minutes);
    } else if (!_hours && !_minutes) {
      return withLeadingZeroHours ? "00:00" : "0:00";
    }
    return withLeadingZeroHours ? "00:".concat(_minutes) : "0:".concat(_minutes);
  }
  if (typeof num === "string" && num.indexOf(",") >= 0) {
    num = num.replace(",", ".");
  }
  var input = typeof num === "string" ? parseFloat(num) : num;
  if (!isDecimal(input)) {
    if (trimZeroMinutes) {
      return withLeadingZeroHours ? withLeadingZero(input) : String(Number(input));
    }
    return withLeadingZeroHours ? "".concat(withLeadingZero(input), ":00") : "".concat(input, ":00");
  }
  var decimal = input.toFixed(2);
  var time = decimal.toString().split(".");
  var hours = time[0];
  if (withLeadingZeroHours) {
    hours = withLeadingZero(hours);
  }
  var minutes = time[1];
  var minutes_formatted = Math.round(parseInt(minutes, 10) / 100 * 60);
  if (trimZeroMinutes && minutes_formatted === 0) {
    return hours;
  }
  return "".concat(hours, ":").concat(withLeadingZero(minutes_formatted));
};
var withLeadingZero = exports.withLeadingZero = function withLeadingZero(num) {
  var size = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
  var s = "".concat(num);
  while (s.length < size) s = "0" + s;
  return s;
};
var isDecimal = exports.isDecimal = function isDecimal(num) {
  return !Number.isInteger(num);
};
var isValidTime = exports.isValidTime = function isValidTime(time) {
  return time === undefined || /^([01]\d|2[0-3]):([0-5]\d)$/.test(time);
};
//# sourceMappingURL=timeUtils.js.map