@knot-technologies/seatmap 0.4.0-dev.0 → 0.4.0-dev.10

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 CHANGED
@@ -102,16 +102,17 @@ function createEndpoint({ transport, channelId, requestTimeoutMs = 1e4 }) {
102
102
  }
103
103
  }
104
104
  return {
105
- request(method, params) {
105
+ request(method, params, options) {
106
106
  if (closed) {
107
107
  return Promise.reject(new RpcError("CHART_DESTROYED", "The chart has been destroyed", method));
108
108
  }
109
109
  const id = nextId++;
110
110
  return new Promise((resolve, reject) => {
111
- const timeout = setTimeout(() => {
111
+ const timeoutMs = options?.timeoutMs === void 0 ? requestTimeoutMs : options.timeoutMs;
112
+ const timeout = timeoutMs === null ? void 0 : setTimeout(() => {
112
113
  pending.delete(id);
113
- reject(new RpcError("ENGINE_UNREACHABLE", `Engine did not reply to '${method}' within ${requestTimeoutMs}ms`, method));
114
- }, requestTimeoutMs);
114
+ reject(new RpcError("ENGINE_UNREACHABLE", `Engine did not reply to '${method}' within ${timeoutMs}ms`, method));
115
+ }, timeoutMs);
115
116
  pending.set(id, { resolve, reject, method, timeout });
116
117
  try {
117
118
  transport.send({
@@ -208,6 +209,41 @@ function createWindowTransport({ target, targetOrigin, channelId }) {
208
209
  };
209
210
  }
210
211
 
212
+ // ../seatmap-types/dist/index.js
213
+ var SECTION_CONTENTS_MODES = [
214
+ "always",
215
+ "seatsOnly",
216
+ "afterZoom"
217
+ ];
218
+ function isSectionContentsMode(value) {
219
+ return SECTION_CONTENTS_MODES.some((mode) => mode === value);
220
+ }
221
+ var LEGEND_CONTENT_MODES = ["nameAndPrice", "name", "price"];
222
+ function isLegendContentMode(value) {
223
+ return LEGEND_CONTENT_MODES.some((mode) => mode === value);
224
+ }
225
+ function isSeatmapLegendConfig(value) {
226
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
227
+ return false;
228
+ }
229
+ return Object.entries(value).every(([key, entry]) => {
230
+ switch (key) {
231
+ case "show":
232
+ case "hideAccessibleSeatsFilter":
233
+ case "hideUnavailableItem":
234
+ case "hideNonSelectableCategories":
235
+ return entry === void 0 || typeof entry === "boolean";
236
+ case "content":
237
+ return entry === void 0 || isLegendContentMode(entry);
238
+ default:
239
+ return false;
240
+ }
241
+ });
242
+ }
243
+ function isSeatmapColorScheme(value) {
244
+ return value === "light" || value === "dark";
245
+ }
246
+
211
247
  // src/chartHandle.ts
212
248
  function isRecord(value) {
213
249
  return typeof value === "object" && value !== null && !Array.isArray(value);
@@ -221,22 +257,273 @@ function isObjectRef(value) {
221
257
  function isObjectRefArray(value) {
222
258
  return Array.isArray(value) && value.every(isObjectRef);
223
259
  }
260
+ function isNonemptyString(value) {
261
+ return typeof value === "string" && value.length > 0;
262
+ }
263
+ function isOptionalString(value) {
264
+ return value === void 0 || typeof value === "string";
265
+ }
266
+ function isOptionalIdentifier(value) {
267
+ return value === void 0 || isNonemptyString(value);
268
+ }
269
+ function isStringArray(value) {
270
+ return Array.isArray(value) && value.every(isNonemptyString);
271
+ }
272
+ function isFloor(value) {
273
+ return isRecord(value) && isNonemptyString(value.id) && isNonemptyString(value.name) && isNonemptyString(value.shortName) && isStringArray(value.categoryIds);
274
+ }
275
+ function isNonnegativeFiniteNumber(value) {
276
+ return typeof value === "number" && Number.isFinite(value) && value >= 0;
277
+ }
278
+ function isPositiveFiniteNumber(value) {
279
+ return typeof value === "number" && Number.isFinite(value) && value > 0;
280
+ }
281
+ function isNonnegativeSafeInteger(value) {
282
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
283
+ }
284
+ function isOptionalPrice(value) {
285
+ return value === void 0 || typeof value === "string" || typeof value === "number" && Number.isFinite(value);
286
+ }
287
+ function toLifecycleIdentity(value) {
288
+ if (!isRecord(value)) {
289
+ return void 0;
290
+ }
291
+ const keys = Object.keys(value);
292
+ if (keys.length === 1 && keys[0] === "sessionId" && isNonemptyString(value.sessionId)) {
293
+ return { sessionId: value.sessionId };
294
+ }
295
+ if (keys.length === 1 && keys[0] === "seasonKey" && isNonemptyString(value.seasonKey)) {
296
+ return { seasonKey: value.seasonKey };
297
+ }
298
+ return void 0;
299
+ }
300
+ function isSeatEventPayload(value) {
301
+ return isRecord(value) && value.type === "seat" && isOptionalIdentifier(value.floorId) && isNonemptyString(value.id) && isNonemptyString(value.sectionId) && isOptionalIdentifier(value.categoryId) && isOptionalString(value.label) && isOptionalString(value.rowLabel) && isOptionalString(value.seatLabel);
302
+ }
303
+ function isSeatClickEventPayload(value) {
304
+ return isRecord(value) && isSeatEventPayload(value) && typeof value.selected === "boolean" && typeof value.status === "string";
305
+ }
306
+ function hasSeatSelectionFields(value, selected) {
307
+ return isRecord(value) && isSeatEventPayload(value) && value.selected === selected && isOptionalString(value.sectionLabel);
308
+ }
309
+ function hasGaAreaFields(value, selected) {
310
+ return isRecord(value) && value.type === "gaArea" && isOptionalIdentifier(value.floorId) && isNonemptyString(value.id) && isNonemptyString(value.categoryId) && typeof value.label === "string" && typeof value.code === "string" && typeof value.selected === "boolean" && (selected === void 0 || value.selected === selected) && isNonnegativeSafeInteger(value.quantity) && isNonnegativeSafeInteger(value.available) && isNonnegativeSafeInteger(value.capacity) && isNonnegativeSafeInteger(value.booked) && isNonnegativeSafeInteger(value.held) && isOptionalPrice(value.price);
311
+ }
312
+ function isGaAreaEventPayload(value) {
313
+ return hasGaAreaFields(value);
314
+ }
315
+ function hasTableFields(value, selected) {
316
+ return isRecord(value) && value.type === "table" && isOptionalIdentifier(value.floorId) && isNonemptyString(value.id) && isStringArray(value.seatIds) && isOptionalIdentifier(value.sectionId) && isOptionalIdentifier(value.categoryId) && (value.bookingMode === void 0 || value.bookingMode === "seat" || value.bookingMode === "table" || value.bookingMode === "variable") && isOptionalString(value.label) && typeof value.selected === "boolean" && (selected === void 0 || value.selected === selected) && isNonnegativeSafeInteger(value.quantity);
317
+ }
318
+ function isTableEventPayload(value) {
319
+ return hasTableFields(value);
320
+ }
321
+ function isSelectionEventPayload(value, selected) {
322
+ if (!isRecord(value)) {
323
+ return false;
324
+ }
325
+ switch (value.type) {
326
+ case "seat":
327
+ return hasSeatSelectionFields(value, selected);
328
+ case "gaArea":
329
+ return hasGaAreaFields(value, selected);
330
+ case "table":
331
+ return hasTableFields(value, selected);
332
+ default:
333
+ return false;
334
+ }
335
+ }
336
+ function isSelectedObject(value) {
337
+ if (!isRecord(value)) {
338
+ return false;
339
+ }
340
+ if (value.type === "table") {
341
+ return hasTableFields(value, true) && isNonnegativeSafeInteger(value.available) && isNonnegativeSafeInteger(value.capacity) && isNonnegativeSafeInteger(value.booked) && isNonnegativeSafeInteger(value.held);
342
+ }
343
+ return isSelectionEventPayload(value, true);
344
+ }
345
+ function isClickedObject(value) {
346
+ if (!isRecord(value)) {
347
+ return false;
348
+ }
349
+ switch (value.type) {
350
+ case "seat":
351
+ return isSeatClickEventPayload(value);
352
+ case "gaArea":
353
+ return isGaAreaEventPayload(value);
354
+ case "table":
355
+ return isTableEventPayload(value);
356
+ default:
357
+ return false;
358
+ }
359
+ }
360
+ function isMouseObject(value) {
361
+ if (!isRecord(value)) {
362
+ return false;
363
+ }
364
+ switch (value.type) {
365
+ case "seat":
366
+ return isSeatEventPayload(value);
367
+ case "gaArea":
368
+ return isGaAreaEventPayload(value);
369
+ case "table":
370
+ return isTableEventPayload(value);
371
+ default:
372
+ return false;
373
+ }
374
+ }
375
+ function isSeatBlockedEventPayload(value) {
376
+ return isRecord(value) && isSeatEventPayload(value) && isNonemptyString(value.reason) && isOptionalString(value.message);
377
+ }
378
+ function isObjectStatusChangedEventPayload(value) {
379
+ return isRecord(value) && isSeatEventPayload(value) && isNonemptyString(value.status) && isNonemptyString(value.previousStatus) && typeof value.selected === "boolean";
380
+ }
381
+ function isZoomLevelChangedEventPayload(value) {
382
+ return isRecord(value) && (value.level === "overview" || value.level === "section" || value.level === "seat") && isPositiveFiniteNumber(value.scale) && isNonnegativeFiniteNumber(value.seatPixelSize);
383
+ }
384
+ function isViewportChangedEventPayload(value) {
385
+ if (!isRecord(value) || !isRecord(value.transform) || !isRecord(value.boundsWorld)) {
386
+ return false;
387
+ }
388
+ return isPositiveFiniteNumber(value.transform.scale) && typeof value.transform.x === "number" && Number.isFinite(value.transform.x) && typeof value.transform.y === "number" && Number.isFinite(value.transform.y) && typeof value.boundsWorld.minX === "number" && Number.isFinite(value.boundsWorld.minX) && typeof value.boundsWorld.minY === "number" && Number.isFinite(value.boundsWorld.minY) && typeof value.boundsWorld.maxX === "number" && Number.isFinite(value.boundsWorld.maxX) && typeof value.boundsWorld.maxY === "number" && Number.isFinite(value.boundsWorld.maxY);
389
+ }
390
+ function isSectionFocusEventPayload(value) {
391
+ return isRecord(value) && isNonemptyString(value.sectionId) && isOptionalIdentifier(value.floorId);
392
+ }
393
+ function isInventorySelection(value) {
394
+ if (!isRecord(value)) {
395
+ return false;
396
+ }
397
+ return Object.keys(value).every((key) => key === "seatIds" || key === "gaAreas") && Array.isArray(value.seatIds) && value.seatIds.every((id) => typeof id === "string") && Array.isArray(value.gaAreas) && value.gaAreas.every(
398
+ (area) => isRecord(area) && Object.keys(area).every((key) => key === "id" || key === "quantity") && typeof area.id === "string" && typeof area.quantity === "number" && Number.isSafeInteger(area.quantity) && area.quantity >= 1
399
+ );
400
+ }
401
+ function toInventorySelection(selection) {
402
+ return {
403
+ seatIds: [...selection.seatIds],
404
+ gaAreas: selection.gaAreas.map((area) => ({
405
+ id: area.id,
406
+ quantity: area.quantity
407
+ }))
408
+ };
409
+ }
410
+ var SEASON_HOLD_OPERATIONS = ["create", "replace", "renew", "release"];
411
+ var SEASON_HOLD_TERMINAL_STATUSES = ["released", "expired", "booked"];
412
+ var SEASON_HOLD_ALLOWED_KEYS = {
413
+ idle: ["status", "seasonKey"],
414
+ updating: ["status", "seasonKey", "operation", "holdId"],
415
+ active: ["status", "seasonKey", "holdId", "selection", "expiresAt"],
416
+ released: ["status", "seasonKey", "holdId"],
417
+ expired: ["status", "seasonKey", "holdId"],
418
+ booked: ["status", "seasonKey", "holdId"]
419
+ };
420
+ function hasOnlyApprovedKeys(value, status) {
421
+ const allowed = SEASON_HOLD_ALLOWED_KEYS[status];
422
+ if (!allowed) {
423
+ return false;
424
+ }
425
+ return Object.keys(value).every((key) => allowed.includes(key));
426
+ }
427
+ function isSeasonHoldState(value) {
428
+ if (!isRecord(value) || typeof value.seasonKey !== "string") {
429
+ return false;
430
+ }
431
+ if (typeof value.status !== "string") {
432
+ return false;
433
+ }
434
+ switch (value.status) {
435
+ case "idle":
436
+ break;
437
+ case "updating":
438
+ if (typeof value.operation !== "string" || !SEASON_HOLD_OPERATIONS.includes(value.operation) || value.holdId !== void 0 && typeof value.holdId !== "string") {
439
+ return false;
440
+ }
441
+ break;
442
+ case "active":
443
+ if (typeof value.holdId !== "string" || typeof value.selection !== "object" || value.selection === null || !isInventorySelection(value.selection) || typeof value.expiresAt !== "string") {
444
+ return false;
445
+ }
446
+ break;
447
+ default:
448
+ if (!SEASON_HOLD_TERMINAL_STATUSES.includes(value.status) || typeof value.holdId !== "string") {
449
+ return false;
450
+ }
451
+ break;
452
+ }
453
+ return hasOnlyApprovedKeys(value, value.status);
454
+ }
455
+ function toPublicSeasonHoldState(value) {
456
+ switch (value.status) {
457
+ case "idle":
458
+ return { status: "idle", seasonKey: value.seasonKey };
459
+ case "updating": {
460
+ const updating = {
461
+ status: "updating",
462
+ seasonKey: value.seasonKey,
463
+ operation: value.operation
464
+ };
465
+ if (value.holdId !== void 0) {
466
+ updating.holdId = value.holdId;
467
+ }
468
+ return updating;
469
+ }
470
+ case "active":
471
+ return {
472
+ status: "active",
473
+ seasonKey: value.seasonKey,
474
+ holdId: value.holdId,
475
+ selection: toInventorySelection(value.selection),
476
+ expiresAt: value.expiresAt
477
+ };
478
+ default: {
479
+ const terminal = {
480
+ status: value.status,
481
+ seasonKey: value.seasonKey,
482
+ holdId: value.holdId
483
+ };
484
+ return terminal;
485
+ }
486
+ }
487
+ }
224
488
  function isEventPayload(event, payload) {
225
489
  switch (event) {
226
490
  case "chartRendered":
227
- return payload === void 0;
491
+ return toLifecycleIdentity(payload) !== void 0;
228
492
  case "chartRenderingFailed":
229
493
  return isRecord(payload) && typeof payload.message === "string";
230
- case "selectionChanged":
231
- return isRecord(payload) && isObjectRefArray(payload.selected);
232
494
  case "objectClicked":
495
+ return isClickedObject(payload);
233
496
  case "objectSelected":
497
+ return isSelectionEventPayload(payload, true);
234
498
  case "objectDeselected":
499
+ return isSelectionEventPayload(payload, false);
500
+ case "selectionChanged":
501
+ return isRecord(payload) && Array.isArray(payload.selected) && payload.selected.every(isSelectedObject);
235
502
  case "objectMouseOver":
236
503
  case "objectMouseOut":
237
- return isObjectRef(payload);
504
+ return isMouseObject(payload);
505
+ case "seatBlocked":
506
+ return isSeatBlockedEventPayload(payload);
507
+ case "objectStatusChanged":
508
+ return isObjectStatusChangedEventPayload(payload);
509
+ case "zoomLevelChanged":
510
+ return isZoomLevelChangedEventPayload(payload);
511
+ case "viewportChanged":
512
+ return isViewportChangedEventPayload(payload);
513
+ case "sectionFocused":
514
+ case "sectionUnfocused":
515
+ return isSectionFocusEventPayload(payload);
516
+ case "floorChanged":
517
+ return isRecord(payload) && (payload.floor === null || isRecord(payload.floor) && isFloor(payload.floor));
518
+ case "seasonHoldChanged":
519
+ return isRecord(payload) && isSeasonHoldState(payload);
238
520
  }
239
521
  }
522
+ function copyFloorChanged(payload) {
523
+ return {
524
+ floor: payload.floor ? { ...payload.floor, categoryIds: [...payload.floor.categoryIds] } : null
525
+ };
526
+ }
240
527
  function destroyedError(method) {
241
528
  return new RpcError(
242
529
  "CHART_DESTROYED",
@@ -244,36 +531,162 @@ function destroyedError(method) {
244
531
  method
245
532
  );
246
533
  }
247
- async function requestVoid(endpoint, method, params) {
248
- await endpoint.request(method, params);
534
+ async function requestVoid(endpoint, method, params, waitForConfirmation = false) {
535
+ await endpoint.request(
536
+ method,
537
+ params,
538
+ waitForConfirmation ? { timeoutMs: null } : void 0
539
+ );
249
540
  }
250
- function createChartHandle(endpoint, removeIframe) {
541
+ function createChartHandle(endpoint, removeIframe, options) {
251
542
  let destroyed = false;
252
- let chartRendered = false;
543
+ let chartRenderedIdentity;
544
+ let currentSeasonHold = options?.initialSeasonHold;
545
+ let currentFloor;
546
+ const unsubscribeFloorChanged = endpoint.on("floorChanged", (payload) => {
547
+ if (isEventPayload("floorChanged", payload))
548
+ currentFloor = copyFloorChanged(payload);
549
+ });
253
550
  const subscriptions = [];
254
- const unsubscribeChartRendered = endpoint.on("chartRendered", () => {
255
- chartRendered = true;
551
+ const unsubscribeChartRendered = endpoint.on("chartRendered", (payload) => {
552
+ const identity = toLifecycleIdentity(payload);
553
+ if (identity !== void 0) {
554
+ chartRenderedIdentity = identity;
555
+ }
256
556
  });
557
+ const unsubscribeSeasonHoldChanged = endpoint.on(
558
+ "seasonHoldChanged",
559
+ (payload) => {
560
+ if (isRecord(payload) && isSeasonHoldState(payload)) {
561
+ currentSeasonHold = toPublicSeasonHoldState(payload);
562
+ }
563
+ }
564
+ );
257
565
  function assertActive(method) {
258
566
  if (destroyed) {
259
567
  throw destroyedError(method);
260
568
  }
261
569
  }
570
+ function onSeasonHoldChanged(handler) {
571
+ if (subscriptions.some(
572
+ (subscription2) => subscription2.event === "seasonHoldChanged" && subscription2.handler === handler
573
+ )) {
574
+ return;
575
+ }
576
+ let liveEventSeen = false;
577
+ const unsubscribe = endpoint.on("seasonHoldChanged", (payload) => {
578
+ if (isRecord(payload) && isSeasonHoldState(payload)) {
579
+ liveEventSeen = true;
580
+ handler(toPublicSeasonHoldState(payload));
581
+ }
582
+ });
583
+ let active = true;
584
+ const subscription = {
585
+ event: "seasonHoldChanged",
586
+ handler,
587
+ unsubscribe() {
588
+ active = false;
589
+ unsubscribe();
590
+ }
591
+ };
592
+ subscriptions.push(subscription);
593
+ if (currentSeasonHold !== void 0) {
594
+ const stickyState = toPublicSeasonHoldState(currentSeasonHold);
595
+ queueMicrotask(() => {
596
+ if (active && !liveEventSeen) {
597
+ handler(stickyState);
598
+ }
599
+ });
600
+ }
601
+ }
602
+ function onChartRendered(handler) {
603
+ if (subscriptions.some(
604
+ (subscription) => subscription.event === "chartRendered" && subscription.handler === handler
605
+ )) {
606
+ return;
607
+ }
608
+ const stickyIdentity = chartRenderedIdentity;
609
+ if (stickyIdentity !== void 0) {
610
+ let active = true;
611
+ const subscription = {
612
+ event: "chartRendered",
613
+ handler,
614
+ unsubscribe() {
615
+ active = false;
616
+ }
617
+ };
618
+ subscriptions.push(subscription);
619
+ queueMicrotask(() => {
620
+ if (active) {
621
+ handler(stickyIdentity);
622
+ }
623
+ });
624
+ return;
625
+ }
626
+ const unsubscribe = endpoint.on("chartRendered", (payload) => {
627
+ const identity = toLifecycleIdentity(payload);
628
+ if (identity !== void 0) {
629
+ handler(identity);
630
+ }
631
+ });
632
+ subscriptions.push({
633
+ event: "chartRendered",
634
+ handler,
635
+ unsubscribe
636
+ });
637
+ }
262
638
  return {
263
639
  get isDestroyed() {
264
640
  return destroyed;
265
641
  },
266
642
  async selectObjects(objects) {
267
643
  assertActive("selectObjects");
268
- await requestVoid(endpoint, "selectObjects", { objects });
644
+ await requestVoid(
645
+ endpoint,
646
+ "selectObjects",
647
+ { objects },
648
+ options?.confirmSeatSelection
649
+ );
269
650
  },
270
651
  async deselectObjects(objects) {
271
652
  assertActive("deselectObjects");
272
- await requestVoid(endpoint, "deselectObjects", { objects });
653
+ await requestVoid(
654
+ endpoint,
655
+ "deselectObjects",
656
+ { objects },
657
+ options?.confirmSeatSelection
658
+ );
273
659
  },
274
660
  async clearSelection() {
275
661
  assertActive("clearSelection");
276
- await requestVoid(endpoint, "clearSelection", {});
662
+ await requestVoid(
663
+ endpoint,
664
+ "clearSelection",
665
+ {},
666
+ options?.confirmSeatSelection
667
+ );
668
+ },
669
+ async listFloors() {
670
+ assertActive("listFloors");
671
+ const result = await endpoint.request("listFloors", {});
672
+ if (!Array.isArray(result) || !result.every(
673
+ (floor) => isRecord(floor) && isFloor(floor)
674
+ )) {
675
+ throw new RpcError(
676
+ "ENGINE_INCOMPATIBLE",
677
+ "Engine returned invalid floors",
678
+ "listFloors"
679
+ );
680
+ }
681
+ return result;
682
+ },
683
+ async goToFloor(floorId) {
684
+ assertActive("goToFloor");
685
+ await requestVoid(endpoint, "goToFloor", { floorId });
686
+ },
687
+ async goToAllFloors() {
688
+ assertActive("goToAllFloors");
689
+ await requestVoid(endpoint, "goToAllFloors", {});
277
690
  },
278
691
  async listSelectedObjects() {
279
692
  assertActive("listSelectedObjects");
@@ -355,61 +768,72 @@ function createChartHandle(endpoint, removeIframe) {
355
768
  assertActive("findObject");
356
769
  await requestVoid(endpoint, "findObject", {});
357
770
  },
358
- async zoomToObjects() {
771
+ async zoomToObjects(objects) {
359
772
  assertActive("zoomToObjects");
360
- await requestVoid(endpoint, "zoomToObjects", {});
773
+ await requestVoid(endpoint, "zoomToObjects", { objects });
361
774
  },
362
- async zoomToSection() {
775
+ async zoomToSection(sectionId) {
363
776
  assertActive("zoomToSection");
364
- await requestVoid(endpoint, "zoomToSection", {});
777
+ await requestVoid(endpoint, "zoomToSection", { sectionId });
365
778
  },
366
779
  async resetView() {
367
780
  assertActive("resetView");
368
781
  await requestVoid(endpoint, "resetView", {});
369
782
  },
370
- async changeConfig() {
783
+ async changeConfig(config) {
371
784
  assertActive("changeConfig");
372
- await requestVoid(endpoint, "changeConfig", {});
785
+ await requestVoid(endpoint, "changeConfig", config);
373
786
  },
374
787
  async destroy() {
375
788
  assertActive("destroy");
376
789
  destroyed = true;
377
790
  subscriptions.splice(0).forEach((subscription) => subscription.unsubscribe());
378
791
  unsubscribeChartRendered();
792
+ unsubscribeFloorChanged();
793
+ unsubscribeSeasonHoldChanged();
379
794
  endpoint.close();
380
795
  removeIframe();
381
796
  },
382
797
  on(event, handler) {
383
798
  assertActive("on");
799
+ if (event === "seasonHoldChanged") {
800
+ onSeasonHoldChanged(
801
+ handler
802
+ );
803
+ return;
804
+ }
805
+ if (event === "chartRendered") {
806
+ onChartRendered(handler);
807
+ return;
808
+ }
384
809
  if (subscriptions.some(
385
810
  (subscription) => subscription.event === event && subscription.handler === handler
386
811
  )) {
387
812
  return;
388
813
  }
389
- if (event === "chartRendered" && chartRendered) {
390
- let active = true;
391
- const subscription = {
392
- event,
393
- handler,
394
- unsubscribe() {
395
- active = false;
396
- }
397
- };
398
- subscriptions.push(subscription);
399
- const chartRenderedHandler = handler;
400
- queueMicrotask(() => {
401
- if (active) {
402
- chartRenderedHandler();
403
- }
404
- });
405
- return;
406
- }
814
+ let liveEventSeen = false;
815
+ let active = true;
407
816
  const unsubscribe = endpoint.on(event, (payload) => {
408
817
  if (isEventPayload(event, payload)) {
818
+ liveEventSeen = true;
409
819
  handler(payload);
410
820
  }
411
821
  });
412
- subscriptions.push({ event, handler, unsubscribe });
822
+ subscriptions.push({
823
+ event,
824
+ handler,
825
+ unsubscribe() {
826
+ active = false;
827
+ unsubscribe();
828
+ }
829
+ });
830
+ if (event === "floorChanged" && currentFloor !== void 0) {
831
+ const initialFloor = copyFloorChanged(currentFloor);
832
+ queueMicrotask(() => {
833
+ if (active && !liveEventSeen && isEventPayload(event, initialFloor))
834
+ handler(initialFloor);
835
+ });
836
+ }
413
837
  },
414
838
  off(event, handler) {
415
839
  assertActive("off");
@@ -424,8 +848,208 @@ function createChartHandle(endpoint, removeIframe) {
424
848
  };
425
849
  }
426
850
 
851
+ // src/core/startSeatmapSession.ts
852
+ function isRecord2(value) {
853
+ return typeof value === "object" && value !== null && !Array.isArray(value);
854
+ }
855
+ function isHandshakeAcknowledgement(value) {
856
+ if (typeof value !== "object" || value === null) {
857
+ return false;
858
+ }
859
+ const result = value;
860
+ return result.protocolVersion === PROTOCOL_VERSION;
861
+ }
862
+ function invalidConfigError() {
863
+ return new RpcError(
864
+ "ENGINE_INCOMPATIBLE",
865
+ "Seatmap config must provide either classic or Season authority, never both"
866
+ );
867
+ }
868
+ function isCategoryFilter(value) {
869
+ if (!isRecord2(value)) {
870
+ return false;
871
+ }
872
+ const keys = Object.keys(value);
873
+ if (keys.length !== 2 || !keys.every((key) => ["categoryIds", "nonMatchingObjects"].includes(key)) || !Array.isArray(value.categoryIds) || !value.categoryIds.every((id) => typeof id === "string" && id.length > 0) || new Set(value.categoryIds).size !== value.categoryIds.length) {
874
+ return false;
875
+ }
876
+ return value.nonMatchingObjects === "hidden" || value.nonMatchingObjects === "disabled";
877
+ }
878
+ function hasValidOptions(config) {
879
+ return (config.colorScheme === void 0 || isSeatmapColorScheme(config.colorScheme)) && (config.sectionContents === void 0 || isSectionContentsMode(config.sectionContents)) && (config.categoryFilter === void 0 || isCategoryFilter(config.categoryFilter)) && (config.startFloor === void 0 || typeof config.startFloor === "string") && (config.lockedFloor === void 0 || typeof config.lockedFloor === "string") && (config.scrollZoom === void 0 || typeof config.scrollZoom === "boolean") && (config.showFloorPicker === void 0 || typeof config.showFloorPicker === "boolean") && (config.showActiveBlockOnMobile === void 0 || typeof config.showActiveBlockOnMobile === "boolean") && (config.legend === void 0 || isSeatmapLegendConfig(config.legend));
880
+ }
881
+ function copyCategoryFilter(filter) {
882
+ return {
883
+ categoryIds: [...filter.categoryIds],
884
+ nonMatchingObjects: filter.nonMatchingObjects
885
+ };
886
+ }
887
+ function hasValidClassicAuthority(config) {
888
+ return typeof config.sessionId === "string" && config.sessionId.length > 0 && typeof config.clientToken === "string" && config.clientToken.length > 0 && config.season === void 0 && config.tokenProvider === void 0 && config.holdId === void 0 && (config.onSeatSelectionChange === void 0 || typeof config.onSeatSelectionChange === "function") && hasValidOptions(config);
889
+ }
890
+ function hasValidSeasonAuthority(config) {
891
+ return !("onSeatSelectionChange" in config && config.onSeatSelectionChange !== void 0) && config.sessionId === void 0 && config.clientToken === void 0 && typeof config.season === "object" && config.season !== null && typeof config.season.seasonKey === "string" && config.season.seasonKey.length > 0 && typeof config.tokenProvider === "function" && hasValidOptions(config);
892
+ }
893
+ function validateMountConfig(config) {
894
+ return hasValidClassicAuthority(config) || hasValidSeasonAuthority(config) ? void 0 : invalidConfigError();
895
+ }
896
+ function classicHelloParams(config) {
897
+ const params = {
898
+ sessionId: config.sessionId,
899
+ clientToken: config.clientToken
900
+ };
901
+ if (config.channelKeys !== void 0) {
902
+ params.channelKeys = config.channelKeys;
903
+ }
904
+ if (config.includePublic !== void 0) {
905
+ params.includePublic = config.includePublic;
906
+ }
907
+ if (config.categoryFilter !== void 0) {
908
+ params.categoryFilter = copyCategoryFilter(config.categoryFilter);
909
+ }
910
+ if (config.sectionContents !== void 0)
911
+ params.sectionContents = config.sectionContents;
912
+ if (config.startFloor !== void 0) params.startFloor = config.startFloor;
913
+ if (config.lockedFloor !== void 0) params.lockedFloor = config.lockedFloor;
914
+ if (config.showFloorPicker !== void 0)
915
+ params.showFloorPicker = config.showFloorPicker;
916
+ if (config.scrollZoom !== void 0) params.scrollZoom = config.scrollZoom;
917
+ if (config.colorScheme !== void 0) params.colorScheme = config.colorScheme;
918
+ if (config.legend !== void 0) params.legend = { ...config.legend };
919
+ if (config.showActiveBlockOnMobile !== void 0)
920
+ params.showActiveBlockOnMobile = config.showActiveBlockOnMobile;
921
+ return {
922
+ ...params,
923
+ ...config.onSeatSelectionChange ? { confirmSeatSelection: true } : {}
924
+ };
925
+ }
926
+ function seasonHelloParams(config) {
927
+ const params = {
928
+ mode: "season",
929
+ seasonKey: config.season.seasonKey
930
+ };
931
+ if (config.channelKeys !== void 0) {
932
+ params.channelKeys = config.channelKeys;
933
+ }
934
+ if (config.includePublic !== void 0) {
935
+ params.includePublic = config.includePublic;
936
+ }
937
+ if (config.categoryFilter !== void 0) {
938
+ params.categoryFilter = copyCategoryFilter(config.categoryFilter);
939
+ }
940
+ if (config.sectionContents !== void 0)
941
+ params.sectionContents = config.sectionContents;
942
+ if (config.startFloor !== void 0) params.startFloor = config.startFloor;
943
+ if (config.lockedFloor !== void 0) params.lockedFloor = config.lockedFloor;
944
+ if (config.showFloorPicker !== void 0)
945
+ params.showFloorPicker = config.showFloorPicker;
946
+ if (config.scrollZoom !== void 0) params.scrollZoom = config.scrollZoom;
947
+ if (config.colorScheme !== void 0) params.colorScheme = config.colorScheme;
948
+ if (config.legend !== void 0) params.legend = { ...config.legend };
949
+ if (config.showActiveBlockOnMobile !== void 0)
950
+ params.showActiveBlockOnMobile = config.showActiveBlockOnMobile;
951
+ return params;
952
+ }
953
+ function startSeatmapSession({
954
+ transport,
955
+ channelId,
956
+ config,
957
+ onHandshake,
958
+ onTerminate
959
+ }) {
960
+ const endpoint = createEndpoint({ transport, channelId });
961
+ const isSeasonMount = config.season !== void 0;
962
+ if ("onSeatSelectionChange" in config && config.onSeatSelectionChange) {
963
+ const confirm = config.onSeatSelectionChange;
964
+ endpoint.handle("selection.confirmSeats", async (params) => {
965
+ if (!isRecord2(params) || !Array.isArray(params.seatIds) || params.seatIds.length === 0 || !params.seatIds.every((id) => typeof id === "string" && id.length > 0) || typeof params.selected !== "boolean") {
966
+ throw new RpcError(
967
+ "RPC_INVALID_ARGS",
968
+ "Invalid seat selection request"
969
+ );
970
+ }
971
+ await confirm({
972
+ seatIds: [...params.seatIds],
973
+ selected: params.selected
974
+ });
975
+ });
976
+ }
977
+ if (isSeasonMount && "season" in config) {
978
+ const { holdId, tokenProvider } = config;
979
+ endpoint.handle("season.getToken", () => tokenProvider());
980
+ endpoint.handle("season.getHoldId", () => holdId);
981
+ }
982
+ const chart = createChartHandle(
983
+ endpoint,
984
+ onTerminate,
985
+ isSeasonMount && "season" in config ? {
986
+ initialSeasonHold: {
987
+ status: "idle",
988
+ seasonKey: config.season.seasonKey
989
+ }
990
+ } : {
991
+ confirmSeatSelection: "onSeatSelectionChange" in config && Boolean(config.onSeatSelectionChange)
992
+ }
993
+ );
994
+ let detachHandshake;
995
+ const removeHandshakeListener = () => {
996
+ detachHandshake?.();
997
+ detachHandshake = void 0;
998
+ };
999
+ const ready = new Promise((resolve, reject) => {
1000
+ detachHandshake = onHandshake((data) => {
1001
+ if (!isRecord2(data) || data.channelId !== channelId || data.type !== "hello" || typeof data.protocolVersion !== "number") {
1002
+ return;
1003
+ }
1004
+ removeHandshakeListener();
1005
+ if (data.protocolVersion !== PROTOCOL_VERSION) {
1006
+ reject(
1007
+ new RpcError(
1008
+ "ENGINE_INCOMPATIBLE",
1009
+ `Engine uses unsupported protocol version ${data.protocolVersion}`
1010
+ )
1011
+ );
1012
+ return;
1013
+ }
1014
+ if (isEnvelope(data) && data.error) {
1015
+ reject(
1016
+ new RpcError(
1017
+ data.error.code,
1018
+ data.error.message || "Engine rejected the handshake",
1019
+ data.error.method
1020
+ )
1021
+ );
1022
+ return;
1023
+ }
1024
+ if (!isEnvelope(data) || !isHandshakeAcknowledgement(data.result)) {
1025
+ reject(
1026
+ new RpcError(
1027
+ "ENGINE_INCOMPATIBLE",
1028
+ "Engine acknowledged with an incompatible protocol version"
1029
+ )
1030
+ );
1031
+ return;
1032
+ }
1033
+ resolve(chart);
1034
+ });
1035
+ });
1036
+ transport.send({
1037
+ protocolVersion: PROTOCOL_VERSION,
1038
+ channelId,
1039
+ type: "hello",
1040
+ params: config.season !== void 0 && "season" in config ? seasonHelloParams(config) : classicHelloParams(config)
1041
+ });
1042
+ return {
1043
+ ready,
1044
+ close() {
1045
+ removeHandshakeListener();
1046
+ endpoint.close();
1047
+ }
1048
+ };
1049
+ }
1050
+
427
1051
  // src/engineUrl.ts
428
- var ENGINE_ARTIFACT = "e452ecefc6c565f3";
1052
+ var ENGINE_ARTIFACT = "b5c64dede4a9bc1a";
429
1053
  var CDN_BASE = "https://dev-seatmap.knottech.ai";
430
1054
  function resolveEngineUrl({
431
1055
  artifact,
@@ -444,19 +1068,14 @@ function engineOrigin(engineUrl) {
444
1068
  }
445
1069
  return url.origin;
446
1070
  }
1071
+ function getSeatmapEngineOrigin() {
1072
+ return engineOrigin(
1073
+ resolveEngineUrl({ artifact: ENGINE_ARTIFACT, cdnBase: CDN_BASE })
1074
+ );
1075
+ }
447
1076
 
448
1077
  // src/mountSeatmap.ts
449
1078
  var HANDSHAKE_TIMEOUT_MS = 5e3;
450
- function isRecord2(value) {
451
- return typeof value === "object" && value !== null && !Array.isArray(value);
452
- }
453
- function isHandshakeAcknowledgement(value) {
454
- if (typeof value !== "object" || value === null) {
455
- return false;
456
- }
457
- const result = value;
458
- return result.protocolVersion === PROTOCOL_VERSION;
459
- }
460
1079
  function unreachableError(artifact) {
461
1080
  return new RpcError(
462
1081
  "ENGINE_UNREACHABLE",
@@ -472,6 +1091,10 @@ function configureIframe(iframe, src) {
472
1091
  iframe.style.border = "0";
473
1092
  }
474
1093
  function mountSeatmap(container, config, signal) {
1094
+ const configError = validateMountConfig(config);
1095
+ if (configError) {
1096
+ return Promise.reject(configError);
1097
+ }
475
1098
  return new Promise((resolve, reject) => {
476
1099
  const artifact = ENGINE_ARTIFACT;
477
1100
  const engineUrl = resolveEngineUrl({ artifact, cdnBase: CDN_BASE });
@@ -479,9 +1102,8 @@ function mountSeatmap(container, config, signal) {
479
1102
  const channelId = crypto.randomUUID();
480
1103
  const iframe = document.createElement("iframe");
481
1104
  configureIframe(iframe, engineUrl);
482
- let endpoint;
1105
+ let session;
483
1106
  let transport;
484
- let detachHandshake;
485
1107
  let channelStarted = false;
486
1108
  let settled = false;
487
1109
  const timer = window.setTimeout(() => {
@@ -497,14 +1119,9 @@ function mountSeatmap(container, config, signal) {
497
1119
  iframe.removeEventListener("error", handleError);
498
1120
  signal?.removeEventListener("abort", handleAbort);
499
1121
  }
500
- function removeHandshakeListener() {
501
- detachHandshake?.();
502
- detachHandshake = void 0;
503
- }
504
1122
  function closeChannel() {
505
- removeHandshakeListener();
506
- if (endpoint) {
507
- endpoint.close();
1123
+ if (session) {
1124
+ session.close();
508
1125
  } else {
509
1126
  transport?.close();
510
1127
  }
@@ -540,58 +1157,37 @@ function mountSeatmap(container, config, signal) {
540
1157
  return;
541
1158
  }
542
1159
  transport = createWindowTransport({ target, targetOrigin, channelId });
543
- endpoint = createEndpoint({ transport, channelId });
544
- const chart = createChartHandle(endpoint, () => {
545
- iframe.remove();
546
- });
547
- const receiveHandshake = (event) => {
548
- if (event.origin !== targetOrigin || event.source !== target || !isRecord2(event.data) || event.data.channelId !== channelId || event.data.type !== "hello" || typeof event.data.protocolVersion !== "number") {
549
- return;
550
- }
551
- if (event.data.protocolVersion !== PROTOCOL_VERSION) {
552
- fail(
553
- new RpcError(
554
- "ENGINE_INCOMPATIBLE",
555
- `Engine uses unsupported protocol version ${event.data.protocolVersion}`
556
- )
557
- );
558
- return;
559
- }
560
- if (isEnvelope(event.data) && event.data.error) {
561
- fail(
562
- new RpcError(
563
- event.data.error.code,
564
- event.data.error.message || "Engine rejected the handshake",
565
- event.data.error.method
566
- )
567
- );
568
- return;
569
- }
570
- if (!isEnvelope(event.data) || !isHandshakeAcknowledgement(event.data.result)) {
571
- fail(
572
- new RpcError(
573
- "ENGINE_INCOMPATIBLE",
574
- "Engine acknowledged with an incompatible protocol version"
575
- )
576
- );
577
- return;
578
- }
579
- settled = true;
580
- window.clearTimeout(timer);
581
- removeMountListeners();
582
- removeHandshakeListener();
583
- resolve(chart);
584
- };
585
- window.addEventListener("message", receiveHandshake);
586
- detachHandshake = () => {
587
- window.removeEventListener("message", receiveHandshake);
588
- };
589
- transport.send({
590
- protocolVersion: PROTOCOL_VERSION,
1160
+ session = startSeatmapSession({
1161
+ transport,
591
1162
  channelId,
592
- type: "hello",
593
- params: config
1163
+ config,
1164
+ onHandshake: (listener) => {
1165
+ const receive = (event) => {
1166
+ if (event.origin === targetOrigin && event.source === target) {
1167
+ listener(event.data);
1168
+ }
1169
+ };
1170
+ window.addEventListener("message", receive);
1171
+ return () => {
1172
+ window.removeEventListener("message", receive);
1173
+ };
1174
+ },
1175
+ onTerminate: () => {
1176
+ iframe.remove();
1177
+ }
594
1178
  });
1179
+ session.ready.then(
1180
+ (chart) => {
1181
+ if (settled) {
1182
+ return;
1183
+ }
1184
+ settled = true;
1185
+ window.clearTimeout(timer);
1186
+ removeMountListeners();
1187
+ resolve(chart);
1188
+ },
1189
+ (error) => fail(error)
1190
+ );
595
1191
  }
596
1192
  iframe.addEventListener("load", handleLoad);
597
1193
  iframe.addEventListener("error", handleError);
@@ -603,6 +1199,7 @@ function mountSeatmap(container, config, signal) {
603
1199
  });
604
1200
  }
605
1201
  export {
1202
+ getSeatmapEngineOrigin,
606
1203
  mountSeatmap
607
1204
  };
608
1205
  //# sourceMappingURL=index.js.map