// Parsing for the dust threshold field in Settings.
//
// Pure: no DOM, no state, so the accepted set can be unit tested directly
// instead of through the settings view.
//
// Accepted input is plain decimal digits only, meaning a whole number of
// gwei, zero or greater. Zero is a real setting: it hides nothing.
//
// Deliberately rejected, not coerced:
//   ""          nothing to save
//   "-1"        a negative threshold has no meaning
//   "1.5"       fractional gwei is not a threshold the filter can use
//   "100 gwei"  the unit is already printed beside the field
//   "0x10"      hex, which Number() would silently read as 16
//   "1e3"       exponent notation, which Number() would silently read as 1000
//
// The last two are the reason this is a digit test and not a Number() test.
// Number() accepts both, and accepting them would put a number in the field
// that the user did not type — the same silent substitution the visible
// rejection message exists to end.

// Must render on ONE line of #flash-msg, whose reserved height
// (min-h-[1.25rem]) is exactly one line at text-xs. A string long enough to
// wrap to two lines pushes the settings view down, which the No Layout Shift
// policy forbids. Do not lengthen this without re-running the layout test in
// tests/e2e/run.js, which measures the flash line and goes red on a shift.
const DUST_THRESHOLD_MESSAGE =
    "Please enter a whole number of gwei, zero or greater.";

// Returns the threshold in gwei, or null if the input is not one.
function parseDustThresholdGwei(raw) {
    if (typeof raw !== "string") return null;
    const trimmed = raw.trim();
    if (!/^[0-9]+$/.test(trimmed)) return null;
    const val = Number(trimmed);
    // A run of digits long enough to exceed Number's exact integer range
    // would round on the way in, so it is not a threshold we can store.
    if (!Number.isSafeInteger(val)) return null;
    return val;
}

module.exports = { DUST_THRESHOLD_MESSAGE, parseDustThresholdGwei };
