"use strict";Object.defineProperty(exports, "__esModule", {value: true});

var _chunkV6U2YSQUcjs = require('./chunk-V6U2YSQU.cjs');


var _chunkGNRTDXICcjs = require('./chunk-GNRTDXIC.cjs');



var _chunkZ6EUSUADcjs = require('./chunk-Z6EUSUAD.cjs');

// src/hooks/useNumberInput/utils/formatValue.ts
var 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 ? `${_chunkGNRTDXICcjs.escapeRegExp.call(void 0, 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
  };
  const numberFormatter = intlConfig ? new Intl.NumberFormat(
    intlConfig.locale,
    intlConfig.currency ? _chunkZ6EUSUADcjs.__spreadProps.call(void 0, _chunkZ6EUSUADcjs.__spreadValues.call(void 0, {}, defaultNumberFormatOptions), {
      style: "currency",
      currency: intlConfig.currency
    }) : defaultNumberFormatOptions
  ) : new Intl.NumberFormat(void 0, defaultNumberFormatOptions);
  const parts = numberFormatter.formatToParts(Number(value));
  let formatted = replaceParts(parts, options);
  const intlSuffix = _chunkV6U2YSQUcjs.getSuffix.call(void 0, formatted, _chunkZ6EUSUADcjs.__spreadValues.call(void 0, {}, 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+)(${_chunkGNRTDXICcjs.escapeRegExp.call(void 0, 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("");
};
var replaceDecimalSeparator = (value, decimalSeparator, isNegative) => {
  let newValue = value;
  if (decimalSeparator && decimalSeparator !== ".") {
    newValue = newValue.replace(
      RegExp(_chunkGNRTDXICcjs.escapeRegExp.call(void 0, decimalSeparator), "g"),
      "."
    );
    if (isNegative && decimalSeparator === "-") {
      newValue = `-${newValue.slice(1)}`;
    }
  }
  return newValue;
};
var 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("");
};



exports.formatValue = formatValue;
