import { escapeRegExp } from "./escapeRegExp.js";
//#region src/hooks/useNumberInput/utils/parseAbbrValue.ts
/**
* Abbreviate number eg. 1000 = 1k
*
* Source: https://stackoverflow.com/a/9345181
*/
const abbrValue = (value, decimalSeparator = ".", _decimalPlaces = 10) => {
	if (value > 999) {
		let valueLength = ("" + value).length;
		const p = Math.pow;
		const d = p(10, _decimalPlaces);
		valueLength -= valueLength % 3;
		return (Math.round(value * d / p(10, valueLength)) / d + " kMGTPE"[valueLength / 3]).replace(".", decimalSeparator);
	}
	return String(value);
};
const abbrMap = {
	k: 1e3,
	m: 1e6,
	b: 1e9
};
/**
* Parse a value with abbreviation e.g 1k = 1000
*/
const parseAbbrValue = (value, decimalSeparator = ".") => {
	const reg = new RegExp(`(\\d+(${escapeRegExp(decimalSeparator)}\\d*)?)([kmb])$`, "i");
	const match = value.match(reg);
	if (match) {
		const [, digits, , abbr] = match;
		const multiplier = abbrMap[abbr.toLowerCase()];
		return Number(digits.replace(decimalSeparator, ".")) * multiplier;
	}
};
//#endregion
export { abbrValue, parseAbbrValue };
