@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,705 @@
1
+ import {
2
+ SHEET_CELL_TEXT_ALIGN_ENUMS,
3
+ SHEET_CELL_TEXT_WRAP_ENUMS,
4
+ SHEET_CELL_VERTICAL_ALIGN_ENUMS,
5
+ SHEET_DEFAULT_COLUMN_COUNT,
6
+ SHEET_DEFAULT_ROW_COUNT,
7
+ SHEET_MAX_COLUMN_COUNT,
8
+ SHEET_MAX_ROW_COUNT,
9
+ SHEET_VIEWPORT_MAX_COLUMNS,
10
+ SHEET_VIEWPORT_MAX_ROWS,
11
+ } from '../constants/sheet.ts';
12
+ import type {
13
+ SheetAxisDesignObj,
14
+ SheetCellBorderStyleValue,
15
+ SheetCellFormatObj,
16
+ SheetCellNumberFormatObj,
17
+ SheetCellStyleObj,
18
+ SheetCellTextAlignEnum,
19
+ SheetCellTextWrapEnum,
20
+ SheetCellVerticalAlignEnum,
21
+ SheetDesignObj,
22
+ SheetGridViewportObj,
23
+ SheetMergedRangeObj,
24
+ SheetStructureOperationEnum,
25
+ } from '../types/sheet.d.ts';
26
+ import { isSheetCellNumberFormatType } from './sheetNumberFormat.ts';
27
+
28
+ const FILE_CONSTANTS = {
29
+ borderSides: ['Top', 'Right', 'Bottom', 'Left'] as const,
30
+ borderStyleValues: ['solid', 'dashed', 'dotted', 'double'] as const,
31
+ textStyleBooleanKeys: ['bold', 'italic', 'underline', 'strikethrough'] as const,
32
+ };
33
+
34
+ export const SHEET_CELL_STYLE_MAX_FONT_SIZE = 48;
35
+
36
+ /*
37
+ * Return a plain object from flexible saved Sheet style input.
38
+ */
39
+ function getSheetCellStyleObject(value?: unknown): Record<string, any> {
40
+ if (!value) {
41
+ return {};
42
+ }
43
+
44
+ if (typeof value === 'object' && !Array.isArray(value)) {
45
+ return value as Record<string, any>;
46
+ }
47
+
48
+ if (typeof value !== 'string') {
49
+ return {};
50
+ }
51
+
52
+ try {
53
+ const parsed = JSON.parse(value);
54
+
55
+ return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : {};
56
+ } catch {
57
+ return {};
58
+ }
59
+ }
60
+
61
+ /*
62
+ * Return a positive rounded integer value when a style number is usable.
63
+ */
64
+ function normalizeSheetCellStylePositiveInteger(value: unknown) {
65
+ const numberValue = Math.round(Number(value));
66
+
67
+ return Number.isFinite(numberValue) && numberValue > 0 ? numberValue : null;
68
+ }
69
+
70
+ /*
71
+ * Return a valid Sheet cell font size rounded to an integer and capped to the supported range.
72
+ */
73
+ export function normalizeSheetCellFontSize(value: unknown) {
74
+ const fontSize = normalizeSheetCellStylePositiveInteger(value);
75
+
76
+ return fontSize ? Math.min(SHEET_CELL_STYLE_MAX_FONT_SIZE, fontSize) : null;
77
+ }
78
+
79
+ /*
80
+ * Return a supported cell border width or null when the side should be disabled.
81
+ */
82
+ function normalizeSheetCellBorderWidth(value: unknown) {
83
+ const width = normalizeSheetCellStylePositiveInteger(value);
84
+
85
+ return width && width >= 1 && width <= 4 ? width : null;
86
+ }
87
+
88
+ /*
89
+ * Return a supported cell border style or null when the style should be dropped.
90
+ */
91
+ function normalizeSheetCellBorderStyle(value: unknown): SheetCellBorderStyleValue | null {
92
+ return FILE_CONSTANTS.borderStyleValues.includes(value as SheetCellBorderStyleValue) ? (value as SheetCellBorderStyleValue) : null;
93
+ }
94
+
95
+ /*
96
+ * Return a non-empty style color string or null when the color should be dropped.
97
+ */
98
+ function normalizeSheetCellStyleColor(value: unknown) {
99
+ return typeof value === 'string' && value.trim() ? value : null;
100
+ }
101
+
102
+ /*
103
+ * Return a boolean cell style setting only when the value is explicitly boolean.
104
+ */
105
+ function normalizeSheetCellStyleBoolean(value: unknown) {
106
+ return typeof value === 'boolean' ? value : null;
107
+ }
108
+
109
+ /*
110
+ * Return a supported text wrap style value or null when it should be dropped.
111
+ */
112
+ export function normalizeSheetCellTextWrap(value: unknown): SheetCellTextWrapEnum | null {
113
+ return SHEET_CELL_TEXT_WRAP_ENUMS.includes(value as SheetCellTextWrapEnum) ? (value as SheetCellTextWrapEnum) : null;
114
+ }
115
+
116
+ /*
117
+ * Return a supported horizontal text alignment value or null when it should be dropped.
118
+ */
119
+ export function normalizeSheetCellTextAlign(value: unknown): SheetCellTextAlignEnum | null {
120
+ return SHEET_CELL_TEXT_ALIGN_ENUMS.includes(value as SheetCellTextAlignEnum) ? (value as SheetCellTextAlignEnum) : null;
121
+ }
122
+
123
+ /*
124
+ * Return a supported vertical text alignment value or null when it should be dropped.
125
+ */
126
+ export function normalizeSheetCellVerticalAlign(value: unknown): SheetCellVerticalAlignEnum | null {
127
+ return SHEET_CELL_VERTICAL_ALIGN_ENUMS.includes(value as SheetCellVerticalAlignEnum) ? (value as SheetCellVerticalAlignEnum) : null;
128
+ }
129
+
130
+ /*
131
+ * Add normalized border fields for one side into a Sheet style result object.
132
+ */
133
+ function addNormalizedSheetCellBorderSideStyle(
134
+ result: SheetCellStyleObj,
135
+ source: Record<string, any>,
136
+ side: (typeof FILE_CONSTANTS.borderSides)[number],
137
+ ) {
138
+ const widthKey = `border${side}Width` as keyof SheetCellStyleObj;
139
+ const colorKey = `border${side}Color` as keyof SheetCellStyleObj;
140
+ const styleKey = `border${side}Style` as keyof SheetCellStyleObj;
141
+ const width = normalizeSheetCellBorderWidth(source[widthKey]);
142
+
143
+ if (!width) {
144
+ return;
145
+ }
146
+
147
+ const color = normalizeSheetCellStyleColor(source[colorKey]);
148
+ const style = normalizeSheetCellBorderStyle(source[styleKey]);
149
+ const mutableResult = result as Record<string, any>;
150
+
151
+ mutableResult[widthKey] = width;
152
+
153
+ if (color) {
154
+ mutableResult[colorKey] = color;
155
+ }
156
+
157
+ if (style) {
158
+ mutableResult[styleKey] = style;
159
+ }
160
+ }
161
+
162
+ /*
163
+ * Add normalized full-cell text style booleans into one Sheet style result object.
164
+ */
165
+ function addNormalizedSheetCellTextStyleBooleans(result: SheetCellStyleObj, source: Record<string, any>) {
166
+ FILE_CONSTANTS.textStyleBooleanKeys.forEach((key) => {
167
+ const value = normalizeSheetCellStyleBoolean(source[key]);
168
+
169
+ if (value !== null) {
170
+ result[key] = value;
171
+ }
172
+ });
173
+ }
174
+
175
+ /*
176
+ * Return the only style fields Sheets currently supports for cells and ranges.
177
+ */
178
+ export function normalizeSheetCellStyle(style?: Partial<SheetCellStyleObj> | Record<string, any> | string | null): SheetCellStyleObj {
179
+ const source = getSheetCellStyleObject(style);
180
+ const fontSize = normalizeSheetCellFontSize(source.fontSize);
181
+ const normalized: SheetCellStyleObj = {};
182
+
183
+ if (fontSize) {
184
+ normalized.fontSize = fontSize;
185
+ }
186
+
187
+ const textColor = normalizeSheetCellStyleColor(source.textColor);
188
+ if (textColor) {
189
+ normalized.textColor = textColor;
190
+ }
191
+
192
+ const fillColor = normalizeSheetCellStyleColor(source.fillColor);
193
+ if (fillColor) {
194
+ normalized.fillColor = fillColor;
195
+ }
196
+
197
+ const textWrap = normalizeSheetCellTextWrap(source.textWrap);
198
+ if (textWrap) {
199
+ normalized.textWrap = textWrap;
200
+ }
201
+
202
+ const textAlign = normalizeSheetCellTextAlign(source.textAlign);
203
+ if (textAlign) {
204
+ normalized.textAlign = textAlign;
205
+ }
206
+
207
+ const verticalAlign = normalizeSheetCellVerticalAlign(source.verticalAlign);
208
+ if (verticalAlign) {
209
+ normalized.verticalAlign = verticalAlign;
210
+ }
211
+
212
+ const disableMarkdown = normalizeSheetCellStyleBoolean(source.disableMarkdown);
213
+ if (disableMarkdown !== null) {
214
+ normalized.disableMarkdown = disableMarkdown;
215
+ }
216
+
217
+ addNormalizedSheetCellTextStyleBooleans(normalized, source);
218
+ FILE_CONSTANTS.borderSides.forEach((side) => {
219
+ addNormalizedSheetCellBorderSideStyle(normalized, source, side);
220
+ });
221
+
222
+ return normalized;
223
+ }
224
+
225
+ /*
226
+ * Return axis design values with supported cell style and format fields
227
+ * normalized.
228
+ */
229
+ function normalizeSheetAxisDesignMap(map?: Record<string, SheetAxisDesignObj> | null) {
230
+ return Object.fromEntries(
231
+ Object.entries(map || {}).map(([key, value]) => {
232
+ const { format: _format, style: _style, ...axisDesign } = value || {};
233
+ const style = normalizeSheetCellStyle(_style);
234
+ const format = normalizeSheetCellFormat(_format);
235
+ const next: SheetAxisDesignObj = { ...axisDesign };
236
+
237
+ if (Object.keys(style).length) {
238
+ next.style = style;
239
+ }
240
+
241
+ if (Object.keys(format).length) {
242
+ next.format = format;
243
+ }
244
+
245
+ return [key, next];
246
+ }),
247
+ );
248
+ }
249
+
250
+ /*
251
+ * Return a fresh default sheet design object.
252
+ */
253
+
254
+ export function getDefaultSheetDesign(): SheetDesignObj {
255
+ return {
256
+ version: 1,
257
+ grid: {
258
+ rowCount: SHEET_DEFAULT_ROW_COUNT,
259
+ columnCount: SHEET_DEFAULT_COLUMN_COUNT,
260
+ frozenRows: 0,
261
+ frozenColumns: 0,
262
+ },
263
+ columns: {},
264
+ rows: {},
265
+ defaultCellStyle: {},
266
+ defaultCellFormat: {},
267
+ namedRanges: [],
268
+ metadata: {},
269
+ };
270
+ }
271
+
272
+ /*
273
+ * Return the validated merged-cell ranges stored in one sheet design's
274
+ * metadata. Invalid or single-cell entries are dropped.
275
+ */
276
+ export function getSheetMergedRanges(metadata?: Record<string, any> | null): SheetMergedRangeObj[] {
277
+ const merges = Array.isArray(metadata?.merges) ? metadata.merges : [];
278
+
279
+ return merges.flatMap((merge: Partial<SheetMergedRangeObj>) => {
280
+ const startRowIndex = Math.floor(Number(merge?.startRowIndex || 0));
281
+ const startColumnIndex = Math.floor(Number(merge?.startColumnIndex || 0));
282
+ const endRowIndex = Math.floor(Number(merge?.endRowIndex || 0));
283
+ const endColumnIndex = Math.floor(Number(merge?.endColumnIndex || 0));
284
+
285
+ if (
286
+ startRowIndex < 1 ||
287
+ startColumnIndex < 1 ||
288
+ endRowIndex < startRowIndex ||
289
+ endColumnIndex < startColumnIndex ||
290
+ (endRowIndex === startRowIndex && endColumnIndex === startColumnIndex)
291
+ ) {
292
+ return [];
293
+ }
294
+
295
+ return [{ startRowIndex, startColumnIndex, endRowIndex, endColumnIndex }];
296
+ });
297
+ }
298
+
299
+ /*
300
+ * Return whether one cell coordinate falls inside one merged range.
301
+ */
302
+ export function isSheetCellInMergedRange(merge: SheetMergedRangeObj, rowIndex: number, columnIndex: number) {
303
+ return (
304
+ rowIndex >= merge.startRowIndex &&
305
+ rowIndex <= merge.endRowIndex &&
306
+ columnIndex >= merge.startColumnIndex &&
307
+ columnIndex <= merge.endColumnIndex
308
+ );
309
+ }
310
+
311
+ /*
312
+ * Return the merged range containing one cell coordinate, or null.
313
+ */
314
+ export function getSheetMergedRangeAtCell(merges: SheetMergedRangeObj[], rowIndex: number, columnIndex: number) {
315
+ return merges.find((merge) => isSheetCellInMergedRange(merge, rowIndex, columnIndex)) || null;
316
+ }
317
+
318
+ /*
319
+ * Return whether one cell is the anchor (top-left) of one merged range.
320
+ */
321
+ export function isSheetMergedRangeAnchor(merge: SheetMergedRangeObj, rowIndex: number, columnIndex: number) {
322
+ return merge.startRowIndex === rowIndex && merge.startColumnIndex === columnIndex;
323
+ }
324
+
325
+ /*
326
+ * Return whether two merged ranges intersect.
327
+ */
328
+ export function sheetMergedRangesIntersect(a: SheetMergedRangeObj, b: SheetMergedRangeObj) {
329
+ return (
330
+ a.startRowIndex <= b.endRowIndex &&
331
+ a.endRowIndex >= b.startRowIndex &&
332
+ a.startColumnIndex <= b.endColumnIndex &&
333
+ a.endColumnIndex >= b.startColumnIndex
334
+ );
335
+ }
336
+
337
+ /*
338
+ * Return merged ranges with one new range added; existing ranges it overlaps
339
+ * are absorbed (removed), matching the merge-over behavior of spreadsheets.
340
+ */
341
+ export function addSheetMergedRange(merges: SheetMergedRangeObj[], range: SheetMergedRangeObj) {
342
+ return [...merges.filter((merge) => !sheetMergedRangesIntersect(merge, range)), range];
343
+ }
344
+
345
+ /*
346
+ * Return merged ranges without any range intersecting the given bounds.
347
+ */
348
+ export function removeSheetMergedRangesIntersecting(merges: SheetMergedRangeObj[], bounds: SheetMergedRangeObj) {
349
+ return merges.filter((merge) => !sheetMergedRangesIntersect(merge, bounds));
350
+ }
351
+
352
+ /*
353
+ * Return one axis span shifted by a row/column insert or delete at the target
354
+ * index, or null when the span collapses entirely.
355
+ */
356
+ function shiftSheetMergedRangeAxis(start: number, end: number, targetIndex: number, delta: 1 | -1): [number, number] | null {
357
+ if (delta === 1) {
358
+ // Insert before targetIndex: spans at/after shift; spans containing the
359
+ // index grow
360
+ if (targetIndex <= start) {
361
+ return [start + 1, end + 1];
362
+ }
363
+ if (targetIndex <= end) {
364
+ return [start, end + 1];
365
+ }
366
+ return [start, end];
367
+ }
368
+
369
+ // Delete targetIndex: spans after shift back; spans containing it shrink
370
+ if (targetIndex < start) {
371
+ return [start - 1, end - 1];
372
+ }
373
+ if (targetIndex <= end) {
374
+ return start === end ? null : [start, end - 1];
375
+ }
376
+ return [start, end];
377
+ }
378
+
379
+ /*
380
+ * Return merged ranges shifted by one row/column structure edit. Ranges that
381
+ * collapse to a single cell or lose their span entirely are dropped.
382
+ */
383
+ export function shiftSheetMergedRangesForStructureEdit(
384
+ merges: SheetMergedRangeObj[],
385
+ operation: SheetStructureOperationEnum,
386
+ targetIndex: number,
387
+ ): SheetMergedRangeObj[] {
388
+ const rowDelta = operation === 'INSERT_ROW_ABOVE' ? 1 : operation === 'DELETE_ROW' ? -1 : 0;
389
+ const columnDelta = operation === 'INSERT_COLUMN_LEFT' ? 1 : operation === 'DELETE_COLUMN' ? -1 : 0;
390
+
391
+ if (!rowDelta && !columnDelta) {
392
+ return merges;
393
+ }
394
+
395
+ return merges.flatMap((merge) => {
396
+ const rowSpan = rowDelta
397
+ ? shiftSheetMergedRangeAxis(merge.startRowIndex, merge.endRowIndex, targetIndex, rowDelta as 1 | -1)
398
+ : ([merge.startRowIndex, merge.endRowIndex] as [number, number]);
399
+ const columnSpan = columnDelta
400
+ ? shiftSheetMergedRangeAxis(merge.startColumnIndex, merge.endColumnIndex, targetIndex, columnDelta as 1 | -1)
401
+ : ([merge.startColumnIndex, merge.endColumnIndex] as [number, number]);
402
+
403
+ if (!rowSpan || !columnSpan) {
404
+ return [];
405
+ }
406
+
407
+ const next = {
408
+ startRowIndex: rowSpan[0],
409
+ endRowIndex: rowSpan[1],
410
+ startColumnIndex: columnSpan[0],
411
+ endColumnIndex: columnSpan[1],
412
+ };
413
+
414
+ // A merge reduced to one cell is no longer a merge
415
+ if (next.startRowIndex === next.endRowIndex && next.startColumnIndex === next.endColumnIndex) {
416
+ return [];
417
+ }
418
+
419
+ return [next];
420
+ });
421
+ }
422
+
423
+ /*
424
+ * Return a sheet design object with all top-level defaults filled in.
425
+ */
426
+
427
+ export function normalizeSheetDesign(design?: Partial<SheetDesignObj> | null): SheetDesignObj {
428
+ const baseDesign = getDefaultSheetDesign();
429
+ const grid: Partial<SheetDesignObj['grid']> = design?.grid || {};
430
+
431
+ return {
432
+ ...baseDesign,
433
+ ...(design || {}),
434
+ version: Number(design?.version || baseDesign.version),
435
+ grid: {
436
+ rowCount: Math.min(SHEET_MAX_ROW_COUNT, Math.max(1, Math.floor(Number(grid.rowCount || baseDesign.grid.rowCount)))),
437
+ columnCount: Math.min(SHEET_MAX_COLUMN_COUNT, Math.max(1, Math.floor(Number(grid.columnCount || baseDesign.grid.columnCount)))),
438
+ frozenRows: Math.max(0, Math.floor(Number(grid.frozenRows || 0))),
439
+ frozenColumns: Math.max(0, Math.floor(Number(grid.frozenColumns || 0))),
440
+ },
441
+ columns: normalizeSheetAxisDesignMap(design?.columns),
442
+ rows: normalizeSheetAxisDesignMap(design?.rows),
443
+ defaultCellStyle: normalizeSheetCellStyle(design?.defaultCellStyle),
444
+ defaultCellFormat: normalizeSheetCellFormat(design?.defaultCellFormat),
445
+ namedRanges: Array.isArray(design?.namedRanges) ? design.namedRanges : [],
446
+ metadata: design?.metadata || {},
447
+ };
448
+ }
449
+
450
+ /*
451
+ * Return a viewport bounded to positive indexes and backend query limits.
452
+ */
453
+
454
+ export function normalizeSheetViewport(viewport: Partial<SheetGridViewportObj>): SheetGridViewportObj {
455
+ return {
456
+ startRowIndex: Math.max(1, Math.floor(Number(viewport.startRowIndex || 1))),
457
+ startColumnIndex: Math.max(1, Math.floor(Number(viewport.startColumnIndex || 1))),
458
+ rowCount: Math.min(SHEET_VIEWPORT_MAX_ROWS, Math.max(1, Math.floor(Number(viewport.rowCount || 1)))),
459
+ columnCount: Math.min(SHEET_VIEWPORT_MAX_COLUMNS, Math.max(1, Math.floor(Number(viewport.columnCount || 1)))),
460
+ };
461
+ }
462
+
463
+ /*
464
+ * Return the whole-grid sheetView viewport for one normalized sheet design.
465
+ * Every caller must build sheetView query variables through this function so
466
+ * the query cache key stays identical between mounted hooks and prefetches.
467
+ */
468
+
469
+ export function getSheetViewFullViewport(design: SheetDesignObj): SheetGridViewportObj {
470
+ return {
471
+ startRowIndex: 1,
472
+ startColumnIndex: 1,
473
+ rowCount: Math.min(SHEET_MAX_ROW_COUNT, Math.max(1, design.grid.rowCount || 1)),
474
+ columnCount: Math.min(SHEET_MAX_COLUMN_COUNT, Math.max(1, design.grid.columnCount || 1)),
475
+ };
476
+ }
477
+
478
+ /*
479
+ * Return a shallow merged JSON object, ignoring non-object values.
480
+ */
481
+
482
+ export function mergeSheetJSONObjects(...values: Array<Record<string, any> | null | undefined>) {
483
+ return values.reduce<Record<string, any>>((merged, value) => {
484
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
485
+ return merged;
486
+ }
487
+
488
+ return {
489
+ ...merged,
490
+ ...value,
491
+ };
492
+ }, {});
493
+ }
494
+
495
+ /*
496
+ * Return the string key used by sheet design JSON for one column index.
497
+ */
498
+
499
+ export function getSheetColumnDesignKey(columnIndex: number) {
500
+ return String(Math.max(1, Math.floor(Number(columnIndex) || 1)));
501
+ }
502
+
503
+ /*
504
+ * Return the string key used by sheet design JSON for one row index.
505
+ */
506
+
507
+ export function getSheetRowDesignKey(rowIndex: number) {
508
+ return String(Math.max(1, Math.floor(Number(rowIndex) || 1)));
509
+ }
510
+
511
+ /* Return the new row or column design position after a structure edit. */
512
+ function getSheetShiftedAxisDesignIndex(index: number, operation: SheetStructureOperationEnum, targetIndex: number) {
513
+ if (operation === 'INSERT_ROW_ABOVE' || operation === 'INSERT_COLUMN_LEFT') return index >= targetIndex ? index + 1 : index;
514
+ return index === targetIndex ? null : index > targetIndex ? index - 1 : index;
515
+ }
516
+
517
+ /*
518
+ * Shift saved row or column design entries for one sheet structure edit.
519
+ */
520
+
521
+ export function shiftSheetAxisDesignForStructureEdit(
522
+ axisDesign: Record<string, SheetAxisDesignObj> | null | undefined,
523
+ operation: SheetStructureOperationEnum,
524
+ targetIndex: number,
525
+ ) {
526
+ const nextDesign: Record<string, SheetAxisDesignObj> = {};
527
+ const normalizedTargetIndex = Math.max(1, Math.floor(Number(targetIndex) || 1));
528
+
529
+ Object.entries(axisDesign || {}).forEach(([key, design]) => {
530
+ const index = Math.floor(Number(key || 0));
531
+ if (!Number.isFinite(index) || index < 1) {
532
+ nextDesign[key] = design;
533
+ return;
534
+ }
535
+
536
+ const nextIndex = getSheetShiftedAxisDesignIndex(index, operation, normalizedTargetIndex);
537
+ if (!nextIndex) {
538
+ return;
539
+ }
540
+
541
+ nextDesign[String(nextIndex)] = design;
542
+ });
543
+
544
+ return nextDesign;
545
+ }
546
+
547
+ /*
548
+ * Return whether one row design object represents meaningful row-level content.
549
+ */
550
+
551
+ export function sheetAxisDesignHasContent(design?: SheetAxisDesignObj | null) {
552
+ if (!design) {
553
+ return false;
554
+ }
555
+
556
+ return Object.values(design).some((value) => {
557
+ if (value === null || value === undefined || value === false) {
558
+ return false;
559
+ }
560
+
561
+ if (typeof value === 'object') {
562
+ return Object.keys(value).length > 0;
563
+ }
564
+
565
+ return true;
566
+ });
567
+ }
568
+
569
+ /*
570
+ * Return one axis design map (columns or rows) with style deltas merged into a
571
+ * set of lines. Null delta values remove the key from the saved line style;
572
+ * lines whose merged style (and design) end up empty drop out of the map.
573
+ * Whole-column/row formatting persists through these line styles as a single
574
+ * design patch instead of one cell write per covered coordinate.
575
+ */
576
+ export function mergeSheetAxisDesignLineStyles(
577
+ axisDesign: Record<string, SheetAxisDesignObj> | null | undefined,
578
+ lineDeltas: Array<{ delta: Record<string, unknown>; key: string }>,
579
+ ): Record<string, SheetAxisDesignObj> {
580
+ const next: Record<string, SheetAxisDesignObj> = { ...(axisDesign || {}) };
581
+
582
+ lineDeltas.forEach(({ delta, key }) => {
583
+ const lineDesign = next[key] || {};
584
+ const mergedStyle: Record<string, unknown> = {
585
+ ...normalizeSheetCellStyle(lineDesign.style),
586
+ };
587
+
588
+ Object.entries(delta).forEach(([prop, value]) => {
589
+ if (value === null || value === undefined) {
590
+ delete mergedStyle[prop];
591
+ } else {
592
+ mergedStyle[prop] = value;
593
+ }
594
+ });
595
+
596
+ const normalizedStyle = normalizeSheetCellStyle(mergedStyle);
597
+
598
+ if (Object.keys(normalizedStyle).length) {
599
+ next[key] = {
600
+ ...lineDesign,
601
+ style: normalizedStyle,
602
+ };
603
+ return;
604
+ }
605
+
606
+ // An emptied style drops from the line design; an emptied line drops
607
+ // from the axis map so the saved design stays sparse
608
+ const { style: _droppedStyle, ...lineDesignWithoutStyle } = lineDesign;
609
+
610
+ if (Object.keys(lineDesignWithoutStyle).length) {
611
+ next[key] = lineDesignWithoutStyle;
612
+ } else {
613
+ delete next[key];
614
+ }
615
+ });
616
+
617
+ return next;
618
+ }
619
+
620
+ /*
621
+ * Return one canonical hard-NEW cell format from flexible saved input. Only a
622
+ * validated numberFormat survives; null, malformed, and retired keys normalize
623
+ * to key-absent so an empty cascade layer cannot shadow lower layers.
624
+ */
625
+ export function normalizeSheetCellFormat(
626
+ format?: Partial<SheetCellFormatObj> | Record<string, any> | string | null,
627
+ ): SheetCellFormatObj {
628
+ const { numberFormat: rawNumberFormat } = getSheetCellStyleObject(format);
629
+ const normalized: SheetCellFormatObj = {};
630
+
631
+ if (
632
+ rawNumberFormat &&
633
+ typeof rawNumberFormat === 'object' &&
634
+ !Array.isArray(rawNumberFormat) &&
635
+ isSheetCellNumberFormatType(rawNumberFormat.type)
636
+ ) {
637
+ const numberFormat: SheetCellNumberFormatObj = {
638
+ type: rawNumberFormat.type,
639
+ };
640
+
641
+ if (typeof rawNumberFormat.pattern === 'string' && rawNumberFormat.pattern && rawNumberFormat.pattern.length <= 500) {
642
+ numberFormat.pattern = rawNumberFormat.pattern;
643
+ }
644
+
645
+ if (typeof rawNumberFormat.currency === 'string' && /^[A-Z]{3,8}$/.test(rawNumberFormat.currency)) {
646
+ numberFormat.currency = rawNumberFormat.currency;
647
+ }
648
+
649
+ normalized.numberFormat = numberFormat;
650
+ }
651
+
652
+ return normalized;
653
+ }
654
+
655
+ /*
656
+ * Return one axis design map (columns or rows) with number-format layers
657
+ * replaced whole per line. A null/empty format deletes the line format, and
658
+ * lines whose design ends up empty drop out so the saved map stays sparse.
659
+ */
660
+ export function mergeSheetAxisDesignLineFormats(
661
+ axisDesign: Record<string, SheetAxisDesignObj> | null | undefined,
662
+ lineFormats: Array<{ format: SheetCellFormatObj | null; key: string }>,
663
+ ): Record<string, SheetAxisDesignObj> {
664
+ const next: Record<string, SheetAxisDesignObj> = { ...(axisDesign || {}) };
665
+
666
+ lineFormats.forEach(({ format, key }) => {
667
+ const lineDesign = next[key] || {};
668
+ const normalizedFormat = format ? normalizeSheetCellFormat(format) : {};
669
+
670
+ if (Object.keys(normalizedFormat).length) {
671
+ next[key] = {
672
+ ...lineDesign,
673
+ format: normalizedFormat,
674
+ };
675
+ return;
676
+ }
677
+
678
+ // An emptied format drops from the line design; an emptied line drops
679
+ // from the axis map so the saved design stays sparse
680
+ const { format: _droppedFormat, ...lineDesignWithoutFormat } = lineDesign;
681
+
682
+ if (Object.keys(lineDesignWithoutFormat).length) {
683
+ next[key] = lineDesignWithoutFormat;
684
+ } else {
685
+ delete next[key];
686
+ }
687
+ });
688
+
689
+ return next;
690
+ }
691
+
692
+ /* Return a shallow merged canonical cell format using the format cascade. */
693
+ export function mergeSheetCellFormats(
694
+ ...values: Array<Partial<SheetCellFormatObj> | Record<string, any> | string | null | undefined>
695
+ ): SheetCellFormatObj {
696
+ return values.reduce<SheetCellFormatObj>((merged, value) => {
697
+ const format = normalizeSheetCellFormat(value);
698
+
699
+ if (!Object.keys(format).length) {
700
+ return merged;
701
+ }
702
+
703
+ return Object.assign(merged, format);
704
+ }, {});
705
+ }