import isNaN from "lodash.isnan";
import isNil from "lodash.isnil";
import { ACTION_KEYS, NUMBER_KEYS } from "../../utils/constants/keys.constant";
import { formatLocaleNumber, getDecimalSeparator, getNegativeSymbol, getThousandSeparator } from "../../utils/number";
function checkIfValueIsStringAndDoesNotHaveGermanFormat(val) {
    return typeof val === 'string' && getThousandSeparator() !== '.';
}
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) {
    let val = value;
    if (val === '' ||
        val === undefined ||
        val === null ||
        isNaN(val) ||
        val === getNegativeSymbol() ||
        val === getDecimalSeparator() ||
        !isNumber(val)) {
        return undefined;
    }
    if (checkIfValueIsStringAndDoesNotHaveGermanFormat(val)) {
        val = val.split(getThousandSeparator()).join('').split('`').join('').split("'").join('');
    }
    if (typeof val === 'string') {
        val = val.replace(getDecimalSeparator(), '.');
    }
    return decimalPoints === 0 ? parseInt(val, 10) : parseFloat(val);
}
export function toFixedNumber(value, decimalPoints = 0) {
    let val = value;
    if (isNil(val)) {
        return '';
    }
    if (checkIfValueIsStringAndDoesNotHaveGermanFormat(val)) {
        val = val.split(getThousandSeparator()).join('').split('`').join('').split("'").join('');
    }
    if (val.charAt(0) === getDecimalSeparator()) {
        val = `0${val}`;
    }
    const num = decimalPoints === 0 ? parseInt(val, 10) : parseFloat(val.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, ctrlKey, metaKey, selectionStart, selectionEnd, newValue, decimal, onlyPositive, }) {
    //
    // allow select all, copy and paste
    if (['a', 'c', 'v'].includes(key) && (ctrlKey || metaKey)) {
        return true;
    }
    //
    // only allow negative symbols at the start of the input
    if (key === getNegativeSymbol()) {
        if (onlyPositive) {
            return false;
        }
        else {
            if (selectionStart && selectionStart > 0 && selectionEnd && selectionEnd > 0) {
                return false;
            }
        }
    }
    //
    // only allow decimal separator
    if (key === getDecimalSeparator()) {
        if (decimal === 0) {
            return false;
        }
        else if (countDecimalSeparators(newValue) > 1) {
            return false;
        }
    }
    //
    // check if it is an allowed key
    if (![...NUMBER_KEYS, ...ACTION_KEYS, getDecimalSeparator(), getNegativeSymbol()].includes(key)) {
        return false;
    }
    //
    // check if decimal points are reached
    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;
}
