vehicle-path2 1.0.4 → 1.0.5

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