@jsb188/mday 1.1.10 → 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 (119) hide show
  1. package/package.json +105 -54
  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 +35 -9
  7. package/src/constants/dataTableFieldTypes.generated.ts +63 -0
  8. package/src/constants/docAccess.ts +32 -0
  9. package/src/constants/email.ts +20 -4
  10. package/src/constants/fieldDoc.ts +34 -0
  11. package/src/constants/graphqlReactivity.ts +113 -0
  12. package/src/constants/integration.ts +8 -9
  13. package/src/constants/log.ts +7 -410
  14. package/src/constants/logActivityGroupColors.generated.ts +19 -0
  15. package/src/constants/logTaxonomy.generated.ts +1103 -0
  16. package/src/constants/noteDoc.ts +77 -0
  17. package/src/constants/organization.ts +13 -15
  18. package/src/constants/pageRoute.ts +23 -0
  19. package/src/constants/schedule.ts +220 -0
  20. package/src/constants/scheduleRuleTemplateSeed.ts +138 -0
  21. package/src/constants/scheduleRuleTemplates.generated.ts +145 -0
  22. package/src/constants/sheet.ts +154 -6
  23. package/src/constants/sheetRegionSources.generated.ts +578 -0
  24. package/src/constants/square.ts +6 -6
  25. package/src/constants/storage.ts +56 -7
  26. package/src/constants/usage.ts +79 -0
  27. package/src/schemas/editLogEntry.ts +1010 -777
  28. package/src/schemas/fieldDocsQuery.ts +12 -0
  29. package/src/schemas/inboundContact.ts +60 -55
  30. package/src/schemas/logsQuery.ts +55 -49
  31. package/src/schemas/organization.ts +49 -46
  32. package/src/types/account.d.ts +4 -1
  33. package/src/types/agentDoc.d.ts +64 -0
  34. package/src/types/agentMemory.d.ts +74 -0
  35. package/src/types/campaign.d.ts +683 -0
  36. package/src/types/dataTable.d.ts +175 -256
  37. package/src/types/docAccess.d.ts +96 -0
  38. package/src/types/email.d.ts +83 -12
  39. package/src/types/fieldDoc.d.ts +273 -0
  40. package/src/types/inboundContact.d.ts +2 -1
  41. package/src/types/log.d.ts +209 -341
  42. package/src/types/noteDoc.d.ts +86 -0
  43. package/src/types/organization.d.ts +83 -40
  44. package/src/types/pageRoute.d.ts +48 -0
  45. package/src/types/schedule.d.ts +391 -0
  46. package/src/types/sheet.d.ts +862 -30
  47. package/src/types/storage.d.ts +206 -125
  48. package/src/types/usage.d.ts +106 -0
  49. package/src/types/workflow.d.ts +149 -18
  50. package/src/utils/agentDoc.ts +109 -0
  51. package/src/utils/aiChat.ts +26 -0
  52. package/src/utils/campaign.ts +741 -0
  53. package/src/utils/campaignFormEditor.ts +871 -0
  54. package/src/utils/dataTable.ts +425 -410
  55. package/src/utils/dataTableFieldTypeRegistry.ts +105 -0
  56. package/src/utils/dataTableRealtimeAuthority.ts +209 -0
  57. package/src/utils/dataTableRealtimeSelection.ts +134 -0
  58. package/src/utils/dataTableRowDocument.ts +508 -0
  59. package/src/utils/dataTableSource.ts +230 -0
  60. package/src/utils/docAccess.ts +383 -0
  61. package/src/utils/documentName.ts +53 -0
  62. package/src/utils/email.ts +112 -53
  63. package/src/utils/fieldDoc.ts +113 -0
  64. package/src/utils/log.ts +169 -176
  65. package/src/utils/logRegistryValue.ts +90 -0
  66. package/src/utils/logStoreRead.ts +266 -0
  67. package/src/utils/logTaxonomy.ts +76 -0
  68. package/src/utils/logTaxonomyDisplay.ts +439 -0
  69. package/src/utils/noteDoc.ts +827 -0
  70. package/src/utils/organization.ts +510 -91
  71. package/src/utils/pageRoute.ts +71 -0
  72. package/src/utils/schedule.ts +799 -0
  73. package/src/utils/scheduleEvaluation.ts +1994 -0
  74. package/src/utils/scheduleICS.ts +246 -0
  75. package/src/utils/scheduleRuleTemplates.ts +188 -0
  76. package/src/utils/sheet.ts +4842 -39
  77. package/src/utils/sheetAxisMove.ts +830 -0
  78. package/src/utils/sheetBootstrap.ts +1769 -0
  79. package/src/utils/sheetCommand.ts +423 -0
  80. package/src/utils/sheetExport.ts +238 -0
  81. package/src/utils/sheetFormulaEvaluation.ts +914 -0
  82. package/src/utils/sheetFormulaEvaluator.ts +852 -0
  83. package/src/utils/sheetJournal.ts +78 -0
  84. package/src/utils/sheetNumberFormat.ts +1211 -0
  85. package/src/utils/sheetRegion.ts +374 -0
  86. package/src/utils/sheetRegionFilter.ts +1259 -0
  87. package/src/utils/sheetRegionSourceRegistry.ts +594 -0
  88. package/src/utils/sheetSequence.ts +148 -0
  89. package/src/utils/sheetSnapshot.ts +992 -0
  90. package/src/utils/sheetStructureEdit.ts +1284 -0
  91. package/src/utils/square.ts +22 -12
  92. package/src/utils/storage.ts +152 -0
  93. package/src/utils/table-route.ts +127 -11
  94. package/src/utils/workflow.ts +191 -23
  95. package/tests/dataTableFieldTypeRegistry.test.ts +110 -0
  96. package/tests/docAccess.test.ts +525 -0
  97. package/tests/editLogEntry.test.ts +112 -0
  98. package/tests/noteDoc.test.ts +669 -0
  99. package/tests/schedule.test.ts +693 -0
  100. package/tests/scheduleEvaluation.test.ts +1970 -0
  101. package/tests/scheduleICS.test.ts +208 -0
  102. package/tests/scheduleRuleTemplates.test.ts +176 -0
  103. package/tests/scheduleSweepInvariants.test.ts +428 -0
  104. package/tests/sheetAxisMove.test.ts +343 -0
  105. package/tests/sheetFormulaEvaluator.test.ts +575 -0
  106. package/tests/sheetRegionFilter.test.ts +1135 -0
  107. package/tests/sheetSequence.test.ts +41 -0
  108. package/tests/sheetStructureEdit.test.ts +82 -0
  109. package/tsconfig.json +10 -10
  110. package/vitest.config.ts +8 -0
  111. package/src/constants/log-activityTexts.ts +0 -235
  112. package/src/constants/product.ts +0 -42
  113. package/src/constants/report.ts +0 -44
  114. package/src/schemas/productsQuery.ts +0 -53
  115. package/src/schemas/reportsQuery.ts +0 -20
  116. package/src/types/product.d.ts +0 -84
  117. package/src/types/report.d.ts +0 -290
  118. package/src/utils/product-calEvent.ts +0 -455
  119. package/src/utils/report.ts +0 -158
