@blitzreels/contracts 0.2.5 → 0.2.7

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.
Files changed (69) hide show
  1. package/dist/ai-credit-costs.d.ts +24 -0
  2. package/dist/ai-credit-costs.d.ts.map +1 -0
  3. package/dist/ai-credit-costs.js +43 -0
  4. package/dist/ai-credit-costs.js.map +1 -0
  5. package/dist/carousel-document-present.d.ts +42 -0
  6. package/dist/carousel-document-present.d.ts.map +1 -0
  7. package/dist/carousel-document-present.js +117 -0
  8. package/dist/carousel-document-present.js.map +1 -0
  9. package/dist/carousel-pack-catalog.d.ts +10 -0
  10. package/dist/carousel-pack-catalog.d.ts.map +1 -0
  11. package/dist/carousel-pack-catalog.js +47 -0
  12. package/dist/carousel-pack-catalog.js.map +1 -0
  13. package/dist/carousel-recipe.d.ts +100 -0
  14. package/dist/carousel-recipe.d.ts.map +1 -0
  15. package/dist/carousel-recipe.js +305 -0
  16. package/dist/carousel-recipe.js.map +1 -0
  17. package/dist/carousel-slot-asset.d.ts +24 -0
  18. package/dist/carousel-slot-asset.d.ts.map +1 -0
  19. package/dist/carousel-slot-asset.js +89 -0
  20. package/dist/carousel-slot-asset.js.map +1 -0
  21. package/dist/carousel-slot-extras.d.ts +60 -0
  22. package/dist/carousel-slot-extras.d.ts.map +1 -0
  23. package/dist/carousel-slot-extras.js +181 -0
  24. package/dist/carousel-slot-extras.js.map +1 -0
  25. package/dist/carousel-slot-layout.d.ts +174 -0
  26. package/dist/carousel-slot-layout.d.ts.map +1 -0
  27. package/dist/carousel-slot-layout.js +387 -0
  28. package/dist/carousel-slot-layout.js.map +1 -0
  29. package/dist/carousel-slot-look.d.ts +79 -0
  30. package/dist/carousel-slot-look.d.ts.map +1 -0
  31. package/dist/carousel-slot-look.js +216 -0
  32. package/dist/carousel-slot-look.js.map +1 -0
  33. package/dist/carousel-slot-overlay.d.ts +45 -0
  34. package/dist/carousel-slot-overlay.d.ts.map +1 -0
  35. package/dist/carousel-slot-overlay.js +71 -0
  36. package/dist/carousel-slot-overlay.js.map +1 -0
  37. package/dist/carousel-template-catalog.d.ts +201 -0
  38. package/dist/carousel-template-catalog.d.ts.map +1 -0
  39. package/dist/carousel-template-catalog.js +666 -0
  40. package/dist/carousel-template-catalog.js.map +1 -0
  41. package/dist/carousel-theme.d.ts +134 -0
  42. package/dist/carousel-theme.d.ts.map +1 -0
  43. package/dist/carousel-theme.js +374 -0
  44. package/dist/carousel-theme.js.map +1 -0
  45. package/dist/carousels-contract.d.ts +664 -0
  46. package/dist/carousels-contract.d.ts.map +1 -0
  47. package/dist/carousels-contract.js +352 -0
  48. package/dist/carousels-contract.js.map +1 -0
  49. package/dist/clip-batch-estimate.d.ts +16 -0
  50. package/dist/clip-batch-estimate.d.ts.map +1 -0
  51. package/dist/clip-batch-estimate.js +35 -0
  52. package/dist/clip-batch-estimate.js.map +1 -0
  53. package/dist/index.d.ts +18 -0
  54. package/dist/index.d.ts.map +1 -1
  55. package/dist/index.js +17 -0
  56. package/dist/index.js.map +1 -1
  57. package/dist/plan-limits.d.ts +13 -0
  58. package/dist/plan-limits.d.ts.map +1 -0
  59. package/dist/plan-limits.js +53 -0
  60. package/dist/plan-limits.js.map +1 -0
  61. package/dist/short-suggestion-budget.d.ts +6 -0
  62. package/dist/short-suggestion-budget.d.ts.map +1 -0
  63. package/dist/short-suggestion-budget.js +17 -0
  64. package/dist/short-suggestion-budget.js.map +1 -0
  65. package/dist/short-suggestion-duration.d.ts +9 -0
  66. package/dist/short-suggestion-duration.d.ts.map +1 -0
  67. package/dist/short-suggestion-duration.js +18 -0
  68. package/dist/short-suggestion-duration.js.map +1 -0
  69. package/package.json +5 -3
