// The Lucia project.
// Copyright 2023-2026 DSP, inc.  All rights reserved.
// Authors:
//   chiayin.kuo@dsp.im (chiayin), 2023/1/31
//   imacat.yang@dsp.im (imacat), 2023/9/23
//   cindy.chang@dsp.im (Cindy Chang), 2024/5/30
/** @module numberLabel Number formatting with comma separators. */

/**
 * Formats an integer string by inserting commas every three digits.
 * @param {string} numberStr - A string of digits to format.
 * @returns {string} The formatted string with commas (e.g. "1,000,000").
 */
const formatNumberWithCommas = (numberStr) => {
  let reversedStr = numberStr.split("").reverse().join("");
  let groupedStr = reversedStr.match(/.{1,3}/g) || [];
  let joinedStr = groupedStr.join(",");
  let finalStr = joinedStr.split("").reverse().join("");

  return finalStr;
};

/**
 * Converts a number to a string with comma-separated thousands.
 *
 * Handles decimal numbers by formatting only the integer part.
 *
 * @param {number} num - The number to format.
 * @returns {string} The formatted number string (e.g. "1,234.56").
 */
export default function numberLabel(num) {
  let parts = num.toString().split(".");
  parts[0] = formatNumberWithCommas(parts[0]);
  return parts.join(".");
}
