import ngeohash from 'ngeohash'; // library that does the geohasing encoding/decoding yippee 

// encodes the latitude/longitude pair to a 9 character string geohash
// (~5m precision) - this matches the precision addMessage() now stores on
// every message (see messages.js), which is the maximum precision this app
// ever needs, so getQueryPrefix() below always has room to use any shorter
// prefix without breaking the range query.
export function encode(lat, lng) {
    return ngeohash.encode(lat, lng, 9);
}

// encodes a lat/lng pair to a string geohash used as a Firestore query
// prefix - the "nearby" area shrinks as this precision increases. Safe to
// tune anywhere from 1-9: a shorter prefix can always match a longer stored
// geohash via the >= / < range query in getNearbyMessages, just not the
// other way around (a prefix LONGER than the stored geohash's 9 characters
// would never match anything - same failure mode as before).
export function getQueryPrefix(lat, lng) {
    return ngeohash.encode(lat, lng, 7);
}

// --- geohash cell-boundary problem ----------------------------------------
// A single precision-6 geohash cell is only ~1.2km x 0.6km. Two devices
// standing in the same real-world spot can still land in DIFFERENT cells if
// their reported coordinates fall on opposite sides of a cell edge - this is
// very common, since desktop browsers usually resolve location via WiFi/IP
// (often off by hundreds of meters) while phones use GPS (much tighter).
// Querying only getQueryPrefix(lat, lng)'s single cell means one device's
// query can miss messages that are physically just a few meters away, on the
// other side of that edge - which is exactly the "different pin counts on
// different devices" symptom.
//
// getNearbyGeohashCells returns the user's cell PLUS its 8 surrounding cells
// (a 3x3 grid, via ngeohash.neighbors). Querying all 9 means that as long as
// both devices' reported positions are within roughly one cell-width of each
// other (which covers normal GPS/WiFi error), their 3x3 grids overlap enough
// to cover the same messages - so both devices see the same "nearby" set
// even if they're each centered in a different cell.
//
// IMPORTANT for callers (messages.js): each cell returned here is used as a
// Firestore "starts with" prefix range, i.e.
//   where('geohash', '>=', cell), where('geohash', '<', cell + '\uF8FF')
// '\uF8FF' (not 'z') must be the upper-bound suffix - 'z' is itself a valid
// geohash character (the highest one), so a stored geohash whose very next
// character is also 'z' (e.g. cell "xn774bg" + stored "xn774bgzf") would be
// >= cell + 'z' and get wrongly excluded. '\uF8FF' is far above any geohash
// character, so cell + '\uF8FF' always matches every geohash starting with cell.
export function getNearbyGeohashCells(lat, lng) {
    const center = getQueryPrefix(lat, lng);
    return [center, ...ngeohash.neighbors(center)];
}

// --- "passport stamps" region key ------------------------------------------
// encodes a lat/lng pair to a 4 character geohash prefix (~20-40km,
// roughly city-sized). Used by stamps.js to group messages into distinct
// "regions" a user has posted from - much coarser than getQueryPrefix's
// "nearby" cells above, which is the point: someone shouldn't earn a new
// stamp just for moving a few blocks, only for posting from a genuinely
// different area.
export function getRegionPrefix(lat, lng) {
    return ngeohash.encode(lat, lng, 4);
}