@seatmap-fyi/map-domain 0.1.0-alpha.2 → 0.1.0-alpha.4

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/schema.js CHANGED
@@ -1,5 +1,7 @@
1
- import { DocumentLimits, NumberingScheme, LabelDirection, RowLabelPosition, SeatingKind, CurveMode, DecorationKind, LabelAlignment, RowAlignment, SectionOutlineRole, AreaKind, ShapeKind, VenueGlyph, } from "./types.js";
1
+ import { DocumentLimits, TicketRestriction, TableBookingMode, TableChairDistribution, StandingBookingMode, SeatShape, TicketOptionLimit, NumberingScheme, LabelDirection, RowLabelPosition, AccessNeed, SeatingKind, CurveMode, DecorationKind, LabelAlignment, RowAlignment, RowEnds, RowJoin, SectionOutlineRole, AreaKind, ShapeKind, VenueGlyph, } from "./types.js";
2
2
  import { z } from "zod";
3
+ import { tableSellingValid, standingSellingValid } from "./selling.js";
4
+ import { categoryIssues, ticketOptionNameKey } from "./categories.js";
3
5
  import { issue, IssueCode } from "./issues.js";
4
6
  import { footprint, generateGroup, project, worldMatrices, } from "./geometry/geometry.js";
5
7
  const uuid = z
