vehicle-path2 1.0.7 → 1.0.9

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.
@@ -0,0 +1,944 @@
1
+ import { useState as _, useCallback as p, useRef as A, useMemo as G, useEffect as K, createContext as ue, useContext as le } from "react";
2
+ import { distance as fe, initializeAllVehicles as te, buildGraph as de, updateAxlePosition as re, handleArrival as me, prepareCommandPath as B, calculateFrontAxlePosition as se, initializeMovingVehicle as ge, createInitialMovementState as he } from "./core.js";
3
+ import { v as N, p as ve, a as W, f as pe, h as Pe, e as Ie, V as ne } from "./vehicle-helpers-BYb2Ibex.js";
4
+ import { jsx as xe } from "react/jsx-runtime";
5
+ function J(t) {
6
+ return Array.isArray(t) ? { x: t[0], y: t[1] } : t;
7
+ }
8
+ function H(t) {
9
+ return {
10
+ id: t.id,
11
+ start: J(t.start),
12
+ end: J(t.end)
13
+ };
14
+ }
15
+ function U(t) {
16
+ const o = t.fromIsPercentage !== !1, V = t.toIsPercentage !== !1;
17
+ return {
18
+ fromLineId: t.from,
19
+ toLineId: t.to,
20
+ // No conversion needed - internal format is now 0-1 (same as API)
21
+ fromOffset: t.fromPosition,
22
+ fromIsPercentage: t.fromPosition !== void 0 ? o : void 0,
23
+ toOffset: t.toPosition,
24
+ toIsPercentage: t.toPosition !== void 0 ? V : void 0
25
+ };
26
+ }
27
+ function Y(t) {
28
+ const o = t.position ?? 0, V = t.isPercentage !== !1;
29
+ return {
30
+ vehicleId: t.id,
31
+ lineId: t.lineId,
32
+ // No conversion needed - internal format is now 0-1 (same as API)
33
+ offset: o,
34
+ isPercentage: V
35
+ };
36
+ }
37
+ function Z(t) {
38
+ const o = t.isPercentage !== !1, V = t.targetPosition ?? 1;
39
+ return {
40
+ vehicleId: t.vehicleId,
41
+ targetLineId: t.targetLineId,
42
+ // No conversion needed - internal format is now 0-1 (same as API)
43
+ targetOffset: V,
44
+ isPercentage: o,
45
+ awaitConfirmation: t.wait,
46
+ payload: t.payload
47
+ };
48
+ }
49
+ function Me(t) {
50
+ const o = [], V = /* @__PURE__ */ new Set();
51
+ for (const r of t.lines)
52
+ V.has(r.id) && o.push(`Duplicate line ID: ${r.id}`), V.add(r.id);
53
+ if (t.connections)
54
+ for (const r of t.connections) {
55
+ V.has(r.from) || o.push(`Connection references non-existent line: ${r.from}`), V.has(r.to) || o.push(`Connection references non-existent line: ${r.to}`);
56
+ const m = r.fromIsPercentage !== !1, e = r.toIsPercentage !== !1;
57
+ !m && r.fromPosition === void 0 && o.push("fromPosition is required when fromIsPercentage is false"), !e && r.toPosition === void 0 && o.push("toPosition is required when toIsPercentage is false"), r.fromPosition !== void 0 && (m && (r.fromPosition < 0 || r.fromPosition > 1) ? o.push(`Invalid fromPosition: ${r.fromPosition} (must be 0-1 for percentage)`) : !m && r.fromPosition < 0 && o.push(`Invalid fromPosition: ${r.fromPosition} (must be >= 0 for absolute distance)`)), r.toPosition !== void 0 && (e && (r.toPosition < 0 || r.toPosition > 1) ? o.push(`Invalid toPosition: ${r.toPosition} (must be 0-1 for percentage)`) : !e && r.toPosition < 0 && o.push(`Invalid toPosition: ${r.toPosition} (must be >= 0 for absolute distance)`));
58
+ }
59
+ return { valid: o.length === 0, errors: o };
60
+ }
61
+ function oe() {
62
+ const [t, o] = _([]), [V, r] = _([]), [m, e] = _(null), f = p((s, v) => {
63
+ o(s), r(v), e(null);
64
+ }, []), M = p((s) => {
65
+ const v = Me(s);
66
+ if (!v.valid)
67
+ return e(v.errors.join("; ")), { success: !1, errors: v.errors };
68
+ const a = s.lines.map(H), h = s.connections?.map(U) || [];
69
+ return f(a, h), { success: !0 };
70
+ }, [f]), O = p((s) => {
71
+ if (t.some((a) => a.id === s.id)) {
72
+ const a = `Line with ID '${s.id}' already exists`;
73
+ return e(a), { success: !1, error: a };
74
+ }
75
+ return o((a) => [...a, H(s)]), e(null), { success: !0 };
76
+ }, [t]), T = p((s, v) => {
77
+ if (t.findIndex((h) => h.id === s) === -1) {
78
+ const h = `Line with ID '${s}' not found`;
79
+ return e(h), { success: !1, error: h };
80
+ }
81
+ return o((h) => h.map((C) => C.id !== s ? C : {
82
+ ...C,
83
+ start: v.start ? J(v.start) : C.start,
84
+ end: v.end ? J(v.end) : C.end
85
+ })), e(null), { success: !0 };
86
+ }, [t]), y = p((s) => {
87
+ if (!t.some((a) => a.id === s)) {
88
+ const a = `Line with ID '${s}' not found`;
89
+ return e(a), { success: !1, error: a };
90
+ }
91
+ return o((a) => a.filter((h) => h.id !== s)), r((a) => a.filter((h) => h.fromLineId !== s && h.toLineId !== s)), e(null), { success: !0 };
92
+ }, [t]), L = p((s) => {
93
+ const v = t.some((d) => d.id === s.from), a = t.some((d) => d.id === s.to);
94
+ if (!v) {
95
+ const d = `Line '${s.from}' not found`;
96
+ return e(d), { success: !1, error: d };
97
+ }
98
+ if (!a) {
99
+ const d = `Line '${s.to}' not found`;
100
+ return e(d), { success: !1, error: d };
101
+ }
102
+ const h = s.fromIsPercentage !== !1, C = s.toIsPercentage !== !1;
103
+ if (!h && s.fromPosition === void 0) {
104
+ const d = "fromPosition is required when fromIsPercentage is false";
105
+ return e(d), { success: !1, error: d };
106
+ }
107
+ if (!C && s.toPosition === void 0) {
108
+ const d = "toPosition is required when toIsPercentage is false";
109
+ return e(d), { success: !1, error: d };
110
+ }
111
+ if (V.some((d) => d.fromLineId === s.from && d.toLineId === s.to)) {
112
+ const d = `Connection from '${s.from}' to '${s.to}' already exists`;
113
+ return e(d), { success: !1, error: d };
114
+ }
115
+ return r((d) => [...d, U(s)]), e(null), { success: !0 };
116
+ }, [t, V]), P = p((s, v, a) => {
117
+ const h = V.findIndex((l) => l.fromLineId === s && l.toLineId === v);
118
+ if (h === -1) {
119
+ const l = `Connection from '${s}' to '${v}' not found`;
120
+ return e(l), { success: !1, error: l };
121
+ }
122
+ const C = V[h], Q = a.fromIsPercentage ?? C.fromIsPercentage, d = a.toIsPercentage ?? C.toIsPercentage;
123
+ let D;
124
+ a.fromOffset !== void 0 ? D = a.fromOffset : C.fromOffset !== void 0 && (D = C.fromOffset);
125
+ let $;
126
+ if (a.toOffset !== void 0 ? $ = a.toOffset : C.toOffset !== void 0 && ($ = C.toOffset), D !== void 0) {
127
+ if (Q !== !1 && (D < 0 || D > 1)) {
128
+ const l = `Invalid fromOffset: ${D} (must be 0-1 for percentage)`;
129
+ return e(l), { success: !1, error: l };
130
+ }
131
+ if (Q === !1 && D < 0) {
132
+ const l = `Invalid fromOffset: ${D} (must be >= 0 for absolute distance)`;
133
+ return e(l), { success: !1, error: l };
134
+ }
135
+ }
136
+ if ($ !== void 0) {
137
+ if (d !== !1 && ($ < 0 || $ > 1)) {
138
+ const l = `Invalid toOffset: ${$} (must be 0-1 for percentage)`;
139
+ return e(l), { success: !1, error: l };
140
+ }
141
+ if (d === !1 && $ < 0) {
142
+ const l = `Invalid toOffset: ${$} (must be >= 0 for absolute distance)`;
143
+ return e(l), { success: !1, error: l };
144
+ }
145
+ }
146
+ if (Q === !1 && D === void 0) {
147
+ const l = "fromOffset is required when fromIsPercentage is false";
148
+ return e(l), { success: !1, error: l };
149
+ }
150
+ if (d === !1 && $ === void 0) {
151
+ const l = "toOffset is required when toIsPercentage is false";
152
+ return e(l), { success: !1, error: l };
153
+ }
154
+ const k = {
155
+ from: s,
156
+ to: v,
157
+ fromPosition: D,
158
+ fromIsPercentage: Q,
159
+ toPosition: $,
160
+ toIsPercentage: d
161
+ };
162
+ return r((l) => l.map(
163
+ (n, i) => i === h ? U(k) : n
164
+ )), e(null), { success: !0 };
165
+ }, [V]), I = p((s, v) => {
166
+ if (!V.some((h) => h.fromLineId === s && h.toLineId === v)) {
167
+ const h = `Connection from '${s}' to '${v}' not found`;
168
+ return e(h), { success: !1, error: h };
169
+ }
170
+ return r((h) => h.filter((C) => !(C.fromLineId === s && C.toLineId === v))), e(null), { success: !0 };
171
+ }, [V]), c = p(() => {
172
+ o([]), r([]), e(null);
173
+ }, []);
174
+ return {
175
+ lines: t,
176
+ curves: V,
177
+ setScene: M,
178
+ addLine: O,
179
+ updateLine: T,
180
+ removeLine: y,
181
+ addConnection: L,
182
+ updateConnection: P,
183
+ removeConnection: I,
184
+ clear: c,
185
+ error: m,
186
+ _loadScene: f
187
+ };
188
+ }
189
+ function ce({ lines: t, wheelbase: o }) {
190
+ const [V, r] = _([]), [m, e] = _(null), f = A([]), M = p((P) => {
191
+ f.current = P, r(P), e(null);
192
+ }, []), O = p((P) => {
193
+ const I = Array.isArray(P) ? P : [P], c = [];
194
+ for (const h of I)
195
+ f.current.some((Q) => Q.id === h.id) && c.push(`Vehicle with ID '${h.id}' already exists`);
196
+ if (c.length > 0)
197
+ return e(c.join("; ")), { success: !1, errors: c };
198
+ const s = I.map(Y), { vehicles: v, errors: a } = N(
199
+ s,
200
+ t,
201
+ o
202
+ );
203
+ return a.length > 0 ? (e(a.join("; ")), { success: !1, errors: a }) : (f.current = [...f.current, ...v], r(f.current), e(null), { success: !0 });
204
+ }, [t, o]), T = p((P, I) => {
205
+ const c = f.current.findIndex(($) => $.id === P);
206
+ if (c === -1) {
207
+ const $ = `Vehicle with ID '${P}' not found`;
208
+ return e($), { success: !1, error: $ };
209
+ }
210
+ const s = f.current[c];
211
+ if (s.state !== "idle") {
212
+ const $ = `Cannot update vehicle '${P}' while it is ${s.state}. Vehicle must be idle.`;
213
+ return e($), { success: !1, error: $ };
214
+ }
215
+ const v = I.lineId ?? s.lineId;
216
+ if (!t.find(($) => $.id === v)) {
217
+ const $ = `Line '${v}' not found`;
218
+ return e($), { success: !1, error: $ };
219
+ }
220
+ let h, C;
221
+ I.lineId !== void 0 && I.position === void 0 ? (h = 0, C = !0) : I.position !== void 0 ? (h = I.position, C = I.isPercentage ?? !0) : (h = s.offset, C = s.isPercentage);
222
+ const Q = {
223
+ vehicleId: P,
224
+ lineId: v,
225
+ offset: h,
226
+ isPercentage: C
227
+ }, { vehicles: d, errors: D } = N(
228
+ [Q],
229
+ t,
230
+ o
231
+ );
232
+ return D.length > 0 ? (e(D.join("; ")), { success: !1, error: D.join("; ") }) : (f.current = f.current.map(
233
+ ($, k) => k === c ? d[0] : $
234
+ ), r(f.current), e(null), { success: !0 });
235
+ }, [t, o]), y = p((P) => {
236
+ if (!f.current.some((c) => c.id === P)) {
237
+ const c = `Vehicle with ID '${P}' not found`;
238
+ return e(c), { success: !1, error: c };
239
+ }
240
+ return f.current = f.current.filter((c) => c.id !== P), r(f.current), e(null), { success: !0 };
241
+ }, []), L = p(() => {
242
+ f.current = [], r([]), e(null);
243
+ }, []);
244
+ return {
245
+ vehicles: V,
246
+ addVehicles: O,
247
+ updateVehicle: T,
248
+ removeVehicle: y,
249
+ clear: L,
250
+ error: m,
251
+ _loadVehicles: M
252
+ };
253
+ }
254
+ function ie({ vehicles: t, lines: o }) {
255
+ const [V, r] = _(/* @__PURE__ */ new Map()), [m, e] = _(null), f = A(/* @__PURE__ */ new Map()), M = p(() => f.current, []), O = p((L) => {
256
+ f.current = L, r(L), e(null);
257
+ }, []), T = p((L, P) => {
258
+ if (!t.some((d) => d.id === L)) {
259
+ const d = `Vehicle '${L}' not found`;
260
+ return e(d), { success: !1, error: d };
261
+ }
262
+ const c = o.find((d) => d.id === P.targetLineId);
263
+ if (!c) {
264
+ const d = `Line '${P.targetLineId}' not found`;
265
+ return e(d), { success: !1, error: d };
266
+ }
267
+ const s = P.isPercentage !== !1, v = fe(c.start, c.end);
268
+ if (!s && P.targetPosition === void 0) {
269
+ const d = "targetPosition is required when isPercentage is false";
270
+ return e(d), { success: !1, error: d };
271
+ }
272
+ const a = P.targetPosition ?? 1;
273
+ if (s) {
274
+ if (a < 0 || a > 1) {
275
+ const d = `Invalid targetPosition: ${a} (must be 0-1 for percentage)`;
276
+ return e(d), { success: !1, error: d };
277
+ }
278
+ } else {
279
+ if (a < 0) {
280
+ const d = `Invalid targetPosition: ${a} (must be >= 0 for absolute distance)`;
281
+ return e(d), { success: !1, error: d };
282
+ }
283
+ if (a > v) {
284
+ const d = `Position ${a} exceeds line length ${v}`;
285
+ return e(d), { success: !1, error: d };
286
+ }
287
+ }
288
+ const h = Z({ vehicleId: L, ...P }), C = new Map(f.current), Q = C.get(L) || [];
289
+ return C.set(L, [...Q, h]), f.current = C, r(C), e(null), { success: !0 };
290
+ }, [t, o]), y = p((L) => {
291
+ if (L !== void 0) {
292
+ if (!t.some((c) => c.id === L)) {
293
+ const c = `Vehicle '${L}' not found`;
294
+ return e(c), { success: !1, error: c };
295
+ }
296
+ const I = new Map(f.current);
297
+ I.delete(L), f.current = I, r(I);
298
+ } else
299
+ f.current = /* @__PURE__ */ new Map(), r(/* @__PURE__ */ new Map());
300
+ return e(null), { success: !0 };
301
+ }, [t]);
302
+ return {
303
+ vehicleQueues: V,
304
+ getVehicleQueues: M,
305
+ queueMovement: T,
306
+ clearQueue: y,
307
+ error: m,
308
+ _loadQueues: O
309
+ };
310
+ }
311
+ function Ve({
312
+ vehicles: t,
313
+ lines: o,
314
+ vehicleQueues: V,
315
+ getVehicleQueues: r,
316
+ wheelbase: m,
317
+ tangentMode: e,
318
+ curves: f,
319
+ eventEmitter: M
320
+ }) {
321
+ const [O, T] = _([]), y = A([]), L = p((l) => {
322
+ typeof l == "function" ? T((n) => {
323
+ const i = l(n);
324
+ return y.current = i, i;
325
+ }) : (y.current = l, T(l));
326
+ }, []), P = p(() => y.current, []), I = G(() => ({
327
+ wheelbase: m,
328
+ tangentMode: e
329
+ }), [m, e]), c = G(
330
+ () => new Map(o.map((l) => [l.id, l])),
331
+ [o]
332
+ ), s = A(/* @__PURE__ */ new Map());
333
+ K(() => {
334
+ const { movingVehicles: l, stateMap: n } = te(t, c);
335
+ s.current = n;
336
+ const i = setTimeout(() => {
337
+ L(l);
338
+ }, 0);
339
+ return () => clearTimeout(i);
340
+ }, [t, c]);
341
+ const v = A(null);
342
+ K(() => {
343
+ o.length > 0 && (v.current = de(o, f, I));
344
+ }, [o, f, I]);
345
+ const a = A(0), h = A(!1), C = p((l) => {
346
+ if (!h.current) return !1;
347
+ let n = !1;
348
+ for (const [, u] of s.current)
349
+ if (u.vehicle.state === "moving") {
350
+ n = !0;
351
+ break;
352
+ }
353
+ if (!n)
354
+ return !1;
355
+ const i = [];
356
+ for (const [u, g] of s.current) {
357
+ if (g.vehicle.state !== "moving" || !g.execution) continue;
358
+ const x = g.execution;
359
+ let S;
360
+ if (x.front.currentSegmentIndex < x.path.segments.length) {
361
+ const j = x.path.segments[x.front.currentSegmentIndex];
362
+ if (j.type === "line") {
363
+ const w = c.get(j.lineId);
364
+ w && (S = Math.sqrt(
365
+ Math.pow(w.end.x - w.start.x, 2) + Math.pow(w.end.y - w.start.y, 2)
366
+ ));
367
+ }
368
+ }
369
+ const b = re(
370
+ g.vehicle.rear,
371
+ x.rear,
372
+ x.path,
373
+ l,
374
+ c,
375
+ x.curveDataMap
376
+ ), q = re(
377
+ g.vehicle.front,
378
+ x.front,
379
+ x.path,
380
+ l,
381
+ c,
382
+ x.curveDataMap,
383
+ S
384
+ );
385
+ if (g.vehicle = {
386
+ ...g.vehicle,
387
+ rear: b.axleState,
388
+ front: q.axleState
389
+ }, g.execution.rear = b.execution, g.execution.front = q.execution, b.completed) {
390
+ const w = me(g, {
391
+ linesMap: c,
392
+ config: I,
393
+ vehicleQueues: V,
394
+ curves: f,
395
+ graphRef: v,
396
+ prepareCommandPath: B,
397
+ onCommandComplete: (F) => i.push({ type: "commandComplete", data: F }),
398
+ onCommandStart: (F) => i.push({ type: "commandStart", data: F })
399
+ });
400
+ g.vehicle = w.vehicle, w.newExecution !== void 0 && (g.execution = w.newExecution), w.vehicle.state !== "moving" && i.push({
401
+ type: "stateChange",
402
+ data: { vehicleId: u, from: "moving", to: w.vehicle.state }
403
+ });
404
+ const E = w.vehicle.rear.position, R = w.vehicle.front.position;
405
+ i.push({
406
+ type: "positionUpdate",
407
+ data: {
408
+ vehicleId: u,
409
+ rear: E,
410
+ front: R,
411
+ center: { x: (E.x + R.x) / 2, y: (E.y + R.y) / 2 },
412
+ angle: Math.atan2(R.y - E.y, R.x - E.x)
413
+ }
414
+ });
415
+ }
416
+ }
417
+ if (L((u) => u.map((g) => {
418
+ const x = s.current.get(g.id);
419
+ return x ? x.vehicle : g;
420
+ })), M && i.length > 0) {
421
+ const u = a.current;
422
+ setTimeout(() => {
423
+ a.current === u && i.forEach(({ type: g, data: x }) => {
424
+ M.emit(g, x);
425
+ });
426
+ }, 0);
427
+ }
428
+ for (const [, u] of s.current)
429
+ if (u.vehicle.state === "moving")
430
+ return !0;
431
+ return h.current = !1, !1;
432
+ }, [c, f, I, V, M]), Q = p(() => {
433
+ const l = v.current;
434
+ if (!l) return !1;
435
+ const n = r ? r() : V, i = [];
436
+ let u = !1;
437
+ for (const [g, x] of s.current) {
438
+ const S = x.vehicle;
439
+ if (S.state === "moving") continue;
440
+ const b = n.get(g);
441
+ if (!b || b.length === 0) continue;
442
+ const q = b[0], w = B(S, q, { graph: l, linesMap: c, curves: f, config: I });
443
+ if (!w) {
444
+ console.warn(`No path found for vehicle ${g}`);
445
+ continue;
446
+ }
447
+ const E = se(w.path, 0, 0, m);
448
+ s.current.set(g, {
449
+ ...x,
450
+ execution: {
451
+ path: w.path,
452
+ curveDataMap: w.curveDataMap,
453
+ currentCommandIndex: 0,
454
+ rear: { currentSegmentIndex: 0, segmentDistance: 0 },
455
+ front: E ? { currentSegmentIndex: E.segmentIndex, segmentDistance: E.segmentDistance } : { currentSegmentIndex: 0, segmentDistance: 0 }
456
+ },
457
+ vehicle: { ...S, state: "moving" }
458
+ }), u = !0, i.push({
459
+ id: g,
460
+ fromState: S.state,
461
+ command: q,
462
+ startPosition: {
463
+ lineId: S.rear.lineId,
464
+ absoluteOffset: S.rear.absoluteOffset,
465
+ position: S.rear.position
466
+ }
467
+ });
468
+ }
469
+ if (!u) return !1;
470
+ if (h.current = !0, L((g) => g.map((x) => {
471
+ const S = s.current.get(x.id);
472
+ return S ? S.vehicle : x;
473
+ })), M && i.length > 0) {
474
+ const g = a.current;
475
+ setTimeout(() => {
476
+ a.current === g && i.forEach(({ id: x, fromState: S, command: b, startPosition: q }) => {
477
+ M.emit("commandStart", {
478
+ vehicleId: x,
479
+ command: b,
480
+ commandIndex: 0,
481
+ startPosition: q
482
+ }), M.emit("stateChange", {
483
+ vehicleId: x,
484
+ from: S,
485
+ to: "moving"
486
+ });
487
+ });
488
+ }, 0);
489
+ }
490
+ return !0;
491
+ }, [c, f, V, r, I, m, M]), d = p(() => {
492
+ a.current++, h.current = !1;
493
+ const { movingVehicles: l, stateMap: n } = te(t, c);
494
+ s.current = n, L(l);
495
+ }, [t, c]), D = p((l) => {
496
+ const n = t.find((x) => x.id === l);
497
+ if (!n || !c.get(n.lineId)) return;
498
+ const u = ge(n), g = he(u);
499
+ s.current.set(l, g), L((x) => x.map(
500
+ (S) => S.id === l ? u : S
501
+ ));
502
+ }, [t, c]), $ = p((l) => {
503
+ const n = s.current.get(l);
504
+ if (!n || n.vehicle.state !== "waiting")
505
+ return !1;
506
+ const i = V.get(l), u = n.execution;
507
+ if (!u) return !1;
508
+ const g = u.currentCommandIndex + 1;
509
+ if (i && g < i.length) {
510
+ const x = v.current;
511
+ if (x) {
512
+ const S = i[g], b = { graph: x, linesMap: c, curves: f, config: I }, q = B(n.vehicle, S, b);
513
+ if (q) {
514
+ const j = se(q.path, 0, 0, m);
515
+ if (n.execution = {
516
+ path: q.path,
517
+ curveDataMap: q.curveDataMap,
518
+ currentCommandIndex: g,
519
+ rear: { currentSegmentIndex: 0, segmentDistance: 0 },
520
+ front: j ? { currentSegmentIndex: j.segmentIndex, segmentDistance: j.segmentDistance } : { currentSegmentIndex: 0, segmentDistance: 0 }
521
+ }, n.vehicle = { ...n.vehicle, state: "moving" }, L((w) => w.map((E) => E.id === l ? n.vehicle : E)), M) {
522
+ const w = a.current;
523
+ setTimeout(() => {
524
+ a.current === w && M.emit("stateChange", {
525
+ vehicleId: l,
526
+ from: "waiting",
527
+ to: "moving"
528
+ });
529
+ }, 0);
530
+ }
531
+ return !0;
532
+ }
533
+ }
534
+ }
535
+ if (n.vehicle = { ...n.vehicle, state: "idle" }, n.execution = null, L((x) => x.map((S) => S.id === l ? n.vehicle : S)), M) {
536
+ const x = a.current;
537
+ setTimeout(() => {
538
+ a.current === x && M.emit("stateChange", {
539
+ vehicleId: l,
540
+ from: "waiting",
541
+ to: "idle"
542
+ });
543
+ }, 0);
544
+ }
545
+ return !0;
546
+ }, [V, c, f, I, m, M]), k = p(() => {
547
+ for (const [, l] of s.current)
548
+ if (l.vehicle.state === "moving")
549
+ return !0;
550
+ return !1;
551
+ }, []);
552
+ return {
553
+ movingVehicles: O,
554
+ getMovingVehicles: P,
555
+ prepare: Q,
556
+ tick: C,
557
+ reset: d,
558
+ resetVehicle: D,
559
+ continueVehicle: $,
560
+ isMoving: k,
561
+ isPrepared: h.current
562
+ };
563
+ }
564
+ function ye({
565
+ wheelbase: t,
566
+ tangentMode: o = "proportional-40",
567
+ eventEmitter: V
568
+ }) {
569
+ const r = oe(), m = ce({ lines: r.lines, wheelbase: t }), e = ie({
570
+ vehicles: m.vehicles,
571
+ lines: r.lines,
572
+ curves: r.curves
573
+ }), f = Ve({
574
+ vehicles: m.vehicles,
575
+ lines: r.lines,
576
+ vehicleQueues: e.vehicleQueues,
577
+ getVehicleQueues: e.getVehicleQueues,
578
+ wheelbase: t,
579
+ tangentMode: o,
580
+ curves: r.curves,
581
+ eventEmitter: V
582
+ }), M = p((n) => m.vehicles.filter((i) => i.lineId === n || i.rear.lineId === n), [m.vehicles]), O = p((n) => M(n).length > 0, [M]), T = p((n) => {
583
+ const i = r.addLine(n);
584
+ return i.success ? { success: !0 } : { success: !1, error: i.error };
585
+ }, [r]), y = p((n, i) => {
586
+ const u = r.updateLine(n, i);
587
+ return u.success ? { success: !0 } : { success: !1, error: u.error };
588
+ }, [r]), L = p((n) => {
589
+ const i = [], u = M(n);
590
+ u.length > 0 && (i.push({
591
+ type: "vehicle_on_removed_line",
592
+ message: `${u.length} vehicle(s) are on line '${n}'`,
593
+ details: {
594
+ lineId: n,
595
+ vehicleIds: u.map((S) => S.id)
596
+ }
597
+ }), u.forEach((S) => {
598
+ m.removeVehicle(S.id), e.clearQueue(S.id);
599
+ }));
600
+ const g = r.curves.filter(
601
+ (S) => S.fromLineId === n || S.toLineId === n
602
+ );
603
+ g.length > 0 && i.push({
604
+ type: "orphaned_connection",
605
+ message: `${g.length} connection(s) will be removed`,
606
+ details: {
607
+ lineId: n,
608
+ connectionCount: g.length
609
+ }
610
+ });
611
+ const x = r.removeLine(n);
612
+ return x.success ? {
613
+ success: !0,
614
+ warnings: i.length > 0 ? i : void 0
615
+ } : { success: !1, error: x.error };
616
+ }, [r, M, m, e]), P = p(() => {
617
+ r.clear(), m.clear(), e.clearQueue();
618
+ }, [r, m, e]), I = p((n, i, u) => {
619
+ const g = r.addConnection({
620
+ from: n,
621
+ to: i,
622
+ fromPosition: u?.fromOffset,
623
+ fromIsPercentage: u?.fromIsPercentage,
624
+ toPosition: u?.toOffset,
625
+ toIsPercentage: u?.toIsPercentage
626
+ });
627
+ return g.success ? { success: !0 } : { success: !1, error: g.error };
628
+ }, [r]), c = p((n, i, u) => {
629
+ const g = r.updateConnection(n, i, u);
630
+ return g.success ? { success: !0 } : { success: !1, error: g.error };
631
+ }, [r]), s = p((n, i) => {
632
+ const u = r.removeConnection(n, i);
633
+ return u.success ? { success: !0 } : { success: !1, error: u.error };
634
+ }, [r]), v = p((n) => {
635
+ const i = m.addVehicles(n);
636
+ return i.success ? { success: !0 } : { success: !1, error: i.errors?.join("; ") };
637
+ }, [m]), a = p((n, i) => {
638
+ const u = m.updateVehicle(n, i);
639
+ return u.success ? { success: !0 } : { success: !1, error: u.error };
640
+ }, [m]), h = p((n) => {
641
+ const i = [], u = e.vehicleQueues.get(n);
642
+ u && u.length > 0 && (i.push({
643
+ type: "movement_queue_cleared",
644
+ message: `${u.length} queued movement(s) will be cleared for vehicle '${n}'`,
645
+ details: {
646
+ vehicleId: n
647
+ }
648
+ }), e.clearQueue(n));
649
+ const g = m.removeVehicle(n);
650
+ return g.success ? {
651
+ success: !0,
652
+ warnings: i.length > 0 ? i : void 0
653
+ } : { success: !1, error: g.error };
654
+ }, [m, e]), C = p(() => {
655
+ m.clear(), e.clearQueue();
656
+ }, [m, e]), Q = p((n) => {
657
+ const i = {
658
+ targetLineId: n.lineId,
659
+ targetPosition: n.position ?? 1,
660
+ isPercentage: n.isPercentage,
661
+ wait: n.wait,
662
+ payload: n.payload
663
+ }, u = e.queueMovement(n.id, i);
664
+ return u.success ? { success: !0 } : { success: !1, error: u.error };
665
+ }, [e]), d = p((n) => {
666
+ const i = e.clearQueue(n);
667
+ return i.success ? { success: !0 } : { success: !1, error: i.error };
668
+ }, [e]), D = p((n) => {
669
+ e.clearQueue(n), f.resetVehicle(n);
670
+ }, [e, f]), $ = p((n) => {
671
+ const i = [], u = [], { scene: g, vehicles: x, movements: S } = ve(n);
672
+ g.errors.length > 0 && u.push(...g.errors), x.errors.length > 0 && u.push(...x.errors), S.errors.length > 0 && u.push(...S.errors);
673
+ const b = g.data.lines.map(H), q = (g.data.connections || []).map(U), j = x.data.map(Y), { vehicles: w, errors: E } = N(j, b, t);
674
+ E.length > 0 && u.push(...E);
675
+ const R = /* @__PURE__ */ new Map();
676
+ for (const F of S.data) {
677
+ const ee = R.get(F.vehicleId) || [];
678
+ ee.push(Z(F)), R.set(F.vehicleId, ee);
679
+ }
680
+ return r._loadScene(b, q), m._loadVehicles(w), e._loadQueues(R), u.length > 0 && i.push({
681
+ type: "dsl_parse_error",
682
+ message: `DSL loading had ${u.length} error(s)`,
683
+ details: { errors: u }
684
+ }), {
685
+ success: !0,
686
+ warnings: i.length > 0 ? i : void 0
687
+ };
688
+ }, [r, m, e, t]), k = p((n) => {
689
+ const i = [], u = [], g = n.lines.map(H), x = (n.connections || []).map(U), S = (n.vehicles || []).map(Y), { vehicles: b, errors: q } = N(S, g, t);
690
+ q.length > 0 && u.push(...q);
691
+ const j = /* @__PURE__ */ new Map();
692
+ for (const w of n.movements || []) {
693
+ const E = j.get(w.vehicleId) || [];
694
+ E.push(Z({
695
+ vehicleId: w.vehicleId,
696
+ targetLineId: w.targetLineId,
697
+ targetPosition: w.targetPosition,
698
+ isPercentage: w.isPercentage,
699
+ wait: w.wait,
700
+ payload: w.payload
701
+ })), j.set(w.vehicleId, E);
702
+ }
703
+ return r._loadScene(g, x), m._loadVehicles(b), e._loadQueues(j), u.length > 0 && i.push({
704
+ type: "dsl_parse_error",
705
+ message: `JSON loading had ${u.length} error(s)`,
706
+ details: { errors: u }
707
+ }), {
708
+ success: !0,
709
+ warnings: i.length > 0 ? i : void 0
710
+ };
711
+ }, [r, m, e, t]), l = G(() => r.error || m.error || e.error, [r.error, m.error, e.error]);
712
+ return {
713
+ // State
714
+ lines: r.lines,
715
+ curves: r.curves,
716
+ vehicles: m.vehicles,
717
+ movingVehicles: f.movingVehicles,
718
+ getMovingVehicles: f.getMovingVehicles,
719
+ vehicleQueues: e.vehicleQueues,
720
+ error: l,
721
+ // Scene operations
722
+ addLine: T,
723
+ updateLine: y,
724
+ removeLine: L,
725
+ clearScene: P,
726
+ // Connection operations
727
+ connect: I,
728
+ updateConnection: c,
729
+ disconnect: s,
730
+ // Vehicle operations
731
+ addVehicles: v,
732
+ updateVehicle: a,
733
+ removeVehicle: h,
734
+ clearVehicles: C,
735
+ // Movement operations
736
+ goto: Q,
737
+ clearQueue: d,
738
+ // Animation (delegated to useVehicleMovement)
739
+ prepare: f.prepare,
740
+ tick: f.tick,
741
+ reset: f.reset,
742
+ resetVehicle: D,
743
+ // Uses wrapper that also clears queue
744
+ continueVehicle: f.continueVehicle,
745
+ isMoving: f.isMoving,
746
+ // DSL Loading
747
+ loadFromDSL: $,
748
+ // JSON Loading
749
+ loadFromJSON: k,
750
+ // Utility
751
+ getVehiclesOnLine: M,
752
+ hasVehiclesOnLine: O
753
+ };
754
+ }
755
+ function z(t) {
756
+ return t.map((o) => ({
757
+ id: o.id,
758
+ start: o.start,
759
+ end: o.end
760
+ }));
761
+ }
762
+ function X(t) {
763
+ return t.map((o) => ({
764
+ from: o.fromLineId,
765
+ to: o.toLineId,
766
+ // No conversion needed - internal format is now 0-1 (same as API)
767
+ fromPosition: o.fromOffset,
768
+ fromIsPercentage: o.fromIsPercentage,
769
+ toPosition: o.toOffset,
770
+ toIsPercentage: o.toIsPercentage
771
+ }));
772
+ }
773
+ function Ee() {
774
+ const [t, o] = _(""), [V, r] = _(null), { lines: m, curves: e, setScene: f } = oe(), M = A(!1), O = A("");
775
+ K(() => {
776
+ O.current = t;
777
+ }, [t]), K(() => {
778
+ if (M.current)
779
+ return;
780
+ const P = {
781
+ lines: z(m),
782
+ connections: e.length > 0 ? X(e) : void 0
783
+ }, I = W(P);
784
+ I !== O.current && (M.current = !0, o(I), setTimeout(() => {
785
+ M.current = !1;
786
+ }, 50));
787
+ }, [m, e]);
788
+ const T = p((P) => {
789
+ M.current = !0, o(P);
790
+ try {
791
+ const { data: I, errors: c } = pe(P);
792
+ c.length > 0 && r(c.join("; "));
793
+ const s = f(I);
794
+ !s.success && s.errors ? r((v) => v ? `${v}; ${s.errors.join("; ")}` : s.errors.join("; ")) : c.length === 0 && r(null);
795
+ } catch (I) {
796
+ r(I instanceof Error ? I.message : "Invalid scene definition");
797
+ }
798
+ setTimeout(() => {
799
+ M.current = !1;
800
+ }, 50);
801
+ }, [f]), y = p((P) => {
802
+ const I = typeof P == "function" ? P(m) : P, c = X(e);
803
+ f({
804
+ lines: z(I),
805
+ connections: c.length > 0 ? c : void 0
806
+ });
807
+ const s = {
808
+ lines: z(I),
809
+ connections: c.length > 0 ? c : void 0
810
+ }, v = W(s);
811
+ M.current = !0, o(v), setTimeout(() => {
812
+ M.current = !1;
813
+ }, 50);
814
+ }, [m, e, f]), L = p((P) => {
815
+ const I = typeof P == "function" ? P(e) : P, c = X(I);
816
+ f({
817
+ lines: z(m),
818
+ connections: c.length > 0 ? c : void 0
819
+ });
820
+ const s = {
821
+ lines: z(m),
822
+ connections: c.length > 0 ? c : void 0
823
+ }, v = W(s);
824
+ M.current = !0, o(v), setTimeout(() => {
825
+ M.current = !1;
826
+ }, 50);
827
+ }, [m, e, f]);
828
+ return {
829
+ lines: m,
830
+ curves: e,
831
+ sceneDefinitionText: t,
832
+ sceneError: V,
833
+ isDebouncing: !1,
834
+ // No debouncing - parsing is immediate
835
+ debounceKey: 0,
836
+ // Kept for API compatibility
837
+ setLines: y,
838
+ setCurves: L,
839
+ setSceneDefinitionText: T
840
+ };
841
+ }
842
+ function De({ lines: t, wheelbase: o }) {
843
+ const [V, r] = _(""), [m, e] = _(null), { vehicles: f, addVehicles: M, clear: O, error: T } = ce({ lines: t, wheelbase: o }), y = A(!1), L = p((P) => {
844
+ y.current = !0, r(P);
845
+ try {
846
+ const { data: I, errors: c } = Pe(P), s = [...c];
847
+ O();
848
+ for (const v of I) {
849
+ const a = M(v);
850
+ !a.success && a.errors && s.push(...a.errors);
851
+ }
852
+ s.length > 0 ? e(s.join(`
853
+ `)) : e(null);
854
+ } catch (I) {
855
+ e(I instanceof Error ? I.message : "Invalid initial movement");
856
+ }
857
+ setTimeout(() => {
858
+ y.current = !1;
859
+ }, 50);
860
+ }, [M, O]);
861
+ return {
862
+ vehicles: f,
863
+ initialMovementText: V,
864
+ movementError: m || T,
865
+ isDebouncing: !1,
866
+ // No debouncing - parsing is immediate
867
+ debounceKey: 0,
868
+ // Kept for API compatibility
869
+ setInitialMovementText: L
870
+ };
871
+ }
872
+ function Oe({ lines: t, vehicles: o }) {
873
+ const [V, r] = _(""), [m, e] = _([]), [f, M] = _(null), { vehicleQueues: O, queueMovement: T, clearQueue: y, error: L } = ie({
874
+ vehicles: o,
875
+ lines: t
876
+ }), P = A(!1), I = p((c) => {
877
+ P.current = !0, r(c);
878
+ try {
879
+ const { data: s, errors: v } = Ie(c), a = [...v];
880
+ y();
881
+ for (const h of s) {
882
+ const C = T(h.vehicleId, {
883
+ targetLineId: h.targetLineId,
884
+ targetPosition: h.targetPosition,
885
+ isPercentage: h.isPercentage,
886
+ wait: h.wait,
887
+ payload: h.payload
888
+ });
889
+ !C.success && C.error && a.push(C.error);
890
+ }
891
+ e(s), a.length > 0 ? M(a.join(`
892
+ `)) : M(null);
893
+ } catch (s) {
894
+ M(s instanceof Error ? s.message : "Invalid movement sequence"), e([]);
895
+ }
896
+ setTimeout(() => {
897
+ P.current = !1;
898
+ }, 50);
899
+ }, [T, y]);
900
+ return {
901
+ movementSequenceText: V,
902
+ gotoCommands: m,
903
+ vehicleQueues: O,
904
+ sequenceError: f || L,
905
+ isDebouncing: !1,
906
+ // No debouncing - parsing is immediate
907
+ debounceKey: 0,
908
+ // Kept for API compatibility
909
+ setMovementSequenceText: I
910
+ };
911
+ }
912
+ const ae = ue(null);
913
+ function Se() {
914
+ const t = le(ae);
915
+ if (!t)
916
+ throw new Error("useVehicleEventEmitter must be used within a VehicleEventProvider");
917
+ return t;
918
+ }
919
+ function Te() {
920
+ return G(() => new ne(), []);
921
+ }
922
+ function qe(t, o, V = []) {
923
+ const r = Se();
924
+ K(() => r.on(t, o), [r, t, ...V]);
925
+ }
926
+ function be({ children: t }) {
927
+ const o = G(() => new ne(), []);
928
+ return /* @__PURE__ */ xe(ae.Provider, { value: o, children: t });
929
+ }
930
+ export {
931
+ ae as V,
932
+ be as a,
933
+ Te as b,
934
+ De as c,
935
+ ie as d,
936
+ Oe as e,
937
+ oe as f,
938
+ Ee as g,
939
+ qe as h,
940
+ Se as i,
941
+ ye as j,
942
+ ce as k,
943
+ Ve as u
944
+ };