import isNil from "lodash.isnil";
import { ACTION_KEYS, NUMBER_KEYS } from "../../utils/constants/keys.constant";
import { formatLocaleNumber, getDecimalSeparator, getNegativeSymbol } from "../../utils/number";
import isNaN from "lodash.isnan";
export function isNumber(value) {
  const num = parseFloat(value);
  return typeof num === 'number' && !isNaN(num);
}
export function isNotNumber(value) {
  return !isNumber(value) && value !== '' && value !== getNegativeSymbol() && value !== getDecimalSeparator();
}
export function toNumber(value, decimalPoints = 0) {
  if (value === '' ||
    value === undefined ||
    value === null ||
    isNaN(value) ||
    value === getNegativeSymbol() ||
    value === getDecimalSeparator() ||
    !isNumber(value)) {
    return undefined;
  }
  return decimalPoints === 0 ? parseInt(value, 10) : parseFloat(value);
}
export function toFixedNumber(value, decimalPoints = 0) {
  if (isNil(value)) {
    return '';
  }
  if (value.charAt(0) === getDecimalSeparator()) {
    value = `0${value}`;
  }
  const num = decimalPoints === 0 ? parseInt(value, 10) : parseFloat(value.replace(getDecimalSeparator(), '.'));
  return isNaN(num) ? '' : num.toFixed(decimalPoints);
}
export function mapDecimalSeparator(value) {
  return value.replace('.', getDecimalSeparator());
}
export function toUserFormattedNumber(value, decimalPoints = 0, suffix = '') {
  if (isNil(value)) {
    return '';
  }
  if (value.charAt(0) === getDecimalSeparator()) {
    value = `0${value}`;
  }
  const num = decimalPoints === 0 ? parseInt(value, 10) : parseFloat(value);
  const formattedSuffix = suffix !== '' ? ` ${suffix.trim()}` : '';
  return isNaN(num) ? '' : formatLocaleNumber(num, decimalPoints) + formattedSuffix;
}
export const countDecimalSeparators = (value) => (value.split(getDecimalSeparator()) || []).length - 1 || 0;
export function validateKeyDown({ key, selectionStart, selectionEnd, newValue, decimal, }) {
  if (key === getNegativeSymbol() && selectionStart && selectionStart > 0 && selectionEnd && selectionEnd > 0) {
    return false;
  }
  if (key === getDecimalSeparator()) {
    if (decimal === 0) {
      return false;
    }
    else if (countDecimalSeparators(newValue) > 1) {
      return false;
    }
  }
  if (![...NUMBER_KEYS, ...ACTION_KEYS, getDecimalSeparator(), getNegativeSymbol()].includes(key)) {
    return false;
  }
  if (decimal !== 0 && newValue.includes(getDecimalSeparator()) && [...NUMBER_KEYS].includes(key)) {
    const newValueParts = newValue.split(getDecimalSeparator());
    const decimalPart = newValueParts[newValueParts.length - 1];
    if (decimalPart.length > decimal) {
      return false;
    }
  }
  return true;
}