@@ -0,0 +1,830 @@
1
+ import type {
2
+ SheetAxisDesignObj,
3
+ SheetDesignObj,
4
+ SheetMergedRangeObj,
5
+ } from '../types/sheet.d.ts';
6
+ import {
7
+ getSheetFormulaCellReferenceText,
8
+ getSheetFormulaExpression,
9
+ getSheetFormulaSheetTabNameLookupKey,
10
+ isSheetFormulaText,
11
+ parseSheetFormulaCall,
12
+ parseSheetFormulaExpression,
13
+ type SheetFormulaCellReference,
14
+ type SheetFormulaReferenceToken,
15
+ tokenizeSheetFormulaReferences,
16
+ } from './sheet.ts';
17
+
18
+ export type SheetAxisMoveType = 'COLUMN' | 'ROW';
19
+
20
+ export interface SheetAxisMoveIndexes {
21
+ sourceEndIndex: number;
22
+ sourceStartIndex: number;
23
+ targetEndIndex: number;
24
+ targetStartIndex: number;
25
+ }
26
+
27
+ export interface SheetAxisMoveInput {
28
+ axis: SheetAxisMoveType;
29
+ sourceEndIndex: number;
30
+ sourceStartIndex: number;
31
+ targetStartIndex: number;
32
+ }
33
+
34
+ /*
35
+ * Return the compact semantic move that restores a completed axis permutation.
36
+ * The moved source block occupies the original target interval after the
37
+ * forward move, so replay can invert the operation without materializing cells.
38
+ */
39
+ export function getInverseSheetAxisMoveInput(
40
+ input: SheetAxisMoveInput,
41
+ ): SheetAxisMoveInput {
42
+ const width = input.sourceEndIndex - input.sourceStartIndex + 1;
43
+
44
+ return {
45
+ axis: input.axis,
46
+ sourceEndIndex: input.targetStartIndex + width - 1,
47
+ sourceStartIndex: input.targetStartIndex,
48
+ targetStartIndex: input.sourceStartIndex,
49
+ };
50
+ }
51
+
52
+ export type SheetAxisMoveFormulaResult =
53
+ | {
54
+ changed: false;
55
+ status: 'UNCHANGED';
56
+ text: string;
57
+ }
58
+ | {
59
+ changed: true;
60
+ status: 'CHANGED';
61
+ text: string;
62
+ }
63
+ | {
64
+ changed: false;
65
+ reason: 'DISCONTIGUOUS_RANGE' | 'INVALID_FORMULA' | 'QUALIFIED_REFERENCE';
66
+ status: 'UNSAFE';
67
+ text: string;
68
+ };
69
+
70
+ /*
71
+ * Normalize one client-authored axis permutation and reject out-of-bounds or
72
+ * semantic no-op requests before either client or server planning begins.
73
+ */
74
+ export function normalizeSheetAxisMoveInput(
75
+ input: SheetAxisMoveInput,
76
+ axisCount: number,
77
+ ): SheetAxisMoveIndexes & { axis: SheetAxisMoveType } {
78
+ const sourceStartIndex = Number(input.sourceStartIndex);
79
+ const sourceEndIndex = Number(input.sourceEndIndex);
80
+ const targetStartIndex = Number(input.targetStartIndex);
81
+ const normalizedAxisCount = Number(axisCount);
82
+ const width = sourceEndIndex - sourceStartIndex + 1;
83
+ const targetEndIndex = targetStartIndex + width - 1;
84
+
85
+ if (
86
+ (input.axis !== 'COLUMN' && input.axis !== 'ROW') ||
87
+ !Number.isSafeInteger(sourceStartIndex) ||
88
+ !Number.isSafeInteger(sourceEndIndex) ||
89
+ !Number.isSafeInteger(targetStartIndex) ||
90
+ !Number.isSafeInteger(targetEndIndex) ||
91
+ !Number.isSafeInteger(normalizedAxisCount) ||
92
+ sourceStartIndex < 1 ||
93
+ sourceEndIndex < sourceStartIndex ||
94
+ targetStartIndex < 1 ||
95
+ targetEndIndex > normalizedAxisCount ||
96
+ sourceEndIndex > normalizedAxisCount ||
97
+ targetStartIndex === sourceStartIndex
98
+ ) {
99
+ throw new Error('INVALID_SHEET_AXIS_MOVE');
100
+ }
101
+
102
+ return {
103
+ axis: input.axis,
104
+ sourceEndIndex,
105
+ sourceStartIndex,
106
+ targetEndIndex,
107
+ targetStartIndex,
108
+ };
109
+ }
110
+
111
+ /*
112
+ * Return the one-dimensional source and destination bounds for a complete row
113
+ * or column move.
114
+ */
115
+ export function getSheetAxisMoveIndexes(params: {
116
+ axis: SheetAxisMoveType;
117
+ sourceRange: SheetMergedRangeObj;
118
+ targetRange: SheetMergedRangeObj;
119
+ }): SheetAxisMoveIndexes {
120
+ const movingColumns = params.axis === 'COLUMN';
121
+
122
+ return {
123
+ sourceEndIndex: movingColumns
124
+ ? params.sourceRange.endColumnIndex
125
+ : params.sourceRange.endRowIndex,
126
+ sourceStartIndex: movingColumns
127
+ ? params.sourceRange.startColumnIndex
128
+ : params.sourceRange.startRowIndex,
129
+ targetEndIndex: movingColumns
130
+ ? params.targetRange.endColumnIndex
131
+ : params.targetRange.endRowIndex,
132
+ targetStartIndex: movingColumns
133
+ ? params.targetRange.startColumnIndex
134
+ : params.targetRange.startRowIndex,
135
+ };
136
+ }
137
+
138
+ /*
139
+ * Return the contiguous rectangle affected when a whole row or column block
140
+ * moves by insertion.
141
+ */
142
+ export function getSheetAxisMoveAffectedRange(
143
+ sourceRange: SheetMergedRangeObj,
144
+ targetRange: SheetMergedRangeObj,
145
+ axis: SheetAxisMoveType,
146
+ ): SheetMergedRangeObj {
147
+ if (axis === 'COLUMN') {
148
+ return {
149
+ endColumnIndex: Math.max(
150
+ sourceRange.endColumnIndex,
151
+ targetRange.endColumnIndex,
152
+ ),
153
+ endRowIndex: sourceRange.endRowIndex,
154
+ startColumnIndex: Math.min(
155
+ sourceRange.startColumnIndex,
156
+ targetRange.startColumnIndex,
157
+ ),
158
+ startRowIndex: sourceRange.startRowIndex,
159
+ };
160
+ }
161
+
162
+ return {
163
+ endColumnIndex: sourceRange.endColumnIndex,
164
+ endRowIndex: Math.max(sourceRange.endRowIndex, targetRange.endRowIndex),
165
+ startColumnIndex: sourceRange.startColumnIndex,
166
+ startRowIndex: Math.min(
167
+ sourceRange.startRowIndex,
168
+ targetRange.startRowIndex,
169
+ ),
170
+ };
171
+ }
172
+
173
+ /*
174
+ * Resolve the original axis index that should occupy one destination index
175
+ * after inserting the selected block at its target.
176
+ */
177
+ export function getSheetAxisMoveSourceIndex(params: {
178
+ destinationIndex: number;
179
+ sourceEndIndex: number;
180
+ sourceStartIndex: number;
181
+ targetEndIndex: number;
182
+ targetStartIndex: number;
183
+ }) {
184
+ if (params.targetStartIndex > params.sourceStartIndex) {
185
+ if (params.destinationIndex >= params.targetStartIndex) {
186
+ return (
187
+ params.sourceStartIndex +
188
+ params.destinationIndex -
189
+ params.targetStartIndex
190
+ );
191
+ }
192
+
193
+ return (
194
+ params.sourceEndIndex +
195
+ 1 +
196
+ params.destinationIndex -
197
+ params.sourceStartIndex
198
+ );
199
+ }
200
+
201
+ if (params.destinationIndex <= params.targetEndIndex) {
202
+ return (
203
+ params.sourceStartIndex +
204
+ params.destinationIndex -
205
+ params.targetStartIndex
206
+ );
207
+ }
208
+
209
+ return (
210
+ params.targetStartIndex +
211
+ params.destinationIndex -
212
+ params.targetEndIndex -
213
+ 1
214
+ );
215
+ }
216
+
217
+ /*
218
+ * Resolve the destination occupied by one original axis index after inserting
219
+ * the selected block at its target.
220
+ */
221
+ export function getSheetAxisMoveDestinationIndex(
222
+ sourceIndex: number,
223
+ params: SheetAxisMoveIndexes,
224
+ ) {
225
+ const width = params.sourceEndIndex - params.sourceStartIndex + 1;
226
+
227
+ if (params.targetStartIndex > params.sourceStartIndex) {
228
+ if (
229
+ sourceIndex >= params.sourceStartIndex &&
230
+ sourceIndex <= params.sourceEndIndex
231
+ ) {
232
+ return sourceIndex + params.targetStartIndex - params.sourceStartIndex;
233
+ }
234
+ if (
235
+ sourceIndex > params.sourceEndIndex &&
236
+ sourceIndex <= params.targetEndIndex
237
+ ) {
238
+ return sourceIndex - width;
239
+ }
240
+ return sourceIndex;
241
+ }
242
+
243
+ if (
244
+ sourceIndex >= params.sourceStartIndex &&
245
+ sourceIndex <= params.sourceEndIndex
246
+ ) {
247
+ return sourceIndex + params.targetStartIndex - params.sourceStartIndex;
248
+ }
249
+ if (
250
+ sourceIndex >= params.targetStartIndex &&
251
+ sourceIndex < params.sourceStartIndex
252
+ ) {
253
+ return sourceIndex + width;
254
+ }
255
+ return sourceIndex;
256
+ }
257
+
258
+ /*
259
+ * Return one grid coordinate after the requested row or column permutation.
260
+ */
261
+ export function getSheetCoordinateAfterAxisMove(
262
+ rowIndex: number,
263
+ columnIndex: number,
264
+ axis: SheetAxisMoveType,
265
+ indexes: SheetAxisMoveIndexes,
266
+ ) {
267
+ return {
268
+ columnIndex:
269
+ axis === 'COLUMN'
270
+ ? getSheetAxisMoveDestinationIndex(columnIndex, indexes)
271
+ : columnIndex,
272
+ rowIndex:
273
+ axis === 'ROW'
274
+ ? getSheetAxisMoveDestinationIndex(rowIndex, indexes)
275
+ : rowIndex,
276
+ };
277
+ }
278
+
279
+ /*
280
+ * Return the bounding axis interval and whether it remains contiguous after
281
+ * every original index follows the same permutation.
282
+ */
283
+ export function getSheetMovedAxisRange(
284
+ startIndex: number,
285
+ endIndex: number,
286
+ indexes: SheetAxisMoveIndexes,
287
+ ) {
288
+ const candidates = new Set([startIndex, endIndex]);
289
+ [
290
+ indexes.sourceStartIndex - 1,
291
+ indexes.sourceStartIndex,
292
+ indexes.sourceEndIndex,
293
+ indexes.sourceEndIndex + 1,
294
+ indexes.targetStartIndex - 1,
295
+ indexes.targetStartIndex,
296
+ indexes.targetEndIndex,
297
+ indexes.targetEndIndex + 1,
298
+ ].forEach((index) => {
299
+ if (index >= startIndex && index <= endIndex) {
300
+ candidates.add(index);
301
+ }
302
+ });
303
+ const destinations = [...candidates].map((index) =>
304
+ getSheetAxisMoveDestinationIndex(index, indexes),
305
+ );
306
+ const movedStartIndex = Math.min(...destinations);
307
+ const movedEndIndex = Math.max(...destinations);
308
+
309
+ return {
310
+ contiguous: movedEndIndex - movedStartIndex === endIndex - startIndex,
311
+ endIndex: movedEndIndex,
312
+ startIndex: movedStartIndex,
313
+ };
314
+ }
315
+
316
+ /*
317
+ * Project one coordinate rectangle through an axis permutation. A null result
318
+ * means the move would split the rectangle into a union that Sheet ranges
319
+ * cannot currently represent.
320
+ */
321
+ export function getSheetCoordinateRangeAfterAxisMove<
322
+ TRange extends SheetMergedRangeObj,
323
+ >(
324
+ range: TRange,
325
+ axis: SheetAxisMoveType,
326
+ indexes: SheetAxisMoveIndexes,
327
+ ): TRange | null {
328
+ const movingColumns = axis === 'COLUMN';
329
+ const movedAxis = getSheetMovedAxisRange(
330
+ movingColumns ? range.startColumnIndex : range.startRowIndex,
331
+ movingColumns ? range.endColumnIndex : range.endRowIndex,
332
+ indexes,
333
+ );
334
+ if (!movedAxis.contiguous) {
335
+ return null;
336
+ }
337
+
338
+ return {
339
+ ...range,
340
+ ...(movingColumns
341
+ ? {
342
+ endColumnIndex: movedAxis.endIndex,
343
+ startColumnIndex: movedAxis.startIndex,
344
+ }
345
+ : {
346
+ endRowIndex: movedAxis.endIndex,
347
+ startRowIndex: movedAxis.startIndex,
348
+ }),
349
+ };
350
+ }
351
+
352
+ /*
353
+ * Project every coordinate rectangle through an axis permutation, failing
354
+ * closed when any rectangle would become a discontiguous union.
355
+ */
356
+ export function getSheetCoordinateRangesAfterAxisMove<
357
+ TRange extends SheetMergedRangeObj,
358
+ >(
359
+ ranges: readonly TRange[],
360
+ axis: SheetAxisMoveType,
361
+ indexes: SheetAxisMoveIndexes,
362
+ ): TRange[] | null {
363
+ const movedRanges: TRange[] = [];
364
+ for (const range of ranges) {
365
+ const movedRange = getSheetCoordinateRangeAfterAxisMove(
366
+ range,
367
+ axis,
368
+ indexes,
369
+ );
370
+ if (!movedRange) {
371
+ return null;
372
+ }
373
+ movedRanges.push(movedRange);
374
+ }
375
+ return movedRanges;
376
+ }
377
+
378
+ /*
379
+ * Return whether one coordinate-backed formatting or named range would need
380
+ * to move or split under an axis permutation.
381
+ */
382
+ export function sheetCoordinateRangeNeedsAxisMoveRemap(params: {
383
+ axis: SheetAxisMoveType;
384
+ range: SheetMergedRangeObj;
385
+ sourceRange: SheetMergedRangeObj;
386
+ targetRange: SheetMergedRangeObj;
387
+ }) {
388
+ const movingColumns = params.axis === 'COLUMN';
389
+ const startIndex = movingColumns
390
+ ? params.range.startColumnIndex
391
+ : params.range.startRowIndex;
392
+ const endIndex = movingColumns
393
+ ? params.range.endColumnIndex
394
+ : params.range.endRowIndex;
395
+ const movedRange = getSheetMovedAxisRange(
396
+ startIndex,
397
+ endIndex,
398
+ getSheetAxisMoveIndexes(params),
399
+ );
400
+
401
+ return (
402
+ !movedRange.contiguous ||
403
+ movedRange.startIndex !== startIndex ||
404
+ movedRange.endIndex !== endIndex
405
+ );
406
+ }
407
+
408
+ /*
409
+ * Rotate sparse row or column design entries through the same insertion span
410
+ * as their cells.
411
+ */
412
+ export function getMovedSheetAxisDesign(params: {
413
+ axisDesign?: Record<string, SheetAxisDesignObj> | null;
414
+ sourceEndIndex: number;
415
+ sourceStartIndex: number;
416
+ targetEndIndex: number;
417
+ targetStartIndex: number;
418
+ }) {
419
+ if (params.sourceStartIndex === params.targetStartIndex) {
420
+ return params.axisDesign || {};
421
+ }
422
+
423
+ const affectedStartIndex = Math.min(
424
+ params.sourceStartIndex,
425
+ params.targetStartIndex,
426
+ );
427
+ const affectedEndIndex = Math.max(
428
+ params.sourceEndIndex,
429
+ params.targetEndIndex,
430
+ );
431
+ const next = { ...(params.axisDesign || {}) };
432
+ const affectedEntries = Object.entries(params.axisDesign || {}).filter(
433
+ ([sourceKey]) => {
434
+ const sourceIndex = Number(sourceKey);
435
+ return (
436
+ Number.isSafeInteger(sourceIndex) &&
437
+ sourceIndex >= affectedStartIndex &&
438
+ sourceIndex <= affectedEndIndex &&
439
+ String(sourceIndex) === sourceKey
440
+ );
441
+ },
442
+ );
443
+
444
+ affectedEntries.forEach(([sourceKey]) => {
445
+ delete next[sourceKey];
446
+ });
447
+ affectedEntries.forEach(([sourceKey, sourceDesign]) => {
448
+ const destinationIndex = getSheetAxisMoveDestinationIndex(
449
+ Number(sourceKey),
450
+ {
451
+ sourceEndIndex: params.sourceEndIndex,
452
+ sourceStartIndex: params.sourceStartIndex,
453
+ targetEndIndex: params.targetEndIndex,
454
+ targetStartIndex: params.targetStartIndex,
455
+ },
456
+ );
457
+ if (sourceDesign) {
458
+ next[String(destinationIndex)] = sourceDesign;
459
+ }
460
+ });
461
+
462
+ return next;
463
+ }
464
+
465
+ /*
466
+ * Project sparse axis design, merged ranges, and named ranges through one
467
+ * normalized row or column permutation. A null result fails closed when a
468
+ * stored rectangle cannot remain contiguous.
469
+ */
470
+ export function getSheetDesignAfterAxisMove(
471
+ design: SheetDesignObj,
472
+ input: SheetAxisMoveInput,
473
+ ): SheetDesignObj | null {
474
+ const axisCount =
475
+ input.axis === 'COLUMN'
476
+ ? Number(design.grid.columnCount)
477
+ : Number(design.grid.rowCount);
478
+ const indexes = normalizeSheetAxisMoveInput(input, axisCount);
479
+ const designMergedRanges = design.metadata?.merges;
480
+ const hasMergedRanges = Array.isArray(designMergedRanges);
481
+ const mergedRanges = hasMergedRanges
482
+ ? (designMergedRanges as SheetMergedRangeObj[])
483
+ : [];
484
+ const hasNamedRanges = Array.isArray(design.namedRanges);
485
+ const movedMergedRanges = getSheetCoordinateRangesAfterAxisMove(
486
+ mergedRanges,
487
+ input.axis,
488
+ indexes,
489
+ );
490
+ const movedNamedRanges = getSheetCoordinateRangesAfterAxisMove(
491
+ design.namedRanges || [],
492
+ input.axis,
493
+ indexes,
494
+ );
495
+ if (!movedMergedRanges || !movedNamedRanges) {
496
+ return null;
497
+ }
498
+
499
+ return {
500
+ ...design,
501
+ ...(input.axis === 'COLUMN'
502
+ ? {
503
+ columns: getMovedSheetAxisDesign({
504
+ axisDesign: design.columns,
505
+ ...indexes,
506
+ }),
507
+ }
508
+ : {
509
+ rows: getMovedSheetAxisDesign({
510
+ axisDesign: design.rows,
511
+ ...indexes,
512
+ }),
513
+ }),
514
+ ...(hasMergedRanges
515
+ ? {
516
+ metadata: {
517
+ ...(design.metadata || {}),
518
+ merges: movedMergedRanges,
519
+ },
520
+ }
521
+ : {}),
522
+ ...(hasNamedRanges ? { namedRanges: movedNamedRanges } : {}),
523
+ };
524
+ }
525
+
526
+ /*
527
+ * Return whether one parsed formula reference resolves to the tab whose axis
528
+ * is being moved. Unqualified references resolve to the formula's own tab.
529
+ */
530
+ function sheetAxisMoveFormulaReferenceTargetsMovedTab(
531
+ sheetName: string | null | undefined,
532
+ params: {
533
+ formulaSheetTabId: string;
534
+ movedSheetTabId: string;
535
+ sheetTabIdByNameKey: ReadonlyMap<string, string>;
536
+ },
537
+ ) {
538
+ if (!sheetName) {
539
+ return params.formulaSheetTabId === params.movedSheetTabId;
540
+ }
541
+
542
+ return (
543
+ params.sheetTabIdByNameKey.get(
544
+ getSheetFormulaSheetTabNameLookupKey(sheetName),
545
+ ) === params.movedSheetTabId
546
+ );
547
+ }
548
+
549
+ /*
550
+ * Return one cell coordinate rewritten through an axis permutation.
551
+ */
552
+ function getSheetAxisMovedFormulaCellText(
553
+ reference: SheetFormulaCellReference,
554
+ axis: SheetAxisMoveType,
555
+ indexes: SheetAxisMoveIndexes,
556
+ ) {
557
+ const columnIndex =
558
+ axis === 'COLUMN'
559
+ ? getSheetAxisMoveDestinationIndex(reference.columnIndex, indexes)
560
+ : reference.columnIndex;
561
+ const rowIndex =
562
+ axis === 'ROW'
563
+ ? getSheetAxisMoveDestinationIndex(reference.rowIndex, indexes)
564
+ : reference.rowIndex;
565
+
566
+ return getSheetFormulaCellReferenceText({
567
+ ...reference,
568
+ columnIndex,
569
+ rowIndex,
570
+ });
571
+ }
572
+
573
+ /*
574
+ * Rewrite one complete direct cell token while preserving its exact qualifier
575
+ * spelling and surrounding whitespace.
576
+ */
577
+ function getSheetAxisMovedFormulaCellTokenText(
578
+ token: Extract<SheetFormulaReferenceToken, { kind: 'SHEET_CELL' }>,
579
+ axis: SheetAxisMoveType,
580
+ indexes: SheetAxisMoveIndexes,
581
+ ) {
582
+ return (
583
+ token.text.slice(0, token.coordinateStartIndex - token.startIndex) +
584
+ getSheetAxisMovedFormulaCellText(token, axis, indexes) +
585
+ token.text.slice(token.coordinateEndIndex - token.startIndex)
586
+ );
587
+ }
588
+
589
+ /*
590
+ * Rewrite one complete range token, or return null when the axis permutation
591
+ * would split its rectangular interval into a non-contiguous union.
592
+ */
593
+ function getSheetAxisMovedFormulaRangeTokenText(
594
+ token: Extract<SheetFormulaReferenceToken, { kind: 'SHEET_RANGE' }>,
595
+ axis: SheetAxisMoveType,
596
+ indexes: SheetAxisMoveIndexes,
597
+ ) {
598
+ const startAxisIndex =
599
+ axis === 'COLUMN'
600
+ ? token.startReference.columnIndex
601
+ : token.startReference.rowIndex;
602
+ const endAxisIndex =
603
+ axis === 'COLUMN'
604
+ ? token.endReference.columnIndex
605
+ : token.endReference.rowIndex;
606
+ const movedRange = getSheetMovedAxisRange(
607
+ Math.min(startAxisIndex, endAxisIndex),
608
+ Math.max(startAxisIndex, endAxisIndex),
609
+ indexes,
610
+ );
611
+ if (!movedRange.contiguous) {
612
+ return null;
613
+ }
614
+ if (
615
+ movedRange.startIndex === Math.min(startAxisIndex, endAxisIndex) &&
616
+ movedRange.endIndex === Math.max(startAxisIndex, endAxisIndex)
617
+ ) {
618
+ return token.text;
619
+ }
620
+
621
+ const movedStartReference = {
622
+ ...token.startReference,
623
+ ...(axis === 'COLUMN'
624
+ ? {
625
+ columnIndex:
626
+ startAxisIndex <= endAxisIndex
627
+ ? movedRange.startIndex
628
+ : movedRange.endIndex,
629
+ }
630
+ : {
631
+ rowIndex:
632
+ startAxisIndex <= endAxisIndex
633
+ ? movedRange.startIndex
634
+ : movedRange.endIndex,
635
+ }),
636
+ };
637
+ const movedEndReference = {
638
+ ...token.endReference,
639
+ ...(axis === 'COLUMN'
640
+ ? {
641
+ columnIndex:
642
+ startAxisIndex <= endAxisIndex
643
+ ? movedRange.endIndex
644
+ : movedRange.startIndex,
645
+ }
646
+ : {
647
+ rowIndex:
648
+ startAxisIndex <= endAxisIndex
649
+ ? movedRange.endIndex
650
+ : movedRange.startIndex,
651
+ }),
652
+ };
653
+
654
+ return (
655
+ token.text.slice(
656
+ 0,
657
+ token.startReferenceCoordinateStartIndex - token.startIndex,
658
+ ) +
659
+ getSheetFormulaCellReferenceText(movedStartReference) +
660
+ token.text.slice(
661
+ token.startReferenceCoordinateEndIndex - token.startIndex,
662
+ token.endReferenceCoordinateStartIndex - token.startIndex,
663
+ ) +
664
+ getSheetFormulaCellReferenceText(movedEndReference) +
665
+ token.text.slice(token.endReferenceCoordinateEndIndex - token.startIndex)
666
+ );
667
+ }
668
+
669
+ /*
670
+ * Rewrite every moved-tab reference in one expression. DataTable reference
671
+ * expressions recurse through the same transformer instead of maintaining a
672
+ * second formula-coordinate implementation.
673
+ */
674
+ function rewriteSheetAxisMoveFormulaExpression(
675
+ value: string,
676
+ params: {
677
+ allowQualifiedReferences?: boolean;
678
+ axis: SheetAxisMoveType;
679
+ formulaSheetTabId: string;
680
+ indexes: SheetAxisMoveIndexes;
681
+ movedSheetTabId: string;
682
+ sheetTabIdByNameKey: ReadonlyMap<string, string>;
683
+ },
684
+ ): SheetAxisMoveFormulaResult {
685
+ const tokens = tokenizeSheetFormulaReferences(value);
686
+ if (!tokens.length) {
687
+ return { changed: false, status: 'UNCHANGED', text: value };
688
+ }
689
+
690
+ let changed = false;
691
+ let cursor = 0;
692
+ let text = '';
693
+
694
+ for (const token of tokens) {
695
+ text += value.slice(cursor, token.startIndex);
696
+ let tokenText = token.text;
697
+
698
+ if (
699
+ token.kind === 'SHEET_CELL' &&
700
+ sheetAxisMoveFormulaReferenceTargetsMovedTab(token.sheetName, params)
701
+ ) {
702
+ tokenText = getSheetAxisMovedFormulaCellTokenText(
703
+ token,
704
+ params.axis,
705
+ params.indexes,
706
+ );
707
+ if (
708
+ tokenText !== token.text &&
709
+ token.sheetName &&
710
+ params.allowQualifiedReferences === false
711
+ ) {
712
+ return {
713
+ changed: false,
714
+ reason: 'QUALIFIED_REFERENCE',
715
+ status: 'UNSAFE',
716
+ text: value,
717
+ };
718
+ }
719
+ } else if (
720
+ token.kind === 'SHEET_RANGE' &&
721
+ sheetAxisMoveFormulaReferenceTargetsMovedTab(token.sheetName, params)
722
+ ) {
723
+ const movedText = getSheetAxisMovedFormulaRangeTokenText(
724
+ token,
725
+ params.axis,
726
+ params.indexes,
727
+ );
728
+ if (movedText === null) {
729
+ return {
730
+ changed: false,
731
+ reason: 'DISCONTIGUOUS_RANGE',
732
+ status: 'UNSAFE',
733
+ text: value,
734
+ };
735
+ }
736
+ if (
737
+ movedText !== token.text &&
738
+ token.sheetName &&
739
+ params.allowQualifiedReferences === false
740
+ ) {
741
+ return {
742
+ changed: false,
743
+ reason: 'QUALIFIED_REFERENCE',
744
+ status: 'UNSAFE',
745
+ text: value,
746
+ };
747
+ }
748
+ tokenText = movedText;
749
+ } else if (token.kind === 'DATA_TABLE_CELL') {
750
+ const rowIdentifierText = token.rowIdentifierExpression.trim();
751
+ const nested = rewriteSheetAxisMoveFormulaExpression(
752
+ rowIdentifierText,
753
+ params,
754
+ );
755
+ if (nested.status === 'UNSAFE') {
756
+ return nested;
757
+ }
758
+ const call = parseSheetFormulaCall(token.text);
759
+ if (nested.changed && call?.args.length === 2) {
760
+ tokenText = `${call.name}(${nested.text}, ${call.args[1].trim()})`;
761
+ }
762
+ } else if (token.kind === 'DATA_TABLE_QUERY_CELL') {
763
+ let queryChanged = false;
764
+ const conditions: string[] = [];
765
+ for (const condition of token.conditions) {
766
+ if (condition.valueNode.kind !== 'CELL_REFERENCE') {
767
+ conditions.push(condition.text);
768
+ continue;
769
+ }
770
+ const nested = rewriteSheetAxisMoveFormulaExpression(
771
+ condition.valueExpression,
772
+ params,
773
+ );
774
+ if (nested.status === 'UNSAFE') {
775
+ return nested;
776
+ }
777
+ queryChanged = queryChanged || nested.changed;
778
+ conditions.push(
779
+ condition.text.slice(
780
+ 0,
781
+ condition.text.length - condition.valueExpression.length,
782
+ ) + nested.text,
783
+ );
784
+ }
785
+ const whereIndex = token.text.search(/\bWHERE\b/i);
786
+ if (queryChanged && whereIndex >= 0) {
787
+ tokenText = `${token.text.slice(0, whereIndex).trimEnd()} WHERE ${conditions.join(' AND ')}`;
788
+ }
789
+ }
790
+
791
+ text += tokenText;
792
+ changed = changed || tokenText !== token.text;
793
+ cursor = token.endIndex;
794
+ }
795
+
796
+ text += value.slice(cursor);
797
+ return changed
798
+ ? { changed: true, status: 'CHANGED', text }
799
+ : { changed: false, status: 'UNCHANGED', text: value };
800
+ }
801
+
802
+ /*
803
+ * Rewrite a complete formula through one row/column permutation. Invalid
804
+ * formulas and ranges that would split fail closed before persistence.
805
+ */
806
+ export function rewriteSheetFormulaForAxisMove(
807
+ value: string,
808
+ params: {
809
+ allowQualifiedReferences?: boolean;
810
+ axis: SheetAxisMoveType;
811
+ formulaSheetTabId: string;
812
+ indexes: SheetAxisMoveIndexes;
813
+ movedSheetTabId: string;
814
+ sheetTabIdByNameKey: ReadonlyMap<string, string>;
815
+ },
816
+ ): SheetAxisMoveFormulaResult {
817
+ if (!isSheetFormulaText(value)) {
818
+ return { changed: false, status: 'UNCHANGED', text: value };
819
+ }
820
+ if (!parseSheetFormulaExpression(getSheetFormulaExpression(value))) {
821
+ return {
822
+ changed: false,
823
+ reason: 'INVALID_FORMULA',
824
+ status: 'UNSAFE',
825
+ text: value,
826
+ };
827
+ }
828
+
829
+ return rewriteSheetAxisMoveFormulaExpression(value, params);
830
+ }