@ulm/core 1.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/core.js ADDED
@@ -0,0 +1,908 @@
1
+ import { setup as L, assign as E, enqueueActions as y, stopChild as T, emit as h, createActor as _ } from "xstate";
2
+ function k(e) {
3
+ return e?.type === "single";
4
+ }
5
+ function $(e) {
6
+ return e?.type === "range";
7
+ }
8
+ function b(e) {
9
+ return e.getSnapshot().context.layerType === "layer";
10
+ }
11
+ function m(e) {
12
+ return e.getSnapshot().context.layerType === "layerGroup";
13
+ }
14
+ function u(e, t) {
15
+ return e.find((a) => a.layerActor.id === t);
16
+ }
17
+ function j(e, t) {
18
+ const a = u(e, t);
19
+ if (a)
20
+ return a.layerActor.getSnapshot().context.layerData;
21
+ }
22
+ function D(e, t) {
23
+ return t.parentId ? O(e, t.parentId) : null;
24
+ }
25
+ function O(e, t) {
26
+ const a = u(e, t);
27
+ return !a || !m(a.layerActor) ? null : a.layerActor;
28
+ }
29
+ function Y(e, { layers: t, allowNestedGroupLayers: a }) {
30
+ return e.layerType === "layerGroup" && e.parentId && !a ? (console.warn("Nested group layers are not allowed."), !1) : t.some((r) => r.layerActor.id === e.layerId) ? (console.warn(`Layer with ID ${e.layerId} already exists. Layer not added.`), !1) : !0;
31
+ }
32
+ function C(e, t) {
33
+ return e.parentId && !t ? (console.warn("Unable to find valid parent layer. Layer not added."), !1) : !0;
34
+ }
35
+ function M(e) {
36
+ if (!e)
37
+ return console.warn("Unable to find layer to remove."), !1;
38
+ const { layerType: t } = e.layerActor.getSnapshot().context;
39
+ if (t === "layerGroup") {
40
+ const { children: a } = e.layerActor.getSnapshot().context;
41
+ if (a.length > 0)
42
+ return console.warn("Layer group has children. Not removed."), !1;
43
+ }
44
+ return !0;
45
+ }
46
+ function N(e, t) {
47
+ return e >= 0 && e <= t;
48
+ }
49
+ function g(e, t, a, r) {
50
+ const i = [...e];
51
+ if (a !== void 0 && N(a, e.length)) {
52
+ const o = a > e.length ? e.length : a;
53
+ i.splice(o, 0, t);
54
+ } else r === "top" ? i.push(t) : i.unshift(t);
55
+ return i;
56
+ }
57
+ function S(e, t) {
58
+ const a = [], r = (i) => {
59
+ i.forEach((o) => {
60
+ const l = e.find((n) => n.layerActor.id === o);
61
+ if (!l)
62
+ return;
63
+ a.push(o);
64
+ const s = l.layerActor.getSnapshot();
65
+ if (s.context.layerType === "layerGroup") {
66
+ const n = s.context;
67
+ r(n.childLayerOrder);
68
+ }
69
+ });
70
+ };
71
+ return r(t), a;
72
+ }
73
+ function v(e, t) {
74
+ return e.map((a) => t.find((r) => r.layerActor.id === a)).filter((a) => a !== void 0);
75
+ }
76
+ function z(e, t) {
77
+ return e.map((a) => t.find((r) => r.id === a)).filter((a) => a !== void 0);
78
+ }
79
+ function P(e, t, a, r, i) {
80
+ return a ? H(e.layers, t, a, r, i) : U(e.layers, t, e.childLayerOrder, r, i);
81
+ }
82
+ function G(e) {
83
+ const { parentRef: t } = e.layerActor.getSnapshot().context;
84
+ t && t.send({ type: "LAYERS.REMOVE_CHILD", id: e.layerActor.id });
85
+ }
86
+ function V(e, t) {
87
+ return {
88
+ layers: e.layers.filter((a) => a.layerActor.id !== t),
89
+ childLayerOrder: e.childLayerOrder.filter((a) => a !== t)
90
+ };
91
+ }
92
+ function f(e, t) {
93
+ return e ? e.getSnapshot().context.computedOpacity * t : t;
94
+ }
95
+ function H(e, t, a, r, i) {
96
+ return a.send({ type: "LAYERS.ADD_CHILD", child: t.layerActor, index: r, position: i }), {
97
+ layers: [...e, t]
98
+ };
99
+ }
100
+ function U(e, t, a, r, i) {
101
+ const o = g(a, t.layerActor.id, r, i);
102
+ return {
103
+ layers: [...e, t],
104
+ childLayerOrder: o
105
+ };
106
+ }
107
+ function B() {
108
+ return L({
109
+ types: {
110
+ context: {},
111
+ events: {},
112
+ input: {}
113
+ },
114
+ actions: {
115
+ "Notify children of visibility change": y(
116
+ ({ context: e, enqueue: t }, a) => {
117
+ e.children.forEach((r) => {
118
+ t.sendTo(r, { type: a.visible ? "PARENT.VISIBLE" : "PARENT.HIDDEN" });
119
+ });
120
+ }
121
+ ),
122
+ "Notify children of opacity change": y(({ context: e, enqueue: t }) => {
123
+ e.children.forEach((a) => {
124
+ t.sendTo(a, {
125
+ type: "PARENT.OPACITY_CHANGED",
126
+ opacity: e.computedOpacity
127
+ });
128
+ });
129
+ }),
130
+ "Notify Parent of visibility change": y(({ context: e, enqueue: t }) => {
131
+ e.parentRef && t.sendTo(e.parentRef, {
132
+ type: "CHILD.VISIBLE",
133
+ layerId: e.layerId
134
+ });
135
+ }),
136
+ "Notify Manager of visibility change": y(
137
+ ({ context: e, enqueue: t }, a) => {
138
+ t.sendTo(e.layerManagerRef, {
139
+ type: "LAYER.UPDATE_VISIBILITY",
140
+ layerId: e.layerId,
141
+ visible: a.visible
142
+ });
143
+ }
144
+ ),
145
+ "Change Layer Data": y(({ context: e, enqueue: t }, a) => {
146
+ t.assign({ layerData: a.layerData }), t.sendTo(e.layerManagerRef, {
147
+ type: "LAYER.UPDATE_LAYER_DATA",
148
+ layerId: e.layerId,
149
+ layerData: a.layerData
150
+ });
151
+ }),
152
+ "Update Computed Opacity": y(({ context: e, enqueue: t }) => {
153
+ const a = f(e.parentRef, e.opacity);
154
+ t.assign({
155
+ computedOpacity: a
156
+ }), e.children.forEach((r) => {
157
+ t.sendTo(r, {
158
+ type: "PARENT.OPACITY_CHANGED",
159
+ opacity: a
160
+ });
161
+ }), t.sendTo(e.layerManagerRef, {
162
+ type: "LAYER.UPDATE_OPACITY",
163
+ layerId: e.layerId,
164
+ opacity: e.opacity,
165
+ computedOpacity: e.computedOpacity
166
+ });
167
+ }),
168
+ "Change Layer Opacity": y(
169
+ ({ context: e, enqueue: t }, a) => {
170
+ const r = f(e.parentRef, a.opacity);
171
+ t.assign({
172
+ opacity: a.opacity,
173
+ computedOpacity: r
174
+ }), t.sendTo(e.layerManagerRef, {
175
+ type: "LAYER.UPDATE_OPACITY",
176
+ layerId: e.layerId,
177
+ opacity: a.opacity,
178
+ computedOpacity: r
179
+ }), e.children.forEach((i) => {
180
+ t.sendTo(i, {
181
+ type: "PARENT.OPACITY_CHANGED",
182
+ opacity: r
183
+ });
184
+ });
185
+ }
186
+ ),
187
+ "Change Layer Time Info": y(
188
+ ({ context: e, enqueue: t }, a) => {
189
+ t.assign({ timeInfo: a.timeInfo }), t.sendTo(e.layerManagerRef, {
190
+ type: "LAYER.UPDATE_TIME_INFO",
191
+ layerId: e.layerId,
192
+ timeInfo: a.timeInfo
193
+ });
194
+ }
195
+ ),
196
+ "Update Children": E(
197
+ ({ context: e }, t) => {
198
+ const a = g(
199
+ e.childLayerOrder,
200
+ t.child.id,
201
+ t.index,
202
+ t.position
203
+ );
204
+ return {
205
+ children: [...e.children, t.child],
206
+ childLayerOrder: a
207
+ };
208
+ }
209
+ ),
210
+ "Remove Child": E(({ context: e }, t) => ({
211
+ children: e.children.filter((a) => a.id !== t.id),
212
+ childLayerOrder: e.childLayerOrder.filter((a) => a !== t.id)
213
+ }))
214
+ }
215
+ }).createMachine({
216
+ /** @xstate-layout N4IgpgJg5mDOIC5QBsCGBPMAnA4lg9gK4AOAxAMIASAkgDIAiAdAGrUDK1AQrQKIDaABgC6iUMXywAlgBdJ+AHaiQAD0QBOABwAmRmoCMAgCwa1mjQGYArAHYNAGhDp1jPRu221WgVssDLPgF8AhzRMXAISUloAQQBNHgAlNkZo+noAfSo6ekERJBBxKVkFJVUEa3M1XT0tDWtrY0tDNUMANgcnBCrXd1a9c2tLDT1LNVagkIxsPCIyGPikxgSeAFkAeWYeTJoGXKVCmTlFfLLDY0YK2wFXATcbGw7EEZdzZp99Hz0R14mQUOmIsRGGB5KgAEbISBROKJRj0djRbg8HLCfYSQ4lE6ILT9Ri3NRWNr9ATmcytcyPBADVqMJpfDSteqGLTWPSDX7-cKzYGgiGQRgAN0kUj5pAACtFlgA5AAqjBoaR4Ur2+QOxWOoDK9R01wMagalkqX2slN8hgu+j05Is5NG1g5Uy5JB54MhEEYAAtJBAICDxZKlXLWBwkSqxOj1aVENrdMZWk0NIZrK0tJ5KWcBBavpZGbc9IYbONgn9HTNnRBha6ofNYUrEbwUXlw0UjlGEDjzHiTIS+uYSWSKY5EEnrLTDDU-EMNL5GQ6wmWgRXYFWIBQdkxg1xeGGChHW1j27j8T3iaTyemWXjx3oxq5+t2gsX5PhffB8pyF2iW5jNYgALQ4ro8bmDU5g2imYxaJSXydkYxr9jmwF6HOALciCK5fhiGoqIgYHmrUBbDGy1g+EMlJaK05p0myWikomRhWChTpAuhfLukKIqQphkYHoY5g6ARQzGiRlhkUOVJjGO2bMhYzR+ExC4umxnrer62Fqvuv5dKJjAEa0AgGQYWiGCS6bNIwAwFgY46mDihgKYCjBLhhqp7j+OHtjSJJtHUfTkgMJL2OJpJ6Iw44pgI+pybUWgOdyznKV6PogtxmkeQ0eKvK0vlWqS1hGOm5JhVaN5Wi0Xx8eYj4BEAA */
217
+ id: "layerGroup",
218
+ description: "A machine that represents a collection of layers that can be toggled as a group",
219
+ initial: "disabled",
220
+ context: ({ input: e }) => {
221
+ const t = e.opacity ?? 1, a = f(e.parentRef, t);
222
+ return {
223
+ layerManagerRef: e.layerManagerRef,
224
+ layerId: e.layerId,
225
+ parentRef: e.parentRef,
226
+ children: [],
227
+ childLayerOrder: [],
228
+ layerName: e.layerName,
229
+ layerType: "layerGroup",
230
+ listMode: e.listMode ?? "show",
231
+ timeInfo: e.timeInfo,
232
+ layerData: e.layerData,
233
+ opacity: t,
234
+ computedOpacity: a
235
+ };
236
+ },
237
+ states: {
238
+ enabled: {
239
+ initial: "visible",
240
+ description: "The layer group is enabled",
241
+ states: {
242
+ visible: {
243
+ description: "The layer group should appear visible on the map",
244
+ entry: [
245
+ {
246
+ type: "Notify Parent of visibility change"
247
+ },
248
+ {
249
+ type: "Notify children of visibility change",
250
+ params: { visible: !0 }
251
+ },
252
+ {
253
+ type: "Notify Manager of visibility change",
254
+ params: { visible: !0 }
255
+ }
256
+ ],
257
+ exit: [
258
+ {
259
+ type: "Notify Manager of visibility change",
260
+ params: { visible: !1 }
261
+ },
262
+ {
263
+ type: "Notify children of visibility change",
264
+ params: { visible: !1 }
265
+ }
266
+ ],
267
+ on: {
268
+ "PARENT.HIDDEN": {
269
+ target: "hidden"
270
+ }
271
+ }
272
+ },
273
+ hidden: {
274
+ description: "The layer group should appear hidden on the map as its parent is hidden",
275
+ on: {
276
+ "PARENT.VISIBLE": {
277
+ target: "visible"
278
+ }
279
+ }
280
+ }
281
+ },
282
+ on: {
283
+ "LAYER.DISABLED": {
284
+ target: "disabled"
285
+ }
286
+ }
287
+ },
288
+ disabled: {
289
+ description: "The layer group is disabled",
290
+ initial: "hidden",
291
+ states: {
292
+ hidden: {
293
+ description: "The layer group and its children always appear hidden on the map"
294
+ }
295
+ },
296
+ on: {
297
+ "LAYER.ENABLED": {
298
+ target: "enabled"
299
+ },
300
+ "CHILD.VISIBLE": {
301
+ target: "enabled"
302
+ }
303
+ }
304
+ }
305
+ },
306
+ on: {
307
+ "PARENT.OPACITY_CHANGED": {
308
+ actions: "Update Computed Opacity"
309
+ },
310
+ "CHILD.VISIBLE": {
311
+ actions: "Notify Parent of visibility change"
312
+ },
313
+ "LAYERS.ADD_CHILD": {
314
+ actions: [
315
+ {
316
+ type: "Update Children",
317
+ params: ({ event: e }) => e
318
+ }
319
+ ]
320
+ },
321
+ "LAYERS.REMOVE_CHILD": {
322
+ actions: [
323
+ {
324
+ type: "Remove Child",
325
+ params: ({ event: e }) => e
326
+ }
327
+ ]
328
+ },
329
+ "LAYER.SET_LAYER_DATA": {
330
+ actions: [
331
+ {
332
+ type: "Change Layer Data",
333
+ params: ({ event: e }) => e
334
+ }
335
+ ]
336
+ },
337
+ "LAYER.SET_OPACITY": {
338
+ actions: [
339
+ {
340
+ type: "Change Layer Opacity",
341
+ params: ({ event: e }) => e
342
+ }
343
+ ]
344
+ },
345
+ "LAYER.SET_TIME_INFO": {
346
+ actions: [
347
+ {
348
+ type: "Change Layer Time Info",
349
+ params: ({ event: e }) => e
350
+ }
351
+ ]
352
+ }
353
+ }
354
+ });
355
+ }
356
+ function I(e, t) {
357
+ return L({
358
+ types: {
359
+ context: {},
360
+ events: {},
361
+ input: {}
362
+ },
363
+ actions: {
364
+ "Notify Parent that layer is visible": y(({ context: a, enqueue: r }) => {
365
+ a.parentRef && r.sendTo(a.parentRef, {
366
+ type: "CHILD.VISIBLE",
367
+ layerId: a.layerId
368
+ });
369
+ }),
370
+ "Notify Manager of visibility change": y(
371
+ ({ context: a, enqueue: r }, i) => r.sendTo(a.layerManagerRef, {
372
+ type: "LAYER.UPDATE_VISIBILITY",
373
+ layerId: a.layerId,
374
+ visible: i.visible
375
+ })
376
+ ),
377
+ "Update Computed Opacity": y(({ context: a, enqueue: r }, i) => {
378
+ const o = i.opacity * a.opacity;
379
+ r.assign({
380
+ computedOpacity: o
381
+ }), r.sendTo(a.layerManagerRef, {
382
+ type: "LAYER.UPDATE_OPACITY",
383
+ layerId: a.layerId,
384
+ opacity: a.opacity,
385
+ computedOpacity: o
386
+ });
387
+ }),
388
+ "Change Layer Opacity": y(
389
+ ({ context: a, enqueue: r }, i) => {
390
+ const o = f(a.parentRef, i.opacity);
391
+ r.assign({
392
+ opacity: i.opacity,
393
+ computedOpacity: o
394
+ }), r.sendTo(a.layerManagerRef, {
395
+ type: "LAYER.UPDATE_OPACITY",
396
+ layerId: a.layerId,
397
+ opacity: i.opacity,
398
+ computedOpacity: o
399
+ });
400
+ }
401
+ ),
402
+ "Change Layer Time Info": y(
403
+ ({ context: a, enqueue: r }, i) => {
404
+ r.assign({
405
+ timeInfo: i.timeInfo
406
+ }), r.sendTo(a.layerManagerRef, {
407
+ type: "LAYER.UPDATE_TIME_INFO",
408
+ layerId: a.layerId,
409
+ timeInfo: i.timeInfo
410
+ });
411
+ }
412
+ ),
413
+ "Change Layer Data": y(({ context: a, enqueue: r }, i) => {
414
+ r.assign({ layerData: i.layerData }), r.sendTo(a.layerManagerRef, {
415
+ type: "LAYER.UPDATE_LAYER_DATA",
416
+ layerId: a.layerId,
417
+ layerData: i.layerData
418
+ });
419
+ })
420
+ }
421
+ }).createMachine({
422
+ id: "layer",
423
+ description: "A machine that represents a layer on the map.",
424
+ context: ({ input: a }) => {
425
+ const r = a.opacity ?? 1, i = f(a.parentRef, r);
426
+ return {
427
+ layerManagerRef: a.layerManagerRef,
428
+ parentRef: a.parentRef,
429
+ layerId: a.layerId,
430
+ layerName: a.layerName,
431
+ listMode: a.listMode ?? "show",
432
+ opacity: r,
433
+ computedOpacity: i,
434
+ layerType: "layer",
435
+ timeInfo: a.timeInfo,
436
+ layerData: a.layerData
437
+ };
438
+ },
439
+ initial: e,
440
+ states: {
441
+ enabled: {
442
+ initial: t,
443
+ description: "The layer is enabled",
444
+ states: {
445
+ visible: {
446
+ description: "The layer should appear visible on the map",
447
+ entry: [
448
+ {
449
+ type: "Notify Parent that layer is visible"
450
+ },
451
+ {
452
+ type: "Notify Manager of visibility change",
453
+ params: {
454
+ visible: !0
455
+ }
456
+ }
457
+ ],
458
+ exit: [
459
+ {
460
+ type: "Notify Manager of visibility change",
461
+ params: {
462
+ visible: !1
463
+ }
464
+ }
465
+ ],
466
+ on: {
467
+ "PARENT.HIDDEN": {
468
+ target: "hidden"
469
+ }
470
+ }
471
+ },
472
+ hidden: {
473
+ description: "The layer should appear hidden on the map as its parent is hidden",
474
+ on: {
475
+ "PARENT.VISIBLE": {
476
+ target: "visible"
477
+ }
478
+ }
479
+ }
480
+ },
481
+ on: {
482
+ "LAYER.DISABLED": {
483
+ target: "disabled"
484
+ }
485
+ }
486
+ },
487
+ disabled: {
488
+ description: "The layer is disabled",
489
+ initial: "hidden",
490
+ states: {
491
+ hidden: {
492
+ description: "The layer should always appear hidden on the map"
493
+ }
494
+ },
495
+ on: {
496
+ "LAYER.ENABLED": {
497
+ target: "enabled.visible"
498
+ }
499
+ }
500
+ }
501
+ },
502
+ on: {
503
+ "PARENT.OPACITY_CHANGED": {
504
+ actions: [
505
+ {
506
+ type: "Update Computed Opacity",
507
+ params: ({ event: a }) => a
508
+ }
509
+ ]
510
+ },
511
+ "LAYER.SET_OPACITY": {
512
+ actions: [
513
+ {
514
+ type: "Change Layer Opacity",
515
+ params: ({ event: a }) => a
516
+ }
517
+ ]
518
+ },
519
+ "LAYER.SET_TIME_INFO": {
520
+ actions: [
521
+ {
522
+ type: "Change Layer Time Info",
523
+ params: ({ event: a }) => a
524
+ }
525
+ ]
526
+ },
527
+ "LAYER.SET_LAYER_DATA": {
528
+ actions: [
529
+ {
530
+ type: "Change Layer Data",
531
+ params: ({ event: a }) => a
532
+ }
533
+ ]
534
+ }
535
+ }
536
+ });
537
+ }
538
+ function w() {
539
+ return L({
540
+ types: {
541
+ context: {},
542
+ events: {},
543
+ emitted: {},
544
+ input: {}
545
+ },
546
+ actors: {
547
+ layerMachineEnabledVisible: I("enabled", "visible"),
548
+ layerMachineEnabledHidden: I("enabled", "hidden"),
549
+ layerMachineDisabled: I("disabled", "hidden"),
550
+ layerGroupMachine: B()
551
+ },
552
+ actions: {
553
+ "Add new layer": y(({ enqueue: e, context: t, self: a }, r) => {
554
+ const { layerConfig: i, index: o, visible: l, enabled: s, position: n } = r, d = D(t.layers, i);
555
+ if (!C(i, d))
556
+ return;
557
+ function R(c, p) {
558
+ return p || c && !d ? "layerMachineEnabledVisible" : c && d && d.getSnapshot().value && d.getSnapshot().matches({ disabled: "hidden" }) ? "layerMachineEnabledHidden" : "layerMachineDisabled";
559
+ }
560
+ e.assign(({ spawn: c }) => {
561
+ let p;
562
+ if (i.layerType === "layerGroup") {
563
+ const A = c("layerGroupMachine", {
564
+ id: i.layerId,
565
+ input: {
566
+ layerManagerRef: a,
567
+ parentRef: d,
568
+ ...i
569
+ }
570
+ });
571
+ l && A.send({ type: "LAYER.ENABLED" }), p = {
572
+ type: "layerGroup",
573
+ layerActor: A
574
+ };
575
+ } else
576
+ p = {
577
+ type: "layer",
578
+ layerActor: c(R(s ?? !1, l ?? !1), {
579
+ id: i.layerId,
580
+ input: {
581
+ layerManagerRef: a,
582
+ parentRef: d,
583
+ ...i
584
+ }
585
+ })
586
+ };
587
+ return P(t, p, d, o, n);
588
+ }), e.emit({
589
+ type: "LAYER.ADDED",
590
+ layerId: i.layerId,
591
+ visible: l ?? !1
592
+ });
593
+ }),
594
+ "Remove layer": y(({ enqueue: e, context: t }, a) => {
595
+ const { layerId: r } = a, i = u(t.layers, r);
596
+ !i || !M(i) || (G(i), e.stopChild(r), e.assign(() => V(t, r)), e.emit({ type: "LAYER.REMOVED", layerId: r }));
597
+ }),
598
+ "Emit update layer order": h(({ context: e }) => ({
599
+ type: "LAYER.ORDER_CHANGED",
600
+ layerOrder: S(e.layers, e.childLayerOrder)
601
+ })),
602
+ // Reset actions
603
+ "Reset layer manager": E(({ context: e }) => (e.layers.forEach((t) => {
604
+ T(t.layerActor.id);
605
+ }), {
606
+ layers: [],
607
+ childLayerOrder: []
608
+ }))
609
+ },
610
+ guards: {
611
+ isValidLayerConfig: ({ context: e }, t) => Y(t.layerConfig, e)
612
+ }
613
+ }).createMachine({
614
+ id: "layerManager",
615
+ context: ({ input: e }) => ({
616
+ layers: [],
617
+ childLayerOrder: [],
618
+ allowNestedGroupLayers: e.allowNestedGroupLayers
619
+ }),
620
+ on: {
621
+ "LAYER.UPDATE_VISIBILITY": {
622
+ actions: h(({ event: e }) => ({
623
+ type: "LAYER.VISIBILITY_CHANGED",
624
+ layerId: e.layerId,
625
+ visible: e.visible
626
+ }))
627
+ },
628
+ "LAYER.UPDATE_OPACITY": {
629
+ actions: [
630
+ h(({ event: e }) => ({
631
+ type: "LAYER.OPACITY_CHANGED",
632
+ layerId: e.layerId,
633
+ opacity: e.opacity,
634
+ computedOpacity: e.computedOpacity
635
+ }))
636
+ ]
637
+ },
638
+ "LAYER.UPDATE_TIME_INFO": {
639
+ actions: h(({ event: e }) => ({
640
+ type: "LAYER.TIME_INFO_CHANGED",
641
+ layerId: e.layerId,
642
+ timeInfo: e.timeInfo
643
+ }))
644
+ },
645
+ "LAYER.UPDATE_LAYER_DATA": {
646
+ actions: h(({ event: e }) => ({
647
+ type: "LAYER.LAYER_DATA_CHANGED",
648
+ layerId: e.layerId,
649
+ layerData: e.layerData
650
+ }))
651
+ },
652
+ "LAYER.ADD": {
653
+ guard: {
654
+ type: "isValidLayerConfig",
655
+ params: ({ event: e }) => e.params
656
+ },
657
+ actions: [
658
+ {
659
+ type: "Add new layer",
660
+ params: ({ event: e }) => e.params
661
+ },
662
+ {
663
+ type: "Emit update layer order"
664
+ }
665
+ ]
666
+ },
667
+ "LAYER.REMOVE": {
668
+ actions: [
669
+ {
670
+ type: "Remove layer",
671
+ params: ({ event: e }) => e
672
+ },
673
+ {
674
+ type: "Emit update layer order"
675
+ }
676
+ ]
677
+ },
678
+ RESET: {
679
+ actions: ["Reset layer manager"]
680
+ }
681
+ }
682
+ });
683
+ }
684
+ class J {
685
+ _actor;
686
+ _options;
687
+ _adapter = null;
688
+ /** Cleanup functions for each active XState actor event subscription. */
689
+ _subscriptions = [];
690
+ _destroyed = !1;
691
+ constructor(t = {}) {
692
+ this._options = t, this._actor = _(w(), {
693
+ input: { allowNestedGroupLayers: this._options.allowNestedGroupLayers ?? !1 }
694
+ }), this.start();
695
+ }
696
+ // --------------------------------------------------------------------------
697
+ // State
698
+ // --------------------------------------------------------------------------
699
+ /**
700
+ * Escape hatch for `@xstate/react` integration (e.g. `useSelector`).
701
+ * Prefer the typed methods on this class for all other use cases.
702
+ */
703
+ get actor() {
704
+ return this._actor;
705
+ }
706
+ /** `true` while the underlying XState actor is running (i.e. not yet stopped). */
707
+ get isReady() {
708
+ return this._actor.getSnapshot().status === "active";
709
+ }
710
+ /** `true` after {@link destroy} has been called. The instance cannot be reused once destroyed. */
711
+ get destroyed() {
712
+ return this._destroyed;
713
+ }
714
+ /** The current top-level layers in their display order. */
715
+ get layers() {
716
+ const { childLayerOrder: t, layers: a } = this._actor.getSnapshot().context;
717
+ return v(t, a);
718
+ }
719
+ /** Returns the managed item for `id`, or `undefined` if it does not exist. */
720
+ getLayer(t) {
721
+ const { layers: a } = this._actor.getSnapshot().context;
722
+ return u(a, t);
723
+ }
724
+ // --------------------------------------------------------------------------
725
+ // Lifecycle
726
+ // --------------------------------------------------------------------------
727
+ /** Starts the XState actor, wires event subscriptions, and registers any pending adapter. */
728
+ start() {
729
+ this._actor.start(), this._wireSubscriptions(), this._adapter && this._registerAdapter(this._adapter);
730
+ }
731
+ /** Alias for {@link destroy}. */
732
+ stop() {
733
+ this.destroy();
734
+ }
735
+ /** Resets layer state, unregisters the adapter, cancels all subscriptions, and stops the actor. */
736
+ destroy() {
737
+ this._cleanupSubscriptions(), this._adapter?.unregister?.(), this.reset(), this._actor.stop(), this._destroyed = !0;
738
+ }
739
+ /** Removes all layers and groups, returning the manager to its initial empty state. */
740
+ reset() {
741
+ this._actor.send({ type: "RESET" });
742
+ }
743
+ // --------------------------------------------------------------------------
744
+ // Adapter
745
+ // --------------------------------------------------------------------------
746
+ /**
747
+ * Attach or replace the adapter at any time.
748
+ * Pass `null` to detach the current adapter.
749
+ */
750
+ setAdapter(t) {
751
+ this._adapter?.unregister?.(), this._adapter = t, t && this.isReady && this._registerAdapter(t);
752
+ }
753
+ // --------------------------------------------------------------------------
754
+ // Layer operations
755
+ // --------------------------------------------------------------------------
756
+ /** Adds a single layer to the manager. */
757
+ addLayer(t) {
758
+ this._actor.send({ type: "LAYER.ADD", params: t });
759
+ }
760
+ /** Adds a layer group to the manager. */
761
+ addGroup(t) {
762
+ this._actor.send({ type: "LAYER.ADD", params: t });
763
+ }
764
+ /** Removes the layer or group with the given `layerId`. */
765
+ removeLayer(t) {
766
+ this._actor.send({ type: "LAYER.REMOVE", layerId: t });
767
+ }
768
+ /** Shows or hides the layer with the given `layerId`. */
769
+ setVisibility(t, a) {
770
+ const r = this.getLayer(t);
771
+ r && r.layerActor.send(a ? { type: "LAYER.ENABLED" } : { type: "LAYER.DISABLED" });
772
+ }
773
+ /** Alias for {@link setVisibility}. */
774
+ setEnabled(t, a) {
775
+ this.setVisibility(t, a);
776
+ }
777
+ /** Sets the opacity (0–1) for the layer with the given `layerId`. */
778
+ setOpacity(t, a) {
779
+ const r = this.getLayer(t);
780
+ r && r.layerActor.send({ type: "LAYER.SET_OPACITY", opacity: a });
781
+ }
782
+ /** Sets the time info for the layer with the given `layerId`. */
783
+ setTimeInfo(t, a) {
784
+ const r = this.getLayer(t);
785
+ r && r.layerActor.send({ type: "LAYER.SET_TIME_INFO", timeInfo: a });
786
+ }
787
+ /** Replaces the `layerData` payload for the layer or group with the given `layerId`. */
788
+ updateLayerData(t, a) {
789
+ const r = this.getLayer(t);
790
+ r && r.layerActor.send({ type: "LAYER.SET_LAYER_DATA", layerData: a });
791
+ }
792
+ // --------------------------------------------------------------------------
793
+ // Private helpers
794
+ // --------------------------------------------------------------------------
795
+ /** Subscribes to machine-emitted events and forwards them to the adapter and options callbacks. */
796
+ _wireSubscriptions() {
797
+ const t = this._actor.on("LAYER.ADDED", (s) => {
798
+ const n = this._toManagedLayerInfo(s.layerId, s.visible);
799
+ n && (this._adapter?.onLayerAdded?.(n), this._options.onLayerAdded?.(n));
800
+ });
801
+ this._subscriptions.push(() => t.unsubscribe());
802
+ const a = this._actor.on("LAYER.REMOVED", (s) => {
803
+ this._adapter?.onLayerRemoved?.(s.layerId), this._options.onLayerRemoved?.(s.layerId);
804
+ });
805
+ this._subscriptions.push(() => a.unsubscribe());
806
+ const r = this._actor.on("LAYER.VISIBILITY_CHANGED", (s) => {
807
+ const n = this._toManagedLayerInfo(s.layerId, s.visible);
808
+ n && (this._adapter?.onVisibilityChanged?.(n, s.visible), this._options.onVisibilityChanged?.(n, s.visible));
809
+ });
810
+ this._subscriptions.push(() => r.unsubscribe());
811
+ const i = this._actor.on("LAYER.OPACITY_CHANGED", (s) => {
812
+ const n = this._toManagedLayerInfo(s.layerId);
813
+ n && (this._adapter?.onOpacityChanged?.(n, s.computedOpacity), this._options.onOpacityChanged?.(n, s.computedOpacity));
814
+ });
815
+ this._subscriptions.push(() => i.unsubscribe());
816
+ const o = this._actor.on("LAYER.TIME_INFO_CHANGED", (s) => {
817
+ const n = this._toManagedLayerInfo(s.layerId);
818
+ n && (this._adapter?.onTimeInfoChanged?.(n, s.timeInfo), this._options.onTimeInfoChanged?.(n, s.timeInfo));
819
+ });
820
+ this._subscriptions.push(() => o.unsubscribe());
821
+ const l = this._actor.on("LAYER.LAYER_DATA_CHANGED", (s) => {
822
+ const n = this._toManagedLayerInfo(s.layerId);
823
+ n && this._adapter?.onLayerDataChanged?.(n);
824
+ });
825
+ this._subscriptions.push(() => l.unsubscribe());
826
+ }
827
+ _cleanupSubscriptions() {
828
+ for (const t of this._subscriptions)
829
+ t();
830
+ this._subscriptions.length = 0;
831
+ }
832
+ _registerAdapter(t) {
833
+ const a = {
834
+ getSnapshot: () => this.layers,
835
+ getLayer: (r) => this.getLayer(r)
836
+ };
837
+ t.register?.(this, a);
838
+ }
839
+ /**
840
+ * Converts an internal XState actor context into the stable
841
+ * {@link ManagedLayerInfo} shape exposed to adapters and option callbacks.
842
+ * The `isLayerMachine` branch is required for TypeScript to narrow `ctx`
843
+ * to the correct discriminated union member.
844
+ */
845
+ _toManagedLayerInfo(t, a) {
846
+ const { layers: r } = this._actor.getSnapshot().context, i = u(r, t);
847
+ if (!i)
848
+ return null;
849
+ const o = i.layerActor.getSnapshot(), l = o.matches("enabled"), s = a ?? o.matches({ enabled: "visible" });
850
+ if (b(i.layerActor)) {
851
+ const n = i.layerActor.getSnapshot().context;
852
+ return {
853
+ layerId: n.layerId,
854
+ layerName: n.layerName,
855
+ layerType: "layer",
856
+ layerData: n.layerData,
857
+ opacity: n.opacity,
858
+ computedOpacity: n.computedOpacity,
859
+ enabled: l,
860
+ visible: s,
861
+ timeInfo: n.timeInfo,
862
+ listMode: n.listMode,
863
+ parentId: n.parentRef?.getSnapshot().context.layerId ?? null
864
+ };
865
+ } else {
866
+ const n = i.layerActor.getSnapshot().context;
867
+ return {
868
+ layerId: n.layerId,
869
+ layerName: n.layerName,
870
+ layerType: "layerGroup",
871
+ layerData: n.layerData,
872
+ opacity: n.opacity,
873
+ computedOpacity: n.computedOpacity,
874
+ enabled: l,
875
+ visible: s,
876
+ timeInfo: n.timeInfo,
877
+ listMode: n.listMode,
878
+ parentId: n.parentRef?.getSnapshot().context.layerId ?? null
879
+ };
880
+ }
881
+ }
882
+ }
883
+ export {
884
+ J as LayerManager,
885
+ f as calculateComputedOpacity,
886
+ M as canRemoveLayer,
887
+ G as cleanupLayerReferences,
888
+ w as createLayerManagerMachine,
889
+ u as findManagedLayerById,
890
+ D as findParentActor,
891
+ O as findParentLayerGroupActor,
892
+ S as getFlatLayerOrder,
893
+ j as getLayerDataFromLayerId,
894
+ z as getLayerGroupChildrenInOrder,
895
+ v as getTopLevelLayersInOrder,
896
+ P as getUpdatedLayerStructure,
897
+ V as getUpdatedLayerStructureAfterRemoval,
898
+ m as isLayerGroupMachine,
899
+ b as isLayerMachine,
900
+ $ as isRangeTimeInfo,
901
+ k as isSingleTimeInfo,
902
+ Y as isValidLayerConfig,
903
+ N as isValidLayerIndex,
904
+ C as isValidParentRef,
905
+ B as layerGroupMachine,
906
+ I as layerMachine,
907
+ g as updateLayerOrder
908
+ };