import { escapeRegExp } from "./escapeRegExp.js";
import { parseAbbrValue } from "./parseAbbrValue.js";
import { removeInvalidChars } from "./removeInvalidChars.js";
import { removeSeparators } from "./removeSeparators.js";
//#region src/hooks/useNumberInput/utils/cleanValue.ts
/**
* Remove prefix, separators and extra decimals from value
*/
const cleanValue = ({ value, groupSeparator = ",", decimalSeparator = ".", allowDecimals = true, decimalsLimit = 2, allowNegativeValue = true, disableAbbreviations = false, prefix = "", transformRawValue = (rawValue) => rawValue }) => {
	const transformedValue = transformRawValue(value);
	if (transformedValue === "-") return transformedValue;
	const abbreviations = disableAbbreviations ? [] : [
		"k",
		"m",
		"b"
	];
	const isNegative = new RegExp(`((^|\\D)-\\d)|(-${escapeRegExp(prefix)})`).test(transformedValue);
	const [prefixWithValue, preValue] = RegExp(`(\\d+)-?${escapeRegExp(prefix)}`).exec(value) || [];
	const withoutInvalidChars = removeInvalidChars(removeSeparators(prefix ? prefixWithValue ? transformedValue.replace(prefixWithValue, "").concat(preValue) : transformedValue.replace(prefix, "") : transformedValue, groupSeparator), [
		groupSeparator,
		decimalSeparator,
		...abbreviations
	]);
	let valueOnly = withoutInvalidChars;
	if (!disableAbbreviations) {
		if (abbreviations.some((letter) => letter === withoutInvalidChars.toLowerCase().replace(decimalSeparator, ""))) return "";
		const parsed = parseAbbrValue(withoutInvalidChars, decimalSeparator);
		if (parsed) valueOnly = String(parsed);
	}
	const includeNegative = isNegative && allowNegativeValue ? "-" : "";
	if (decimalSeparator && valueOnly.includes(decimalSeparator)) {
		const [int, decimals] = withoutInvalidChars.split(decimalSeparator);
		const trimmedDecimals = decimalsLimit && decimals ? decimals.slice(0, decimalsLimit) : decimals;
		return `${includeNegative}${int}${allowDecimals ? `${decimalSeparator}${trimmedDecimals}` : ""}`;
	}
	return `${includeNegative}${valueOnly}`;
};
//#endregion
export { cleanValue };
