@jsb188/mday 1.1.8 → 1.1.11

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 (123) hide show
  1. package/package.json +105 -35
  2. package/src/constants/agentDoc.ts +12 -0
  3. package/src/constants/agentMemory.ts +154 -0
  4. package/src/constants/aiChat.ts +5 -0
  5. package/src/constants/campaign.ts +127 -0
  6. package/src/constants/dataTable.ts +48 -0
  7. package/src/constants/dataTableFieldTypes.generated.ts +63 -0
  8. package/src/constants/docAccess.ts +32 -0
  9. package/src/constants/email.ts +44 -0
  10. package/src/constants/fieldDoc.ts +34 -0
  11. package/src/constants/graphqlReactivity.ts +113 -0
  12. package/src/constants/inboundContact.ts +8 -0
  13. package/src/constants/integration.ts +13 -0
  14. package/src/constants/log.ts +7 -366
  15. package/src/constants/logActivityGroupColors.generated.ts +19 -0
  16. package/src/constants/logTaxonomy.generated.ts +1103 -0
  17. package/src/constants/noteDoc.ts +77 -0
  18. package/src/constants/organization.ts +18 -11
  19. package/src/constants/pageRoute.ts +23 -0
  20. package/src/constants/schedule.ts +220 -0
  21. package/src/constants/scheduleRuleTemplateSeed.ts +138 -0
  22. package/src/constants/scheduleRuleTemplates.generated.ts +145 -0
  23. package/src/constants/sheet.ts +168 -0
  24. package/src/constants/sheetRegionSources.generated.ts +578 -0
  25. package/src/constants/square.ts +16 -0
  26. package/src/constants/storage.ts +61 -0
  27. package/src/constants/usage.ts +79 -0
  28. package/src/schemas/editLogEntry.ts +1017 -738
  29. package/src/schemas/fieldDocsQuery.ts +12 -0
  30. package/src/schemas/inboundContact.ts +77 -0
  31. package/src/schemas/logsQuery.ts +57 -316
  32. package/src/schemas/organization.ts +53 -213
  33. package/src/types/account.d.ts +81 -0
  34. package/src/types/agentDoc.d.ts +64 -0
  35. package/src/types/agentMemory.d.ts +74 -0
  36. package/src/types/campaign.d.ts +683 -0
  37. package/src/types/dataTable.d.ts +366 -0
  38. package/src/types/docAccess.d.ts +96 -0
  39. package/src/types/email.d.ts +142 -0
  40. package/src/types/fieldDoc.d.ts +273 -0
  41. package/src/types/inboundContact.d.ts +57 -0
  42. package/src/types/integration.d.ts +42 -0
  43. package/src/types/log.d.ts +169 -190
  44. package/src/types/noteDoc.d.ts +86 -0
  45. package/src/types/organization.d.ts +175 -76
  46. package/src/types/pageRoute.d.ts +48 -0
  47. package/src/types/schedule.d.ts +391 -0
  48. package/src/types/sheet.d.ts +1130 -0
  49. package/src/types/storage.d.ts +242 -0
  50. package/src/types/usage.d.ts +106 -0
  51. package/src/types/workflow.d.ts +305 -0
  52. package/src/utils/agentDoc.ts +109 -0
  53. package/src/utils/aiChat.ts +26 -0
  54. package/src/utils/campaign.ts +741 -0
  55. package/src/utils/campaignFormEditor.ts +871 -0
  56. package/src/utils/dataTable.ts +849 -0
  57. package/src/utils/dataTableFieldTypeRegistry.ts +105 -0
  58. package/src/utils/dataTableRealtimeAuthority.ts +209 -0
  59. package/src/utils/dataTableRealtimeSelection.ts +134 -0
  60. package/src/utils/dataTableRowDocument.ts +508 -0
  61. package/src/utils/dataTableSource.ts +230 -0
  62. package/src/utils/docAccess.ts +383 -0
  63. package/src/utils/documentName.ts +53 -0
  64. package/src/utils/email.ts +172 -0
  65. package/src/utils/fieldDoc.ts +113 -0
  66. package/src/utils/log.ts +173 -778
  67. package/src/utils/logRegistryValue.ts +90 -0
  68. package/src/utils/logStoreRead.ts +266 -0
  69. package/src/utils/logTaxonomy.ts +76 -0
  70. package/src/utils/logTaxonomyDisplay.ts +439 -0
  71. package/src/utils/noteDoc.ts +827 -0
  72. package/src/utils/organization.ts +683 -81
  73. package/src/utils/pageRoute.ts +71 -0
  74. package/src/utils/schedule.ts +799 -0
  75. package/src/utils/scheduleEvaluation.ts +1994 -0
  76. package/src/utils/scheduleICS.ts +246 -0
  77. package/src/utils/scheduleRuleTemplates.ts +188 -0
  78. package/src/utils/sheet.ts +4973 -0
  79. package/src/utils/sheetAxisMove.ts +830 -0
  80. package/src/utils/sheetBootstrap.ts +1769 -0
  81. package/src/utils/sheetCommand.ts +423 -0
  82. package/src/utils/sheetExport.ts +238 -0
  83. package/src/utils/sheetFormulaEvaluation.ts +914 -0
  84. package/src/utils/sheetFormulaEvaluator.ts +852 -0
  85. package/src/utils/sheetJournal.ts +78 -0
  86. package/src/utils/sheetNumberFormat.ts +1211 -0
  87. package/src/utils/sheetRegion.ts +374 -0
  88. package/src/utils/sheetRegionFilter.ts +1259 -0
  89. package/src/utils/sheetRegionSourceRegistry.ts +594 -0
  90. package/src/utils/sheetSequence.ts +148 -0
  91. package/src/utils/sheetSnapshot.ts +992 -0
  92. package/src/utils/sheetStructureEdit.ts +1284 -0
  93. package/src/utils/square.ts +32 -0
  94. package/src/utils/storage.ts +152 -0
  95. package/src/utils/table-route.ts +262 -0
  96. package/src/utils/workflow.ts +380 -0
  97. package/tests/dataTableFieldTypeRegistry.test.ts +110 -0
  98. package/tests/docAccess.test.ts +525 -0
  99. package/tests/editLogEntry.test.ts +112 -0
  100. package/tests/noteDoc.test.ts +669 -0
  101. package/tests/schedule.test.ts +693 -0
  102. package/tests/scheduleEvaluation.test.ts +1970 -0
  103. package/tests/scheduleICS.test.ts +208 -0
  104. package/tests/scheduleRuleTemplates.test.ts +176 -0
  105. package/tests/scheduleSweepInvariants.test.ts +428 -0
  106. package/tests/sheetAxisMove.test.ts +343 -0
  107. package/tests/sheetFormulaEvaluator.test.ts +575 -0
  108. package/tests/sheetRegionFilter.test.ts +1135 -0
  109. package/tests/sheetSequence.test.ts +41 -0
  110. package/tests/sheetStructureEdit.test.ts +82 -0
  111. package/tsconfig.json +12 -0
  112. package/vitest.config.ts +8 -0
  113. package/src/constants/log-activityTexts.ts +0 -209
  114. package/src/constants/product.ts +0 -66
  115. package/src/constants/report.ts +0 -25
  116. package/src/schemas/childOrganizationsQuery.ts +0 -44
  117. package/src/schemas/productsQuery.ts +0 -145
  118. package/src/schemas/reportsQuery.ts +0 -107
  119. package/src/types/action.d.ts +0 -42
  120. package/src/types/product.d.ts +0 -173
  121. package/src/types/report.d.ts +0 -203
  122. package/src/utils/product-calEvent.ts +0 -508
  123. package/src/utils/route-helpers.ts +0 -583
