/**
 * @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"
 */
export const formatHours = function (num, withLeadingZeroHours, trimZeroMinutes) {
  if (withLeadingZeroHours === void 0) {
    withLeadingZeroHours = false;
  }
  if (trimZeroMinutes === void 0) {
    trimZeroMinutes = 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
    let [_hours, _minutes] = num.split(":");
    if (_minutes && _minutes.length === 1 && Number(_minutes) < 10) {
      _minutes = Number(_minutes) + "0";
    }
    if (_hours && _minutes) {
      if (trimZeroMinutes && _minutes === "00") {
        return withLeadingZeroHours ? withLeadingZero(_hours) : String(Number(_hours));
      }
      return withLeadingZeroHours ? withLeadingZero(_hours) + ":" + _minutes : _hours + ":" + _minutes;
    } else if (_hours && !_minutes) {
      return withLeadingZeroHours ? withLeadingZero(_hours) + ":00" : _hours + ":00";
    } else if (!_hours && _minutes) {
      return withLeadingZeroHours ? "00:" + _minutes : "0:" + _minutes;
    } else if (!_hours && !_minutes) {
      return withLeadingZeroHours ? "00:00" : "0:00";
    }
    return withLeadingZeroHours ? "00:" + _minutes : "0:" + _minutes;
  }
  if (typeof num === "string" && num.indexOf(",") >= 0) {
    num = num.replace(",", ".");
  }
  const input = typeof num === "string" ? parseFloat(num) : num;
  if (!isDecimal(input)) {
    if (trimZeroMinutes) {
      return withLeadingZeroHours ? withLeadingZero(input) : String(Number(input));
    }
    return withLeadingZeroHours ? withLeadingZero(input) + ":00" : input + ":00";
  }
  const decimal = input.toFixed(2);
  const time = decimal.toString().split(".");
  let hours = time[0];
  if (withLeadingZeroHours) {
    hours = withLeadingZero(hours);
  }
  const minutes = time[1];
  const minutes_formatted = Math.round(parseInt(minutes, 10) / 100 * 60);
  if (trimZeroMinutes && minutes_formatted === 0) {
    return hours;
  }
  return hours + ":" + withLeadingZero(minutes_formatted);
};
export const withLeadingZero = function (num, size) {
  if (size === void 0) {
    size = 2;
  }
  let s = "" + num;
  while (s.length < size) s = "0" + s;
  return s;
};
export const isDecimal = num => {
  return !Number.isInteger(num);
};
export const isValidTime = time => {
  return time === undefined || /^([01]\d|2[0-3]):([0-5]\d)$/.test(time);
};
//# sourceMappingURL=timeUtils.js.map