@jsb188/mday 1.1.10 → 1.1.12

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 (210) hide show
  1. package/package.json +140 -54
  2. package/src/constants/agentMemory.ts +123 -0
  3. package/src/constants/agentNote.ts +212 -0
  4. package/src/constants/aiChat.ts +10 -0
  5. package/src/constants/billing.ts +126 -0
  6. package/src/constants/campaign.ts +236 -0
  7. package/src/constants/contact.ts +72 -0
  8. package/src/constants/csvExport.ts +133 -0
  9. package/src/constants/dataTable.ts +21 -21
  10. package/src/constants/dataTableFieldTypes.generated.ts +59 -0
  11. package/src/constants/docAccess.ts +33 -0
  12. package/src/constants/dt.ts +234 -0
  13. package/src/constants/email.ts +125 -7
  14. package/src/constants/fieldDoc.ts +104 -0
  15. package/src/constants/graphqlReactivity.ts +196 -0
  16. package/src/constants/integration.ts +8 -9
  17. package/src/constants/log.ts +14 -408
  18. package/src/constants/logDisplay/README.md +88 -0
  19. package/src/constants/logDisplay/defaults.ts +39 -0
  20. package/src/constants/logDisplay/index.ts +27 -0
  21. package/src/constants/logDisplay/operations/arable.ts +16 -0
  22. package/src/constants/logDisplay/operations/farmersMarket.ts +10 -0
  23. package/src/constants/logDisplay/operations/foodManufacturing.ts +13 -0
  24. package/src/constants/logDisplay/shared/accounting.ts +13 -0
  25. package/src/constants/logDisplay/shared/interactions.ts +13 -0
  26. package/src/constants/logDisplay/shared/rackTemperatures.ts +42 -0
  27. package/src/constants/logTaxonomy.generated.ts +2207 -0
  28. package/src/constants/noteDoc.ts +77 -0
  29. package/src/constants/organization.ts +18 -21
  30. package/src/constants/pageRoute.ts +23 -0
  31. package/src/constants/qrCode.ts +30 -0
  32. package/src/constants/sheet.ts +126 -14
  33. package/src/constants/sms.ts +18 -0
  34. package/src/constants/square.ts +6 -6
  35. package/src/constants/storage.ts +80 -7
  36. package/src/constants/usage.ts +82 -0
  37. package/src/constants/workflowSteps.ts +60 -0
  38. package/src/schemas/editLogEntry.ts +836 -780
  39. package/src/schemas/inboundContact.ts +60 -55
  40. package/src/schemas/logsQuery.ts +55 -49
  41. package/src/types/account.d.ts +4 -1
  42. package/src/types/agentMemory.d.ts +98 -0
  43. package/src/types/agentNote.d.ts +443 -0
  44. package/src/types/billing.d.ts +537 -0
  45. package/src/types/campaign.d.ts +1533 -0
  46. package/src/types/contact.d.ts +15 -0
  47. package/src/types/csvExport.d.ts +213 -0
  48. package/src/types/dataAnalysis.d.ts +44 -0
  49. package/src/types/dataTable.d.ts +115 -403
  50. package/src/types/definitionElement.d.ts +35 -0
  51. package/src/types/docAccess.d.ts +96 -0
  52. package/src/types/dt.d.ts +257 -0
  53. package/src/types/email.d.ts +184 -24
  54. package/src/types/emailReplyReport.d.ts +76 -0
  55. package/src/types/fieldDoc.d.ts +702 -0
  56. package/src/types/fieldDocContract.d.ts +90 -0
  57. package/src/types/inboundContact.d.ts +10 -1
  58. package/src/types/log.d.ts +254 -295
  59. package/src/types/logDisplay.types.ts +85 -0
  60. package/src/types/organization.d.ts +91 -43
  61. package/src/types/pageRoute.d.ts +48 -0
  62. package/src/types/qrCode.d.ts +96 -0
  63. package/src/types/richText.d.ts +81 -0
  64. package/src/types/sheet.d.ts +678 -129
  65. package/src/types/sms.d.ts +38 -0
  66. package/src/types/storage.d.ts +449 -129
  67. package/src/types/usage.d.ts +106 -0
  68. package/src/types/workflow.d.ts +315 -54
  69. package/src/utils/agentMemory.ts +15 -0
  70. package/src/utils/agentNote.ts +850 -0
  71. package/src/utils/aiChat.ts +43 -0
  72. package/src/utils/aiChatWork.ts +64 -0
  73. package/src/utils/billing.ts +37 -0
  74. package/src/utils/campaign.ts +1174 -0
  75. package/src/utils/campaignAI.ts +72 -0
  76. package/src/utils/campaignAccess.ts +239 -0
  77. package/src/utils/campaignEmail.ts +601 -0
  78. package/src/utils/campaignEmailCompiler.ts +372 -0
  79. package/src/utils/campaignEmailImages.ts +60 -0
  80. package/src/utils/campaignEmailStyles.ts +544 -0
  81. package/src/utils/campaignLifecycle.ts +104 -0
  82. package/src/utils/campaignPreview.ts +285 -0
  83. package/src/utils/contact.ts +50 -0
  84. package/src/utils/csvExport.ts +19 -0
  85. package/src/utils/dataTable.ts +95 -618
  86. package/src/utils/dataTableFieldTypeRegistry.ts +106 -0
  87. package/src/utils/dataTableRelativeDate.ts +447 -0
  88. package/src/utils/definitionElement.ts +84 -0
  89. package/src/utils/docAccess.ts +383 -0
  90. package/src/utils/documentName.ts +53 -0
  91. package/src/utils/email.ts +317 -51
  92. package/src/utils/emailReplyReport.test.ts +187 -0
  93. package/src/utils/emailReplyReport.ts +143 -0
  94. package/src/utils/fieldDoc.ts +157 -0
  95. package/src/utils/fieldDocAnswers.ts +128 -0
  96. package/src/utils/fieldDocDefaults.ts +36 -0
  97. package/src/utils/fieldDocProgram.ts +36 -0
  98. package/src/utils/fieldDocSummary.ts +30 -0
  99. package/src/utils/log.ts +276 -192
  100. package/src/utils/logDisplay.helpers.ts +177 -0
  101. package/src/utils/logDisplay.ts +87 -0
  102. package/src/utils/logRegistryValue.ts +90 -0
  103. package/src/utils/logStoreRead.ts +262 -0
  104. package/src/utils/logTaxonomy.helpers.ts +17 -0
  105. package/src/utils/logTaxonomy.ts +76 -0
  106. package/src/utils/logTaxonomyDisplay.ts +395 -0
  107. package/src/utils/noteDoc.ts +609 -0
  108. package/src/utils/organization.ts +337 -84
  109. package/src/utils/pageRoute.ts +71 -0
  110. package/src/utils/qrCode.helpers.ts +189 -0
  111. package/src/utils/qrCode.test.ts +519 -0
  112. package/src/utils/qrCode.ts +273 -0
  113. package/src/utils/richTextHTML.ts +385 -0
  114. package/src/utils/sheet.ts +199 -107
  115. package/src/utils/sheetAxisMove.ts +616 -0
  116. package/src/utils/sheetBootstrap.ts +1402 -0
  117. package/src/utils/sheetCellLiteral.ts +101 -0
  118. package/src/utils/sheetCommand.ts +358 -0
  119. package/src/utils/sheetDesign.ts +705 -0
  120. package/src/utils/sheetExport.ts +186 -0
  121. package/src/utils/sheetFeatureReference.ts +183 -0
  122. package/src/utils/sheetFormulaArgs.ts +168 -0
  123. package/src/utils/sheetFormulaCompile.ts +475 -0
  124. package/src/utils/sheetFormulaConditionalAggregate.ts +254 -0
  125. package/src/utils/sheetFormulaCriteria.ts +656 -0
  126. package/src/utils/sheetFormulaDate.ts +444 -0
  127. package/src/utils/sheetFormulaEvaluation.ts +899 -0
  128. package/src/utils/sheetFormulaEvaluator.ts +997 -0
  129. package/src/utils/sheetFormulaFunctionHandlers.ts +403 -0
  130. package/src/utils/sheetFormulaLogical.ts +116 -0
  131. package/src/utils/sheetFormulaLookup.ts +488 -0
  132. package/src/utils/sheetFormulaMath.ts +48 -0
  133. package/src/utils/sheetFormulaParser.ts +729 -0
  134. package/src/utils/sheetFormulaRange.ts +330 -0
  135. package/src/utils/sheetFormulaReference.ts +703 -0
  136. package/src/utils/sheetFormulaReferenceAggregates.ts +313 -0
  137. package/src/utils/sheetFormulaReferenceDates.ts +237 -0
  138. package/src/utils/sheetFormulaReferenceLogical.ts +330 -0
  139. package/src/utils/sheetFormulaReferenceLookup.ts +341 -0
  140. package/src/utils/sheetFormulaReferenceMath.ts +95 -0
  141. package/src/utils/sheetFormulaReferenceRewrite.ts +220 -0
  142. package/src/utils/sheetFormulaReferenceText.ts +462 -0
  143. package/src/utils/sheetFormulaSyntax.ts +1027 -0
  144. package/src/utils/sheetFormulaText.ts +430 -0
  145. package/src/utils/sheetJournal.ts +200 -0
  146. package/src/utils/sheetNumberFormat.ts +1012 -0
  147. package/src/utils/sheetPreview.ts +68 -0
  148. package/src/utils/sheetSequence.ts +118 -0
  149. package/src/utils/sheetSnapshot.ts +739 -0
  150. package/src/utils/sheetStructureEdit.ts +914 -0
  151. package/src/utils/sheetStructureReferences.ts +389 -0
  152. package/src/utils/square.ts +22 -12
  153. package/src/utils/storage.ts +198 -0
  154. package/src/utils/table-route.ts +127 -11
  155. package/src/utils/time.ts +12 -0
  156. package/src/utils/workflow.ts +334 -40
  157. package/src/utils/workflowLogFingerprint.ts +302 -0
  158. package/src/utils/workflowLogRevisionSnapshot.ts +424 -0
  159. package/src/utils/workflowProgress.ts +280 -0
  160. package/src/utils/workflowTriggers.ts +61 -0
  161. package/tests/agentNote.test.ts +395 -0
  162. package/tests/campaignAI.test.ts +119 -0
  163. package/tests/campaignAccess.test.ts +239 -0
  164. package/tests/campaignEmail.test.ts +441 -0
  165. package/tests/campaignLifecycle.test.ts +134 -0
  166. package/tests/campaignPublication.test.ts +309 -0
  167. package/tests/dataTableFieldTypeRegistry.test.ts +111 -0
  168. package/tests/dataTableRelativeDate.test.ts +412 -0
  169. package/tests/docAccess.test.ts +525 -0
  170. package/tests/editLogEntry.test.ts +112 -0
  171. package/tests/email.test.ts +39 -0
  172. package/tests/fieldDocProgram.test.ts +114 -0
  173. package/tests/logDisplay.fixtures.ts +67 -0
  174. package/tests/logDisplay.test.ts +227 -0
  175. package/tests/noteDoc.test.ts +514 -0
  176. package/tests/richTextHTML.test.ts +185 -0
  177. package/tests/sheetAxisMove.test.ts +352 -0
  178. package/tests/sheetBootstrap.test.ts +1967 -0
  179. package/tests/sheetFeatureReference.test.ts +22 -0
  180. package/tests/sheetFormulaCriteria.test.ts +269 -0
  181. package/tests/sheetFormulaEvaluator.test.ts +468 -0
  182. package/tests/sheetFormulaFunctions.test.ts +538 -0
  183. package/tests/sheetFormulaLanguage.test.ts +361 -0
  184. package/tests/sheetFormulaParity.fixtures.ts +872 -0
  185. package/tests/sheetFormulaParity.test.ts +188 -0
  186. package/tests/sheetFormulaRange.test.ts +167 -0
  187. package/tests/sheetFormulaReference.test.ts +78 -0
  188. package/tests/sheetFormulaReferenceRewrite.test.ts +117 -0
  189. package/tests/sheetFormulaTextDate.test.ts +512 -0
  190. package/tests/sheetSequence.test.ts +22 -0
  191. package/tests/sheetSnapshot.test.ts +134 -0
  192. package/tests/sheetStructureEdit.test.ts +537 -0
  193. package/tests/sheetStructureReferences.test.ts +589 -0
  194. package/tests/workflowLifecycleStatus.test.ts +60 -0
  195. package/tests/workflowLogFingerprint.test.ts +203 -0
  196. package/tests/workflowLogRevisionSnapshot.test.ts +96 -0
  197. package/tests/workflowProgress.test.ts +180 -0
  198. package/tests/workflowTriggers.test.ts +29 -0
  199. package/tsconfig.json +10 -10
  200. package/vitest.config.ts +8 -0
  201. package/src/constants/log-activityTexts.ts +0 -235
  202. package/src/constants/product.ts +0 -42
  203. package/src/constants/report.ts +0 -44
  204. package/src/schemas/organization.ts +0 -65
  205. package/src/schemas/productsQuery.ts +0 -53
  206. package/src/schemas/reportsQuery.ts +0 -20
  207. package/src/types/product.d.ts +0 -84
  208. package/src/types/report.d.ts +0 -290
  209. package/src/utils/product-calEvent.ts +0 -455
  210. package/src/utils/report.ts +0 -158
