import { coLocatedGroups } from "./cluster-motion.js"; const STACK_MIN_STEP_PX = 8; const STACK_STEP_SIZE_RATIO = 0.25; const STACK_MAX_SPREAD_PX = 24; export function markerPresentationId(marker) { const data = marker?.userData; // Selection also includes GeoJSON satellites and cable lines. Only markers // owned by an interactable layer participate in surface stack focus. if (!data?.icon_layer_id) return null; const identity = Object.hasOwn(data, "icon_item_id") ? data.icon_item_id ?? marker.uuid : data.id ?? data.source_id ?? data.collector ?? marker.uuid; return `${data.icon_layer_id}:${identity}`; } // Only real, near-identical geographic anchors share a stack. City buckets // and screen-space overlap must never pull different locations into one group. export function markerStackGroups(markers) { return coLocatedGroups(markers.map(marker => ({ marker, id: markerPresentationId(marker), lat: Number(marker.userData.displayLatitude ?? marker.userData.latitude), lon: Number(marker.userData.displayLongitude ?? marker.userData.longitude), }))).map(group => group.map(entry => entry.marker)); } export function markerStackLayout(entries) { const ordered = [...entries].sort((a, b) => a.id.localeCompare(b.id)); const focused = ordered.findIndex(entry => entry.selected); if (focused >= 0) ordered.push(...ordered.splice(focused, 1)); // Expose an edge for small stacks while bounding the whole stack, even if // many records share one coordinate. The front icon stays on its own anchor. const step = Math.min( Math.max(STACK_MIN_STEP_PX, ...ordered.map(entry => entry.size * STACK_STEP_SIZE_RATIO)), STACK_MAX_SPREAD_PX / Math.max(1, ordered.length - 1), ); return new Map(ordered.map((entry, index) => [entry.id, { x: (index - ordered.length + 1) * step, y: (index - ordered.length + 1) * step, zIndex: entry.selected ? 1000 : 10 + index, }])); }