@@ -52,27 +54,70 @@ const paint = z.strictObject({
52
54
  const sequence = z
53
55
  .strictObject({
54
56
  scheme: z.enum(NumberingScheme),
55
- startAt: finite.int().min(1).max(1e9),
57
+ startAt: finite.int().min(0).max(1e9),
56
58
  direction: z.enum(LabelDirection),
57
59
  prefix: text(32).optional(),
58
60
  suffix: text(32).optional(),
61
+ numberFrom: z.enum(["first", "last"]).optional(),
62
+ lastAt: finite.int().min(0).max(1e9).optional(),
59
63
  })
60
- .refine((v) => v.scheme !== NumberingScheme.Odd || v.startAt % 2 === 1, "Odd numbering must start at an odd integer.")
61
- .refine((v) => v.scheme !== NumberingScheme.Even || v.startAt % 2 === 0, "Even numbering must start at an even integer.")
62
- .describe("Odd/even numbering requires a matching odd/even startAt value.");
64
+ .superRefine((value, context) => {
65
+ if (value.numberFrom === "last" && value.lastAt === undefined)
66
+ context.addIssue({
67
+ code: "custom",
68
+ path: ["lastAt"],
69
+ message: "Last-seat anchoring requires a last number.",
70
+ });
71
+ if (value.scheme === NumberingScheme.Roman &&
72
+ (value.startAt > 999 || (value.lastAt ?? 0) > 100999))
73
+ context.addIssue({
74
+ code: "custom",
75
+ path: ["startAt"],
76
+ message: "Roman start numbers must be at most 999.",
77
+ });
78
+ });
63
79
  const labelStyle = z.strictObject({
64
80
  visible: z.boolean(),
65
81
  fontSizeM: dimension,
82
+ style: z.enum(["plain", "pill"]).optional(),
83
+ rotationDeg: angle.optional(),
84
+ color: z
85
+ .string()
86
+ .regex(/^#[0-9a-fA-F]{6}$/)
87
+ .optional(),
88
+ });
89
+ const rowLabelStyle = labelStyle.extend({
90
+ position: z.enum(RowLabelPosition),
91
+ style: z.enum(["plain", "pill"]).optional(),
92
+ rotationDeg: angle.optional(),
93
+ });
94
+ export const seatPresentationSchema = z.strictObject({
95
+ gap: z.boolean().optional(),
96
+ buyerNote: text(160).optional(),
97
+ labelStyle: labelStyle.optional(),
98
+ });
99
+ export const rowPresentationSchema = z.strictObject({
100
+ buyerNoun: text(80).optional(),
101
+ buyerNote: text(160).optional(),
102
+ labelStyle: rowLabelStyle.optional(),
66
103
  });
104
+ export const seatShapeSchema = z.enum(SeatShape);
67
105
  const seat = z.strictObject({
106
+ ...seatPresentationSchema.shape,
107
+ seatShape: seatShapeSchema.optional(),
68
108
  id: uuid,
109
+ categoryId: uuid.nullable().optional(),
69
110
  labelOverride: label.optional(),
70
111
  attributes: z
71
112
  .strictObject({
72
- accessible: z.boolean().optional(),
113
+ wheelchair: z.enum(["seat", "space"]).optional(),
114
+ accessNeeds: z
115
+ .array(z.enum(AccessNeed))
116
+ .max(Object.keys(AccessNeed).length)
117
+ .optional(),
118
+ obstructedView: z.boolean().optional(),
119
+ premium: z.boolean().optional(),
73
120
  restrictedView: z.boolean().optional(),
74
- hearingAssistance: z.boolean().optional(),
75
- semiAmbulatory: z.boolean().optional(),
76
121
  companionForSeatId: uuid.optional(),
77
122
  })
78
123
  .optional(),
@@ -84,11 +129,23 @@ const seat = z.strictObject({
84
129
  })
85
130
  .optional(),
86
131
  });
132
+ export const rowLayoutSchema = z.strictObject({
133
+ originM: point,
134
+ seatPitchM: dimension,
135
+ curveDeg: finite.min(-360).max(360),
136
+ rotationDeg: finite.min(-180).max(180),
137
+ seatRotationOffsetDeg: finite.min(-180).max(180).optional(),
138
+ });
87
139
  const row = z.strictObject({
140
+ ...rowPresentationSchema.shape,
88
141
  id: uuid,
142
+ categoryId: uuid.nullable().optional(),
89
143
  labelOverride: label.optional(),
144
+ bookingLabel: label.optional(),
90
145
  offsetM: point.optional(),
91
146
  seatSequenceOverride: sequence.optional(),
147
+ layout: rowLayoutSchema.optional(),
148
+ aisleSeats: z.enum(RowEnds).optional(),
92
149
  seats: z.array(seat).min(1).max(DocumentLimits.PhysicalSeats),
93
150
  });
94
151
  const drawable = {
@@ -97,8 +154,30 @@ const drawable = {
97
154
  transform,
98
155
  zIndex: finite.int().min(-2147483648).max(2147483647),
99
156
  };
157
+ const partySize = {
158
+ minGuests: finite.int().min(1).max(DocumentLimits.PhysicalSeats),
159
+ maxGuests: finite.int().min(1).max(DocumentLimits.PhysicalSeats),
160
+ };
161
+ const tableSelling = z.discriminatedUnion("mode", [
162
+ z.strictObject({
163
+ mode: z.enum([TableBookingMode.Individual, TableBookingMode.Whole]),
164
+ }),
165
+ z.strictObject({ mode: z.literal(TableBookingMode.Party), ...partySize }),
166
+ ]);
167
+ const standingSelling = z.discriminatedUnion("mode", [
168
+ z.strictObject({
169
+ mode: z.enum([StandingBookingMode.Standing, StandingBookingMode.Whole]),
170
+ wheelchairPlaces: finite.int().min(0).max(2147483647),
171
+ }),
172
+ z.strictObject({
173
+ mode: z.literal(StandingBookingMode.Party),
174
+ wheelchairPlaces: finite.int().min(0).max(2147483647),
175
+ ...partySize,
176
+ }),
177
+ ]);
100
178
  const groupBase = {
101
179
  ...drawable,
180
+ categoryId: uuid.nullable().optional(),
102
181
  name,
103
182
  sectionId: uuid.nullable(),
104
183
  seatSizeM: size,
@@ -108,17 +187,51 @@ const groupBase = {
108
187
  const rowsLabels = z.strictObject({
109
188
  rows: sequence,
110
189
  seats: sequence,
111
- rowStyle: z.strictObject({
112
- visible: z.boolean(),
113
- fontSizeM: dimension,
114
- position: z.enum(RowLabelPosition),
115
- }),
190
+ rowStyle: rowLabelStyle,
191
+ });
192
+ export const segmentedRowSchema = z.strictObject({
193
+ id: uuid,
194
+ label,
195
+ sequence,
196
+ smoothCorners: z.boolean().optional(),
197
+ parts: z
198
+ .array(z.strictObject({
199
+ rowId: uuid,
200
+ joinBefore: z.enum(RowJoin).nullable(),
201
+ }))
202
+ .min(2)
203
+ .max(DocumentLimits.PhysicalSeats),
116
204
  });
117
205
  const tableLabels = z.strictObject({
118
206
  table: label,
119
207
  chairs: sequence,
120
208
  tableStyle: labelStyle,
121
209
  });
210
+ const roundTableRecipe = z.strictObject({
211
+ diameterM: dimension,
212
+ chairGapM: nonNegative,
213
+ startAngleDeg: angle,
214
+ automaticSize: z.boolean().optional(),
215
+ seatArcDeg: z.number().finite().min(120).max(360).optional(),
216
+ });
217
+ const rectangularTableRecipe = z.strictObject({
218
+ widthM: dimension,
219
+ depthM: dimension,
220
+ chairGapM: nonNegative,
221
+ chairPitchM: dimension.optional(),
222
+ chairDistribution: z.enum(TableChairDistribution).optional(),
223
+ });
224
+ const longTableLayout = z
225
+ .strictObject({
226
+ recipe: rectangularTableRecipe,
227
+ sides: z.strictObject({
228
+ top: z.boolean(),
229
+ bottom: z.boolean(),
230
+ left: z.boolean(),
231
+ right: z.boolean(),
232
+ }),
233
+ })
234
+ .refine((value) => Object.values(value.sides).some(Boolean), "Choose at least one table side.");
122
235
  export const groupSchema = z.discriminatedUnion("kind", [
123
236
  z.strictObject({
124
237
  ...groupBase,
@@ -161,12 +274,9 @@ export const groupSchema = z.discriminatedUnion("kind", [
161
274
  .strictObject({
162
275
  ...groupBase,
163
276
  kind: z.literal(SeatingKind.Rectangular),
164
- recipe: z.strictObject({
165
- widthM: dimension,
166
- depthM: dimension,
167
- chairGapM: nonNegative,
168
- chairPitchM: dimension.optional(),
169
- }),
277
+ selling: tableSelling.optional(),
278
+ recipe: rectangularTableRecipe,
279
+ roundLayout: roundTableRecipe.optional(),
170
280
  labels: tableLabels,
171
281
  chairsBySide: z.strictObject({
172
282
  top: z.array(seat).max(DocumentLimits.PhysicalSeats),
@@ -175,18 +285,19 @@ export const groupSchema = z.discriminatedUnion("kind", [
175
285
  left: z.array(seat).max(DocumentLimits.PhysicalSeats),
176
286
  }),
177
287
  })
178
- .refine((v) => Object.values(v.chairsBySide).some((s) => s.length > 0), "A table needs at least one chair."),
179
- z.strictObject({
288
+ .refine((v) => Object.values(v.chairsBySide).some((s) => s.length > 0), "A table needs at least one chair.")
289
+ .refine(tableSellingValid, "Table selling requires valid guest limits and no personal chair settings."),
290
+ z
291
+ .strictObject({
180
292
  ...groupBase,
181
293
  kind: z.literal(SeatingKind.Round),
182
- recipe: z.strictObject({
183
- diameterM: dimension,
184
- chairGapM: nonNegative,
185
- startAngleDeg: angle,
186
- }),
294
+ selling: tableSelling.optional(),
295
+ recipe: roundTableRecipe,
296
+ longLayout: longTableLayout.optional(),
187
297
  labels: tableLabels,
188
298
  chairs: z.array(seat).min(1).max(DocumentLimits.PhysicalSeats),
189
- }),
299
+ })
300
+ .refine(tableSellingValid, "Table selling requires valid guest limits and no personal chair settings."),
190
301
  ]);
191
302
  export const decorationStyleSchema = z.strictObject({
192
303
  fill: paint.optional(),
@@ -242,9 +353,9 @@ const decoration = z.discriminatedUnion("kind", [
242
353
  opacity: finite.min(0).max(1),
243
354
  }),
244
355
  ]);
245
- const area = z.strictObject({
356
+ const areaBase = {
246
357
  ...drawable,
247
- kind: z.literal(AreaKind.GeneralAdmission),
358
+ categoryId: uuid.nullable().optional(),
248
359
  sectionId: uuid.nullable(),
249
360
  label,
250
361
  labelStyle,
@@ -260,6 +371,70 @@ const area = z.strictObject({
260
371
  .refine((v) => v.cornerRadiusM <= Math.min(v.widthM, v.heightM) / 2),
261
372
  z.strictObject({ kind: z.literal(ShapeKind.Polygon), boundary: polygon }),
262
373
  ]),
374
+ };
375
+ const area = z.discriminatedUnion("kind", [
376
+ z
377
+ .strictObject({
378
+ ...areaBase,
379
+ kind: z.literal(AreaKind.GeneralAdmission),
380
+ selling: standingSelling.optional(),
381
+ })
382
+ .refine(standingSellingValid, "Standing selling must fit its capacity and one-party limit."),
383
+ z.strictObject({ ...areaBase, kind: z.literal(AreaKind.Booth) }),
384
+ ]);
385
+ const templatePrice = finite.min(0).max(1_000_000_000);
386
+ export const ticketOptionSchema = z.strictObject({
387
+ id: uuid,
388
+ name: text(80).refine((value) => value.length > 0 && value === value.trim(), "Use a trimmed, nonempty option name."),
389
+ price: templatePrice,
390
+ restriction: z.enum(TicketRestriction).optional(),
391
+ guidance: text(240, true).optional(),
392
+ });
393
+ export const categorySchema = z
394
+ .strictObject({
395
+ id: uuid,
396
+ name,
397
+ color: z.string().regex(/^#[0-9a-fA-F]{6}$/),
398
+ defaultPrice: templatePrice.nullable().optional(),
399
+ onSale: z.boolean().optional(),
400
+ seatShape: seatShapeSchema.optional(),
401
+ ticketOptions: z
402
+ .array(ticketOptionSchema)
403
+ .max(TicketOptionLimit)
404
+ .optional(),
405
+ })
406
+ .superRefine((category, context) => {
407
+ const names = new Set();
408
+ for (const [index, option] of (category.ticketOptions ?? []).entries()) {
409
+ const key = ticketOptionNameKey(option.name);
410
+ if (names.has(key))
411
+ context.addIssue({
412
+ code: "custom",
413
+ path: ["ticketOptions", index, "name"],
414
+ message: `${option.name} is already an option here`,
415
+ });
416
+ names.add(key);
417
+ }
418
+ });
419
+ export const categoriesSchema = z
420
+ .array(categorySchema)
421
+ .max(1024)
422
+ .superRefine((categories, context) => {
423
+ const ids = new Set();
424
+ for (const [index, category] of categories.entries()) {
425
+ for (const id of [
426
+ category.id,
427
+ ...(category.ticketOptions ?? []).map((option) => option.id),
428
+ ]) {
429
+ if (ids.has(id))
430
+ context.addIssue({
431
+ code: "custom",
432
+ path: [index],
433
+ message: "Category and ticket-option identities must be unique.",
434
+ });
435
+ ids.add(id);
436
+ }
437
+ }
263
438
  });
264
439
  export const documentSchema = z.strictObject({
265
440
  schemaVersion: z.literal(1),
@@ -272,10 +447,12 @@ export const documentSchema = z.strictObject({
272
447
  rotation: z.literal("clockwise"),
273
448
  }),
274
449
  background: paint,
450
+ categories: categoriesSchema.optional(),
275
451
  sections: z
276
452
  .array(z.strictObject({
277
453
  id: uuid,
278
454
  label,
455
+ categoryId: uuid.nullable().optional(),
279
456
  displayLabel: text(80, true).optional(),
280
457
  outline: polygon.optional(),
281
458
  outlineRole: z.enum(SectionOutlineRole).optional(),
@@ -292,6 +469,10 @@ export const documentSchema = z.strictObject({
292
469
  .max(1024),
293
470
  collections: z.array(z.strictObject({ ...drawable, name })).max(10000),
294
471
  seatingGroups: z.array(groupSchema).max(DocumentLimits.PhysicalSeats),
472
+ segmentedRows: z
473
+ .array(segmentedRowSchema)
474
+ .max(DocumentLimits.PhysicalSeats)
475
+ .optional(),
295
476
  areas: z.array(area).max(10000),
296
477
  decorations: z.array(decoration).max(10000),
297
478
  assets: z
@@ -358,14 +539,38 @@ export function* seatSlots(group) {
358
539
  for (const side of ["top", "right", "bottom", "left"])
359
540
  yield* group.chairsBySide[side];
360
541
  }
542
+ const inventoryCounts = new WeakMap();
543
+ function slotInventoryCount(slots) {
544
+ let count = inventoryCounts.get(slots);
545
+ if (count === undefined) {
546
+ count = 0;
547
+ for (const slot of slots)
548
+ if (!slot.gap)
549
+ count++;
550
+ inventoryCounts.set(slots, count);
551
+ }
552
+ return count;
553
+ }
554
+ export function groupInventoryCount(group) {
555
+ if ("rows" in group)
556
+ return group.rows.reduce((sum, row) => sum + slotInventoryCount(row.seats), 0);
557
+ if ("chairs" in group)
558
+ return slotInventoryCount(group.chairs);
559
+ return Object.values(group.chairsBySide).reduce((sum, seats) => sum + slotInventoryCount(seats), 0);
560
+ }
361
561
  export function capacity(document) {
362
- return {
363
- physicalSeats: document.seatingGroups.reduce((n, g) => n + groupSeatCount(g), 0),
364
- gaQuantity: document.areas.reduce((n, a) => n + a.capacity, 0),
365
- };
562
+ let physicalSeats = document.seatingGroups.reduce((sum, group) => sum + groupInventoryCount(group), 0);
563
+ let gaQuantity = 0;
564
+ for (const area of document.areas) {
565
+ if (area.kind === AreaKind.Booth)
566
+ physicalSeats++;
567
+ else
568
+ gaQuantity += area.capacity;
569
+ }
570
+ return { physicalSeats, gaQuantity };
366
571
  }
367
572
  export function structuralIssues(document, changedGroups) {
368
- const errors = [];
573
+ const errors = categoryIssues(document);
369
574
  const ids = new Set([document.mapId]);
370
575
  const add = (id) => {
371
576
  if (ids.has(id))
@@ -385,6 +590,11 @@ export function structuralIssues(document, changedGroups) {
385
590
  add(s.id);
386
591
  visitPolygon(s.outline);
387
592
  }
593
+ for (const category of document.categories ?? []) {
594
+ add(category.id);
595
+ for (const option of category.ticketOptions ?? [])
596
+ add(option.id);
597
+ }
388
598
  for (const a of document.assets)
389
599
  add(a.id);
390
600
  for (const object of [
@@ -424,6 +634,25 @@ export function structuralIssues(document, changedGroups) {
424
634
  for (const s of generateGroup(g))
425
635
  add(s.id);
426
636
  }
637
+ const rowOwners = new Map(document.seatingGroups.flatMap((group) => "rows" in group
638
+ ? group.rows.map((row) => [row.id, { group, row }])
639
+ : []));
640
+ const assignedRows = new Set();
641
+ for (const logical of document.segmentedRows ?? []) {
642
+ add(logical.id);
643
+ const sectionId = rowOwners.get(logical.parts[0].rowId)?.group.sectionId;
644
+ for (let index = 0; index < logical.parts.length; index++) {
645
+ const part = logical.parts[index];
646
+ const owner = rowOwners.get(part.rowId);
647
+ if (!owner ||
648
+ assignedRows.has(part.rowId) ||
649
+ !owner.row.layout ||
650
+ owner.group.sectionId !== sectionId ||
651
+ (index === 0 ? part.joinBefore !== null : part.joinBefore === null))
652
+ errors.push(issue(IssueCode.InvalidStructure, "Each row part must have independent geometry, one logical owner, a valid join and the same section.", [logical.id, part.rowId]));
653
+ assignedRows.add(part.rowId);
654
+ }
655
+ }
427
656
  for (const a of document.areas)
428
657
  if (a.shape.kind === ShapeKind.Polygon)
429
658
  visitPolygon(a.shape.boundary);
@@ -433,7 +662,9 @@ export function structuralIssues(document, changedGroups) {
433
662
  else if (d.kind === DecorationKind.Polygon)
434
663
  visitPolygon(d.boundary);
435
664
  const counts = capacity(document);
436
- if (counts.physicalSeats > DocumentLimits.PhysicalSeats ||
665
+ if (document.seatingGroups.reduce((total, group) => total + groupSeatCount(group), 0) +
666
+ document.areas.filter((area) => area.kind === AreaKind.Booth).length >
667
+ DocumentLimits.PhysicalSeats ||
437
668
  !Number.isSafeInteger(counts.gaQuantity + counts.physicalSeats) ||
438
669
  vertices > 100000)
439
670
  errors.push(issue(IssueCode.Limit, "Document exceeds its inventory, capacity or vertex safety limit.", [document.mapId]));
@@ -458,13 +689,14 @@ export function structuralIssues(document, changedGroups) {
458
689
  points = d.pointsM;
459
690
  else if (d.kind === DecorationKind.Polygon)
460
691
  points = [...d.boundary.outer, ...d.boundary.holes.flat()];
461
- else if (d.kind === AreaKind.GeneralAdmission &&
692
+ else if ((d.kind === AreaKind.GeneralAdmission || d.kind === AreaKind.Booth) &&
462
693
  d.shape.kind === ShapeKind.Polygon)
463
694
  points = [...d.shape.boundary.outer, ...d.shape.boundary.holes.flat()];
464
695
  else {
465
696
  const w = d.kind === DecorationKind.Circle
466
697
  ? d.radiusM * 2
467
- : d.kind === AreaKind.GeneralAdmission &&
698
+ : (d.kind === AreaKind.GeneralAdmission ||
699
+ d.kind === AreaKind.Booth) &&
468
700
  d.shape.kind === ShapeKind.Rectangle
469
701
  ? d.shape.widthM
470
702
  : "sizeM" in d
@@ -472,7 +704,8 @@ export function structuralIssues(document, changedGroups) {
472
704
  : 0;
473
705
  const h = d.kind === DecorationKind.Circle
474
706
  ? d.radiusM * 2
475
- : d.kind === AreaKind.GeneralAdmission &&
707
+ : (d.kind === AreaKind.GeneralAdmission ||
708
+ d.kind === AreaKind.Booth) &&
476
709
  d.shape.kind === ShapeKind.Rectangle
477
710
  ? d.shape.heightM
478
711
  : "sizeM" in d
@@ -0,0 +1,34 @@
1
+ import { SeatingKind, TableBookingMode, StandingBookingMode, AreaKind, type SeatingGroup, type InventoryArea, type SeatSlot } from "./types.js";
2
+ export type TableInventory = Extract<SeatingGroup, {
3
+ kind: SeatingKind.Round | SeatingKind.Rectangular;
4
+ }>;
5
+ export type StandingInventory = Extract<InventoryArea, {
6
+ kind: AreaKind.GeneralAdmission;
7
+ }>;
8
+ export declare const StandingPartyLimit = 100;
9
+ export declare const StandingCapacityLimit = 100000;
10
+ /** An explicitly cleared tag still owns settings until Reset, as in the reference editor. */
11
+ export declare function seatHasOwnSettings(slot: SeatSlot): boolean;
12
+ export declare function tableSellingState(table: TableInventory): Readonly<{
13
+ mode: TableBookingMode;
14
+ chairCount: number;
15
+ hasPersonalSettings: boolean;
16
+ minGuests: number;
17
+ maxGuests: number;
18
+ }>;
19
+ export declare function tableSellingValid(table: TableInventory): boolean;
20
+ export declare function setTableBookingMode(table: TableInventory, mode: TableBookingMode): TableInventory;
21
+ export declare function resizeTableSelling(table: TableInventory): TableInventory;
22
+ export declare function setTablePartySize(table: TableInventory, field: "min" | "max", value: number): TableInventory;
23
+ export declare function tableAllowsChairEditing(group: SeatingGroup): boolean;
24
+ export declare function standingSellingState(area: StandingInventory): {
25
+ mode: StandingBookingMode;
26
+ wheelchairPlaces: number;
27
+ minGuests: number;
28
+ maxGuests: number;
29
+ };
30
+ export declare function standingSellingValid(area: StandingInventory): boolean;
31
+ export declare function setStandingBookingMode(area: StandingInventory, mode: StandingBookingMode): StandingInventory;
32
+ export declare function setStandingCapacity(area: StandingInventory, requested: number): StandingInventory;
33
+ export declare function setStandingPartySize(area: StandingInventory, field: "min" | "max", value: number): StandingInventory;
34
+ export declare function setStandingWheelchairPlaces(area: StandingInventory, requested: number): StandingInventory;
@@ -0,0 +1,163 @@
1
+ import { SeatingKind, TableBookingMode, StandingBookingMode, AreaKind, } from "./types.js";
2
+ export const StandingPartyLimit = 100;
3
+ export const StandingCapacityLimit = 100_000;
4
+ /** An explicitly cleared tag still owns settings until Reset, as in the reference editor. */
5
+ export function seatHasOwnSettings(slot) {
6
+ return Object.entries(slot).some(([key, value]) => key !== "id" && value !== undefined);
7
+ }
8
+ const tableStates = new WeakMap();
9
+ export function tableSellingState(table) {
10
+ const cached = tableStates.get(table);
11
+ if (cached)
12
+ return cached;
13
+ const sides = table.kind === SeatingKind.Round
14
+ ? [table.chairs]
15
+ : Object.values(table.chairsBySide);
16
+ let chairCount = 0, hasPersonalSettings = false;
17
+ for (const chairs of sides) {
18
+ chairCount += chairs.length;
19
+ if (!hasPersonalSettings)
20
+ hasPersonalSettings = chairs.some(seatHasOwnSettings);
21
+ }
22
+ const party = table.selling?.mode === TableBookingMode.Party ? table.selling : undefined;
23
+ const state = Object.freeze({
24
+ mode: table.selling?.mode ?? TableBookingMode.Individual,
25
+ chairCount,
26
+ hasPersonalSettings,
27
+ minGuests: party?.minGuests ?? 1,
28
+ maxGuests: party?.maxGuests ?? chairCount,
29
+ });
30
+ tableStates.set(table, state);
31
+ return state;
32
+ }
33
+ function resizedRange(range, capacity) {
34
+ const maxGuests = Math.min(capacity, range.maxGuests);
35
+ return { minGuests: Math.min(range.minGuests, maxGuests), maxGuests };
36
+ }
37
+ function editedRange(range, capacity, field, value) {
38
+ const current = resizedRange(range, capacity);
39
+ return field === "min"
40
+ ? {
41
+ ...current,
42
+ minGuests: Math.max(1, Math.min(current.maxGuests, Math.round(value))),
43
+ }
44
+ : {
45
+ ...current,
46
+ maxGuests: Math.max(current.minGuests, Math.min(capacity, Math.round(value))),
47
+ };
48
+ }
49
+ export function tableSellingValid(table) {
50
+ const state = tableSellingState(table);
51
+ if (state.mode !== TableBookingMode.Individual && state.hasPersonalSettings)
52
+ return false;
53
+ return (state.minGuests >= 1 &&
54
+ state.minGuests <= state.maxGuests &&
55
+ state.maxGuests <= state.chairCount);
56
+ }
57
+ export function setTableBookingMode(table, mode) {
58
+ const state = tableSellingState(table);
59
+ if (state.mode === mode ||
60
+ (mode !== TableBookingMode.Individual && state.hasPersonalSettings))
61
+ return table;
62
+ const selling = mode === TableBookingMode.Individual
63
+ ? undefined
64
+ : mode === TableBookingMode.Party
65
+ ? { mode, minGuests: 1, maxGuests: state.chairCount }
66
+ : { mode };
67
+ return { ...table, selling };
68
+ }
69
+ export function resizeTableSelling(table) {
70
+ if (table.selling?.mode !== TableBookingMode.Party)
71
+ return table;
72
+ const range = resizedRange(table.selling, tableSellingState(table).chairCount);
73
+ if (range.minGuests === table.selling.minGuests &&
74
+ range.maxGuests === table.selling.maxGuests)
75
+ return table;
76
+ return { ...table, selling: { ...table.selling, ...range } };
77
+ }
78
+ export function setTablePartySize(table, field, value) {
79
+ if (table.selling?.mode !== TableBookingMode.Party)
80
+ return table;
81
+ const range = editedRange(table.selling, tableSellingState(table).chairCount, field, value);
82
+ if (range.minGuests === table.selling.minGuests &&
83
+ range.maxGuests === table.selling.maxGuests)
84
+ return table;
85
+ return { ...table, selling: { ...table.selling, ...range } };
86
+ }
87
+ export function tableAllowsChairEditing(group) {
88
+ return (group.kind === SeatingKind.Straight ||
89
+ group.kind === SeatingKind.Curved ||
90
+ tableSellingState(group).mode === TableBookingMode.Individual);
91
+ }
92
+ export function standingSellingState(area) {
93
+ const party = area.selling?.mode === StandingBookingMode.Party ? area.selling : undefined;
94
+ return {
95
+ mode: area.selling?.mode ?? StandingBookingMode.Standing,
96
+ wheelchairPlaces: area.selling?.wheelchairPlaces ?? 0,
97
+ minGuests: party?.minGuests ?? 1,
98
+ maxGuests: party?.maxGuests ?? area.capacity,
99
+ };
100
+ }
101
+ export function standingSellingValid(area) {
102
+ const state = standingSellingState(area);
103
+ return (state.wheelchairPlaces <= area.capacity &&
104
+ (state.mode === StandingBookingMode.Standing ||
105
+ area.capacity <= StandingPartyLimit) &&
106
+ state.minGuests >= 1 &&
107
+ state.minGuests <= state.maxGuests &&
108
+ state.maxGuests <= area.capacity);
109
+ }
110
+ function standingConfiguration(mode, wheelchairPlaces, range) {
111
+ if (mode === StandingBookingMode.Standing && wheelchairPlaces === 0)
112
+ return undefined;
113
+ return mode === StandingBookingMode.Party
114
+ ? { mode, wheelchairPlaces, ...range }
115
+ : { mode, wheelchairPlaces };
116
+ }
117
+ export function setStandingBookingMode(area, mode) {
118
+ const state = standingSellingState(area);
119
+ if (state.mode === mode ||
120
+ (mode !== StandingBookingMode.Standing &&
121
+ area.capacity > StandingPartyLimit))
122
+ return area;
123
+ return {
124
+ ...area,
125
+ selling: standingConfiguration(mode, state.wheelchairPlaces, {
126
+ minGuests: 1,
127
+ maxGuests: area.capacity,
128
+ }),
129
+ };
130
+ }
131
+ export function setStandingCapacity(area, requested) {
132
+ const state = standingSellingState(area);
133
+ const limit = state.mode === StandingBookingMode.Standing
134
+ ? StandingCapacityLimit
135
+ : StandingPartyLimit;
136
+ const capacity = Math.min(limit, Math.max(1, Math.round(requested)));
137
+ if (capacity === area.capacity)
138
+ return area;
139
+ return {
140
+ ...area,
141
+ capacity,
142
+ selling: standingConfiguration(state.mode, Math.min(capacity, state.wheelchairPlaces), resizedRange(state, capacity)),
143
+ };
144
+ }
145
+ export function setStandingPartySize(area, field, value) {
146
+ if (area.selling?.mode !== StandingBookingMode.Party)
147
+ return area;
148
+ const range = editedRange(area.selling, area.capacity, field, value);
149
+ if (range.minGuests === area.selling.minGuests &&
150
+ range.maxGuests === area.selling.maxGuests)
151
+ return area;
152
+ return { ...area, selling: { ...area.selling, ...range } };
153
+ }
154
+ export function setStandingWheelchairPlaces(area, requested) {
155
+ const state = standingSellingState(area);
156
+ const wheelchairPlaces = Math.max(0, Math.min(area.capacity, Math.round(requested)));
157
+ if (wheelchairPlaces === state.wheelchairPlaces)
158
+ return area;
159
+ return {
160
+ ...area,
161
+ selling: standingConfiguration(state.mode, wheelchairPlaces, state),
162
+ };
163
+ }