@tempo-kit/plugin-spatial 0.0.0-stage → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,620 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
18
+
19
+ // auto-inject-version:tempo-kit/plugin/sdk
20
+ var sdk_exports = {};
21
+ __export(sdk_exports, {
22
+ definePlugin: () => definePlugin,
23
+ defineTerm: () => defineTerm
24
+ });
25
+ __reExport(sdk_exports, sdk_star);
26
+ import * as sdk_star from "tempo-kit/plugin/sdk";
27
+ import { definePlugin as originalPlugin, defineTerm as originalTerm } from "tempo-kit/plugin/sdk";
28
+ var definePlugin = (config) => originalPlugin({ ...config, version: "2.0.0" });
29
+ var defineTerm = (config) => originalTerm({ ...config, version: "2.0.0" });
30
+
31
+ // ../../functions/dist/support/assert.js
32
+ var isNumber = (val) => typeof val === "number" && Number.isFinite(val);
33
+ var isString = (val) => typeof val === "string";
34
+ var isText = (val) => isString(val) && val.trim().length > 0;
35
+ var isBoolean = (val) => val === true || val === false;
36
+ var isFunction = (val) => typeof val === "function";
37
+ var isNullish = (val) => val === null || val === void 0;
38
+ var isNotNullish = (val) => !isNullish(val);
39
+ var isUndefined = (val) => val === void 0;
40
+ var isDate = (val) => val instanceof Date && isNumber(val.getTime());
41
+ var isPrimitive = (val) => val === null || typeof val !== "object" && !isFunction(val);
42
+ var isObject = (val) => isNotNullish(val) && typeof val === "object" && !Array.isArray(val);
43
+ var isReference = (val) => !isPrimitive(val);
44
+
45
+ // ../../functions/dist/support/coordinates.js
46
+ var parseCoordNumber = (val) => {
47
+ if (isNullish(val) || isBoolean(val))
48
+ return NaN;
49
+ if (isString(val)) {
50
+ const trimmed = val.trim();
51
+ return trimmed.length > 0 ? Number(trimmed) : NaN;
52
+ }
53
+ return isNumber(val) ? val : NaN;
54
+ };
55
+ var normalizeLat = (lat, round = false) => {
56
+ const n = parseCoordNumber(lat);
57
+ if (!isNumber(n) || n < -90 || n > 90)
58
+ return void 0;
59
+ return round ? Math.round(n * 1e3) / 1e3 : n;
60
+ };
61
+ var normalizeLng = (lng, round = false) => {
62
+ const n = parseCoordNumber(lng);
63
+ if (!isNumber(n) || n < -180 || n > 180)
64
+ return void 0;
65
+ return round ? Math.round(n * 1e3) / 1e3 : n;
66
+ };
67
+ var normalizeCoords = (lat, lng, round = false) => {
68
+ const nLat = normalizeLat(lat, round);
69
+ const nLng = normalizeLng(lng, round);
70
+ return isNotNullish(nLat) && isNotNullish(nLng) ? { lat: nLat, lng: nLng } : void 0;
71
+ };
72
+ function extractRawCoords(input, lngFallback) {
73
+ if (isNullish(input))
74
+ return void 0;
75
+ if (isNumber(input) && isNumber(lngFallback)) {
76
+ const coords = normalizeCoords(input, lngFallback, false);
77
+ return coords ? { ...coords, elevation: 0 } : void 0;
78
+ }
79
+ if (isString(input) && input.includes(",")) {
80
+ const segments = input.split(",").map((s) => s.trim());
81
+ if (segments.length >= 2) {
82
+ const coords = normalizeCoords(segments[0], segments[1], false);
83
+ const elev = segments.length >= 3 ? parseCoordNumber(segments[2]) : void 0;
84
+ return coords ? { ...coords, ...isNumber(elev) ? { elevation: elev } : {} } : void 0;
85
+ }
86
+ return void 0;
87
+ }
88
+ if (Array.isArray(input) && input.length >= 2) {
89
+ const coords = normalizeCoords(input[0], input[1], false);
90
+ const elev = input.length >= 3 ? parseCoordNumber(input[2]) : void 0;
91
+ return coords ? { ...coords, ...isNumber(elev) ? { elevation: elev } : {} } : void 0;
92
+ }
93
+ if (isReference(input) || isObject(input)) {
94
+ const geo = input.geo ?? input.config?.geo ?? input;
95
+ const cfg = input.config?.geo ?? input.config;
96
+ const findFirstNumber = (...candidates) => {
97
+ for (const c of candidates) {
98
+ const n = parseCoordNumber(c);
99
+ if (isNumber(n))
100
+ return n;
101
+ }
102
+ return void 0;
103
+ };
104
+ const lat = findFirstNumber(geo?.latitude, geo?.lat, input.latitude, input.lat, cfg?.latitude, cfg?.lat);
105
+ const lng = findFirstNumber(geo?.longitude, geo?.lng, geo?.lon, geo?.long, input.longitude, input.lng, input.lon, input.long, cfg?.longitude, cfg?.lng, cfg?.lon, cfg?.long);
106
+ const elev = findFirstNumber(geo?.elevation, input.elevation, cfg?.elevation);
107
+ const coords = normalizeCoords(lat, lng, false);
108
+ return coords ? { ...coords, ...isNumber(elev) ? { elevation: elev } : {} } : void 0;
109
+ }
110
+ return void 0;
111
+ }
112
+
113
+ // ../../functions/dist/support/temporal.js
114
+ function extractEpochMs(input) {
115
+ if (isNullish(input))
116
+ return void 0;
117
+ if (isNumber(input))
118
+ return input;
119
+ if (isDate(input))
120
+ return input.getTime();
121
+ if (isObject(input)) {
122
+ if (isNumber(input.epoch?.ms))
123
+ return input.epoch.ms;
124
+ if (isNumber(input.epochMilliseconds))
125
+ return input.epochMilliseconds;
126
+ if (isNumber(input.timestamp))
127
+ return input.timestamp;
128
+ if (isDate(input.date))
129
+ return input.date.getTime();
130
+ if (isFunction(input.toInstant)) {
131
+ try {
132
+ const t = input.toInstant().epochMilliseconds;
133
+ if (isNumber(t))
134
+ return t;
135
+ } catch {
136
+ }
137
+ }
138
+ if (isFunction(input.getTime)) {
139
+ try {
140
+ const t = input.getTime();
141
+ if (isNumber(t))
142
+ return t;
143
+ } catch {
144
+ }
145
+ }
146
+ }
147
+ if (isString(input)) {
148
+ const parsed = Date.parse(input.trim());
149
+ if (isNumber(parsed))
150
+ return parsed;
151
+ }
152
+ return void 0;
153
+ }
154
+
155
+ // ../../functions/dist/spatial/support.js
156
+ function coerceGeo(input) {
157
+ const raw = extractRawCoords(input);
158
+ if (isUndefined(raw))
159
+ return void 0;
160
+ const coords = normalizeCoords(raw.lat, raw.lng, true);
161
+ if (isUndefined(coords))
162
+ return void 0;
163
+ const { lat, lng } = coords;
164
+ const sphere = lat > 1e-3 ? "north" : lat < -1e-3 ? "south" : "equator";
165
+ const geoObj = isReference(input) ? input.geo ?? input.config?.geo ?? input : {};
166
+ return {
167
+ latitude: lat,
168
+ longitude: lng,
169
+ sphere: geoObj?.sphere ?? sphere,
170
+ ...isString(geoObj?.country) ? { country: geoObj.country } : {},
171
+ ...isString(geoObj?.city) ? { city: geoObj.city } : {},
172
+ ...isNumber(geoObj?.elevation) ? { elevation: Math.round(geoObj.elevation * 1e3) / 1e3 } : {},
173
+ ...isString(geoObj?.timezone) ? { timezone: geoObj.timezone } : {}
174
+ };
175
+ }
176
+ function toRadianCoordinates(from, to) {
177
+ const c1 = extractRawCoords(from);
178
+ const c2 = extractRawCoords(to);
179
+ if (!c1 || !c2 || !isNumber(c1.lat) || !isNumber(c1.lng) || !isNumber(c2.lat) || !isNumber(c2.lng))
180
+ return void 0;
181
+ const toRad = Math.PI / 180;
182
+ const lat1 = c1.lat * toRad;
183
+ const lng1 = c1.lng * toRad;
184
+ const lat2 = c2.lat * toRad;
185
+ const lng2 = c2.lng * toRad;
186
+ return {
187
+ lat1,
188
+ lat2,
189
+ lng1,
190
+ lng2,
191
+ dLat: lat2 - lat1,
192
+ dLng: lng2 - lng1
193
+ };
194
+ }
195
+
196
+ // ../../functions/dist/spatial/haversineDistance.js
197
+ function haversineDistance(from, to, unit = "km") {
198
+ const coords = toRadianCoordinates(from, to);
199
+ if (!coords)
200
+ return NaN;
201
+ const { lat1, lat2, dLat, dLng } = coords;
202
+ const a = Math.sin(dLat / 2) ** 2 + Math.cos(lat1) * Math.cos(lat2) * Math.sin(dLng / 2) ** 2;
203
+ const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
204
+ const radius = unit === "miles" ? 3958.7613 : unit === "m" ? 63710088e-1 : 6371.0088;
205
+ const dist = radius * c;
206
+ return unit === "m" ? Math.round(dist) : Math.round(dist * 1e3) / 1e3;
207
+ }
208
+
209
+ // ../../functions/dist/spatial/calculateBearing.js
210
+ function calculateBearing(from, to, options) {
211
+ const coords = toRadianCoordinates(from, to);
212
+ if (!coords)
213
+ return NaN;
214
+ const { lat1, lat2, dLng } = coords;
215
+ const y = Math.sin(dLng) * Math.cos(lat2);
216
+ const x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLng);
217
+ const bearing = (Math.atan2(y, x) * (180 / Math.PI) + 360) % 360;
218
+ const precision = options?.precision ?? 1;
219
+ const factor = Math.pow(10, precision);
220
+ return Math.round(bearing * factor) / factor;
221
+ }
222
+
223
+ // ../../functions/dist/spatial/calculateMidpoint.js
224
+ function calculateMidpoint(from, to) {
225
+ const coords = toRadianCoordinates(from, to);
226
+ if (!coords)
227
+ return void 0;
228
+ const { lat1, lat2, lng1, dLng } = coords;
229
+ const Bx = Math.cos(lat2) * Math.cos(dLng);
230
+ const By = Math.cos(lat2) * Math.sin(dLng);
231
+ const latMidRad = Math.atan2(Math.sin(lat1) + Math.sin(lat2), Math.sqrt((Math.cos(lat1) + Bx) ** 2 + By ** 2));
232
+ const lngMidRad = lng1 + Math.atan2(By, Math.cos(lat1) + Bx);
233
+ const midLat = Math.round(latMidRad * 180 / Math.PI * 1e3) / 1e3;
234
+ const midLng = Math.round(((lngMidRad * 180 / Math.PI + 540) % 360 - 180) * 1e3) / 1e3;
235
+ const sphere = midLat > 1e-3 ? "north" : midLat < -1e-3 ? "south" : "equator";
236
+ return {
237
+ latitude: midLat,
238
+ longitude: midLng,
239
+ sphere
240
+ };
241
+ }
242
+
243
+ // ../../functions/dist/spatial/calculateVelocity.js
244
+ function calculateVelocity(from, to, options) {
245
+ const opts = isString(options) ? { unit: options } : options ?? {};
246
+ const unit = opts.unit ?? "km";
247
+ const timeUnit = opts.timeUnit ?? "hh";
248
+ const dist = haversineDistance(from, to, unit);
249
+ if (isNaN(dist))
250
+ return NaN;
251
+ const t1 = extractEpochMs(from);
252
+ const t2 = extractEpochMs(to);
253
+ if (!isNumber(t1) || !isNumber(t2))
254
+ return NaN;
255
+ const deltaMs = Math.abs(t2 - t1);
256
+ if (deltaMs === 0)
257
+ return dist === 0 ? 0 : Infinity;
258
+ let deltaUnits;
259
+ if (timeUnit === "ss" || timeUnit === "s" || timeUnit === "seconds" || timeUnit === "second") {
260
+ deltaUnits = deltaMs / 1e3;
261
+ } else if (timeUnit === "mi" || timeUnit === "m" || timeUnit === "minutes" || timeUnit === "minute") {
262
+ deltaUnits = deltaMs / 6e4;
263
+ } else {
264
+ deltaUnits = deltaMs / 36e5;
265
+ }
266
+ const velocity = dist / deltaUnits;
267
+ const precision = opts.precision ?? 2;
268
+ const factor = Math.pow(10, precision);
269
+ return Math.round(velocity * factor) / factor;
270
+ }
271
+
272
+ // ../../functions/dist/spatial/isImpossibleTravel.js
273
+ function isImpossibleTravel(from, to, options) {
274
+ const unit = options?.unit ?? "km";
275
+ const defaultMax = unit === "miles" ? 560 : unit === "m" ? 250 : 900;
276
+ let fallbackThreshold;
277
+ if (isNotNullish(options?.maxCommercialSpeedKmH)) {
278
+ if (unit === "miles") {
279
+ fallbackThreshold = options.maxCommercialSpeedKmH * 0.621371;
280
+ } else if (unit === "m") {
281
+ fallbackThreshold = options.maxCommercialSpeedKmH * 1e3 / 3600;
282
+ } else {
283
+ fallbackThreshold = options.maxCommercialSpeedKmH;
284
+ }
285
+ } else {
286
+ fallbackThreshold = defaultMax;
287
+ }
288
+ const threshold = options?.maxSpeed ?? fallbackThreshold;
289
+ const velocity = calculateVelocity(from, to, { unit, timeUnit: unit === "m" ? "ss" : "hh" });
290
+ return velocity > threshold;
291
+ }
292
+
293
+ // ../../functions/dist/spatial/isWithin.js
294
+ function isWithin(from, to, maxDistance, unit = "km") {
295
+ if (!isNumber(maxDistance) || maxDistance < 0)
296
+ return false;
297
+ const c1 = extractRawCoords(from);
298
+ const c2 = extractRawCoords(to);
299
+ if (!c1 || !c2 || !isNumber(c1.lat) || !isNumber(c1.lng) || !isNumber(c2.lat) || !isNumber(c2.lng))
300
+ return false;
301
+ const toRad = Math.PI / 180;
302
+ const lat1 = c1.lat * toRad;
303
+ const lng1 = c1.lng * toRad;
304
+ const lat2 = c2.lat * toRad;
305
+ const lng2 = c2.lng * toRad;
306
+ const dLat = lat2 - lat1;
307
+ const dLng = lng2 - lng1;
308
+ const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(lat1) * Math.cos(lat2) * Math.sin(dLng / 2) * Math.sin(dLng / 2);
309
+ const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
310
+ const radius = unit === "miles" ? 3958.7613 : unit === "m" ? 63710088e-1 : 6371.0088;
311
+ const dist = radius * c;
312
+ return dist <= maxDistance;
313
+ }
314
+
315
+ // ../../functions/dist/spatial/inBoundingBox.js
316
+ function inBoundingBox(coords, bbox) {
317
+ const geo = extractRawCoords(coords);
318
+ if (!geo || !isNumber(geo.lat) || !isNumber(geo.lng))
319
+ return false;
320
+ if (isNullish(bbox))
321
+ return false;
322
+ let minLat;
323
+ let maxLat;
324
+ let minLng;
325
+ let maxLng;
326
+ if (Array.isArray(bbox) && bbox.length >= 4) {
327
+ minLat = bbox[0];
328
+ minLng = bbox[1];
329
+ maxLat = bbox[2];
330
+ maxLng = bbox[3];
331
+ } else if (isReference(bbox)) {
332
+ const b = bbox;
333
+ minLat = b.minLat ?? b.minLatitude;
334
+ maxLat = b.maxLat ?? b.maxLatitude;
335
+ minLng = b.minLng ?? b.minLongitude;
336
+ maxLng = b.maxLng ?? b.maxLongitude;
337
+ }
338
+ if (!isNumber(minLat) || !isNumber(maxLat) || !isNumber(minLng) || !isNumber(maxLng))
339
+ return false;
340
+ const lat = geo.lat;
341
+ const lng = geo.lng;
342
+ if (lat < minLat || lat > maxLat)
343
+ return false;
344
+ return minLng <= maxLng ? lng >= minLng && lng <= maxLng : lng >= minLng || lng <= maxLng;
345
+ }
346
+
347
+ // ../../functions/dist/spatial/solarOffset.js
348
+ var RE_GMT_OFFSET = /GMT([+-])(\d{2}):(\d{2})/;
349
+ var resolveCivilTimezoneOffset = (tz, epochMs) => {
350
+ try {
351
+ const globalTemporal = globalThis.Temporal;
352
+ if (globalTemporal && isFunction(globalTemporal.Instant?.fromEpochMilliseconds)) {
353
+ const zdt = globalTemporal.Instant.fromEpochMilliseconds(epochMs).toZonedDateTimeISO(tz);
354
+ return zdt.offsetNanoseconds / 6e10;
355
+ }
356
+ const parts = new Intl.DateTimeFormat("en-US", {
357
+ timeZone: tz,
358
+ timeZoneName: "longOffset",
359
+ year: "numeric"
360
+ }).formatToParts(new Date(epochMs));
361
+ const tzPart = parts.find((p) => p.type === "timeZoneName")?.value;
362
+ if (tzPart) {
363
+ if (tzPart === "GMT" || tzPart === "UTC")
364
+ return 0;
365
+ const match = tzPart.match(RE_GMT_OFFSET);
366
+ if (match) {
367
+ const sign = match[1] === "-" ? -1 : 1;
368
+ return sign * (parseInt(match[2], 10) * 60 + parseInt(match[3], 10));
369
+ }
370
+ }
371
+ } catch {
372
+ }
373
+ return void 0;
374
+ };
375
+ function solarOffset(coords, options) {
376
+ const geo = coerceGeo(coords);
377
+ if (!geo || !isNumber(geo.latitude) || !isNumber(geo.longitude)) {
378
+ return NaN;
379
+ }
380
+ const lng = geo.longitude;
381
+ const tz = options?.timeZone ?? (isReference(coords) ? coords.timezone ?? coords.tz : void 0) ?? geo.timezone;
382
+ const dateVal = options?.date ?? (isReference(coords) ? extractEpochMs(coords) : void 0);
383
+ const epochMs = isNotNullish(dateVal) ? extractEpochMs(dateVal) : Date.now();
384
+ if (!isNumber(epochMs))
385
+ return NaN;
386
+ const offsetMinutes = isText(tz) ? resolveCivilTimezoneOffset(tz, epochMs) : void 0;
387
+ const refMeridian = isNotNullish(offsetMinutes) ? offsetMinutes / 60 * 15 : Math.round(lng / 15) * 15;
388
+ const deltaLng = lng - refMeridian;
389
+ let offsetMin = deltaLng * 4;
390
+ if (options?.apparent) {
391
+ const d = new Date(epochMs);
392
+ const startOfYear = Date.UTC(d.getUTCFullYear(), 0, 1);
393
+ const dayOfYear = Math.floor((epochMs - startOfYear) / 864e5) + 1;
394
+ const gamma = 2 * Math.PI / 365 * (dayOfYear - 1);
395
+ const eqTime = 229.18 * (75e-6 + 1868e-6 * Math.cos(gamma) - 0.032077 * Math.sin(gamma) - 0.014615 * Math.cos(2 * gamma) - 0.040849 * Math.sin(2 * gamma));
396
+ offsetMin += eqTime;
397
+ }
398
+ const unit = options?.unit ?? "mi";
399
+ let res;
400
+ if (unit === "ss" || unit === "seconds") {
401
+ res = offsetMin * 60;
402
+ } else if (unit === "hh" || unit === "hours") {
403
+ res = offsetMin / 60;
404
+ } else {
405
+ res = offsetMin;
406
+ }
407
+ const precision = options?.precision ?? 2;
408
+ const factor = Math.pow(10, precision);
409
+ return Math.round(res * factor) / factor;
410
+ }
411
+
412
+ // ../../functions/dist/spatial/resolveCulturalLocale.js
413
+ var COMMON_COUNTRY_NAMES = {
414
+ australia: "AU",
415
+ "united states": "US",
416
+ "united kingdom": "GB",
417
+ "great britain": "GB",
418
+ france: "FR",
419
+ germany: "DE",
420
+ italy: "IT",
421
+ spain: "ES",
422
+ japan: "JP",
423
+ china: "CN",
424
+ canada: "CA",
425
+ brazil: "BR",
426
+ egypt: "EG",
427
+ "saudi arabia": "SA",
428
+ russia: "RU",
429
+ india: "IN",
430
+ mexico: "MX",
431
+ "new zealand": "NZ",
432
+ netherlands: "NL",
433
+ switzerland: "CH",
434
+ austria: "AT",
435
+ belgium: "BE",
436
+ sweden: "SE",
437
+ norway: "NO",
438
+ denmark: "DK",
439
+ finland: "FI",
440
+ poland: "PL",
441
+ turkey: "TR",
442
+ greece: "GR",
443
+ israel: "IL",
444
+ thailand: "TH",
445
+ vietnam: "VN",
446
+ indonesia: "ID",
447
+ malaysia: "MY",
448
+ philippines: "PH",
449
+ "south africa": "ZA",
450
+ "south korea": "KR",
451
+ korea: "KR",
452
+ ireland: "IE",
453
+ portugal: "PT",
454
+ argentina: "AR"
455
+ };
456
+ var COUNTRY_PRIMARY_LOCALES = {
457
+ AU: "en-AU",
458
+ US: "en-US",
459
+ GB: "en-GB",
460
+ CA: "en-CA",
461
+ NZ: "en-NZ",
462
+ IE: "en-IE",
463
+ SA: "ar-SA",
464
+ AE: "ar-AE",
465
+ EG: "ar-EG",
466
+ JP: "ja-JP",
467
+ CN: "zh-CN",
468
+ TW: "zh-TW",
469
+ HK: "zh-HK",
470
+ KR: "ko-KR",
471
+ FR: "fr-FR",
472
+ DE: "de-DE",
473
+ IT: "it-IT",
474
+ ES: "es-ES",
475
+ MX: "es-MX",
476
+ AR: "es-AR",
477
+ BR: "pt-BR",
478
+ PT: "pt-PT",
479
+ RU: "ru-RU",
480
+ IN: "hi-IN",
481
+ NL: "nl-NL",
482
+ SE: "sv-SE",
483
+ NO: "nb-NO",
484
+ DK: "da-DK",
485
+ FI: "fi-FI",
486
+ PL: "pl-PL",
487
+ TR: "tr-TR",
488
+ GR: "el-GR",
489
+ IL: "he-IL",
490
+ TH: "th-TH",
491
+ VN: "vi-VN",
492
+ ID: "id-ID",
493
+ MY: "ms-MY",
494
+ PH: "fil-PH",
495
+ ZA: "en-ZA",
496
+ CH: "de-CH",
497
+ AT: "de-AT",
498
+ BE: "nl-BE"
499
+ };
500
+ function cleanLocale(tag) {
501
+ try {
502
+ const loc = new Intl.Locale(tag);
503
+ return {
504
+ language: loc.language,
505
+ script: loc.script || void 0,
506
+ baseName: loc.baseName
507
+ };
508
+ } catch {
509
+ const parts = tag.split("-");
510
+ return {
511
+ language: parts[0]?.toLowerCase(),
512
+ script: parts.length > 2 ? parts[1] : void 0,
513
+ baseName: tag
514
+ };
515
+ }
516
+ }
517
+ function resolveCulturalLocale(currentLocale, countryCode, mode = true) {
518
+ if (mode === false || mode === "none" || mode === "off")
519
+ return void 0;
520
+ const toCode = (val) => {
521
+ const trimmed = val.trim();
522
+ return trimmed.length === 2 ? trimmed.toUpperCase() : COMMON_COUNTRY_NAMES[trimmed.toLowerCase()];
523
+ };
524
+ let country;
525
+ if (Array.isArray(countryCode)) {
526
+ for (const c of countryCode) {
527
+ if (isString(c)) {
528
+ const mapped = toCode(c);
529
+ if (mapped) {
530
+ country = mapped;
531
+ break;
532
+ }
533
+ }
534
+ }
535
+ } else if (isString(countryCode)) {
536
+ country = toCode(countryCode);
537
+ }
538
+ if (isString(mode) && mode !== "regional" && mode !== "region" && mode !== "native" && mode !== "full") {
539
+ const loc2 = cleanLocale(mode);
540
+ return loc2?.baseName ?? mode;
541
+ }
542
+ if (!country)
543
+ return void 0;
544
+ if (mode === "native" || mode === "full") {
545
+ const nativeTag = COUNTRY_PRIMARY_LOCALES[country] ?? `en-${country}`;
546
+ const loc2 = cleanLocale(nativeTag);
547
+ return loc2?.baseName ?? nativeTag;
548
+ }
549
+ let baseLang = "en";
550
+ let script;
551
+ if (isText(currentLocale)) {
552
+ const loc2 = cleanLocale(currentLocale);
553
+ baseLang = loc2?.language ?? currentLocale.split("-")[0] ?? "en";
554
+ if (loc2?.script)
555
+ script = loc2.script;
556
+ }
557
+ const targetTag = script ? `${baseLang}-${script}-${country}` : `${baseLang}-${country}`;
558
+ const loc = cleanLocale(targetTag);
559
+ return loc?.baseName ?? targetTag;
560
+ }
561
+
562
+ // src/index.ts
563
+ var SpatialPlugin = definePlugin({
564
+ name: "spatial",
565
+ install(TempoClass) {
566
+ const installedClass = TempoClass || this;
567
+ if (!(0, sdk_exports.hasOwn)(installedClass, "spatial")) {
568
+ const spatialNamespace = {
569
+ distance: haversineDistance,
570
+ bearing: calculateBearing,
571
+ midpoint: calculateMidpoint,
572
+ velocity: calculateVelocity,
573
+ isImpossibleTravel,
574
+ isWithin,
575
+ inBoundingBox,
576
+ solarOffset
577
+ };
578
+ Object.defineProperty(installedClass, "spatial", {
579
+ value: (0, sdk_exports.deepFreeze)(spatialNamespace),
580
+ writable: false,
581
+ configurable: false,
582
+ enumerable: false
583
+ });
584
+ }
585
+ TempoClass.prototype.spatialDistance = function(to, unit = "km") {
586
+ return haversineDistance(this, to, unit);
587
+ };
588
+ TempoClass.prototype.spatialBearing = function(to, options) {
589
+ return calculateBearing(this, to, options);
590
+ };
591
+ TempoClass.prototype.spatialVelocity = function(to, options) {
592
+ return calculateVelocity(this, to, options);
593
+ };
594
+ TempoClass.prototype.spatialSolarOffset = function(options) {
595
+ return solarOffset(this, options);
596
+ };
597
+ TempoClass.prototype.isWithin = function(to, maxDistance, unit = "km") {
598
+ return isWithin(this, to, maxDistance, unit);
599
+ };
600
+ TempoClass.prototype.inBoundingBox = function(bbox) {
601
+ return inBoundingBox(this, bbox);
602
+ };
603
+ }
604
+ });
605
+ var spatialPlugin = SpatialPlugin;
606
+ var index_default = SpatialPlugin;
607
+ export {
608
+ SpatialPlugin,
609
+ calculateBearing,
610
+ calculateMidpoint,
611
+ calculateVelocity,
612
+ index_default as default,
613
+ haversineDistance,
614
+ inBoundingBox,
615
+ isImpossibleTravel,
616
+ isWithin,
617
+ resolveCulturalLocale,
618
+ solarOffset,
619
+ spatialPlugin
620
+ };
@@ -0,0 +1,2 @@
1
+ export * from './index.js';
2
+ export { SpatialPlugin, spatialPlugin, default } from './index.js';
@@ -0,0 +1 @@
1
+ "use strict";var Magma;((Magma||={}).plugins||={}).spatial=(()=>{var fe=Object.create;var F=Object.defineProperty;var me=Object.getOwnPropertyDescriptor;var de=Object.getOwnPropertyNames;var ue=Object.getPrototypeOf,pe=Object.prototype.hasOwnProperty;var ge=(e,n)=>()=>{try{return n||e((n={exports:{}}).exports,n),n.exports}catch(t){throw n=0,t}},Q=(e,n)=>{for(var t in n)F(e,t,{get:n[t],enumerable:!0})},Z=(e,n,t,o)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of de(n))!pe.call(e,r)&&r!==t&&F(e,r,{get:()=>n[r],enumerable:!(o=me(n,r))||o.enumerable});return e},B=(e,n,t)=>(Z(e,n,"default"),t&&Z(t,n,"default")),ee=(e,n,t)=>(t=e!=null?fe(ue(e)):{},Z(n||!e||!e.__esModule?F(t,"default",{value:e,enumerable:!0}):t,e)),he=e=>Z(F({},"__esModule",{value:!0}),e);var k=ge((De,te)=>{"use strict";te.exports=window.Magma.pluginSdk});var Se={};Q(Se,{SpatialPlugin:()=>P,calculateBearing:()=>E,calculateMidpoint:()=>U,calculateVelocity:()=>N,default:()=>se,haversineDistance:()=>S,inBoundingBox:()=>C,isImpossibleTravel:()=>v,isWithin:()=>R,resolveCulturalLocale:()=>G,solarOffset:()=>A,spatialPlugin:()=>ae});var p={};Q(p,{definePlugin:()=>V,defineTerm:()=>ye});B(p,ee(k()));var Y=ee(k()),V=e=>(0,Y.definePlugin)({...e,version:"2.0.0"}),ye=e=>(0,Y.defineTerm)({...e,version:"2.0.0"});var s=e=>typeof e=="number"&&Number.isFinite(e),u=e=>typeof e=="string",z=e=>u(e)&&e.trim().length>0,ne=e=>e===!0||e===!1,O=e=>typeof e=="function",x=e=>e==null,h=e=>!x(e),K=e=>e===void 0;var j=e=>e instanceof Date&&s(e.getTime()),Te=e=>e===null||typeof e!="object"&&!O(e),H=e=>h(e)&&typeof e=="object"&&!Array.isArray(e),T=e=>!Te(e);var L=e=>{if(x(e)||ne(e))return NaN;if(u(e)){let n=e.trim();return n.length>0?Number(n):NaN}return s(e)?e:NaN},oe=(e,n=!1)=>{let t=L(e);if(!(!s(t)||t<-90||t>90))return n?Math.round(t*1e3)/1e3:t},re=(e,n=!1)=>{let t=L(e);if(!(!s(t)||t<-180||t>180))return n?Math.round(t*1e3)/1e3:t},I=(e,n,t=!1)=>{let o=oe(e,t),r=re(n,t);return h(o)&&h(r)?{lat:o,lng:r}:void 0};function M(e,n){if(!x(e)){if(s(e)&&s(n)){let t=I(e,n,!1);return t?{...t,elevation:0}:void 0}if(u(e)&&e.includes(",")){let t=e.split(",").map(o=>o.trim());if(t.length>=2){let o=I(t[0],t[1],!1),r=t.length>=3?L(t[2]):void 0;return o?{...o,...s(r)?{elevation:r}:{}}:void 0}return}if(Array.isArray(e)&&e.length>=2){let t=I(e[0],e[1],!1),o=e.length>=3?L(e[2]):void 0;return t?{...t,...s(o)?{elevation:o}:{}}:void 0}if(T(e)||H(e)){let t=e.geo??e.config?.geo??e,o=e.config?.geo??e.config,r=(...l)=>{for(let m of l){let d=L(m);if(s(d))return d}},i=r(t?.latitude,t?.lat,e.latitude,e.lat,o?.latitude,o?.lat),a=r(t?.longitude,t?.lng,t?.lon,t?.long,e.longitude,e.lng,e.lon,e.long,o?.longitude,o?.lng,o?.lon,o?.long),c=r(t?.elevation,e.elevation,o?.elevation),f=I(i,a,!1);return f?{...f,...s(c)?{elevation:c}:{}}:void 0}}}function D(e){if(!x(e)){if(s(e))return e;if(j(e))return e.getTime();if(H(e)){if(s(e.epoch?.ms))return e.epoch.ms;if(s(e.epochMilliseconds))return e.epochMilliseconds;if(s(e.timestamp))return e.timestamp;if(j(e.date))return e.date.getTime();if(O(e.toInstant))try{let n=e.toInstant().epochMilliseconds;if(s(n))return n}catch{}if(O(e.getTime))try{let n=e.getTime();if(s(n))return n}catch{}}if(u(e)){let n=Date.parse(e.trim());if(s(n))return n}}}function ie(e){let n=M(e);if(K(n))return;let t=I(n.lat,n.lng,!0);if(K(t))return;let{lat:o,lng:r}=t,i=o>.001?"north":o<-.001?"south":"equator",a=T(e)?e.geo??e.config?.geo??e:{};return{latitude:o,longitude:r,sphere:a?.sphere??i,...u(a?.country)?{country:a.country}:{},...u(a?.city)?{city:a.city}:{},...s(a?.elevation)?{elevation:Math.round(a.elevation*1e3)/1e3}:{},...u(a?.timezone)?{timezone:a.timezone}:{}}}function b(e,n){let t=M(e),o=M(n);if(!t||!o||!s(t.lat)||!s(t.lng)||!s(o.lat)||!s(o.lng))return;let r=Math.PI/180,i=t.lat*r,a=t.lng*r,c=o.lat*r,f=o.lng*r;return{lat1:i,lat2:c,lng1:a,lng2:f,dLat:c-i,dLng:f-a}}function S(e,n,t="km"){let o=b(e,n);if(!o)return NaN;let{lat1:r,lat2:i,dLat:a,dLng:c}=o,f=Math.sin(a/2)**2+Math.cos(r)*Math.cos(i)*Math.sin(c/2)**2,l=2*Math.atan2(Math.sqrt(f),Math.sqrt(1-f)),d=(t==="miles"?3958.7613:t==="m"?63710088e-1:6371.0088)*l;return t==="m"?Math.round(d):Math.round(d*1e3)/1e3}function E(e,n,t){let o=b(e,n);if(!o)return NaN;let{lat1:r,lat2:i,dLng:a}=o,c=Math.sin(a)*Math.cos(i),f=Math.cos(r)*Math.sin(i)-Math.sin(r)*Math.cos(i)*Math.cos(a),l=(Math.atan2(c,f)*(180/Math.PI)+360)%360,m=t?.precision??1,d=Math.pow(10,m);return Math.round(l*d)/d}function U(e,n){let t=b(e,n);if(!t)return;let{lat1:o,lat2:r,lng1:i,dLng:a}=t,c=Math.cos(r)*Math.cos(a),f=Math.cos(r)*Math.sin(a),l=Math.atan2(Math.sin(o)+Math.sin(r),Math.sqrt((Math.cos(o)+c)**2+f**2)),m=i+Math.atan2(f,Math.cos(o)+c),d=Math.round(l*180/Math.PI*1e3)/1e3,g=Math.round(((m*180/Math.PI+540)%360-180)*1e3)/1e3,y=d>.001?"north":d<-.001?"south":"equator";return{latitude:d,longitude:g,sphere:y}}function N(e,n,t){let o=u(t)?{unit:t}:t??{},r=o.unit??"km",i=o.timeUnit??"hh",a=S(e,n,r);if(isNaN(a))return NaN;let c=D(e),f=D(n);if(!s(c)||!s(f))return NaN;let l=Math.abs(f-c);if(l===0)return a===0?0:1/0;let m;i==="ss"||i==="s"||i==="seconds"||i==="second"?m=l/1e3:i==="mi"||i==="m"||i==="minutes"||i==="minute"?m=l/6e4:m=l/36e5;let d=a/m,g=o.precision??2,y=Math.pow(10,g);return Math.round(d*y)/y}function v(e,n,t){let o=t?.unit??"km",r=o==="miles"?560:o==="m"?250:900,i;h(t?.maxCommercialSpeedKmH)?o==="miles"?i=t.maxCommercialSpeedKmH*.621371:o==="m"?i=t.maxCommercialSpeedKmH*1e3/3600:i=t.maxCommercialSpeedKmH:i=r;let a=t?.maxSpeed??i;return N(e,n,{unit:o,timeUnit:o==="m"?"ss":"hh"})>a}function R(e,n,t,o="km"){if(!s(t)||t<0)return!1;let r=M(e),i=M(n);if(!r||!i||!s(r.lat)||!s(r.lng)||!s(i.lat)||!s(i.lng))return!1;let a=Math.PI/180,c=r.lat*a,f=r.lng*a,l=i.lat*a,m=i.lng*a,d=l-c,g=m-f,y=Math.sin(d/2)*Math.sin(d/2)+Math.cos(c)*Math.cos(l)*Math.sin(g/2)*Math.sin(g/2),_=2*Math.atan2(Math.sqrt(y),Math.sqrt(1-y));return(o==="miles"?3958.7613:o==="m"?63710088e-1:6371.0088)*_<=t}function C(e,n){let t=M(e);if(!t||!s(t.lat)||!s(t.lng)||x(n))return!1;let o,r,i,a;if(Array.isArray(n)&&n.length>=4)o=n[0],i=n[1],r=n[2],a=n[3];else if(T(n)){let l=n;o=l.minLat??l.minLatitude,r=l.maxLat??l.maxLatitude,i=l.minLng??l.minLongitude,a=l.maxLng??l.maxLongitude}if(!s(o)||!s(r)||!s(i)||!s(a))return!1;let c=t.lat,f=t.lng;return c<o||c>r?!1:i<=a?f>=i&&f<=a:f>=i||f<=a}var Me=/GMT([+-])(\d{2}):(\d{2})/,xe=(e,n)=>{try{let t=globalThis.Temporal;if(t&&O(t.Instant?.fromEpochMilliseconds))return t.Instant.fromEpochMilliseconds(n).toZonedDateTimeISO(e).offsetNanoseconds/6e10;let r=new Intl.DateTimeFormat("en-US",{timeZone:e,timeZoneName:"longOffset",year:"numeric"}).formatToParts(new Date(n)).find(i=>i.type==="timeZoneName")?.value;if(r){if(r==="GMT"||r==="UTC")return 0;let i=r.match(Me);if(i)return(i[1]==="-"?-1:1)*(parseInt(i[2],10)*60+parseInt(i[3],10))}}catch{}};function A(e,n){let t=ie(e);if(!t||!s(t.latitude)||!s(t.longitude))return NaN;let o=t.longitude,r=n?.timeZone??(T(e)?e.timezone??e.tz:void 0)??t.timezone,i=n?.date??(T(e)?D(e):void 0),a=h(i)?D(i):Date.now();if(!s(a))return NaN;let c=z(r)?xe(r,a):void 0,f=h(c)?c/60*15:Math.round(o/15)*15,m=(o-f)*4;if(n?.apparent){let X=new Date(a),J=Date.UTC(X.getUTCFullYear(),0,1),le=Math.floor((a-J)/864e5)+1,w=2*Math.PI/365*(le-1),ce=229.18*(75e-6+.001868*Math.cos(w)-.032077*Math.sin(w)-.014615*Math.cos(2*w)-.040849*Math.sin(2*w));m+=ce}let d=n?.unit??"mi",g;d==="ss"||d==="seconds"?g=m*60:d==="hh"||d==="hours"?g=m/60:g=m;let y=n?.precision??2,_=Math.pow(10,y);return Math.round(g*_)/_}var $={australia:"AU","united states":"US","united kingdom":"GB","great britain":"GB",france:"FR",germany:"DE",italy:"IT",spain:"ES",japan:"JP",china:"CN",canada:"CA",brazil:"BR",egypt:"EG","saudi arabia":"SA",russia:"RU",india:"IN",mexico:"MX","new zealand":"NZ",netherlands:"NL",switzerland:"CH",austria:"AT",belgium:"BE",sweden:"SE",norway:"NO",denmark:"DK",finland:"FI",poland:"PL",turkey:"TR",greece:"GR",israel:"IL",thailand:"TH",vietnam:"VN",indonesia:"ID",malaysia:"MY",philippines:"PH","south africa":"ZA","south korea":"KR",korea:"KR",ireland:"IE",portugal:"PT",argentina:"AR"},q={AU:"en-AU",US:"en-US",GB:"en-GB",CA:"en-CA",NZ:"en-NZ",IE:"en-IE",SA:"ar-SA",AE:"ar-AE",EG:"ar-EG",JP:"ja-JP",CN:"zh-CN",TW:"zh-TW",HK:"zh-HK",KR:"ko-KR",FR:"fr-FR",DE:"de-DE",IT:"it-IT",ES:"es-ES",MX:"es-MX",AR:"es-AR",BR:"pt-BR",PT:"pt-PT",RU:"ru-RU",IN:"hi-IN",NL:"nl-NL",SE:"sv-SE",NO:"nb-NO",DK:"da-DK",FI:"fi-FI",PL:"pl-PL",TR:"tr-TR",GR:"el-GR",IL:"he-IL",TH:"th-TH",VN:"vi-VN",ID:"id-ID",MY:"ms-MY",PH:"fil-PH",ZA:"en-ZA",CH:"de-CH",AT:"de-AT",BE:"nl-BE"};function W(e){try{let n=new Intl.Locale(e);return{language:n.language,script:n.script||void 0,baseName:n.baseName}}catch{let n=e.split("-");return{language:n[0]?.toLowerCase(),script:n.length>2?n[1]:void 0,baseName:e}}}function G(e,n,t=!0){if(t===!1||t==="none"||t==="off")return;let o=l=>{let m=l.trim();return m.length===2?m.toUpperCase():$[m.toLowerCase()]},r;if(Array.isArray(n)){for(let l of n)if(u(l)){let m=o(l);if(m){r=m;break}}}else u(n)&&(r=o(n));if(u(t)&&t!=="regional"&&t!=="region"&&t!=="native"&&t!=="full")return W(t)?.baseName??t;if(!r)return;if(t==="native"||t==="full"){let l=q[r]??`en-${r}`;return W(l)?.baseName??l}let i="en",a;if(z(e)){let l=W(e);i=l?.language??e.split("-")[0]??"en",l?.script&&(a=l.script)}let c=a?`${i}-${a}-${r}`:`${i}-${r}`;return W(c)?.baseName??c}var P=V({name:"spatial",install(e){let n=e||this;(0,p.hasOwn)(n,"spatial")||Object.defineProperty(n,"spatial",{value:(0,p.deepFreeze)({distance:S,bearing:E,midpoint:U,velocity:N,isImpossibleTravel:v,isWithin:R,inBoundingBox:C,solarOffset:A}),writable:!1,configurable:!1,enumerable:!1}),e.prototype.spatialDistance=function(t,o="km"){return S(this,t,o)},e.prototype.spatialBearing=function(t,o){return E(this,t,o)},e.prototype.spatialVelocity=function(t,o){return N(this,t,o)},e.prototype.spatialSolarOffset=function(t){return A(this,t)},e.prototype.isWithin=function(t,o,r="km"){return R(this,t,o,r)},e.prototype.inBoundingBox=function(t){return C(this,t)}}}),ae=P,se=P;(0,p.autoInstall)(P);return he(Se);})();