/**
 * Number formatting helpers used by `<NumberField.Root>` and `<NumberField.Input>`.
 *
 * Mirrors `packages/react/src/utils/formatNumber.ts`. A module-scope cache
 * keyed by `(locale, options)` keeps repeated formats cheap — constructing
 * `Intl.NumberFormat` dominates the cost.
 */

const FORMATTER_CACHE = new Map();
export function getFormatter(locale, options) {
  const key = JSON.stringify({
    locale,
    options
  });
  const cached = FORMATTER_CACHE.get(key);
  if (cached) return cached;
  const formatter = new Intl.NumberFormat(locale, options);
  FORMATTER_CACHE.set(key, formatter);
  return formatter;
}

/**
 * Formats `value` with `Intl.NumberFormat(locale, format)`. Returns the
 * empty string when `value` is `null` or non-finite so the visible `<input>`
 * can clear cleanly.
 */
export function formatNumber(value, locale, format) {
  if (value == null || Number.isNaN(value)) return "";
  return getFormatter(locale, format).format(value);
}

/**
 * Like {@link formatNumber} but forces maximum precision (20 fraction
 * digits) when the caller didn't pin an explicit precision. Used for
 * controlled-input display where the consumer's `value` prop must round-trip
 * through the formatter without losing digits.
 */
export function formatNumberMaxPrecision(value, locale, format) {
  if (value == null || Number.isNaN(value)) return "";
  const nextFormat = {
    ...format,
    maximumFractionDigits: 20
  };
  return getFormatter(locale, nextFormat).format(value);
}
//# sourceMappingURL=format-number.jsx.map
