// Holder counts, and the one rule that decides whether a count is "low".
//
// The block explorer's holders_count is optional: it is absent on a token it
// has only just indexed, and it goes missing on a degraded or changed API.
// Absent means the count is unknown.  It does not mean the token has no
// holders, and collapsing the two hides a token the user really holds as if
// it were spam.  Every call site reads the count through here so the
// distinction cannot be lost again in one place while holding in the others.

const LOW_HOLDER_THRESHOLD = 1000;

// Parse an explorer-supplied holders_count into a number, or null when the
// explorer did not report one.  Anything unparseable is unknown too: a count
// we cannot read is not a count of zero.
function parseHoldersCount(raw) {
    if (raw === null || raw === undefined || raw === "") return null;
    const n = parseInt(raw, 10);
    return Number.isFinite(n) ? n : null;
}

// True only for a token the explorer reported as having fewer holders than
// the threshold.  An unknown count is never low: showing a spam token the
// user can see is unusual costs less than hiding an asset they own.
function isLowHolderCount(holders) {
    return holders != null && holders < LOW_HOLDER_THRESHOLD;
}

module.exports = {
    LOW_HOLDER_THRESHOLD,
    parseHoldersCount,
    isLowHolderCount,
};
