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