@@ -0,0 +1,1284 @@
1
+ /**
2
+ * Sheet structure-edit helpers shared by the client and the server.
3
+ * Formerly at packages/@jsb188:sheet/src/libs/sheet-structure-edit.ts
4
+ * (moved verbatim for layering; check that path for older git blame).
5
+ */
6
+
7
+ import { parseJSON, stringifyJSON } from '@jsb188/app/utils/object.ts';
8
+ import {
9
+ SHEET_DATA_TABLE_REGION_DEFAULT_LIMIT,
10
+ SHEET_DATA_TABLE_REGION_MAX_ROWS,
11
+ SHEET_MAX_COLUMN_COUNT,
12
+ SHEET_MAX_ROW_COUNT,
13
+ } from '@jsb188/mday/constants/sheet.ts';
14
+ import type {
15
+ SheetAxisDesignObj,
16
+ SheetDesignObj,
17
+ SheetRangeData,
18
+ SheetRangeGQL,
19
+ SheetRegionColumnObj,
20
+ SheetRegionOptionsObj,
21
+ SheetStructureOperationEnum,
22
+ } from '@jsb188/mday/types/sheet.d.ts';
23
+ import {
24
+ getSheetMergedRanges,
25
+ normalizeSheetDesign,
26
+ type SheetProtectedAxisSpan,
27
+ shiftSheetAxisDesignForStructureEdit,
28
+ shiftSheetMergedRangesForStructureEdit,
29
+ } from '@jsb188/mday/utils/sheet.ts';
30
+
31
+ /*
32
+ * Return one design patch field as a parsed object. Patch fields carry
33
+ * serialized JSON, but already-parsed objects pass through unchanged. Local
34
+ * because this module is shared with the server.
35
+ */
36
+ function parseSheetDesignPatchObject<T extends Record<string, any>>(
37
+ value: unknown,
38
+ fallback: T,
39
+ ): T {
40
+ if (value && typeof value === 'object' && !Array.isArray(value)) {
41
+ return value as T;
42
+ }
43
+
44
+ if (typeof value !== 'string' || !value) {
45
+ return fallback;
46
+ }
47
+
48
+ const parsed = parseJSON(value);
49
+ return parsed && typeof parsed === 'object' && !Array.isArray(parsed)
50
+ ? (parsed as T)
51
+ : fallback;
52
+ }
53
+
54
+ export interface SheetStructureBounds {
55
+ endColumnIndex: number;
56
+ endRowIndex: number;
57
+ startColumnIndex: number;
58
+ startRowIndex: number;
59
+ }
60
+
61
+ export interface SheetStructureRegionFootprint {
62
+ columns?: SheetRegionColumnObj[] | null;
63
+ options?: SheetRegionOptionsObj | null;
64
+ source?: {
65
+ dataTableId?: number | bigint | string | null;
66
+ type?: string | null;
67
+ } | null;
68
+ sourceDataTableId?: number | bigint | string | null;
69
+ startColumnIndex?: number | null;
70
+ startRowIndex?: number | null;
71
+ type?: string | null;
72
+ }
73
+
74
+ export interface SheetStructureCellCoord {
75
+ columnIndex: number;
76
+ rowIndex: number;
77
+ }
78
+
79
+ export interface SheetStructureCellLike {
80
+ columnIndex?: number | null;
81
+ rowIndex?: number | null;
82
+ }
83
+
84
+ export type SheetStructureRangeLike = Pick<
85
+ SheetRangeData | SheetRangeGQL,
86
+ 'endColumnIndex' | 'endRowIndex' | 'startColumnIndex' | 'startRowIndex'
87
+ >;
88
+
89
+ export interface SheetStructureBoundsPredicate {
90
+ args: Array<number | bigint>;
91
+ text: string;
92
+ }
93
+
94
+ /*
95
+ * Return a normalized positive one-based sheet index.
96
+ */
97
+ function normalizeSheetStructureIndex(value: number | null | undefined) {
98
+ const index = Math.floor(Number(value || 0));
99
+
100
+ return Number.isFinite(index) && index > 0 ? index : null;
101
+ }
102
+
103
+ /*
104
+ * Return a stable coordinate key for one one-based sheet cell.
105
+ */
106
+ export function getSheetStructureCoordKey(
107
+ rowIndex: number,
108
+ columnIndex: number,
109
+ ) {
110
+ return `${rowIndex}:${columnIndex}`;
111
+ }
112
+
113
+ /*
114
+ * Return the default finite end row for one data table region.
115
+ */
116
+ function getSheetStructureDataTableRegionDefaultEndRowIndex(
117
+ region: Pick<SheetStructureRegionFootprint, 'options' | 'startRowIndex'>,
118
+ ) {
119
+ return (
120
+ getSheetStructureRegionDataStartRowIndex(region) +
121
+ SHEET_DATA_TABLE_REGION_DEFAULT_LIMIT -
122
+ 1
123
+ );
124
+ }
125
+
126
+ /*
127
+ * Return the configured finite end row for one region, defaulting unset limits.
128
+ */
129
+ export function getSheetStructureRegionConfiguredEndRowIndex(
130
+ region: Pick<SheetStructureRegionFootprint, 'options' | 'startRowIndex'>,
131
+ ) {
132
+ const endRowIndex = Math.floor(Number(region.options?.endRowIndex || 0));
133
+ const dataStartRowIndex = getSheetStructureRegionDataStartRowIndex(region);
134
+
135
+ return Number.isFinite(endRowIndex) && endRowIndex >= dataStartRowIndex
136
+ ? Math.min(
137
+ endRowIndex,
138
+ getSheetStructureDataTableRegionMaxEndRowIndex(region),
139
+ )
140
+ : getSheetStructureDataTableRegionDefaultEndRowIndex(region);
141
+ }
142
+
143
+ /*
144
+ * Return the first row that should contain data table source rows for one region.
145
+ */
146
+ export function getSheetStructureRegionDataStartRowIndex(
147
+ region: Pick<SheetStructureRegionFootprint, 'options' | 'startRowIndex'>,
148
+ ) {
149
+ const startRowIndex = normalizeSheetStructureIndex(region.startRowIndex) || 1;
150
+
151
+ return startRowIndex + (region.options?.includeHeaderRow ? 1 : 0);
152
+ }
153
+
154
+ /*
155
+ * Return the last row index one data table region may generate under the hard row cap.
156
+ */
157
+ export function getSheetStructureDataTableRegionMaxEndRowIndex(
158
+ region: Pick<SheetStructureRegionFootprint, 'options' | 'startRowIndex'>,
159
+ ) {
160
+ return (
161
+ getSheetStructureRegionDataStartRowIndex(region) +
162
+ SHEET_DATA_TABLE_REGION_MAX_ROWS -
163
+ 1
164
+ );
165
+ }
166
+
167
+ /*
168
+ * Return the rectangular generated output footprint for one data table region.
169
+ */
170
+ export function getSheetStructureRegionContentBounds(
171
+ region: SheetStructureRegionFootprint,
172
+ sourceRowCount: number,
173
+ ): SheetStructureBounds | null {
174
+ const startRowIndex = normalizeSheetStructureIndex(region.startRowIndex);
175
+ const startColumnIndex = normalizeSheetStructureIndex(
176
+ region.startColumnIndex,
177
+ );
178
+ const columnCount = Array.isArray(region.columns) ? region.columns.length : 0;
179
+
180
+ if (!startRowIndex || !startColumnIndex || !columnCount) {
181
+ return null;
182
+ }
183
+
184
+ const normalizedRegion = {
185
+ ...region,
186
+ startRowIndex,
187
+ startColumnIndex,
188
+ };
189
+ const configuredEndRowIndex =
190
+ getSheetStructureRegionConfiguredEndRowIndex(normalizedRegion);
191
+ const allowedEndRowIndex = Math.min(
192
+ configuredEndRowIndex ||
193
+ getSheetStructureDataTableRegionMaxEndRowIndex(normalizedRegion),
194
+ getSheetStructureDataTableRegionMaxEndRowIndex(normalizedRegion),
195
+ );
196
+ const headerLastRowIndex = normalizedRegion.options?.includeHeaderRow
197
+ ? startRowIndex
198
+ : 0;
199
+ const dataStartRowIndex =
200
+ getSheetStructureRegionDataStartRowIndex(normalizedRegion);
201
+ const sourceRowsToRender = Math.min(
202
+ Math.max(0, Math.floor(Number(sourceRowCount) || 0)),
203
+ SHEET_DATA_TABLE_REGION_MAX_ROWS,
204
+ );
205
+ const sourceLastRowIndex = sourceRowsToRender
206
+ ? dataStartRowIndex + sourceRowsToRender - 1
207
+ : 0;
208
+ const endRowIndex = Math.min(
209
+ allowedEndRowIndex,
210
+ Math.max(headerLastRowIndex, sourceLastRowIndex),
211
+ );
212
+
213
+ if (endRowIndex < startRowIndex) {
214
+ return null;
215
+ }
216
+
217
+ return {
218
+ endColumnIndex: startColumnIndex + columnCount - 1,
219
+ endRowIndex,
220
+ startColumnIndex,
221
+ startRowIndex,
222
+ };
223
+ }
224
+
225
+ /*
226
+ * Return the configured footprint reserved by one data table-backed region.
227
+ */
228
+ export function getSheetStructureRegionConfiguredBounds(
229
+ region: SheetStructureRegionFootprint,
230
+ ) {
231
+ return getSheetStructureRegionContentBounds(
232
+ region,
233
+ SHEET_DATA_TABLE_REGION_MAX_ROWS,
234
+ );
235
+ }
236
+
237
+ /*
238
+ * Return whether one region should be protected during row or column structure edits.
239
+ */
240
+ export function isSheetStructureProtectedRegion(
241
+ region: SheetStructureRegionFootprint,
242
+ ) {
243
+ return (
244
+ region.type === 'DATA_TABLE' &&
245
+ Boolean(region.sourceDataTableId || region.source?.dataTableId)
246
+ );
247
+ }
248
+
249
+ /*
250
+ * Return the active region footprints that should be protected during a structure edit.
251
+ */
252
+ export function getSheetStructureProtectedBounds(
253
+ regions?: SheetStructureRegionFootprint[] | null,
254
+ ) {
255
+ return (regions || [])
256
+ .filter(isSheetStructureProtectedRegion)
257
+ .map((region) => getSheetStructureRegionConfiguredBounds(region))
258
+ .filter((bounds): bounds is SheetStructureBounds => Boolean(bounds));
259
+ }
260
+
261
+ /*
262
+ * Return axis spans from protected region bounds for row or column design shifting.
263
+ */
264
+ export function getSheetStructureProtectedAxisSpans(
265
+ bounds: SheetStructureBounds[],
266
+ axis: 'ROW' | 'COLUMN',
267
+ ): SheetProtectedAxisSpan[] {
268
+ return bounds.map((item) =>
269
+ axis === 'ROW'
270
+ ? {
271
+ endIndex: item.endRowIndex,
272
+ startIndex: item.startRowIndex,
273
+ }
274
+ : {
275
+ endIndex: item.endColumnIndex,
276
+ startIndex: item.startColumnIndex,
277
+ },
278
+ );
279
+ }
280
+
281
+ /*
282
+ * Return whether one Sheet coordinate falls inside a rectangular protected footprint.
283
+ */
284
+ export function sheetStructureBoundsContainCell(
285
+ bounds: SheetStructureBounds,
286
+ rowIndex: number,
287
+ columnIndex: number,
288
+ ) {
289
+ return (
290
+ rowIndex >= bounds.startRowIndex &&
291
+ rowIndex <= bounds.endRowIndex &&
292
+ columnIndex >= bounds.startColumnIndex &&
293
+ columnIndex <= bounds.endColumnIndex
294
+ );
295
+ }
296
+
297
+ /*
298
+ * Return whether one cell coordinate is outside every protected footprint.
299
+ */
300
+ export function isSheetStructureCellOutsideProtectedBounds(
301
+ bounds: SheetStructureBounds[],
302
+ rowIndex: number,
303
+ columnIndex: number,
304
+ ) {
305
+ return !bounds.some((bound) =>
306
+ sheetStructureBoundsContainCell(bound, rowIndex, columnIndex),
307
+ );
308
+ }
309
+
310
+ /*
311
+ * Return whether one rectangular range overlaps a protected footprint.
312
+ */
313
+ export function sheetStructureBoundsOverlapRange(
314
+ bounds: SheetStructureBounds,
315
+ startRowIndex: number,
316
+ endRowIndex: number,
317
+ startColumnIndex: number,
318
+ endColumnIndex: number,
319
+ ) {
320
+ return (
321
+ startRowIndex <= bounds.endRowIndex &&
322
+ endRowIndex >= bounds.startRowIndex &&
323
+ startColumnIndex <= bounds.endColumnIndex &&
324
+ endColumnIndex >= bounds.startColumnIndex
325
+ );
326
+ }
327
+
328
+ /*
329
+ * Return whether one range is outside every protected footprint.
330
+ */
331
+ export function isSheetStructureRangeOutsideProtectedBounds(
332
+ bounds: SheetStructureBounds[],
333
+ startRowIndex: number,
334
+ endRowIndex: number,
335
+ startColumnIndex: number,
336
+ endColumnIndex: number,
337
+ ) {
338
+ return !bounds.some((bound) =>
339
+ sheetStructureBoundsOverlapRange(
340
+ bound,
341
+ startRowIndex,
342
+ endRowIndex,
343
+ startColumnIndex,
344
+ endColumnIndex,
345
+ ),
346
+ );
347
+ }
348
+
349
+ /*
350
+ * Return whether a protected region prevents one row-delete upward shift.
351
+ */
352
+ export function isSheetStructureRowDeleteShiftBlocked(
353
+ bounds: SheetStructureBounds[],
354
+ rowIndex: number,
355
+ columnIndex: number,
356
+ targetIndex: number,
357
+ ) {
358
+ return bounds.some(
359
+ (bound) =>
360
+ columnIndex >= bound.startColumnIndex &&
361
+ columnIndex <= bound.endColumnIndex &&
362
+ bound.startRowIndex <= rowIndex - 1 &&
363
+ bound.endRowIndex >= targetIndex,
364
+ );
365
+ }
366
+
367
+ /*
368
+ * Return whether a protected region prevents one column-delete left shift.
369
+ */
370
+ export function isSheetStructureColumnDeleteShiftBlocked(
371
+ bounds: SheetStructureBounds[],
372
+ rowIndex: number,
373
+ columnIndex: number,
374
+ targetIndex: number,
375
+ ) {
376
+ return bounds.some(
377
+ (bound) =>
378
+ rowIndex >= bound.startRowIndex &&
379
+ rowIndex <= bound.endRowIndex &&
380
+ bound.startColumnIndex <= columnIndex - 1 &&
381
+ bound.endColumnIndex >= targetIndex,
382
+ );
383
+ }
384
+
385
+ /*
386
+ * Return whether a protected region prevents one row-range delete upward shift.
387
+ */
388
+ export function isSheetStructureRangeRowDeleteShiftBlocked(
389
+ bounds: SheetStructureBounds[],
390
+ startRowIndex: number,
391
+ startColumnIndex: number,
392
+ endColumnIndex: number,
393
+ targetIndex: number,
394
+ ) {
395
+ return bounds.some(
396
+ (bound) =>
397
+ startColumnIndex <= bound.endColumnIndex &&
398
+ endColumnIndex >= bound.startColumnIndex &&
399
+ bound.startRowIndex <= startRowIndex - 1 &&
400
+ bound.endRowIndex >= targetIndex,
401
+ );
402
+ }
403
+
404
+ /*
405
+ * Return whether a protected region prevents one column-range delete left shift.
406
+ */
407
+ export function isSheetStructureRangeColumnDeleteShiftBlocked(
408
+ bounds: SheetStructureBounds[],
409
+ startColumnIndex: number,
410
+ startRowIndex: number,
411
+ endRowIndex: number,
412
+ targetIndex: number,
413
+ ) {
414
+ return bounds.some(
415
+ (bound) =>
416
+ startRowIndex <= bound.endRowIndex &&
417
+ endRowIndex >= bound.startRowIndex &&
418
+ bound.startColumnIndex <= startColumnIndex - 1 &&
419
+ bound.endColumnIndex >= targetIndex,
420
+ );
421
+ }
422
+
423
+ /*
424
+ * Return the row and column coordinate for one cell after a structure edit.
425
+ */
426
+ export function getSheetStructureCellCoordAfterEdit(
427
+ rowIndex: number,
428
+ columnIndex: number,
429
+ operation: SheetStructureOperationEnum,
430
+ targetIndex: number,
431
+ bounds: SheetStructureBounds[] = [],
432
+ ): SheetStructureCellCoord | null {
433
+ if (operation === 'INSERT_ROW_ABOVE' && rowIndex >= targetIndex) {
434
+ return isSheetStructureCellOutsideProtectedBounds(
435
+ bounds,
436
+ rowIndex,
437
+ columnIndex,
438
+ ) &&
439
+ isSheetStructureCellOutsideProtectedBounds(
440
+ bounds,
441
+ rowIndex + 1,
442
+ columnIndex,
443
+ )
444
+ ? {
445
+ columnIndex,
446
+ rowIndex: rowIndex + 1,
447
+ }
448
+ : {
449
+ columnIndex,
450
+ rowIndex,
451
+ };
452
+ }
453
+
454
+ if (operation === 'INSERT_COLUMN_LEFT' && columnIndex >= targetIndex) {
455
+ return isSheetStructureCellOutsideProtectedBounds(
456
+ bounds,
457
+ rowIndex,
458
+ columnIndex,
459
+ ) &&
460
+ isSheetStructureCellOutsideProtectedBounds(
461
+ bounds,
462
+ rowIndex,
463
+ columnIndex + 1,
464
+ )
465
+ ? {
466
+ columnIndex: columnIndex + 1,
467
+ rowIndex,
468
+ }
469
+ : {
470
+ columnIndex,
471
+ rowIndex,
472
+ };
473
+ }
474
+
475
+ if (operation === 'DELETE_ROW') {
476
+ if (
477
+ rowIndex === targetIndex &&
478
+ isSheetStructureCellOutsideProtectedBounds(bounds, rowIndex, columnIndex)
479
+ ) {
480
+ return null;
481
+ }
482
+
483
+ if (
484
+ rowIndex > targetIndex &&
485
+ isSheetStructureCellOutsideProtectedBounds(
486
+ bounds,
487
+ rowIndex,
488
+ columnIndex,
489
+ ) &&
490
+ isSheetStructureCellOutsideProtectedBounds(
491
+ bounds,
492
+ rowIndex - 1,
493
+ columnIndex,
494
+ ) &&
495
+ !isSheetStructureRowDeleteShiftBlocked(
496
+ bounds,
497
+ rowIndex,
498
+ columnIndex,
499
+ targetIndex,
500
+ )
501
+ ) {
502
+ return {
503
+ columnIndex,
504
+ rowIndex: rowIndex - 1,
505
+ };
506
+ }
507
+ }
508
+
509
+ if (operation === 'DELETE_COLUMN') {
510
+ if (
511
+ columnIndex === targetIndex &&
512
+ isSheetStructureCellOutsideProtectedBounds(bounds, rowIndex, columnIndex)
513
+ ) {
514
+ return null;
515
+ }
516
+
517
+ if (
518
+ columnIndex > targetIndex &&
519
+ isSheetStructureCellOutsideProtectedBounds(
520
+ bounds,
521
+ rowIndex,
522
+ columnIndex,
523
+ ) &&
524
+ isSheetStructureCellOutsideProtectedBounds(
525
+ bounds,
526
+ rowIndex,
527
+ columnIndex - 1,
528
+ ) &&
529
+ !isSheetStructureColumnDeleteShiftBlocked(
530
+ bounds,
531
+ rowIndex,
532
+ columnIndex,
533
+ targetIndex,
534
+ )
535
+ ) {
536
+ return {
537
+ columnIndex: columnIndex - 1,
538
+ rowIndex,
539
+ };
540
+ }
541
+ }
542
+
543
+ return {
544
+ columnIndex,
545
+ rowIndex,
546
+ };
547
+ }
548
+
549
+ /*
550
+ * Return the range coordinate fields after a structure edit.
551
+ */
552
+ export function getSheetStructureRangeAfterEdit<
553
+ T extends SheetStructureRangeLike,
554
+ >(
555
+ range: T,
556
+ operation: SheetStructureOperationEnum,
557
+ targetIndex: number,
558
+ bounds: SheetStructureBounds[] = [],
559
+ ): T | null {
560
+ const startRowIndex = normalizeSheetStructureIndex(range.startRowIndex);
561
+ const endRowIndex = normalizeSheetStructureIndex(range.endRowIndex);
562
+ const startColumnIndex = normalizeSheetStructureIndex(range.startColumnIndex);
563
+ const endColumnIndex = normalizeSheetStructureIndex(range.endColumnIndex);
564
+
565
+ if (!startRowIndex || !endRowIndex || !startColumnIndex || !endColumnIndex) {
566
+ return range;
567
+ }
568
+
569
+ const outside = isSheetStructureRangeOutsideProtectedBounds(
570
+ bounds,
571
+ startRowIndex,
572
+ endRowIndex,
573
+ startColumnIndex,
574
+ endColumnIndex,
575
+ );
576
+
577
+ if (!outside) {
578
+ return range;
579
+ }
580
+
581
+ if (operation === 'INSERT_ROW_ABOVE') {
582
+ if (startRowIndex >= targetIndex) {
583
+ return {
584
+ ...range,
585
+ endRowIndex: endRowIndex + 1,
586
+ startRowIndex: startRowIndex + 1,
587
+ };
588
+ }
589
+
590
+ if (startRowIndex < targetIndex && endRowIndex >= targetIndex) {
591
+ return {
592
+ ...range,
593
+ endRowIndex: endRowIndex + 1,
594
+ };
595
+ }
596
+ }
597
+
598
+ if (operation === 'INSERT_COLUMN_LEFT') {
599
+ if (startColumnIndex >= targetIndex) {
600
+ return {
601
+ ...range,
602
+ endColumnIndex: endColumnIndex + 1,
603
+ startColumnIndex: startColumnIndex + 1,
604
+ };
605
+ }
606
+
607
+ if (startColumnIndex < targetIndex && endColumnIndex >= targetIndex) {
608
+ return {
609
+ ...range,
610
+ endColumnIndex: endColumnIndex + 1,
611
+ };
612
+ }
613
+ }
614
+
615
+ if (operation === 'DELETE_ROW') {
616
+ if (startRowIndex === targetIndex && endRowIndex === targetIndex) {
617
+ return null;
618
+ }
619
+
620
+ if (startRowIndex <= targetIndex && endRowIndex >= targetIndex) {
621
+ return {
622
+ ...range,
623
+ endRowIndex: endRowIndex - 1,
624
+ };
625
+ }
626
+
627
+ if (
628
+ startRowIndex > targetIndex &&
629
+ !isSheetStructureRangeRowDeleteShiftBlocked(
630
+ bounds,
631
+ startRowIndex,
632
+ startColumnIndex,
633
+ endColumnIndex,
634
+ targetIndex,
635
+ )
636
+ ) {
637
+ return {
638
+ ...range,
639
+ endRowIndex: endRowIndex - 1,
640
+ startRowIndex: startRowIndex - 1,
641
+ };
642
+ }
643
+ }
644
+
645
+ if (operation === 'DELETE_COLUMN') {
646
+ if (startColumnIndex === targetIndex && endColumnIndex === targetIndex) {
647
+ return null;
648
+ }
649
+
650
+ if (startColumnIndex <= targetIndex && endColumnIndex >= targetIndex) {
651
+ return {
652
+ ...range,
653
+ endColumnIndex: endColumnIndex - 1,
654
+ };
655
+ }
656
+
657
+ if (
658
+ startColumnIndex > targetIndex &&
659
+ !isSheetStructureRangeColumnDeleteShiftBlocked(
660
+ bounds,
661
+ startColumnIndex,
662
+ startRowIndex,
663
+ endRowIndex,
664
+ targetIndex,
665
+ )
666
+ ) {
667
+ return {
668
+ ...range,
669
+ endColumnIndex: endColumnIndex - 1,
670
+ startColumnIndex: startColumnIndex - 1,
671
+ };
672
+ }
673
+ }
674
+
675
+ return range;
676
+ }
677
+
678
+ /*
679
+ * Return sparse cells keyed by their post-structure-edit coordinates.
680
+ */
681
+ export function applySheetStructureEditToCellsByCoord<
682
+ T extends SheetStructureCellLike,
683
+ >(
684
+ cellsByCoord: Map<string, T>,
685
+ operation: SheetStructureOperationEnum,
686
+ targetIndex: number,
687
+ bounds: SheetStructureBounds[] = [],
688
+ ) {
689
+ let changed = false;
690
+ const nextCellsByCoord = new Map<string, T>();
691
+
692
+ cellsByCoord.forEach((cell, coordKey) => {
693
+ const rowIndex = normalizeSheetStructureIndex(cell.rowIndex);
694
+ const columnIndex = normalizeSheetStructureIndex(cell.columnIndex);
695
+
696
+ if (!rowIndex || !columnIndex) {
697
+ nextCellsByCoord.set(coordKey, cell);
698
+ return;
699
+ }
700
+
701
+ const nextCoord = getSheetStructureCellCoordAfterEdit(
702
+ rowIndex,
703
+ columnIndex,
704
+ operation,
705
+ targetIndex,
706
+ bounds,
707
+ );
708
+ if (!nextCoord) {
709
+ changed = true;
710
+ return;
711
+ }
712
+
713
+ const nextCoordKey = getSheetStructureCoordKey(
714
+ nextCoord.rowIndex,
715
+ nextCoord.columnIndex,
716
+ );
717
+ const nextCell =
718
+ nextCoord.rowIndex === rowIndex && nextCoord.columnIndex === columnIndex
719
+ ? cell
720
+ : ({
721
+ ...cell,
722
+ columnIndex: nextCoord.columnIndex,
723
+ rowIndex: nextCoord.rowIndex,
724
+ } as T);
725
+
726
+ if (nextCoordKey !== coordKey || nextCell !== cell) {
727
+ changed = true;
728
+ }
729
+
730
+ nextCellsByCoord.set(nextCoordKey, nextCell);
731
+ });
732
+
733
+ return changed ? nextCellsByCoord : cellsByCoord;
734
+ }
735
+
736
+ /*
737
+ * Apply one structure edit to a pending-edit map: shift the coordinate keys
738
+ * plus each entry's preview cell and edit-input coordinates. Entries whose
739
+ * coordinate is removed by the edit are dropped.
740
+ */
741
+ export function applySheetStructureEditToPendingEdits<
742
+ T extends {
743
+ input: { cell: { rowIndex: number; columnIndex: number } };
744
+ previewCell: { rowIndex?: number | null; columnIndex?: number | null };
745
+ },
746
+ >(
747
+ pendingEditsByCoord: Map<string, T>,
748
+ operation: SheetStructureOperationEnum,
749
+ targetIndex: number,
750
+ bounds: SheetStructureBounds[] = [],
751
+ ) {
752
+ let changed = false;
753
+ const nextPendingEditsByCoord = new Map<string, T>();
754
+
755
+ pendingEditsByCoord.forEach((pendingEdit, coordKey) => {
756
+ const rowIndex = normalizeSheetStructureIndex(
757
+ pendingEdit.input.cell.rowIndex,
758
+ );
759
+ const columnIndex = normalizeSheetStructureIndex(
760
+ pendingEdit.input.cell.columnIndex,
761
+ );
762
+
763
+ if (!rowIndex || !columnIndex) {
764
+ nextPendingEditsByCoord.set(coordKey, pendingEdit);
765
+ return;
766
+ }
767
+
768
+ const nextCoord = getSheetStructureCellCoordAfterEdit(
769
+ rowIndex,
770
+ columnIndex,
771
+ operation,
772
+ targetIndex,
773
+ bounds,
774
+ );
775
+ if (!nextCoord) {
776
+ changed = true;
777
+ return;
778
+ }
779
+
780
+ if (
781
+ nextCoord.rowIndex === rowIndex &&
782
+ nextCoord.columnIndex === columnIndex
783
+ ) {
784
+ nextPendingEditsByCoord.set(coordKey, pendingEdit);
785
+ return;
786
+ }
787
+
788
+ changed = true;
789
+ nextPendingEditsByCoord.set(
790
+ getSheetStructureCoordKey(nextCoord.rowIndex, nextCoord.columnIndex),
791
+ {
792
+ ...pendingEdit,
793
+ input: {
794
+ ...pendingEdit.input,
795
+ cell: {
796
+ ...pendingEdit.input.cell,
797
+ columnIndex: nextCoord.columnIndex,
798
+ rowIndex: nextCoord.rowIndex,
799
+ },
800
+ },
801
+ previewCell: {
802
+ ...pendingEdit.previewCell,
803
+ columnIndex: nextCoord.columnIndex,
804
+ rowIndex: nextCoord.rowIndex,
805
+ },
806
+ },
807
+ );
808
+ });
809
+
810
+ return changed ? nextPendingEditsByCoord : pendingEditsByCoord;
811
+ }
812
+
813
+ /*
814
+ * Return saved ranges with their coordinates projected through one structure edit.
815
+ */
816
+ export function applySheetStructureEditToRanges<
817
+ T extends SheetStructureRangeLike,
818
+ >(
819
+ ranges: T[] | null | undefined,
820
+ operation: SheetStructureOperationEnum,
821
+ targetIndex: number,
822
+ bounds: SheetStructureBounds[] = [],
823
+ ) {
824
+ let changed = false;
825
+ const nextRanges: T[] = [];
826
+
827
+ (ranges || []).forEach((range) => {
828
+ const nextRange = getSheetStructureRangeAfterEdit(
829
+ range,
830
+ operation,
831
+ targetIndex,
832
+ bounds,
833
+ );
834
+
835
+ if (!nextRange) {
836
+ changed = true;
837
+ return;
838
+ }
839
+
840
+ if (nextRange !== range) {
841
+ changed = true;
842
+ }
843
+
844
+ nextRanges.push(nextRange);
845
+ });
846
+
847
+ return changed ? nextRanges : ranges || [];
848
+ }
849
+
850
+ /*
851
+ * Return the row or column design after one structure edit.
852
+ */
853
+ export function getSheetStructureDesignAfterEdit(
854
+ designInput: SheetDesignObj,
855
+ operation: SheetStructureOperationEnum,
856
+ targetIndex: number,
857
+ bounds: SheetStructureBounds[] = [],
858
+ ) {
859
+ const design = normalizeSheetDesign(designInput);
860
+ const rowSpans = getSheetStructureProtectedAxisSpans(bounds, 'ROW');
861
+ const columnSpans = getSheetStructureProtectedAxisSpans(bounds, 'COLUMN');
862
+ const rowDelta =
863
+ operation === 'INSERT_ROW_ABOVE' ? 1 : operation === 'DELETE_ROW' ? -1 : 0;
864
+ const columnDelta =
865
+ operation === 'INSERT_COLUMN_LEFT'
866
+ ? 1
867
+ : operation === 'DELETE_COLUMN'
868
+ ? -1
869
+ : 0;
870
+
871
+ const merges = getSheetMergedRanges(design.metadata);
872
+ const shiftedMerges = merges.length
873
+ ? shiftSheetMergedRangesForStructureEdit(merges, operation, targetIndex)
874
+ : merges;
875
+
876
+ return {
877
+ ...design,
878
+ grid: {
879
+ ...design.grid,
880
+ columnCount: Math.min(
881
+ SHEET_MAX_COLUMN_COUNT,
882
+ Math.max(1, design.grid.columnCount + columnDelta),
883
+ ),
884
+ rowCount: Math.min(
885
+ SHEET_MAX_ROW_COUNT,
886
+ Math.max(1, design.grid.rowCount + rowDelta),
887
+ ),
888
+ },
889
+ columns: columnDelta
890
+ ? shiftSheetAxisDesignForStructureEdit(
891
+ design.columns,
892
+ operation,
893
+ targetIndex,
894
+ columnSpans,
895
+ )
896
+ : design.columns,
897
+ rows: rowDelta
898
+ ? shiftSheetAxisDesignForStructureEdit(
899
+ design.rows,
900
+ operation,
901
+ targetIndex,
902
+ rowSpans,
903
+ )
904
+ : design.rows,
905
+ ...(merges.length
906
+ ? {
907
+ metadata: {
908
+ ...(design.metadata || {}),
909
+ merges: shiftedMerges,
910
+ },
911
+ }
912
+ : {}),
913
+ };
914
+ }
915
+
916
+ /*
917
+ * Return one queued design-save patch with its axis maps and merged ranges
918
+ * projected through a structure edit. A patch built before a collaborator's
919
+ * insert/delete would otherwise write pre-shift indexes over the design the
920
+ * server already shifted.
921
+ */
922
+ export function applySheetStructureEditToDesignPatch<
923
+ T extends {
924
+ columns?: string | null;
925
+ metadata?: string | null;
926
+ rows?: string | null;
927
+ },
928
+ >(
929
+ patch: T,
930
+ operation: SheetStructureOperationEnum,
931
+ targetIndex: number,
932
+ bounds: SheetStructureBounds[] = [],
933
+ ): T {
934
+ const rowDelta =
935
+ operation === 'INSERT_ROW_ABOVE' ? 1 : operation === 'DELETE_ROW' ? -1 : 0;
936
+ const columnDelta =
937
+ operation === 'INSERT_COLUMN_LEFT'
938
+ ? 1
939
+ : operation === 'DELETE_COLUMN'
940
+ ? -1
941
+ : 0;
942
+ let next = patch;
943
+ const ensureNext = () => {
944
+ if (next === patch) {
945
+ next = { ...patch };
946
+ }
947
+ return next;
948
+ };
949
+
950
+ if (patch.columns && columnDelta) {
951
+ const columnSpans = getSheetStructureProtectedAxisSpans(bounds, 'COLUMN');
952
+ const columns = parseSheetDesignPatchObject<
953
+ Record<string, SheetAxisDesignObj>
954
+ >(patch.columns, {});
955
+
956
+ ensureNext().columns = stringifyJSON(
957
+ shiftSheetAxisDesignForStructureEdit(
958
+ columns,
959
+ operation,
960
+ targetIndex,
961
+ columnSpans,
962
+ ),
963
+ );
964
+ }
965
+
966
+ if (patch.rows && rowDelta) {
967
+ const rowSpans = getSheetStructureProtectedAxisSpans(bounds, 'ROW');
968
+ const rows = parseSheetDesignPatchObject<
969
+ Record<string, SheetAxisDesignObj>
970
+ >(patch.rows, {});
971
+
972
+ ensureNext().rows = stringifyJSON(
973
+ shiftSheetAxisDesignForStructureEdit(
974
+ rows,
975
+ operation,
976
+ targetIndex,
977
+ rowSpans,
978
+ ),
979
+ );
980
+ }
981
+
982
+ if (patch.metadata) {
983
+ const metadata = parseSheetDesignPatchObject<Record<string, any>>(
984
+ patch.metadata,
985
+ {},
986
+ );
987
+ const merges = getSheetMergedRanges(metadata);
988
+
989
+ if (merges.length) {
990
+ ensureNext().metadata = stringifyJSON({
991
+ ...metadata,
992
+ merges: shiftSheetMergedRangesForStructureEdit(
993
+ merges,
994
+ operation,
995
+ targetIndex,
996
+ ),
997
+ });
998
+ }
999
+ }
1000
+
1001
+ return next;
1002
+ }
1003
+
1004
+ /*
1005
+ * Return one loaded row count after an optimistic row structure edit.
1006
+ */
1007
+ export function getSheetLoadedRowCountAfterStructureEdit(
1008
+ loadedRowCount: number,
1009
+ operation: SheetStructureOperationEnum,
1010
+ nextDesign: SheetDesignObj,
1011
+ ) {
1012
+ const rowDelta =
1013
+ operation === 'INSERT_ROW_ABOVE' ? 1 : operation === 'DELETE_ROW' ? -1 : 0;
1014
+
1015
+ return Math.max(
1016
+ 1,
1017
+ Math.min(nextDesign.grid.rowCount, loadedRowCount + rowDelta),
1018
+ );
1019
+ }
1020
+
1021
+ /*
1022
+ * Return one last-content row index after an optimistic row structure edit.
1023
+ */
1024
+ export function getSheetLastContentRowIndexAfterStructureEdit(
1025
+ lastContentRowIndex: number | null | undefined,
1026
+ operation: SheetStructureOperationEnum,
1027
+ targetIndex: number,
1028
+ ) {
1029
+ if (
1030
+ !lastContentRowIndex ||
1031
+ operation === 'INSERT_COLUMN_LEFT' ||
1032
+ operation === 'DELETE_COLUMN'
1033
+ ) {
1034
+ return lastContentRowIndex || null;
1035
+ }
1036
+
1037
+ if (operation === 'INSERT_ROW_ABOVE' && targetIndex <= lastContentRowIndex) {
1038
+ return lastContentRowIndex + 1;
1039
+ }
1040
+
1041
+ if (operation === 'DELETE_ROW' && targetIndex <= lastContentRowIndex) {
1042
+ return Math.max(1, lastContentRowIndex - 1);
1043
+ }
1044
+
1045
+ return lastContentRowIndex;
1046
+ }
1047
+
1048
+ /*
1049
+ * Return a SQL predicate that excludes coordinates inside protected region bounds.
1050
+ */
1051
+ export function getSheetOutsideProtectedCellBoundsPredicate(
1052
+ bounds: SheetStructureBounds[],
1053
+ rowExpression: string,
1054
+ columnExpression: string,
1055
+ startParameterIndex: number,
1056
+ ): SheetStructureBoundsPredicate {
1057
+ if (!bounds.length) {
1058
+ return {
1059
+ args: [],
1060
+ text: 'TRUE',
1061
+ };
1062
+ }
1063
+
1064
+ const args: number[] = [];
1065
+ const clauses = bounds.map((bound, index) => {
1066
+ const parameterIndex = startParameterIndex + index * 4;
1067
+
1068
+ args.push(
1069
+ bound.startRowIndex,
1070
+ bound.endRowIndex,
1071
+ bound.startColumnIndex,
1072
+ bound.endColumnIndex,
1073
+ );
1074
+
1075
+ return `(${rowExpression} BETWEEN $${parameterIndex} AND $${parameterIndex + 1}
1076
+ AND ${columnExpression} BETWEEN $${parameterIndex + 2} AND $${parameterIndex + 3})`;
1077
+ });
1078
+
1079
+ return {
1080
+ args,
1081
+ text: `NOT (${clauses.join(' OR ')})`,
1082
+ };
1083
+ }
1084
+
1085
+ /*
1086
+ * Return a SQL predicate that excludes ranges overlapping protected region bounds.
1087
+ */
1088
+ export function getSheetOutsideProtectedRangeBoundsPredicate(
1089
+ bounds: SheetStructureBounds[],
1090
+ startRowExpression: string,
1091
+ endRowExpression: string,
1092
+ startColumnExpression: string,
1093
+ endColumnExpression: string,
1094
+ startParameterIndex: number,
1095
+ ): SheetStructureBoundsPredicate {
1096
+ if (!bounds.length) {
1097
+ return {
1098
+ args: [],
1099
+ text: 'TRUE',
1100
+ };
1101
+ }
1102
+
1103
+ const args: number[] = [];
1104
+ const clauses = bounds.map((bound, index) => {
1105
+ const parameterIndex = startParameterIndex + index * 4;
1106
+
1107
+ args.push(
1108
+ bound.endRowIndex,
1109
+ bound.startRowIndex,
1110
+ bound.endColumnIndex,
1111
+ bound.startColumnIndex,
1112
+ );
1113
+
1114
+ return `(${startRowExpression} <= $${parameterIndex}
1115
+ AND ${endRowExpression} >= $${parameterIndex + 1}
1116
+ AND ${startColumnExpression} <= $${parameterIndex + 2}
1117
+ AND ${endColumnExpression} >= $${parameterIndex + 3})`;
1118
+ });
1119
+
1120
+ return {
1121
+ args,
1122
+ text: `NOT (${clauses.join(' OR ')})`,
1123
+ };
1124
+ }
1125
+
1126
+ /*
1127
+ * Return a SQL predicate that prevents row-delete shifts from crossing protected row spans.
1128
+ */
1129
+ export function getSheetRowDeleteBarrierPredicate(
1130
+ bounds: SheetStructureBounds[],
1131
+ rowExpression: string,
1132
+ columnExpression: string,
1133
+ targetIndexExpression: string,
1134
+ startParameterIndex: number,
1135
+ ): SheetStructureBoundsPredicate {
1136
+ if (!bounds.length) {
1137
+ return {
1138
+ args: [],
1139
+ text: 'TRUE',
1140
+ };
1141
+ }
1142
+
1143
+ const args: number[] = [];
1144
+ const clauses = bounds.map((bound, index) => {
1145
+ const parameterIndex = startParameterIndex + index * 4;
1146
+
1147
+ args.push(
1148
+ bound.startColumnIndex,
1149
+ bound.endColumnIndex,
1150
+ bound.startRowIndex,
1151
+ bound.endRowIndex,
1152
+ );
1153
+
1154
+ return `(${columnExpression} BETWEEN $${parameterIndex} AND $${parameterIndex + 1}
1155
+ AND $${parameterIndex + 2} <= ${rowExpression} - 1
1156
+ AND $${parameterIndex + 3} >= ${targetIndexExpression})`;
1157
+ });
1158
+
1159
+ return {
1160
+ args,
1161
+ text: `NOT (${clauses.join(' OR ')})`,
1162
+ };
1163
+ }
1164
+
1165
+ /*
1166
+ * Return a SQL predicate that prevents column-delete shifts from crossing protected column spans.
1167
+ */
1168
+ export function getSheetColumnDeleteBarrierPredicate(
1169
+ bounds: SheetStructureBounds[],
1170
+ rowExpression: string,
1171
+ columnExpression: string,
1172
+ targetIndexExpression: string,
1173
+ startParameterIndex: number,
1174
+ ): SheetStructureBoundsPredicate {
1175
+ if (!bounds.length) {
1176
+ return {
1177
+ args: [],
1178
+ text: 'TRUE',
1179
+ };
1180
+ }
1181
+
1182
+ const args: number[] = [];
1183
+ const clauses = bounds.map((bound, index) => {
1184
+ const parameterIndex = startParameterIndex + index * 4;
1185
+
1186
+ args.push(
1187
+ bound.startRowIndex,
1188
+ bound.endRowIndex,
1189
+ bound.startColumnIndex,
1190
+ bound.endColumnIndex,
1191
+ );
1192
+
1193
+ return `(${rowExpression} BETWEEN $${parameterIndex} AND $${parameterIndex + 1}
1194
+ AND $${parameterIndex + 2} <= ${columnExpression} - 1
1195
+ AND $${parameterIndex + 3} >= ${targetIndexExpression})`;
1196
+ });
1197
+
1198
+ return {
1199
+ args,
1200
+ text: `NOT (${clauses.join(' OR ')})`,
1201
+ };
1202
+ }
1203
+
1204
+ /*
1205
+ * Return a SQL predicate that prevents row-range shifts from crossing protected bounds.
1206
+ */
1207
+ export function getSheetRangeRowDeleteBarrierPredicate(
1208
+ bounds: SheetStructureBounds[],
1209
+ startRowExpression: string,
1210
+ startColumnExpression: string,
1211
+ endColumnExpression: string,
1212
+ targetIndexExpression: string,
1213
+ startParameterIndex: number,
1214
+ ): SheetStructureBoundsPredicate {
1215
+ if (!bounds.length) {
1216
+ return {
1217
+ args: [],
1218
+ text: 'TRUE',
1219
+ };
1220
+ }
1221
+
1222
+ const args: number[] = [];
1223
+ const clauses = bounds.map((bound, index) => {
1224
+ const parameterIndex = startParameterIndex + index * 4;
1225
+
1226
+ args.push(
1227
+ bound.startColumnIndex,
1228
+ bound.endColumnIndex,
1229
+ bound.startRowIndex,
1230
+ bound.endRowIndex,
1231
+ );
1232
+
1233
+ return `(${startColumnExpression} <= $${parameterIndex + 1}
1234
+ AND ${endColumnExpression} >= $${parameterIndex}
1235
+ AND $${parameterIndex + 2} <= ${startRowExpression} - 1
1236
+ AND $${parameterIndex + 3} >= ${targetIndexExpression})`;
1237
+ });
1238
+
1239
+ return {
1240
+ args,
1241
+ text: `NOT (${clauses.join(' OR ')})`,
1242
+ };
1243
+ }
1244
+
1245
+ /*
1246
+ * Return a SQL predicate that prevents column-range shifts from crossing protected bounds.
1247
+ */
1248
+ export function getSheetRangeColumnDeleteBarrierPredicate(
1249
+ bounds: SheetStructureBounds[],
1250
+ startColumnExpression: string,
1251
+ startRowExpression: string,
1252
+ endRowExpression: string,
1253
+ targetIndexExpression: string,
1254
+ startParameterIndex: number,
1255
+ ): SheetStructureBoundsPredicate {
1256
+ if (!bounds.length) {
1257
+ return {
1258
+ args: [],
1259
+ text: 'TRUE',
1260
+ };
1261
+ }
1262
+
1263
+ const args: number[] = [];
1264
+ const clauses = bounds.map((bound, index) => {
1265
+ const parameterIndex = startParameterIndex + index * 4;
1266
+
1267
+ args.push(
1268
+ bound.startRowIndex,
1269
+ bound.endRowIndex,
1270
+ bound.startColumnIndex,
1271
+ bound.endColumnIndex,
1272
+ );
1273
+
1274
+ return `(${startRowExpression} <= $${parameterIndex + 1}
1275
+ AND ${endRowExpression} >= $${parameterIndex}
1276
+ AND $${parameterIndex + 2} <= ${startColumnExpression} - 1
1277
+ AND $${parameterIndex + 3} >= ${targetIndexExpression})`;
1278
+ });
1279
+
1280
+ return {
1281
+ args,
1282
+ text: `NOT (${clauses.join(' OR ')})`,
1283
+ };
1284
+ }