import { escapeRegExp } from "./escapeRegExp.js";
import { getSuffix } from "./getSuffix.js";
//#region src/hooks/useNumberInput/utils/formatValue.ts
/**
* Format value with decimal separator, group separator and prefix
*/
const formatValue = (options) => {
	const { value: _value, decimalSeparator, intlConfig, decimalScale, prefix = "", suffix = "" } = options;
	if (_value === "" || _value === void 0) return "";
	if (_value === "-") return "-";
	const isNegative = new RegExp(`^\\d?-${prefix ? `${escapeRegExp(prefix)}?` : ""}\\d`).test(_value);
	let value = decimalSeparator !== "." ? replaceDecimalSeparator(_value, decimalSeparator, isNegative) : _value;
	if (decimalSeparator && decimalSeparator !== "-" && value.startsWith(decimalSeparator)) value = "0" + value;
	const defaultNumberFormatOptions = {
		minimumFractionDigits: decimalScale || 0,
		maximumFractionDigits: 20
	};
	let formatted = replaceParts((intlConfig ? new Intl.NumberFormat(intlConfig.locale, intlConfig.currency ? {
		...defaultNumberFormatOptions,
		style: "currency",
		currency: intlConfig.currency
	} : defaultNumberFormatOptions) : new Intl.NumberFormat(void 0, defaultNumberFormatOptions)).formatToParts(Number(value)), options);
	const intlSuffix = getSuffix(formatted, { ...options });
	const includeDecimalSeparator = _value.slice(-1) === decimalSeparator ? decimalSeparator : "";
	const [, decimals] = value.match(RegExp("\\d+\\.(\\d+)")) || [];
	if (decimalScale === void 0 && decimals && decimalSeparator) if (formatted.includes(decimalSeparator)) formatted = formatted.replace(RegExp(`(\\d+)(${escapeRegExp(decimalSeparator)})(\\d+)`, "g"), `$1$2${decimals}`);
	else if (intlSuffix && !suffix) formatted = formatted.replace(intlSuffix, `${decimalSeparator}${decimals}${intlSuffix}`);
	else formatted = `${formatted}${decimalSeparator}${decimals}`;
	if (suffix && includeDecimalSeparator) return `${formatted}${includeDecimalSeparator}${suffix}`;
	if (intlSuffix && includeDecimalSeparator) return formatted.replace(intlSuffix, `${includeDecimalSeparator}${intlSuffix}`);
	if (intlSuffix && suffix) return formatted.replace(intlSuffix, `${includeDecimalSeparator}${suffix}`);
	return [
		formatted,
		includeDecimalSeparator,
		suffix
	].join("");
};
/**
* Before converting to Number, decimal separator has to be .
*/
const replaceDecimalSeparator = (value, decimalSeparator, isNegative) => {
	let newValue = value;
	if (decimalSeparator && decimalSeparator !== ".") {
		newValue = newValue.replace(RegExp(escapeRegExp(decimalSeparator), "g"), ".");
		if (isNegative && decimalSeparator === "-") newValue = `-${newValue.slice(1)}`;
	}
	return newValue;
};
const replaceParts = (parts, { prefix, groupSeparator, decimalSeparator, decimalScale, disableGroupSeparators = false }) => {
	return parts.reduce((prev, { type, value }, i) => {
		if (i === 0 && prefix) {
			if (type === "minusSign") return [value, prefix];
			if (type === "currency") return [...prev, prefix];
			return [prefix, value];
		}
		if (type === "currency") return prefix ? prev : [...prev, value];
		if (type === "group") return !disableGroupSeparators ? [...prev, groupSeparator !== void 0 ? groupSeparator : value] : prev;
		if (type === "decimal") {
			if (decimalScale !== void 0 && decimalScale === 0) return prev;
			return [...prev, decimalSeparator !== void 0 ? decimalSeparator : value];
		}
		if (type === "fraction") return [...prev, decimalScale !== void 0 ? value.slice(0, decimalScale) : value];
		return [...prev, value];
	}, [""]).join("");
};
//#endregion
export { formatValue };
