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

var _chunkIO33D4VHcjs = require('./chunk-IO33D4VH.cjs');


var _chunkWNB3MZJOcjs = require('./chunk-WNB3MZJO.cjs');


var _chunk4FRWKWXJcjs = require('./chunk-4FRWKWXJ.cjs');


var _chunkYEN2IQ6Zcjs = require('./chunk-YEN2IQ6Z.cjs');


var _chunkMEPSDTIKcjs = require('./chunk-MEPSDTIK.cjs');


var _chunkJ26VDYFVcjs = require('./chunk-J26VDYFV.cjs');


var _chunkQV4YFGGGcjs = require('./chunk-QV4YFGGG.cjs');


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



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

// src/hooks/useNumberInput/useNumberInput.tsx
var _react = require('react');
function useNumberInput({
  allowDecimals = true,
  allowNegativeValue = false,
  decimalsLimit,
  maxLength: userMaxLength,
  value: userValue,
  onChange,
  fixedDecimalLength,
  decimalScale,
  prefix,
  suffix,
  intlConfig,
  step,
  min,
  max,
  disableGroupSeparators = false,
  disableAbbreviations = false,
  decimalSeparator: _decimalSeparator,
  groupSeparator: _groupSeparator,
  transformRawValue,
  formatValueOnBlur = true,
  ref
}) {
  if (_decimalSeparator && _chunkIO33D4VHcjs.isNumber.call(void 0, _decimalSeparator)) {
    throw new Error("decimalSeparator cannot be a number");
  }
  if (_groupSeparator && _chunkIO33D4VHcjs.isNumber.call(void 0, _groupSeparator)) {
    throw new Error("groupSeparator cannot be a number");
  }
  const localeConfig = _react.useMemo.call(void 0, () => _chunkQV4YFGGGcjs.getLocaleConfig.call(void 0, intlConfig), [intlConfig]);
  const decimalSeparator = _decimalSeparator || localeConfig.decimalSeparator || "";
  const groupSeparator = _groupSeparator || localeConfig.groupSeparator || "";
  if (decimalSeparator && groupSeparator && decimalSeparator === groupSeparator && disableGroupSeparators === false) {
    throw new Error("decimalSeparator cannot be the same as groupSeparator");
  }
  const formatValueOptions = {
    decimalSeparator,
    groupSeparator,
    disableGroupSeparators,
    intlConfig,
    prefix: prefix || localeConfig.prefix,
    suffix
  };
  const cleanValueOptions = {
    decimalSeparator,
    groupSeparator,
    allowDecimals,
    decimalsLimit: decimalsLimit || fixedDecimalLength || 2,
    allowNegativeValue,
    disableAbbreviations,
    prefix: prefix || localeConfig.prefix,
    transformRawValue
  };
  const [stateValue, setStateValue] = _react.useState.call(void 0, 
    () => userValue != null ? _chunkJ26VDYFVcjs.formatValue.call(void 0, _chunkZ6EUSUADcjs.__spreadProps.call(void 0, _chunkZ6EUSUADcjs.__spreadValues.call(void 0, {}, formatValueOptions), {
      decimalScale,
      value: String(userValue)
    })) : ""
  );
  const [dirty, setDirty] = _react.useState.call(void 0, false);
  const [cursor, setCursor] = _react.useState.call(void 0, 0);
  const [changeCount, setChangeCount] = _react.useState.call(void 0, 0);
  const [lastKeyStroke, setLastKeyStroke] = _react.useState.call(void 0, null);
  const processChange = (value, selectionStart) => {
    setDirty(true);
    const { modifiedValue, cursorPosition } = _chunk4FRWKWXJcjs.repositionCursor.call(void 0, {
      selectionStart,
      value,
      lastKeyStroke,
      stateValue,
      groupSeparator
    });
    const stringValue = _chunkYEN2IQ6Zcjs.cleanValue.call(void 0, _chunkZ6EUSUADcjs.__spreadValues.call(void 0, {
      value: modifiedValue
    }, cleanValueOptions));
    if (userMaxLength && stringValue.replace(/-/g, "").length > userMaxLength) {
      return;
    }
    if (stringValue === "" || stringValue === "-" || stringValue === decimalSeparator) {
      onChange && onChange("", {
        float: null,
        formatted: "",
        value: ""
      });
      setStateValue(stringValue);
      setCursor(1);
      return;
    }
    const stringValueWithoutSeparator = decimalSeparator ? stringValue.replace(decimalSeparator, ".") : stringValue;
    const numberValue = parseFloat(stringValueWithoutSeparator);
    const formattedValue = _chunkJ26VDYFVcjs.formatValue.call(void 0, _chunkZ6EUSUADcjs.__spreadValues.call(void 0, {
      value: stringValue
    }, formatValueOptions));
    if (cursorPosition != null) {
      let newCursor = cursorPosition + (formattedValue.length - value.length);
      newCursor = newCursor <= 0 ? prefix ? prefix.length : 0 : newCursor;
      setCursor(newCursor);
      setChangeCount(changeCount + 1);
    }
    setStateValue(formattedValue);
    if (onChange) {
      const values = {
        float: numberValue,
        formatted: formattedValue,
        value: stringValue
      };
      onChange(stringValue, values);
    }
  };
  const handleOnChange = (event) => {
    const {
      target: { value, selectionStart }
    } = event;
    processChange(value, selectionStart);
  };
  const handleOnFocus = (event) => {
    return stateValue ? stateValue.length : 0;
  };
  const handleOnBlur = (event) => {
    const {
      target: { value }
    } = event;
    const valueOnly = _chunkYEN2IQ6Zcjs.cleanValue.call(void 0, _chunkZ6EUSUADcjs.__spreadValues.call(void 0, { value }, cleanValueOptions));
    if (valueOnly === "-" || valueOnly === decimalSeparator || !valueOnly) {
      setStateValue("");
      return;
    }
    const fixedDecimals = _chunkMEPSDTIKcjs.fixedDecimalValue.call(void 0, 
      valueOnly,
      decimalSeparator,
      fixedDecimalLength
    );
    const newValue = _chunkWNB3MZJOcjs.padTrimValue.call(void 0, 
      fixedDecimals,
      decimalSeparator,
      decimalScale !== void 0 ? decimalScale : fixedDecimalLength
    );
    const numberValue = parseFloat(newValue.replace(decimalSeparator, "."));
    const formattedValue = _chunkJ26VDYFVcjs.formatValue.call(void 0, _chunkZ6EUSUADcjs.__spreadProps.call(void 0, _chunkZ6EUSUADcjs.__spreadValues.call(void 0, {}, formatValueOptions), {
      value: newValue
    }));
    if (onChange && formatValueOnBlur) {
      onChange(newValue, {
        float: numberValue,
        formatted: formattedValue,
        value: newValue
      });
    }
    setStateValue(formattedValue);
  };
  const handleOnKeyDown = (event) => {
    const { key } = event;
    setLastKeyStroke(key);
    if (step && (key === "ArrowUp" || key === "ArrowDown")) {
      event.preventDefault();
      setCursor(stateValue.length);
      const currentValue = parseFloat(
        userValue != null ? String(userValue).replace(decimalSeparator, ".") : _chunkYEN2IQ6Zcjs.cleanValue.call(void 0, _chunkZ6EUSUADcjs.__spreadValues.call(void 0, { value: stateValue }, cleanValueOptions))
      ) || 0;
      const newValue = key === "ArrowUp" ? currentValue + step : currentValue - step;
      if (min !== void 0 && newValue < Number(min)) {
        return;
      }
      if (max !== void 0 && newValue > Number(max)) {
        return;
      }
      const fixedLength = String(step).includes(".") ? Number(String(step).split(".")[1].length) : void 0;
      processChange(
        String(fixedLength ? newValue.toFixed(fixedLength) : newValue).replace(
          ".",
          decimalSeparator
        )
      );
    }
  };
  const handleOnKeyUp = (event) => {
    const {
      key,
      currentTarget: { selectionStart }
    } = event;
    if (key !== "ArrowUp" && key !== "ArrowDown" && stateValue !== "-") {
      const suffix2 = _chunkV6U2YSQUcjs.getSuffix.call(void 0, stateValue, {
        groupSeparator,
        decimalSeparator
      });
      if (suffix2 && selectionStart && selectionStart > stateValue.length - suffix2.length) {
        if (ref && ref.current) {
          const newCursor = stateValue.length - suffix2.length;
          ref.current.setSelectionRange(newCursor, newCursor);
        }
      }
    }
  };
  _react.useEffect.call(void 0, () => {
    if (userValue == null) {
      setStateValue("");
    }
  }, [userValue]);
  _react.useEffect.call(void 0, () => {
    if (dirty && stateValue !== "-" && ref && ref.current && document.activeElement === ref.current) {
      ref.current.setSelectionRange(cursor, cursor);
    }
  }, [stateValue, cursor, ref, dirty, changeCount]);
  const getRenderValue = () => {
    if (userValue != null && stateValue !== "-" && (!decimalSeparator || stateValue !== decimalSeparator)) {
      return _chunkJ26VDYFVcjs.formatValue.call(void 0, _chunkZ6EUSUADcjs.__spreadProps.call(void 0, _chunkZ6EUSUADcjs.__spreadValues.call(void 0, {}, formatValueOptions), {
        decimalScale: dirty ? void 0 : decimalScale,
        value: String(userValue)
      }));
    }
    return stateValue;
  };
  return {
    getRenderValue,
    handleOnChange,
    handleOnBlur,
    handleOnFocus,
    handleOnKeyDown,
    handleOnKeyUp
  };
}



exports.useNumberInput = useNumberInput;