@@ -0,0 +1,666 @@
1
+ import { carouselIsStampKey, carouselIsStickerKey, carouselStampKeys, carouselStickerKeys, } from "./carousel-slot-extras.js";
2
+ import { carouselLayerZIndex, carouselSlotBoxKey, carouselSlotsHaveLayout, } from "./carousel-slot-layout.js";
3
+ import { CAROUSEL_MAX_CHIPS, carouselPresentChipKeys, carouselSlotsHaveLook, readCarouselChipValue, } from "./carousel-slot-look.js";
4
+ import { CAROUSEL_OVERLAY_KEY, CAROUSEL_PAGE_COLOR_KEY, } from "./carousel-slot-overlay.js";
5
+ import { pruneVacantCarouselSlots, stripCarouselPersistSlots, } from "./carousel-theme.js";
6
+ export const CAROUSEL_NAMED_TEMPLATE_IDS = [
7
+ "photo_chips",
8
+ "tool_list",
9
+ "dual_cta",
10
+ "title_body",
11
+ "quote",
12
+ "stat",
13
+ ];
14
+ export const CAROUSEL_TEMPLATE_CATALOG = [
15
+ {
16
+ id: "photo_chips",
17
+ label: "Photo + chips",
18
+ family: "photo",
19
+ slots: Array.from({ length: CAROUSEL_MAX_CHIPS }, (_, index) => {
20
+ const n = index + 1;
21
+ return { key: `chip${n}`, label: `Chip ${n}` };
22
+ }),
23
+ },
24
+ {
25
+ id: "tool_list",
26
+ label: "Tool list",
27
+ family: "designed",
28
+ slots: [
29
+ { key: "title", label: "Title" },
30
+ ...Array.from({ length: 8 }, (_, index) => {
31
+ const n = index + 1;
32
+ return [
33
+ { key: `row${n}_name`, label: `Row ${n} name` },
34
+ { key: `row${n}_role`, label: `Row ${n} role` },
35
+ { key: `row${n}_price`, label: `Row ${n} price` },
36
+ { key: `row${n}_icon`, label: `Row ${n} icon` },
37
+ ];
38
+ }).flat(),
39
+ ],
40
+ },
41
+ {
42
+ id: "dual_cta",
43
+ label: "Dual CTA",
44
+ family: "designed",
45
+ slots: [
46
+ { key: "primary_title", label: "Primary title" },
47
+ { key: "primary_sub", label: "Primary sub" },
48
+ { key: "primary_btn", label: "Primary button" },
49
+ { key: "primary_link", label: "Primary link" },
50
+ { key: "primary_shot", label: "Primary image" },
51
+ { key: "secondary_body", label: "Secondary body" },
52
+ { key: "secondary_btn", label: "Secondary button" },
53
+ { key: "secondary_shot", label: "Secondary image" },
54
+ ],
55
+ },
56
+ {
57
+ id: "title_body",
58
+ label: "Title + body",
59
+ family: "designed",
60
+ slots: [
61
+ { key: "title", label: "Title" },
62
+ { key: "body", label: "Body" },
63
+ ],
64
+ },
65
+ {
66
+ id: "quote",
67
+ label: "Quote",
68
+ family: "designed",
69
+ slots: [
70
+ { key: "quote", label: "Quote" },
71
+ { key: "attribution", label: "Attribution" },
72
+ ],
73
+ },
74
+ {
75
+ id: "stat",
76
+ label: "Big stat",
77
+ family: "designed",
78
+ slots: [
79
+ { key: "value", label: "Value" },
80
+ { key: "label", label: "Label" },
81
+ { key: "detail", label: "Detail" },
82
+ ],
83
+ },
84
+ ];
85
+ export const CAROUSEL_ALGOMAX_ALIAS_PREFIX = "algomax_stack_v10/";
86
+ const ALGOMAX_ALIAS_TO_NAMED = {
87
+ "01-hook": "photo_chips",
88
+ "02-tech": "tool_list",
89
+ "03-coding": "tool_list",
90
+ "04-marketing": "tool_list",
91
+ "05-ai": "tool_list",
92
+ "06-video": "tool_list",
93
+ "07-cta": "dual_cta",
94
+ };
95
+ export function isCarouselNamedTemplateId(value) {
96
+ return CAROUSEL_NAMED_TEMPLATE_IDS.includes(value);
97
+ }
98
+ export function resolveCarouselNamedTemplateId({ templateId, }) {
99
+ if (isCarouselNamedTemplateId(templateId))
100
+ return templateId;
101
+ const slideId = algomaxPackSlideId({ templateId });
102
+ if (!slideId)
103
+ return null;
104
+ return ALGOMAX_ALIAS_TO_NAMED[slideId] ?? null;
105
+ }
106
+ export function algomaxAliasTemplateId({ slideId, }) {
107
+ return `${CAROUSEL_ALGOMAX_ALIAS_PREFIX}${slideId}`;
108
+ }
109
+ export function algomaxPackSlideId({ templateId, }) {
110
+ if (!templateId.startsWith(CAROUSEL_ALGOMAX_ALIAS_PREFIX))
111
+ return null;
112
+ return templateId.slice(CAROUSEL_ALGOMAX_ALIAS_PREFIX.length);
113
+ }
114
+ export function isKnownCarouselTemplateId({ templateId, }) {
115
+ return resolveCarouselNamedTemplateId({ templateId }) !== null;
116
+ }
117
+ export function carouselTemplateCatalogEntry({ templateId, }) {
118
+ const named = resolveCarouselNamedTemplateId({ templateId });
119
+ if (!named)
120
+ return null;
121
+ return CAROUSEL_TEMPLATE_CATALOG.find((entry) => entry.id === named) ?? null;
122
+ }
123
+ export function resolveCarouselSlideTemplateId({ templateId, visualKind, }) {
124
+ if (templateId) {
125
+ const named = resolveCarouselNamedTemplateId({ templateId });
126
+ if (named)
127
+ return named;
128
+ }
129
+ if (visualKind === "media_asset")
130
+ return "photo_chips";
131
+ return null;
132
+ }
133
+ export function readCarouselSlot({ slots, key, fallback = "", }) {
134
+ const value = slots[key];
135
+ if (typeof value !== "string")
136
+ return fallback;
137
+ return value;
138
+ }
139
+ export function carouselChipSlots({ slots, lines, }) {
140
+ const fromSlots = carouselPresentChipKeys({ slots, lines })
141
+ .map((key) => readCarouselSlot({ slots, key }).trim())
142
+ .filter((value) => value.length > 0);
143
+ if (fromSlots.length > 0)
144
+ return fromSlots;
145
+ return (lines ?? [])
146
+ .map((line) => line.trim())
147
+ .filter((line) => line.length > 0);
148
+ }
149
+ export function carouselToolRows({ slots, }) {
150
+ const rows = [];
151
+ for (let index = 1; index <= 8; index += 1) {
152
+ const name = readCarouselSlot({ slots, key: `row${index}_name` }).trim();
153
+ if (!name)
154
+ continue;
155
+ rows.push({
156
+ name,
157
+ role: readCarouselSlot({ slots, key: `row${index}_role` }).trim(),
158
+ price: readCarouselSlot({ slots, key: `row${index}_price` }).trim(),
159
+ icon: readCarouselSlot({ slots, key: `row${index}_icon` }).trim(),
160
+ });
161
+ }
162
+ return rows;
163
+ }
164
+ export function mergeCarouselSlots({ base, patch, }) {
165
+ const next = { ...base };
166
+ for (const [key, value] of Object.entries(patch)) {
167
+ next[key] = value;
168
+ }
169
+ return next;
170
+ }
171
+ export function slotsFromChipLines({ lines, }) {
172
+ const slots = {};
173
+ for (const [index, line] of lines.entries()) {
174
+ if (index >= CAROUSEL_MAX_CHIPS)
175
+ break;
176
+ slots[`chip${index + 1}`] = line;
177
+ }
178
+ return slots;
179
+ }
180
+ export function mergeChipLinesIntoSlots({ slots, lines, }) {
181
+ if (!lines || lines.length === 0)
182
+ return slots;
183
+ const next = { ...slots };
184
+ for (const [index, line] of lines.entries()) {
185
+ if (index >= CAROUSEL_MAX_CHIPS)
186
+ break;
187
+ const key = `chip${index + 1}`;
188
+ if (!Object.hasOwn(next, key) && line.trim())
189
+ next[key] = line;
190
+ }
191
+ return next;
192
+ }
193
+ export function emptySlotsForTemplateId({ templateId, }) {
194
+ const entry = carouselTemplateCatalogEntry({ templateId });
195
+ if (!entry)
196
+ return {};
197
+ return Object.fromEntries(entry.slots.map((slot) => [slot.key, ""]));
198
+ }
199
+ export function editableCarouselSlots({ templateId, slots, lines, }) {
200
+ const entry = carouselTemplateCatalogEntry({ templateId });
201
+ if (!entry)
202
+ return [];
203
+ const visibleKeys = new Set(entry.slots.map((slot) => slot.key));
204
+ if (entry.id === "photo_chips") {
205
+ const keep = new Set(carouselPresentChipKeys({ slots, lines }));
206
+ const chips = keep.size === 0
207
+ ? []
208
+ : [...keep].map((key) => ({
209
+ key,
210
+ label: `Chip ${key.slice(4)}`,
211
+ value: readCarouselChipValue({
212
+ slots,
213
+ lines,
214
+ index: Number(key.slice(4)),
215
+ }),
216
+ }));
217
+ return [...chips, ...extraEditableSlots({ slots })];
218
+ }
219
+ if (entry.id === "tool_list") {
220
+ const rows = carouselToolRows({ slots });
221
+ const keep = new Set(["title"]);
222
+ const rowCount = Math.max(rows.length, 1);
223
+ for (let index = 1; index <= Math.min(rowCount + 1, 8); index += 1) {
224
+ keep.add(`row${index}_name`);
225
+ keep.add(`row${index}_role`);
226
+ keep.add(`row${index}_price`);
227
+ keep.add(`row${index}_icon`);
228
+ }
229
+ const listed = entry.slots
230
+ .filter((slot) => keep.has(slot.key) && visibleKeys.has(slot.key))
231
+ .map((slot) => ({
232
+ ...slot,
233
+ value: readCarouselSlot({ slots, key: slot.key }),
234
+ }));
235
+ return [...listed, ...extraEditableSlots({ slots })];
236
+ }
237
+ return [
238
+ ...entry.slots.map((slot) => ({
239
+ ...slot,
240
+ value: readCarouselSlot({ slots, key: slot.key }),
241
+ })),
242
+ ...extraEditableSlots({ slots }),
243
+ ];
244
+ }
245
+ function extraEditableSlots({ slots, }) {
246
+ return [
247
+ ...carouselStickerKeys({ slots }).map((key) => ({
248
+ key,
249
+ label: `Text ${key.slice("sticker".length)}`,
250
+ value: readCarouselSlot({ slots, key }),
251
+ })),
252
+ ...carouselStampKeys({ slots }).map((key) => ({
253
+ key,
254
+ label: `Image ${key.slice("stamp".length)}`,
255
+ value: readCarouselSlot({ slots, key }),
256
+ })),
257
+ ];
258
+ }
259
+ export function carouselSlotControlKind({ key, }) {
260
+ if (key === "body" ||
261
+ key === "quote" ||
262
+ key === "detail" ||
263
+ key.endsWith("_sub") ||
264
+ key.endsWith("_body")) {
265
+ return "area";
266
+ }
267
+ if (key === "title" ||
268
+ key === "primary_title" ||
269
+ key === "value" ||
270
+ key.startsWith("chip")) {
271
+ return "title";
272
+ }
273
+ return "text";
274
+ }
275
+ export function carouselSlotPartKey({ key }) {
276
+ const parts = key.split("_");
277
+ return parts[parts.length - 1] ?? key;
278
+ }
279
+ export const CAROUSEL_PHOTO_SLOT_KEY = "photo";
280
+ export function focusCarouselInspectorGroups({ groups, activeKey, }) {
281
+ if (!activeKey || activeKey === CAROUSEL_PHOTO_SLOT_KEY) {
282
+ return [];
283
+ }
284
+ const row = /^row(\d+)_/.exec(activeKey);
285
+ if (row) {
286
+ const index = Number(row[1]);
287
+ const match = groups.filter((group) => group.kind === "row" && group.index === index);
288
+ return match;
289
+ }
290
+ if (activeKey.startsWith("primary_")) {
291
+ const match = groups.filter((group) => group.kind === "fields" && group.titleKey === "primary");
292
+ return match;
293
+ }
294
+ if (activeKey.startsWith("secondary_")) {
295
+ const match = groups.filter((group) => group.kind === "fields" && group.titleKey === "secondary");
296
+ return match;
297
+ }
298
+ const match = groups.filter((group) => group.kind === "fields" &&
299
+ group.fields.some((field) => field.key === activeKey));
300
+ if (match.length === 0) {
301
+ return [];
302
+ }
303
+ return match.map((group) => ({
304
+ ...group,
305
+ fields: group.fields.filter((field) => field.key === activeKey),
306
+ }));
307
+ }
308
+ export function groupCarouselSlotFields({ fields, }) {
309
+ const rows = new Map();
310
+ const primary = [];
311
+ const secondary = [];
312
+ const rest = [];
313
+ for (const field of fields) {
314
+ const row = /^row(\d+)_/.exec(field.key);
315
+ if (row) {
316
+ const index = Number(row[1]);
317
+ const list = rows.get(index) ?? [];
318
+ list.push(field);
319
+ rows.set(index, list);
320
+ continue;
321
+ }
322
+ if (field.key.startsWith("primary_")) {
323
+ primary.push(field);
324
+ continue;
325
+ }
326
+ if (field.key.startsWith("secondary_")) {
327
+ secondary.push(field);
328
+ continue;
329
+ }
330
+ rest.push(field);
331
+ }
332
+ const groups = [];
333
+ if (rest.length > 0) {
334
+ groups.push({ kind: "fields", fields: rest });
335
+ }
336
+ const rowIndexes = [...rows.keys()].sort((left, right) => left - right);
337
+ for (const index of rowIndexes) {
338
+ groups.push({
339
+ kind: "row",
340
+ index,
341
+ fields: rows.get(index) ?? [],
342
+ });
343
+ }
344
+ if (primary.length > 0) {
345
+ groups.push({ kind: "fields", titleKey: "primary", fields: primary });
346
+ }
347
+ if (secondary.length > 0) {
348
+ groups.push({
349
+ kind: "fields",
350
+ titleKey: "secondary",
351
+ fields: secondary,
352
+ });
353
+ }
354
+ return groups;
355
+ }
356
+ export function carouselInspectorLayers({ family, fields, photoLabel, rowLabel, primaryLabel, secondaryLabel, }) {
357
+ const items = [];
358
+ if (family === "photo") {
359
+ items.push({ key: CAROUSEL_PHOTO_SLOT_KEY, label: photoLabel });
360
+ }
361
+ for (const group of groupCarouselSlotFields({ fields })) {
362
+ if (group.kind === "row") {
363
+ const name = group.fields.find((field) => field.key.endsWith("_name"))?.value;
364
+ items.push({
365
+ key: `row${group.index}_name`,
366
+ label: name?.trim() || rowLabel(group.index),
367
+ });
368
+ continue;
369
+ }
370
+ if (group.titleKey === "primary") {
371
+ items.push({
372
+ key: group.fields[0]?.key ?? "primary_title",
373
+ label: primaryLabel,
374
+ });
375
+ continue;
376
+ }
377
+ if (group.titleKey === "secondary") {
378
+ items.push({
379
+ key: group.fields[0]?.key ?? "secondary_body",
380
+ label: secondaryLabel,
381
+ });
382
+ continue;
383
+ }
384
+ for (const field of group.fields) {
385
+ items.push({ key: field.key, label: field.label });
386
+ }
387
+ }
388
+ return items;
389
+ }
390
+ export function carouselLayerStack({ family, fields, photoLabel, rowLabel, primaryLabel, secondaryLabel, pageLabel, overlayLabel, slots, }) {
391
+ const inspector = carouselInspectorLayers({
392
+ family,
393
+ fields,
394
+ photoLabel,
395
+ rowLabel,
396
+ primaryLabel,
397
+ secondaryLabel,
398
+ });
399
+ const stickers = [];
400
+ const stamps = [];
401
+ const chips = [];
402
+ const named = [];
403
+ const photo = [];
404
+ for (const item of inspector) {
405
+ if (item.key === CAROUSEL_PHOTO_SLOT_KEY) {
406
+ photo.push({ ...item, kind: "photo" });
407
+ continue;
408
+ }
409
+ if (carouselIsStampKey({ key: item.key })) {
410
+ stamps.push({ ...item, kind: "image" });
411
+ continue;
412
+ }
413
+ if (carouselIsStickerKey({ key: item.key })) {
414
+ stickers.push({ ...item, kind: "text" });
415
+ continue;
416
+ }
417
+ if (item.key.startsWith("chip")) {
418
+ chips.push({ ...item, kind: "text" });
419
+ continue;
420
+ }
421
+ named.push({ ...item, kind: "text" });
422
+ }
423
+ const items = [
424
+ ...stickers,
425
+ ...stamps,
426
+ ...chips,
427
+ ...named,
428
+ { key: CAROUSEL_OVERLAY_KEY, label: overlayLabel, kind: "overlay" },
429
+ ...photo,
430
+ { key: CAROUSEL_PAGE_COLOR_KEY, label: pageLabel, kind: "page" },
431
+ ];
432
+ if (!slots)
433
+ return items;
434
+ return [...items].sort((left, right) => {
435
+ const delta = carouselLayerZIndex({
436
+ slots,
437
+ boxKey: carouselSlotBoxKey({ key: right.key }),
438
+ }) -
439
+ carouselLayerZIndex({
440
+ slots,
441
+ boxKey: carouselSlotBoxKey({ key: left.key }),
442
+ });
443
+ return delta;
444
+ });
445
+ }
446
+ export function planCarouselSlidePatch({ templateId, visualKind, mediaAssetId, sourceMediaAssetId, slots, lines, patch, }) {
447
+ const currentNamed = resolveCarouselSlideTemplateId({
448
+ templateId,
449
+ visualKind,
450
+ });
451
+ const nextTemplateId = patch.template_id ?? currentNamed ?? "photo_chips";
452
+ const named = resolveCarouselNamedTemplateId({ templateId: nextTemplateId });
453
+ const family = named
454
+ ? (carouselTemplateCatalogEntry({ templateId: nextTemplateId })?.family ??
455
+ "designed")
456
+ : "designed";
457
+ const nextNamed = resolveCarouselNamedTemplateId({
458
+ templateId: nextTemplateId,
459
+ });
460
+ const switchingTemplate = Boolean(patch.template_id && nextNamed !== currentNamed);
461
+ const baseSlots = switchingTemplate
462
+ ? emptySlotsForTemplateId({ templateId: nextTemplateId })
463
+ : slots;
464
+ const nextLines = patch.lines ?? (switchingTemplate ? [] : lines);
465
+ const nextSlots = mergeCarouselSlots({
466
+ base: {
467
+ ...baseSlots,
468
+ ...(switchingTemplate ? {} : slotsFromChipLines({ lines: nextLines })),
469
+ },
470
+ patch: patch.slots ?? {},
471
+ });
472
+ const nextSource = patch.media_asset_id ?? sourceMediaAssetId ?? mediaAssetId ?? undefined;
473
+ const prunedSlots = pruneVacantCarouselSlots({ slots: nextSlots });
474
+ const chips = carouselChipSlots({ slots: prunedSlots, lines: nextLines });
475
+ if (family === "photo") {
476
+ return {
477
+ slots: prunedSlots,
478
+ lines: chips,
479
+ visualKind: "media_asset",
480
+ templateId: nextTemplateId,
481
+ mediaAssetId: mediaAssetId ?? undefined,
482
+ sourceMediaAssetId: nextSource,
483
+ bake: carouselSlotsHaveLayout({ slots: prunedSlots }) ||
484
+ carouselSlotsHaveLook({ slots: prunedSlots })
485
+ ? "raster"
486
+ : "chips",
487
+ };
488
+ }
489
+ return {
490
+ slots: prunedSlots,
491
+ lines: nextLines,
492
+ visualKind: "template",
493
+ templateId: nextTemplateId,
494
+ mediaAssetId: mediaAssetId ?? undefined,
495
+ sourceMediaAssetId: nextSource,
496
+ bake: "raster",
497
+ };
498
+ }
499
+ function reindexCarouselSlides({ slides, }) {
500
+ return slides.map((slide, index) => ({
501
+ ...slide,
502
+ slots: { ...slide.slots },
503
+ visual: { ...slide.visual },
504
+ index,
505
+ }));
506
+ }
507
+ function mutationError({ code, message, field, repair, }) {
508
+ return { ok: false, code, message, field, repair };
509
+ }
510
+ export function planCarouselDocumentMutation({ slides, selectedIndex = 0, mutation, maxSlides, }) {
511
+ if (mutation.op === "remove") {
512
+ const index = mutation.index;
513
+ if (index === undefined || !slides[index]) {
514
+ return mutationError({
515
+ code: "slide_not_found",
516
+ message: "That slide does not exist.",
517
+ field: "index",
518
+ repair: "Pass an index from GET /carousels/{id}.",
519
+ });
520
+ }
521
+ if (slides.length <= 1) {
522
+ return mutationError({
523
+ code: "cannot_remove_last",
524
+ message: "A carousel needs at least one slide.",
525
+ field: "index",
526
+ repair: "Insert another slide before removing this one.",
527
+ });
528
+ }
529
+ const next = slides.filter((_, slideIndex) => slideIndex !== index);
530
+ const nextSelected = selectedIndex === index
531
+ ? Math.min(index, next.length - 1)
532
+ : selectedIndex > index
533
+ ? selectedIndex - 1
534
+ : selectedIndex;
535
+ return {
536
+ ok: true,
537
+ slides: reindexCarouselSlides({ slides: next }),
538
+ selectedIndex: nextSelected,
539
+ };
540
+ }
541
+ if (mutation.op === "reorder") {
542
+ const from = mutation.from;
543
+ const to = mutation.to;
544
+ if (from === undefined ||
545
+ to === undefined ||
546
+ !slides[from] ||
547
+ to < 0 ||
548
+ to >= slides.length) {
549
+ return mutationError({
550
+ code: "slide_not_found",
551
+ message: "Reorder needs two indexes that exist on the document.",
552
+ field: "from",
553
+ repair: "Pass from and to indexes from GET /carousels/{id}.",
554
+ });
555
+ }
556
+ if (from === to) {
557
+ return {
558
+ ok: true,
559
+ slides: reindexCarouselSlides({ slides }),
560
+ selectedIndex,
561
+ };
562
+ }
563
+ const next = [...slides];
564
+ const [moved] = next.splice(from, 1);
565
+ if (!moved) {
566
+ return mutationError({
567
+ code: "slide_not_found",
568
+ message: "Reorder needs two indexes that exist on the document.",
569
+ field: "from",
570
+ repair: "Pass from and to indexes from GET /carousels/{id}.",
571
+ });
572
+ }
573
+ next.splice(to, 0, moved);
574
+ let nextSelected = selectedIndex;
575
+ if (selectedIndex === from)
576
+ nextSelected = to;
577
+ else if (from < to && selectedIndex > from && selectedIndex <= to) {
578
+ nextSelected = selectedIndex - 1;
579
+ }
580
+ else if (to < from && selectedIndex >= to && selectedIndex < from) {
581
+ nextSelected = selectedIndex + 1;
582
+ }
583
+ return {
584
+ ok: true,
585
+ slides: reindexCarouselSlides({ slides: next }),
586
+ selectedIndex: nextSelected,
587
+ };
588
+ }
589
+ const index = mutation.index ?? slides.length;
590
+ if (index < 0 || index > slides.length) {
591
+ return mutationError({
592
+ code: "slide_not_found",
593
+ message: "Insert index is outside the document.",
594
+ field: "index",
595
+ repair: "Pass 0 through slide count inclusive.",
596
+ });
597
+ }
598
+ if (slides.length >= maxSlides) {
599
+ return mutationError({
600
+ code: "too_many_slides",
601
+ message: `This platform allows at most ${maxSlides} slides.`,
602
+ field: "index",
603
+ repair: "Remove a slide before inserting another.",
604
+ });
605
+ }
606
+ if (mutation.source_index !== undefined) {
607
+ const source = slides[mutation.source_index];
608
+ if (!source) {
609
+ return mutationError({
610
+ code: "slide_not_found",
611
+ message: "That slide does not exist.",
612
+ field: "source_index",
613
+ repair: "Pass a source_index from GET /carousels/{id}.",
614
+ });
615
+ }
616
+ const inserted = {
617
+ role: source.role,
618
+ lines: [...source.lines],
619
+ slots: stripCarouselPersistSlots({ slots: source.slots }),
620
+ layout: source.layout,
621
+ visual: {
622
+ kind: source.visual.kind,
623
+ template_id: source.visual.template_id,
624
+ source_media_asset_id: source.visual.source_media_asset_id,
625
+ },
626
+ };
627
+ const next = [...slides];
628
+ next.splice(index, 0, inserted);
629
+ return {
630
+ ok: true,
631
+ slides: reindexCarouselSlides({ slides: next }),
632
+ selectedIndex: index,
633
+ bakeIndex: index,
634
+ };
635
+ }
636
+ const templateId = mutation.template_id ?? "photo_chips";
637
+ if (!isKnownCarouselTemplateId({ templateId })) {
638
+ return mutationError({
639
+ code: "unknown_template",
640
+ message: `unknown template_id '${templateId}'.`,
641
+ field: "template_id",
642
+ repair: "Use a named template such as photo_chips, tool_list, dual_cta, quote, or stat.",
643
+ });
644
+ }
645
+ const named = resolveCarouselNamedTemplateId({ templateId }) ?? "photo_chips";
646
+ const family = carouselTemplateCatalogEntry({ templateId: named })?.family ?? "designed";
647
+ const inserted = {
648
+ role: "body",
649
+ lines: [],
650
+ slots: emptySlotsForTemplateId({ templateId: named }),
651
+ layout: family === "photo" ? "chip_stack_face_safe" : "title_body_card",
652
+ visual: {
653
+ kind: family === "photo" ? "media_asset" : "template",
654
+ template_id: named,
655
+ },
656
+ };
657
+ const next = [...slides];
658
+ next.splice(index, 0, inserted);
659
+ return {
660
+ ok: true,
661
+ slides: reindexCarouselSlides({ slides: next }),
662
+ selectedIndex: index,
663
+ bakeIndex: index,
664
+ };
665
+ }
666
+ //# sourceMappingURL=carousel-template-catalog.js.map