when-am-i / files / src / utils / calendarConflicts.ts
calendarConflicts.ts
3cbfe9ad 2026-06-22 Pavel Safronov | 1 | // Which calendars can actually appear together, for color assignment. |
| 2 | // | |
| 3 | // Two calendars only need visually distinct colors if they can ever show up as | |
| 4 | // simultaneous answers — either two eras in the same region overlapping in | |
| 5 | // time, or two eras in *different* regions whose areas overlap geographically | |
| 6 | // and whose time ranges overlap. Most calendars in the dataset are nowhere | |
| 7 | // near each other (Japan and Iran never compete for a color), so the actual | |
| 8 | // "must be distinct" graph is sparse — far sparser than "every calendar must | |
| 9 | // differ from every other calendar," which is what forced near-collisions | |
| 10 | // once the dataset grew past ~20 calendars sharing one hue wheel. | |
| 11 | ||
| 12 | import type { CalendarRegion, Era, Geometry } from '../types'; | |
| 13 | import { parseGregorianDate } from './julianDay'; | |
| 14 | import { MODERN_DEFAULT_START_YEAR } from './resolve'; | |
| 15 | ||
| 16 | interface BBox { | |
| 17 | minLng: number; | |
| 18 | minLat: number; | |
| 19 | maxLng: number; | |
| 20 | maxLat: number; | |
| 21 | } | |
| 22 | ||
| 23 | function expandBBox(box: BBox, lng: number, lat: number): void { | |
| 24 | if (lng < box.minLng) box.minLng = lng; | |
| 25 | if (lng > box.maxLng) box.maxLng = lng; | |
| 26 | if (lat < box.minLat) box.minLat = lat; | |
| 27 | if (lat > box.maxLat) box.maxLat = lat; | |
| 28 | } | |
| 29 | ||
| 30 | /** A conservative bounding box for a Polygon/MultiPolygon (over-approximates the shape). */ | |
| 31 | function geometryBBox(geo: Geometry): BBox { | |
| 32 | const box: BBox = { minLng: Infinity, minLat: Infinity, maxLng: -Infinity, maxLat: -Infinity }; | |
| 33 | const polygons = geo.type === 'Polygon' ? [geo.coordinates] : geo.coordinates; | |
| 34 | for (const rings of polygons) { | |
| 35 | for (const ring of rings) { | |
| 36 | for (const [lng, lat] of ring) expandBBox(box, lng, lat); | |
| 37 | } | |
| 38 | } | |
| 39 | return box; | |
| 40 | } | |
| 41 | ||
| 42 | function bboxesOverlap(a: BBox, b: BBox): boolean { | |
| 43 | return a.minLng <= b.maxLng && b.minLng <= a.maxLng && a.minLat <= b.maxLat && b.minLat <= a.maxLat; | |
| 44 | } | |
| 45 | ||
| 46 | /** [start, end) in astronomical years; null start/end means open-ended. */ | |
| 47 | interface TimeRange { | |
| 48 | start: number; | |
| 49 | end: number; | |
| 50 | } | |
| 51 | ||
| 52 | function eraTimeRange(era: Era): TimeRange { | |
| 53 | return { | |
| 54 | start: era.start ? parseGregorianDate(era.start).year : -Infinity, | |
| 55 | end: era.end ? parseGregorianDate(era.end).year : Infinity, | |
| 56 | }; | |
| 57 | } | |
| 58 | ||
| 59 | function rangesOverlap(a: TimeRange, b: TimeRange): boolean { | |
| 60 | return a.start < b.end && b.start < a.end; | |
| 61 | } | |
| 62 | ||
| 63 | /** | |
| 64 | * Build the conflict graph: a map from each (normalized) calendar name to the | |
| 65 | * set of other calendar names it can co-occur with, given the loaded region | |
| 66 | * data plus the modern global Gregorian default (which can pair with anything | |
| 67 | * still active from MODERN_DEFAULT_START_YEAR on, anywhere on Earth). | |
| 68 | */ | |
| 69 | export function buildConflictGraph(regions: CalendarRegion[]): Map<string, Set<string>> { | |
| 70 | const graph = new Map<string, Set<string>>(); | |
| 71 | const addEdge = (a: string, b: string) => { | |
| 72 | const ka = a.trim().toLowerCase(); | |
| 73 | const kb = b.trim().toLowerCase(); | |
| 74 | if (ka === kb) return; | |
| 75 | if (!graph.has(ka)) graph.set(ka, new Set()); | |
| 76 | if (!graph.has(kb)) graph.set(kb, new Set()); | |
| 77 | graph.get(ka)!.add(kb); | |
| 78 | graph.get(kb)!.add(ka); | |
| 79 | }; | |
| 80 | ||
| 81 | const entries = regions.map((cr) => ({ | |
| 82 | bbox: geometryBBox(cr.region.geo), | |
| 83 | eras: cr.eras.map((era) => ({ calendar: era.calendar, range: eraTimeRange(era) })), | |
| 84 | })); | |
| 85 | ||
| 86 | for (let i = 0; i < entries.length; i++) { | |
| 87 | // Eras within the same region: any two overlapping in time conflict, | |
| 88 | // regardless of geometry (they're the same place by definition). | |
| 89 | const { eras } = entries[i]; | |
| 90 | for (let a = 0; a < eras.length; a++) { | |
| 91 | for (let b = a + 1; b < eras.length; b++) { | |
| 92 | if (rangesOverlap(eras[a].range, eras[b].range)) { | |
| 93 | addEdge(eras[a].calendar, eras[b].calendar); | |
| 94 | } | |
| 95 | } | |
| 96 | } | |
| 97 | // Eras across different regions: only conflict if the regions' | |
| 98 | // bounding boxes overlap (conservative — may flag a few pairs whose | |
| 99 | // exact shapes don't actually touch, but never misses a real one). | |
| 100 | for (let j = i + 1; j < entries.length; j++) { | |
| 101 | if (!bboxesOverlap(entries[i].bbox, entries[j].bbox)) continue; | |
| 102 | for (const eraA of entries[i].eras) { | |
| 103 | for (const eraB of entries[j].eras) { | |
| 104 | if (rangesOverlap(eraA.range, eraB.range)) { | |
| 105 | addEdge(eraA.calendar, eraB.calendar); | |
| 106 | } | |
| 107 | } | |
| 108 | } | |
| 109 | } | |
| 110 | } | |
| 111 | ||
| 112 | // The modern global Gregorian default applies everywhere from | |
| 113 | // MODERN_DEFAULT_START_YEAR on, so it conflicts with anything else still | |
| 114 | // active in that window (mirrors resolve.ts's DEFAULT_MODERN_ERA). | |
| 115 | const modernRange: TimeRange = { start: MODERN_DEFAULT_START_YEAR, end: Infinity }; | |
| 116 | for (const { eras } of entries) { | |
| 117 | for (const era of eras) { | |
| 118 | if (era.calendar.trim().toLowerCase() === 'gregorian') continue; | |
| 119 | if (rangesOverlap(era.range, modernRange)) addEdge(era.calendar, 'Gregorian'); | |
| 120 | } | |
| 121 | } | |
| 122 | ||
| 123 | return graph; | |
| 124 | } |