@@ -0,0 +1,616 @@
1
+ import { SHEET_MAX_ROW_COUNT } from '../constants/sheet.ts';
2
+ import type { SheetAxisDesignObj, SheetDesignObj, SheetMergedRangeObj } from '../types/sheet.d.ts';
3
+ import { parseSheetFormulaExpression, tokenizeSheetFormulaReferences } from './sheetFormulaParser.ts';
4
+ import {
5
+ getSheetFormulaCellReferenceText,
6
+ getSheetFormulaColumnReferenceText,
7
+ getSheetFormulaExpression,
8
+ getSheetFormulaSheetTabNameLookupKey,
9
+ isSheetFormulaText,
10
+ type SheetFormulaCellReference,
11
+ type SheetFormulaReferenceToken,
12
+ } from './sheetFormulaSyntax.ts';
13
+
14
+ export type SheetAxisMoveType = 'COLUMN' | 'ROW';
15
+
16
+ export interface SheetAxisMoveIndexes {
17
+ sourceEndIndex: number;
18
+ sourceStartIndex: number;
19
+ targetEndIndex: number;
20
+ targetStartIndex: number;
21
+ }
22
+
23
+ export interface SheetAxisMoveInput {
24
+ axis: SheetAxisMoveType;
25
+ sourceEndIndex: number;
26
+ sourceStartIndex: number;
27
+ targetStartIndex: number;
28
+ }
29
+
30
+ /*
31
+ * Return the compact semantic move that restores a completed axis permutation.
32
+ * The moved source block occupies the original target interval after the
33
+ * forward move, so replay can invert the operation without materializing cells.
34
+ */
35
+ export function getInverseSheetAxisMoveInput(input: SheetAxisMoveInput): SheetAxisMoveInput {
36
+ const width = input.sourceEndIndex - input.sourceStartIndex + 1;
37
+
38
+ return {
39
+ axis: input.axis,
40
+ sourceEndIndex: input.targetStartIndex + width - 1,
41
+ sourceStartIndex: input.targetStartIndex,
42
+ targetStartIndex: input.sourceStartIndex,
43
+ };
44
+ }
45
+
46
+ export type SheetAxisMoveFormulaResult =
47
+ | {
48
+ changed: false;
49
+ status: 'UNCHANGED';
50
+ text: string;
51
+ }
52
+ | {
53
+ changed: true;
54
+ status: 'CHANGED';
55
+ text: string;
56
+ }
57
+ | {
58
+ changed: false;
59
+ reason: 'DISCONTIGUOUS_RANGE' | 'INVALID_FORMULA' | 'QUALIFIED_REFERENCE';
60
+ status: 'UNSAFE';
61
+ text: string;
62
+ };
63
+
64
+ /*
65
+ * Normalize one client-authored axis permutation and reject out-of-bounds or
66
+ * semantic no-op requests before either client or server planning begins.
67
+ */
68
+ export function normalizeSheetAxisMoveInput(
69
+ input: SheetAxisMoveInput,
70
+ axisCount: number,
71
+ ): SheetAxisMoveIndexes & { axis: SheetAxisMoveType } {
72
+ const sourceStartIndex = Number(input.sourceStartIndex);
73
+ const sourceEndIndex = Number(input.sourceEndIndex);
74
+ const targetStartIndex = Number(input.targetStartIndex);
75
+ const normalizedAxisCount = Number(axisCount);
76
+ const width = sourceEndIndex - sourceStartIndex + 1;
77
+ const targetEndIndex = targetStartIndex + width - 1;
78
+
79
+ if (
80
+ (input.axis !== 'COLUMN' && input.axis !== 'ROW') ||
81
+ !Number.isSafeInteger(sourceStartIndex) ||
82
+ !Number.isSafeInteger(sourceEndIndex) ||
83
+ !Number.isSafeInteger(targetStartIndex) ||
84
+ !Number.isSafeInteger(targetEndIndex) ||
85
+ !Number.isSafeInteger(normalizedAxisCount) ||
86
+ sourceStartIndex < 1 ||
87
+ sourceEndIndex < sourceStartIndex ||
88
+ targetStartIndex < 1 ||
89
+ targetEndIndex > normalizedAxisCount ||
90
+ sourceEndIndex > normalizedAxisCount ||
91
+ targetStartIndex === sourceStartIndex
92
+ ) {
93
+ throw new Error('INVALID_SHEET_AXIS_MOVE');
94
+ }
95
+
96
+ return {
97
+ axis: input.axis,
98
+ sourceEndIndex,
99
+ sourceStartIndex,
100
+ targetEndIndex,
101
+ targetStartIndex,
102
+ };
103
+ }
104
+
105
+ /*
106
+ * Return the one-dimensional source and destination bounds for a complete row
107
+ * or column move.
108
+ */
109
+ export function getSheetAxisMoveIndexes(params: {
110
+ axis: SheetAxisMoveType;
111
+ sourceRange: SheetMergedRangeObj;
112
+ targetRange: SheetMergedRangeObj;
113
+ }): SheetAxisMoveIndexes {
114
+ const movingColumns = params.axis === 'COLUMN';
115
+
116
+ return {
117
+ sourceEndIndex: movingColumns ? params.sourceRange.endColumnIndex : params.sourceRange.endRowIndex,
118
+ sourceStartIndex: movingColumns ? params.sourceRange.startColumnIndex : params.sourceRange.startRowIndex,
119
+ targetEndIndex: movingColumns ? params.targetRange.endColumnIndex : params.targetRange.endRowIndex,
120
+ targetStartIndex: movingColumns ? params.targetRange.startColumnIndex : params.targetRange.startRowIndex,
121
+ };
122
+ }
123
+
124
+ /*
125
+ * Return the contiguous rectangle affected when a whole row or column block
126
+ * moves by insertion.
127
+ */
128
+ export function getSheetAxisMoveAffectedRange(
129
+ sourceRange: SheetMergedRangeObj,
130
+ targetRange: SheetMergedRangeObj,
131
+ axis: SheetAxisMoveType,
132
+ ): SheetMergedRangeObj {
133
+ if (axis === 'COLUMN') {
134
+ return {
135
+ endColumnIndex: Math.max(sourceRange.endColumnIndex, targetRange.endColumnIndex),
136
+ endRowIndex: sourceRange.endRowIndex,
137
+ startColumnIndex: Math.min(sourceRange.startColumnIndex, targetRange.startColumnIndex),
138
+ startRowIndex: sourceRange.startRowIndex,
139
+ };
140
+ }
141
+
142
+ return {
143
+ endColumnIndex: sourceRange.endColumnIndex,
144
+ endRowIndex: Math.max(sourceRange.endRowIndex, targetRange.endRowIndex),
145
+ startColumnIndex: sourceRange.startColumnIndex,
146
+ startRowIndex: Math.min(sourceRange.startRowIndex, targetRange.startRowIndex),
147
+ };
148
+ }
149
+
150
+ /*
151
+ * Resolve the original axis index that should occupy one destination index
152
+ * after inserting the selected block at its target.
153
+ */
154
+ export function getSheetAxisMoveSourceIndex(params: {
155
+ destinationIndex: number;
156
+ sourceEndIndex: number;
157
+ sourceStartIndex: number;
158
+ targetEndIndex: number;
159
+ targetStartIndex: number;
160
+ }) {
161
+ if (params.targetStartIndex > params.sourceStartIndex) {
162
+ if (params.destinationIndex >= params.targetStartIndex) {
163
+ return params.sourceStartIndex + params.destinationIndex - params.targetStartIndex;
164
+ }
165
+
166
+ return params.sourceEndIndex + 1 + params.destinationIndex - params.sourceStartIndex;
167
+ }
168
+
169
+ if (params.destinationIndex <= params.targetEndIndex) {
170
+ return params.sourceStartIndex + params.destinationIndex - params.targetStartIndex;
171
+ }
172
+
173
+ return params.targetStartIndex + params.destinationIndex - params.targetEndIndex - 1;
174
+ }
175
+
176
+ /*
177
+ * Resolve the destination occupied by one original axis index after inserting
178
+ * the selected block at its target.
179
+ */
180
+ export function getSheetAxisMoveDestinationIndex(sourceIndex: number, params: SheetAxisMoveIndexes) {
181
+ const width = params.sourceEndIndex - params.sourceStartIndex + 1;
182
+
183
+ if (params.targetStartIndex > params.sourceStartIndex) {
184
+ if (sourceIndex >= params.sourceStartIndex && sourceIndex <= params.sourceEndIndex) {
185
+ return sourceIndex + params.targetStartIndex - params.sourceStartIndex;
186
+ }
187
+ if (sourceIndex > params.sourceEndIndex && sourceIndex <= params.targetEndIndex) {
188
+ return sourceIndex - width;
189
+ }
190
+ return sourceIndex;
191
+ }
192
+
193
+ if (sourceIndex >= params.sourceStartIndex && sourceIndex <= params.sourceEndIndex) {
194
+ return sourceIndex + params.targetStartIndex - params.sourceStartIndex;
195
+ }
196
+ if (sourceIndex >= params.targetStartIndex && sourceIndex < params.sourceStartIndex) {
197
+ return sourceIndex + width;
198
+ }
199
+ return sourceIndex;
200
+ }
201
+
202
+ /*
203
+ * Return one grid coordinate after the requested row or column permutation.
204
+ */
205
+ export function getSheetCoordinateAfterAxisMove(
206
+ rowIndex: number,
207
+ columnIndex: number,
208
+ axis: SheetAxisMoveType,
209
+ indexes: SheetAxisMoveIndexes,
210
+ ) {
211
+ return {
212
+ columnIndex: axis === 'COLUMN' ? getSheetAxisMoveDestinationIndex(columnIndex, indexes) : columnIndex,
213
+ rowIndex: axis === 'ROW' ? getSheetAxisMoveDestinationIndex(rowIndex, indexes) : rowIndex,
214
+ };
215
+ }
216
+
217
+ /*
218
+ * Return the bounding axis interval and whether it remains contiguous after
219
+ * every original index follows the same permutation.
220
+ */
221
+ export function getSheetMovedAxisRange(startIndex: number, endIndex: number, indexes: SheetAxisMoveIndexes) {
222
+ const candidates = new Set([startIndex, endIndex]);
223
+ [
224
+ indexes.sourceStartIndex - 1,
225
+ indexes.sourceStartIndex,
226
+ indexes.sourceEndIndex,
227
+ indexes.sourceEndIndex + 1,
228
+ indexes.targetStartIndex - 1,
229
+ indexes.targetStartIndex,
230
+ indexes.targetEndIndex,
231
+ indexes.targetEndIndex + 1,
232
+ ].forEach((index) => {
233
+ if (index >= startIndex && index <= endIndex) {
234
+ candidates.add(index);
235
+ }
236
+ });
237
+ const destinations = [...candidates].map((index) => getSheetAxisMoveDestinationIndex(index, indexes));
238
+ const movedStartIndex = Math.min(...destinations);
239
+ const movedEndIndex = Math.max(...destinations);
240
+
241
+ return {
242
+ contiguous: movedEndIndex - movedStartIndex === endIndex - startIndex,
243
+ endIndex: movedEndIndex,
244
+ startIndex: movedStartIndex,
245
+ };
246
+ }
247
+
248
+ /*
249
+ * Project one coordinate rectangle through an axis permutation. A null result
250
+ * means the move would split the rectangle into a union that Sheet ranges
251
+ * cannot currently represent.
252
+ */
253
+ export function getSheetCoordinateRangeAfterAxisMove<TRange extends SheetMergedRangeObj>(
254
+ range: TRange,
255
+ axis: SheetAxisMoveType,
256
+ indexes: SheetAxisMoveIndexes,
257
+ ): TRange | null {
258
+ const movingColumns = axis === 'COLUMN';
259
+ const movedAxis = getSheetMovedAxisRange(
260
+ movingColumns ? range.startColumnIndex : range.startRowIndex,
261
+ movingColumns ? range.endColumnIndex : range.endRowIndex,
262
+ indexes,
263
+ );
264
+ if (!movedAxis.contiguous) {
265
+ return null;
266
+ }
267
+
268
+ return {
269
+ ...range,
270
+ ...(movingColumns
271
+ ? {
272
+ endColumnIndex: movedAxis.endIndex,
273
+ startColumnIndex: movedAxis.startIndex,
274
+ }
275
+ : {
276
+ endRowIndex: movedAxis.endIndex,
277
+ startRowIndex: movedAxis.startIndex,
278
+ }),
279
+ };
280
+ }
281
+
282
+ /*
283
+ * Project every coordinate rectangle through an axis permutation, failing
284
+ * closed when any rectangle would become a discontiguous union.
285
+ */
286
+ export function getSheetCoordinateRangesAfterAxisMove<TRange extends SheetMergedRangeObj>(
287
+ ranges: readonly TRange[],
288
+ axis: SheetAxisMoveType,
289
+ indexes: SheetAxisMoveIndexes,
290
+ ): TRange[] | null {
291
+ const movedRanges: TRange[] = [];
292
+ for (const range of ranges) {
293
+ const movedRange = getSheetCoordinateRangeAfterAxisMove(range, axis, indexes);
294
+ if (!movedRange) {
295
+ return null;
296
+ }
297
+ movedRanges.push(movedRange);
298
+ }
299
+ return movedRanges;
300
+ }
301
+
302
+ /*
303
+ * Return whether one coordinate-backed formatting or named range would need
304
+ * to move or split under an axis permutation.
305
+ */
306
+ export function sheetCoordinateRangeNeedsAxisMoveRemap(params: {
307
+ axis: SheetAxisMoveType;
308
+ range: SheetMergedRangeObj;
309
+ sourceRange: SheetMergedRangeObj;
310
+ targetRange: SheetMergedRangeObj;
311
+ }) {
312
+ const movingColumns = params.axis === 'COLUMN';
313
+ const startIndex = movingColumns ? params.range.startColumnIndex : params.range.startRowIndex;
314
+ const endIndex = movingColumns ? params.range.endColumnIndex : params.range.endRowIndex;
315
+ const movedRange = getSheetMovedAxisRange(startIndex, endIndex, getSheetAxisMoveIndexes(params));
316
+
317
+ return !movedRange.contiguous || movedRange.startIndex !== startIndex || movedRange.endIndex !== endIndex;
318
+ }
319
+
320
+ /*
321
+ * Rotate sparse row or column design entries through the same insertion span
322
+ * as their cells.
323
+ */
324
+ export function getMovedSheetAxisDesign(params: {
325
+ axisDesign?: Record<string, SheetAxisDesignObj> | null;
326
+ sourceEndIndex: number;
327
+ sourceStartIndex: number;
328
+ targetEndIndex: number;
329
+ targetStartIndex: number;
330
+ }) {
331
+ if (params.sourceStartIndex === params.targetStartIndex) {
332
+ return params.axisDesign || {};
333
+ }
334
+
335
+ const affectedStartIndex = Math.min(params.sourceStartIndex, params.targetStartIndex);
336
+ const affectedEndIndex = Math.max(params.sourceEndIndex, params.targetEndIndex);
337
+ const next = { ...(params.axisDesign || {}) };
338
+ const affectedEntries = Object.entries(params.axisDesign || {}).filter(([sourceKey]) => {
339
+ const sourceIndex = Number(sourceKey);
340
+ return (
341
+ Number.isSafeInteger(sourceIndex) &&
342
+ sourceIndex >= affectedStartIndex &&
343
+ sourceIndex <= affectedEndIndex &&
344
+ String(sourceIndex) === sourceKey
345
+ );
346
+ });
347
+
348
+ affectedEntries.forEach(([sourceKey]) => {
349
+ delete next[sourceKey];
350
+ });
351
+ affectedEntries.forEach(([sourceKey, sourceDesign]) => {
352
+ const destinationIndex = getSheetAxisMoveDestinationIndex(Number(sourceKey), {
353
+ sourceEndIndex: params.sourceEndIndex,
354
+ sourceStartIndex: params.sourceStartIndex,
355
+ targetEndIndex: params.targetEndIndex,
356
+ targetStartIndex: params.targetStartIndex,
357
+ });
358
+ if (sourceDesign) {
359
+ next[String(destinationIndex)] = sourceDesign;
360
+ }
361
+ });
362
+
363
+ return next;
364
+ }
365
+
366
+ /*
367
+ * Project sparse axis design, merged ranges, and named ranges through one
368
+ * normalized row or column permutation. A null result fails closed when a
369
+ * stored rectangle cannot remain contiguous.
370
+ */
371
+ export function getSheetDesignAfterAxisMove(design: SheetDesignObj, input: SheetAxisMoveInput): SheetDesignObj | null {
372
+ const axisCount = input.axis === 'COLUMN' ? Number(design.grid.columnCount) : Number(design.grid.rowCount);
373
+ const indexes = normalizeSheetAxisMoveInput(input, axisCount);
374
+ const designMergedRanges = design.metadata?.merges;
375
+ const hasMergedRanges = Array.isArray(designMergedRanges);
376
+ const mergedRanges = hasMergedRanges ? (designMergedRanges as SheetMergedRangeObj[]) : [];
377
+ const hasNamedRanges = Array.isArray(design.namedRanges);
378
+ const movedMergedRanges = getSheetCoordinateRangesAfterAxisMove(mergedRanges, input.axis, indexes);
379
+ const movedNamedRanges = getSheetCoordinateRangesAfterAxisMove(design.namedRanges || [], input.axis, indexes);
380
+ if (!movedMergedRanges || !movedNamedRanges) {
381
+ return null;
382
+ }
383
+
384
+ return {
385
+ ...design,
386
+ ...(input.axis === 'COLUMN'
387
+ ? {
388
+ columns: getMovedSheetAxisDesign({
389
+ axisDesign: design.columns,
390
+ ...indexes,
391
+ }),
392
+ }
393
+ : {
394
+ rows: getMovedSheetAxisDesign({
395
+ axisDesign: design.rows,
396
+ ...indexes,
397
+ }),
398
+ }),
399
+ ...(hasMergedRanges
400
+ ? {
401
+ metadata: {
402
+ ...(design.metadata || {}),
403
+ merges: movedMergedRanges,
404
+ },
405
+ }
406
+ : {}),
407
+ ...(hasNamedRanges ? { namedRanges: movedNamedRanges } : {}),
408
+ };
409
+ }
410
+
411
+ /*
412
+ * Return whether one parsed formula reference resolves to the tab whose axis
413
+ * is being moved. Unqualified references resolve to the formula's own tab.
414
+ */
415
+ function sheetAxisMoveFormulaReferenceTargetsMovedTab(
416
+ sheetName: string | null | undefined,
417
+ params: {
418
+ formulaSheetTabId: string;
419
+ movedSheetTabId: string;
420
+ sheetTabIdByNameKey: ReadonlyMap<string, string>;
421
+ },
422
+ ) {
423
+ if (!sheetName) {
424
+ return params.formulaSheetTabId === params.movedSheetTabId;
425
+ }
426
+
427
+ return params.sheetTabIdByNameKey.get(getSheetFormulaSheetTabNameLookupKey(sheetName)) === params.movedSheetTabId;
428
+ }
429
+
430
+ /*
431
+ * Return one cell coordinate rewritten through an axis permutation.
432
+ */
433
+ function getSheetAxisMovedFormulaCellText(
434
+ reference: SheetFormulaCellReference,
435
+ axis: SheetAxisMoveType,
436
+ indexes: SheetAxisMoveIndexes,
437
+ ) {
438
+ const columnIndex = axis === 'COLUMN' ? getSheetAxisMoveDestinationIndex(reference.columnIndex, indexes) : reference.columnIndex;
439
+ const rowIndex = axis === 'ROW' ? getSheetAxisMoveDestinationIndex(reference.rowIndex, indexes) : reference.rowIndex;
440
+
441
+ return getSheetFormulaCellReferenceText({
442
+ ...reference,
443
+ columnIndex,
444
+ rowIndex,
445
+ });
446
+ }
447
+
448
+ /*
449
+ * Rewrite one complete direct cell token while preserving its exact qualifier
450
+ * spelling and surrounding whitespace.
451
+ */
452
+ function getSheetAxisMovedFormulaCellTokenText(
453
+ token: Extract<SheetFormulaReferenceToken, { kind: 'SHEET_CELL' }>,
454
+ axis: SheetAxisMoveType,
455
+ indexes: SheetAxisMoveIndexes,
456
+ ) {
457
+ return (
458
+ token.text.slice(0, token.coordinateStartIndex - token.startIndex) +
459
+ getSheetAxisMovedFormulaCellText(token, axis, indexes) +
460
+ token.text.slice(token.coordinateEndIndex - token.startIndex)
461
+ );
462
+ }
463
+
464
+ /*
465
+ * Rewrite one complete range token, or return null when the axis permutation
466
+ * would split its rectangular interval into a non-contiguous union.
467
+ */
468
+ function getSheetAxisMovedFormulaRangeTokenText(
469
+ token: Extract<SheetFormulaReferenceToken, { kind: 'SHEET_RANGE' }>,
470
+ axis: SheetAxisMoveType,
471
+ indexes: SheetAxisMoveIndexes,
472
+ ) {
473
+ const startAxisIndex = axis === 'COLUMN' ? token.startReference.columnIndex : token.startReference.rowIndex;
474
+ const endAxisIndex = axis === 'COLUMN' ? token.endReference.columnIndex : token.endReference.rowIndex;
475
+ const movedRange = getSheetMovedAxisRange(Math.min(startAxisIndex, endAxisIndex), Math.max(startAxisIndex, endAxisIndex), indexes);
476
+ if (!movedRange.contiguous) {
477
+ return null;
478
+ }
479
+ if (
480
+ movedRange.startIndex === Math.min(startAxisIndex, endAxisIndex) &&
481
+ movedRange.endIndex === Math.max(startAxisIndex, endAxisIndex)
482
+ ) {
483
+ return token.text;
484
+ }
485
+
486
+ const movedStartReference = {
487
+ ...token.startReference,
488
+ ...(axis === 'COLUMN'
489
+ ? {
490
+ columnIndex: startAxisIndex <= endAxisIndex ? movedRange.startIndex : movedRange.endIndex,
491
+ }
492
+ : {
493
+ rowIndex: startAxisIndex <= endAxisIndex ? movedRange.startIndex : movedRange.endIndex,
494
+ }),
495
+ };
496
+ const movedEndReference = {
497
+ ...token.endReference,
498
+ ...(axis === 'COLUMN'
499
+ ? {
500
+ columnIndex: startAxisIndex <= endAxisIndex ? movedRange.endIndex : movedRange.startIndex,
501
+ }
502
+ : {
503
+ rowIndex: startAxisIndex <= endAxisIndex ? movedRange.endIndex : movedRange.startIndex,
504
+ }),
505
+ };
506
+
507
+ return (
508
+ token.text.slice(0, token.startReferenceCoordinateStartIndex - token.startIndex) +
509
+ getSheetFormulaCellReferenceText(movedStartReference) +
510
+ token.text.slice(
511
+ token.startReferenceCoordinateEndIndex - token.startIndex,
512
+ token.endReferenceCoordinateStartIndex - token.startIndex,
513
+ ) +
514
+ (token.endRowOpen && movedEndReference.rowIndex === SHEET_MAX_ROW_COUNT
515
+ ? getSheetFormulaColumnReferenceText(movedEndReference)
516
+ : getSheetFormulaCellReferenceText(movedEndReference)) +
517
+ token.text.slice(token.endReferenceCoordinateEndIndex - token.startIndex)
518
+ );
519
+ }
520
+
521
+ /*
522
+ * Rewrite references to the moved tab through one row or column permutation.
523
+ */
524
+ function rewriteSheetAxisMoveFormulaExpression(
525
+ value: string,
526
+ params: {
527
+ allowQualifiedReferences?: boolean;
528
+ axis: SheetAxisMoveType;
529
+ formulaSheetTabId: string;
530
+ indexes: SheetAxisMoveIndexes;
531
+ movedSheetTabId: string;
532
+ sheetTabIdByNameKey: ReadonlyMap<string, string>;
533
+ },
534
+ ): SheetAxisMoveFormulaResult {
535
+ const tokens = tokenizeSheetFormulaReferences(value);
536
+ if (!tokens.length) {
537
+ return { changed: false, status: 'UNCHANGED', text: value };
538
+ }
539
+
540
+ let changed = false;
541
+ let cursor = 0;
542
+ let text = '';
543
+
544
+ for (const token of tokens) {
545
+ text += value.slice(cursor, token.startIndex);
546
+ let tokenText = token.text;
547
+
548
+ if (token.kind === 'SHEET_CELL' && sheetAxisMoveFormulaReferenceTargetsMovedTab(token.sheetName, params)) {
549
+ tokenText = getSheetAxisMovedFormulaCellTokenText(token, params.axis, params.indexes);
550
+ if (tokenText !== token.text && token.sheetName && params.allowQualifiedReferences === false) {
551
+ return {
552
+ changed: false,
553
+ reason: 'QUALIFIED_REFERENCE',
554
+ status: 'UNSAFE',
555
+ text: value,
556
+ };
557
+ }
558
+ } else if (token.kind === 'SHEET_RANGE' && sheetAxisMoveFormulaReferenceTargetsMovedTab(token.sheetName, params)) {
559
+ const movedText = getSheetAxisMovedFormulaRangeTokenText(token, params.axis, params.indexes);
560
+ if (movedText === null) {
561
+ return {
562
+ changed: false,
563
+ reason: 'DISCONTIGUOUS_RANGE',
564
+ status: 'UNSAFE',
565
+ text: value,
566
+ };
567
+ }
568
+ if (movedText !== token.text && token.sheetName && params.allowQualifiedReferences === false) {
569
+ return {
570
+ changed: false,
571
+ reason: 'QUALIFIED_REFERENCE',
572
+ status: 'UNSAFE',
573
+ text: value,
574
+ };
575
+ }
576
+ tokenText = movedText;
577
+ }
578
+
579
+ text += tokenText;
580
+ changed = changed || tokenText !== token.text;
581
+ cursor = token.endIndex;
582
+ }
583
+
584
+ text += value.slice(cursor);
585
+ return changed ? { changed: true, status: 'CHANGED', text } : { changed: false, status: 'UNCHANGED', text: value };
586
+ }
587
+
588
+ /*
589
+ * Rewrite a complete formula through one row/column permutation. Invalid
590
+ * formulas and ranges that would split fail closed before persistence.
591
+ */
592
+ export function rewriteSheetFormulaForAxisMove(
593
+ value: string,
594
+ params: {
595
+ allowQualifiedReferences?: boolean;
596
+ axis: SheetAxisMoveType;
597
+ formulaSheetTabId: string;
598
+ indexes: SheetAxisMoveIndexes;
599
+ movedSheetTabId: string;
600
+ sheetTabIdByNameKey: ReadonlyMap<string, string>;
601
+ },
602
+ ): SheetAxisMoveFormulaResult {
603
+ if (!isSheetFormulaText(value)) {
604
+ return { changed: false, status: 'UNCHANGED', text: value };
605
+ }
606
+ if (!parseSheetFormulaExpression(getSheetFormulaExpression(value))) {
607
+ return {
608
+ changed: false,
609
+ reason: 'INVALID_FORMULA',
610
+ status: 'UNSAFE',
611
+ text: value,
612
+ };
613
+ }
614
+
615
+ return rewriteSheetAxisMoveFormulaExpression(value, params);
616
+ }