import { __assign } from "tslib";
import Decimal from 'big.js';
import { trimLeadingPlus } from './utils';
export var EMPTY_DECIMAL = new Decimal(0);
export function isPotentialValue(value) {
    return value === '' || value === '.' || value === '-' || value === '+';
}
export function isDecimal(value) {
    return /^[-+]?\d*\.?\d*$/.test(value);
}
export function getDelta(decimal, step) {
    if (Number.isFinite(step)) {
        return new Decimal(step);
    }
    return new Decimal(1).div(Math.pow(10, decimal));
}
function fromPotential(v) {
    v = String(v);
    if (isDecimal(v)) {
        return new Decimal(trimLeadingPlus(v));
    }
    return null;
}
export function normalizeMinMax(props) {
    var min = fromPotential(props.min);
    var max = fromPotential(props.max);
    return {
        min: min,
        max: max,
    };
}
export function normalizeValue(value, min, max, decimalPlaces, showTooltip) {
    var pop;
    if (value === undefined || value === null) {
        return {
            input: '',
            value: EMPTY_DECIMAL,
        };
    }
    value = String(value);
    if (isPotentialValue(value) || !isDecimal(value)) {
        return {
            input: '',
            value: EMPTY_DECIMAL,
        };
    }
    if (value === '' || isPotentialValue(value)) {
        return {
            input: value,
            value: EMPTY_DECIMAL,
        };
    }
    var decimal = new Decimal(trimLeadingPlus(value));
    if (min !== null) {
        if (min.cmp(decimal) === 1) {
            decimal = min;
            showTooltip &&
                (pop = {
                    visible: true,
                    text: String(min),
                    type: 'min',
                });
        }
    }
    if (max !== null) {
        if (max.cmp(decimal) === -1) {
            decimal = max;
            showTooltip &&
                (pop = {
                    visible: true,
                    text: String(max),
                    type: 'max',
                });
        }
    }
    var popState = pop && showTooltip ? { pop: pop } : {};
    return __assign({ input: decimal.toFixed(decimalPlaces), value: decimal }, popState);
}
export function calculateLimit(value, min, max) {
    var canDec = true;
    var canInc = true;
    if (min !== null) {
        canDec = min.cmp(value) === -1;
    }
    if (max !== null) {
        canInc = max.cmp(value) === 1;
    }
    return {
        canDec: canDec,
        canInc: canInc,
    };
}
