"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.integerSanitizer = exports.floatSanitizer = void 0;
const integerRegexp = /[^\d]+/g;
const integerNegativeRegexp = /[^\d-]+/g;
const integerSymbols = new Set(['-']);
const floatRegexp = /[^\d.,]+/g;
const floatNegativeRegexp = /[^\d.,-]+/g;
const floatSymbols = new Set(['-', '.', ',']);
const integerSanitizer = exports.integerSanitizer = {
  onKeyDown: ({
    value,
    prevValue,
    negativeAllowed
  }) => {
    if (!value) {
      return false;
    }
    const regexp = negativeAllowed ? integerNegativeRegexp : integerRegexp;
    const nextValue = value.replace(regexp, '');
    return !(!nextValue || isInvalidPartialValue(value, prevValue, integerSymbols));
  },
  onChange: ({
    value,
    prevValue,
    negativeAllowed
  }) => {
    if (!value) {
      return value;
    }
    const regexp = negativeAllowed ? integerNegativeRegexp : integerRegexp;
    const nextValue = value.replace(regexp, '');
    const parsedValue = parseInt(nextValue);
    if (isNaN(parsedValue)) {
      return prevValue;
    }
    return parsedValue.toString();
  }
};
const floatSanitizer = exports.floatSanitizer = {
  onKeyDown: ({
    value,
    prevValue,
    negativeAllowed
  }) => {
    if (!value) {
      return false;
    }
    const regexp = negativeAllowed ? floatNegativeRegexp : floatRegexp;
    const nextValue = value.replace(regexp, '');
    return !(!nextValue || isInvalidPartialValue(value, prevValue, floatSymbols));
  },
  onChange: ({
    value,
    prevValue,
    negativeAllowed
  }) => {
    if (!value) {
      return value;
    }
    const regexp = negativeAllowed ? floatNegativeRegexp : floatRegexp;
    const nextValue = value.replace(regexp, '');
    const parsedValue = parseFloat(nextValue);
    if (isNaN(parsedValue)) {
      return prevValue;
    }
    return parsedValue.toString();
  }
};
function isInvalidPartialValue(value, prevValue, symbols) {
  return symbols.has(value) && prevValue.includes(value);
}