Repository navigation
Expand file tree
/
Copy pathprotocol-translated-echo.ts
More file actions
513 lines (485 loc) · 26.3 KB
/
Copy pathprotocol-translated-echo.ts
File metadata and controls
513 lines (485 loc) · 26.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* The write door's inverse of the `/meta` read's TRANSLATION: an object body
* that comes back carrying a label the read translated is stored with the
* source text, never with the translation.
*
* ## Why the write door owes this
*
* A `/meta` read of an object is translated for the caller's locale on its way
* out (the REST read exits run `translateMetadataDocument` from
* `@objectstack/spec/system`): a field label the bundle carries an entry for,
* and every injected system column's label through the built-in table, is
* served in the caller's language. Studio edits by GET → edit → PUT of the
* WHOLE body, and the write path persists the body verbatim (ADR-0005 appendix
* (c)), so a zh-CN session stored the translation as the document:
*
* - an injected column is no longer byte-identical to the platform's
* definition, so `stripInjectedSystemColumns` keeps it as the AUTHOR's own
* field. For `queue` that is a refusal: the registry refuses an author's
* `queue` on a user-owned object, so the object never registered and its
* data door answered 404 after a publish that said it succeeded. For
* `owner_id` and the others it is a silent change of what every other
* locale reads: the stored label is the author's now, so the translator
* yields to it and an `en` reader is served `负责人`.
* - an author's field the catalog translates stores the translation as its
* own label, which the catalog then can no longer translate for anyone.
*
* Its siblings on the same door, for the same reason: the read decorations
* (`stripReadDecorations`), the derived provenance keys
* (`stripDerivedProvenance`) and the injected columns themselves
* (`stripServedObjectColumns`) — what the READ adds comes off again on the way
* in. This one runs first, so the strip that follows sees the platform's own
* definition again and removes it.
*
* ## The rule — comparison against the source the read translated
*
* A slot is restored when, and only when, the incoming value differs from the
* SOURCE value and equals what the translator makes of that source in some
* locale. The source is the field's definition before the read translated it:
*
* - the stored row this save overwrites (the draft, or the active row a first
* draft overlays — the baseline the credential carry-forward reads); then
* - for an injected column, the platform's own definition, exactly as the
* read's `applyInjectedSystemColumns` adds it. Asked second, and also when
* the stored row declares the column: a row a zh-CN session already stored
* carries the echo itself, so compared with itself nothing differs, and
* compared with the platform's `queue` its label is `队列` for `Queue`. That
* is what lets the next save of such a row — and the stored-row migration,
* which re-saves through this door — repair it;
* - last, for what a CODE extender brings and the stored row does not
* declare at all, the packaged base: a field the packaged owner does not
* declare, and a `relatedLists` entry whose `list` the owner does not name
* (an extender's `place`). The read serves both from the fold over a stored
* row of any age, and translates them against the packaged base; a row
* saved before the extending package was installed carries no copy, so
* without this source the translation would be stored as the text.
*
* The locales tried are every one the read could have answered in: the
* bundle's, and the built-in system-field table's
* (`BUILTIN_SYSTEM_FIELD_LABEL_LOCALES`), which the read applies with no
* bundle at all. A request locale matches one of those or translates nothing.
*
* It is the same comparison-based provenance the translator itself applies
* (`scalarOverridesPackagedBase`, ADR-0029 D9.2a), run backwards, and its edge
* is the same: an author who deliberately types exactly the translation of
* the source in some locale gets the source back — which the reader in that
* locale is served anyway, since the catalog translates it. Nothing else is
* touched: a label that is not a translation of its source is the author's
* edit and is kept, and a field no source declares (neither the stored row nor,
* for what a code extender brings, the packaged base) has nothing to be
* compared with. The packaged base is never a source for a field or entry the
* stored row declares, nor for one the packaged owner declares: a stored slot
* is the stored row's to explain (a diverged one is served as written), and an
* owner's field or entry the stored row dropped is not served by the fold, so
* a body carrying it again carries the author's text.
*
* The slots are the ones the read translates: the object's scalars, each
* field's text slots and option labels, and the `label` of each object-form
* `relatedLists` entry (matched to its source entry by `list`).
*
* The source is translated with the read's own options, the packaged base
* included: a slot the read serves untranslated (an overlay that diverged
* from the packaged text) is no translation's source.
*
* Pure: no store, no service. The caller hands in the source row, the
* bundle, the packaged base and the packaged owner.
*/
import type { RuntimeAuthoringIssue } from '@objectstack/spec/api';
import { injectedSystemColumnDefs } from '@objectstack/spec/data';
import {
BUILTIN_SYSTEM_FIELD_LABEL_LOCALES,
scalarOverridesPackagedBase,
translateObject,
type ObjectLike,
type TranslateDocumentOptions,
type TranslationBundle,
} from '@objectstack/spec/system';
/**
* The `conversionId` the stored-row migration (`migrateStoredMetadata`) files
* a restored slot under. Not an ADR-0087 `MetadataConversion`: no stored
* spelling is retired here, the row simply carries a value the save door now
* keeps out — the same standing the `registerFlow` id has on a flow row's
* TODOs.
*/
export const TRANSLATED_ECHO_NOTICE_ID = 'translated-label-echo';
/** One slot the door put back to its source text. */
export interface RestoredTranslatedSlot {
/** Where, e.g. `fields.queue.label` or `fields.status.options[open].label`. */
path: string;
/** The translation the body carried. */
from: string;
/** The source text stored instead (`undefined` when the source has no such slot). */
to: string | undefined;
}
/**
* One `info` advisory per author slot a save put back to its source text, for
* the save answer's `advisories` — beside the credential carry-forward's
* `withheld-key-dropped` notices on the same door. The `path` is the object
* write's name-keyed one (`objects.NAME.fields.F.label`). The platform's
* injected columns are not reported: the stored document never carries them.
*/
export function translatedEchoNotices(name: string, restored: readonly RestoredTranslatedSlot[]): RuntimeAuthoringIssue[] {
return restored.map((slot) => ({
rule: TRANSLATED_ECHO_NOTICE_ID,
severity: 'info' as const,
where: `object "${name}"`,
path: `objects.${name}.${slot.path}`,
message: slot.to === undefined
? `this save left '${slot.path}' unset rather than storing '${slot.from}': that is what a /meta read serves for it in another language, translated from the source, which sets no text there.`
: `this save stored '${slot.to}' at '${slot.path}', not '${slot.from}': '${slot.from}' is what a /meta read serves for '${slot.to}' in another language, so it is the read's translation, not the document.`,
hint: 'Readers in that language are served the translation from the catalog. To change the text itself, edit it in the source language, or change the catalog entry.',
}));
}
/** The field slots the object translator writes. */
const FIELD_TEXT_SLOTS = ['label', 'description', 'inlineHelpText', 'inlineTitle', 'relatedListTitle'] as const;
/** The object scalars the object translator writes. */
const OBJECT_TEXT_SLOTS = ['label', 'pluralLabel', 'description'] as const;
type Rec = Record<string, unknown>;
/**
* A slot read off a source that does not declare the field (or option) at
* all: a sentinel no incoming string can equal, so that source explains
* nothing about it.
*/
const NOT_DECLARED: unique symbol = Symbol('not-declared');
function isRec(value: unknown): value is Rec {
return !!value && typeof value === 'object' && !Array.isArray(value);
}
/** A document's `fields` as `name -> definition`, from either the record or the array shape. */
function fieldMap(doc: unknown): Map<string, Rec> {
const out = new Map<string, Rec>();
if (!isRec(doc)) return out;
const fields = doc.fields;
if (Array.isArray(fields)) {
for (const f of fields) if (isRec(f) && typeof f.name === 'string') out.set(f.name, f);
} else if (isRec(fields)) {
for (const [name, f] of Object.entries(fields)) if (isRec(f)) out.set(name, f);
}
return out;
}
function optionKey(option: unknown): string | undefined {
if (!isRec(option)) return undefined;
const v = option.value;
return typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean' ? String(v) : undefined;
}
function optionOf(options: unknown, key: string): Rec | undefined {
return Array.isArray(options) ? (options.find((o) => optionKey(o) === key) as Rec | undefined) : undefined;
}
function optionLabel(options: unknown, key: string): unknown {
return optionOf(options, key)?.label;
}
/** The object-form `relatedLists` entry naming `list`, the first one. */
function relatedListEntry(entries: unknown, list: string): Rec | undefined {
return Array.isArray(entries) ? (entries.find((e) => isRec(e) && e.list === list) as Rec | undefined) : undefined;
}
/** Every `list` the entries name, as a bare string or an object entry's `list`. */
function relatedListNames(entries: unknown): Set<string> {
const out = new Set<string>();
if (!Array.isArray(entries)) return out;
for (const e of entries) {
if (typeof e === 'string') out.add(e);
else if (isRec(e) && typeof e.list === 'string') out.add(e.list);
}
return out;
}
/** That entry's `label`; {@link NOT_DECLARED} when no entry names `list` with a string label. */
function relatedListLabel(entries: unknown, list: string): unknown {
const label = relatedListEntry(entries, list)?.label;
return typeof label === 'string' ? label : NOT_DECLARED;
}
/**
* The locales a read could have translated into: the bundle's own, then the
* built-in system-field table's. Order is irrelevant to the verdict.
*/
export function translatedEchoLocales(bundle: TranslationBundle | undefined): string[] {
return [...new Set([...Object.keys(bundle ?? {}), ...BUILTIN_SYSTEM_FIELD_LABEL_LOCALES])];
}
/**
* One source document and what every locale's read made of it: `fields` holds
* only the names the incoming body declares.
*/
interface TranslatedSource {
/** Which source: the stored row, one patch entry's base, the platform's columns, or what a code extender brings. */
kind: 'stored' | 'patch' | 'injected' | 'extender';
doc: Rec;
fields: Map<string, Rec>;
reads: Array<{ doc: Rec; fields: Map<string, Rec> }>;
}
type PatchBundles = NonNullable<TranslateDocumentOptions['patchBundles']>;
function translatedSource(
kind: TranslatedSource['kind'],
doc: Rec,
locales: readonly string[],
bundle: TranslationBundle | undefined,
packagedBase: unknown,
patchBundles: PatchBundles | undefined,
): TranslatedSource {
return {
kind,
doc,
fields: fieldMap(doc),
reads: locales.map((locale) => {
// The read's own options: with the packaged base, a slot that
// diverged from the packaged text is served untranslated in every
// locale (ADR-0029 D9.2a), so it is no translation's source here
// either. [#33952] With the patch entries, a slot a code package's
// `fieldPatches` wrote reads that package's own translation, as
// the read serves it.
const read = translateObject(doc as unknown as ObjectLike, bundle, {
locale,
...(packagedBase !== undefined && packagedBase !== null ? { packagedBase } : {}),
...(patchBundles !== undefined && patchBundles.length > 0 ? { patchBundles } : {}),
}) as unknown as Rec;
return { doc: read, fields: fieldMap(read) };
}),
};
}
/**
* Put every translated slot of `item` back to its source text. Returns the
* SAME reference when nothing was restored. See the module header.
*
* The sources, asked in order, the first that explains the value wins: the
* stored row's own declaration, each patch entry's base, then — for an
* injected column — the platform's definition, and last — given
* `packagedOwner` — what a code extender brings that the stored row does not
* declare. The injected one is what heals a row a zh-CN session already
* stored: its `queue` declaration IS the translation, so compared with itself
* nothing differs, while compared with the platform's `queue` the label is
* `队列` for `Queue`.
*/
export function restoreTranslatedEchoes<T>(
item: T,
args: {
source: unknown;
bundle: TranslationBundle | undefined;
/**
* The packaged base the `/meta` read translates against (the code
* layer of a packaged object), passed through as `translateObject`'s
* `packagedBase`. Without it, a stored overlay slot that diverged from
* the packaged text (`Title` renamed `Subject`) would read as
* translated to the catalog's entry for the packaged text (`标题`), and
* an author typing exactly that would be put back to `Subject`. Absent
* for an object no package ships.
*/
packagedBase?: unknown;
/**
* [#33952] The read's `patchBundles` (`TranslateDocumentOptions`):
* one entry per code package whose `fieldPatches` wrote a slot of
* this object, its patched base and its own translations. Passed to
* every read of every source, and each entry's patched base is a
* source of its own (after the stored row, before the platform's
* columns): with no stored row the source is the packaged base, which
* never carries a patch, so the patch text a read translated is
* explained only by the base that carries it. Absent when no code
* package patches the object or none translates its patch.
*
* [#34836] A patch entry is asked for a slot only where its patch
* changed that slot (a patch of `label` explains nothing about the
* field's `description` or options), and only where the stored row
* does not declare the slot, carries the packaged text there, or
* holds an empty string. A stored slot with text of its own is the
* tenant's: the read serves it as written in every locale, so a save
* carrying it back keeps it, even when it equals the patching
* package's translation.
*/
patchBundles?: PatchBundles;
/**
* The packaged OWNER's own declaration (the registry's
* `getPackagedObjectOwner`), the base before any code extender widened
* it. With it and `packagedBase`, what the base carries beyond the
* owner is the last source, for an incoming field or `relatedLists`
* entry the stored row does not declare at all: a field a code
* extender adds (a name the owner does not declare) and an entry a
* code extender's `place` splices in (a `list` the owner does not
* name). Those the read serves from the fold over a stored row that
* predates the extending package, translated against the base, and
* nothing else explains them. Absent, there is no such source.
*/
packagedOwner?: unknown;
},
): { item: T; restored: RestoredTranslatedSlot[] } {
const restored: RestoredTranslatedSlot[] = [];
if (!isRec(item) || typeof item.name !== 'string') return { item, restored };
const incoming = fieldMap(item);
const patchBundles = args.patchBundles !== undefined && args.patchBundles.length > 0 ? args.patchBundles : undefined;
const locales = [...new Set([
...translatedEchoLocales(args.bundle),
...(patchBundles ?? []).flatMap((entry) => Object.keys((entry.bundle as Rec | undefined) ?? {})),
])];
const storedFields = fieldMap(args.source);
const storedDoc: Rec = { name: item.name, fields: {} };
for (const name of incoming.keys()) {
const def = storedFields.get(name);
if (def) (storedDoc.fields as Rec)[name] = { ...def };
}
if (isRec(args.source)) {
for (const slot of OBJECT_TEXT_SLOTS) {
if (typeof args.source[slot] === 'string') storedDoc[slot] = args.source[slot];
}
// The parent's `relatedLists`: the read translates an object-form
// entry's `label` (`objects.NAME._relatedLists.LIST.label`).
if (Array.isArray(args.source.relatedLists)) storedDoc.relatedLists = args.source.relatedLists;
}
const injectedDoc: Rec = { name: item.name, fields: {} };
for (const [name, def] of Object.entries(injectedSystemColumnDefs(item))) {
if (incoming.has(name)) (injectedDoc.fields as Rec)[name] = { ...def };
}
// Each patch entry's base, cut to the incoming fields its patch changed:
// a field it left as packaged is the stored row's or the packaged base's
// to explain, never a second source that could out-explain a tenant's own
// text on it. The cut is finished per slot in `sourceOf` [#34836].
const packagedFields = fieldMap(args.packagedBase);
const patchDocs: Rec[] = (patchBundles ?? []).map((entry) => {
const patchedFields = fieldMap(entry.patchedBase);
const doc: Rec = { name: item.name, fields: {} };
for (const name of incoming.keys()) {
const def = patchedFields.get(name);
if (def && JSON.stringify(def) !== JSON.stringify(packagedFields.get(name))) (doc.fields as Rec)[name] = { ...def };
}
return doc;
});
// What a code extender brings, cut to what the stored row does not
// declare: the packaged base's fields the owner does not declare, and its
// `relatedLists` entries whose `list` the owner does not name. A slot the
// stored row declares is the stored row's to explain, so this source can
// never out-explain a tenant's own text; with no stored row the source IS
// the packaged base, and this cut is empty.
const extenderDoc: Rec = { name: item.name, fields: {} };
if (isRec(args.packagedOwner) && isRec(args.packagedBase) && args.packagedBase !== args.packagedOwner) {
const ownerFields = fieldMap(args.packagedOwner);
for (const name of incoming.keys()) {
const def = packagedFields.get(name);
if (def && !ownerFields.has(name) && !storedFields.has(name)) (extenderDoc.fields as Rec)[name] = { ...def };
}
const ownerLists = relatedListNames(args.packagedOwner.relatedLists);
const storedLists = relatedListNames(isRec(args.source) ? args.source.relatedLists : undefined);
const placed = Array.isArray(args.packagedBase.relatedLists)
? args.packagedBase.relatedLists.filter((e) => isRec(e) && typeof e.list === 'string' && !ownerLists.has(e.list) && !storedLists.has(e.list))
: [];
if (placed.length > 0) extenderDoc.relatedLists = placed;
}
const sources = ([['stored', storedDoc], ...patchDocs.map((doc) => ['patch', doc] as const), ['injected', injectedDoc], ['extender', extenderDoc]] as const)
.filter(([, doc]) => Object.keys(doc.fields as Rec).length > 0 || Object.keys(doc).length > 2)
.map(([kind, doc]) => translatedSource(kind, doc, locales, args.bundle, args.packagedBase, patchBundles));
if (sources.length === 0) return { item, restored };
const storedView = { doc: storedDoc, fields: fieldMap(storedDoc) };
const packagedView = { doc: { name: item.name, fields: {} } as Rec, fields: packagedFields };
const unset = (v: unknown): boolean => v === NOT_DECLARED || v === undefined;
/**
* The source text `value` is a translation of, through `pick` (which reads
* one slot off a source document or a read of it, {@link NOT_DECLARED}
* where that source does not declare it); `null` when no source explains
* it. A source the value equals explains nothing and the next is asked:
* that is the stored echo meeting the platform's definition.
*
* [#34836] A patch entry answers a slot only where its patch changed that
* slot against the packaged base, and only where the stored row does not
* override the packaged text there: a stored slot with text of its own
* (`税号` typed over the patched `Tax ID`) is the tenant's, served as
* written in every locale, never the patching package's translation
* coming back. "Overrides" is the fold's own question, asked through
* `packagedOwner` (the packaged field, option or object that holds the
* slot, and its key) by {@link scalarOverridesPackagedBase}, ⛔ never a
* second copy: the fold applies the patch over a stored slot that is
* unset, equal to the packaged text or an empty string, so the read
* serves the patch text there and its translation comes back.
*/
const sourceOf = (
value: unknown,
pick: (doc: Rec, fields: Map<string, Rec>) => unknown,
packagedOwner: () => { base: unknown; key: string },
): { to: unknown } | null => {
if (typeof value !== 'string') return null;
for (const source of sources) {
const original = pick(source.doc, source.fields);
if (original === NOT_DECLARED || original === value) continue;
if (source.kind === 'patch') {
const packaged = pick(packagedView.doc, packagedView.fields);
if (original === packaged || (unset(original) && unset(packaged))) continue;
const { base, key } = packagedOwner();
const stored = pick(storedView.doc, storedView.fields);
if (scalarOverridesPackagedBase(base, key as Parameters<typeof scalarOverridesPackagedBase>[1], stored)) continue;
}
if (source.reads.some((read) => {
const translated = pick(read.doc, read.fields);
return translated !== original && translated === value;
})) return { to: original };
}
return null;
};
let nextItem: Rec = item;
for (const slot of OBJECT_TEXT_SLOTS) {
const hit = sourceOf(item[slot], (doc) => (typeof doc[slot] === 'string' ? doc[slot] : NOT_DECLARED), () => ({ base: args.packagedBase, key: slot }));
if (!hit || typeof hit.to !== 'string') continue;
restored.push({ path: slot, from: item[slot] as string, to: hit.to });
nextItem = nextItem === item ? { ...item } : nextItem;
nextItem[slot] = hit.to;
}
// A `relatedLists` entry's `label`, by the entry's `list`: the read
// overlays `objects.NAME._relatedLists.LIST.label` onto it, so a save
// carrying that translation back stores the source title instead.
if (Array.isArray(item.relatedLists)) {
let entries: unknown[] | undefined;
item.relatedLists.forEach((entry, index) => {
if (!isRec(entry) || typeof entry.list !== 'string' || typeof entry.label !== 'string') return;
const list = entry.list;
const hit = sourceOf(
entry.label,
(doc) => relatedListLabel(doc.relatedLists, list),
() => ({ base: relatedListEntry(isRec(args.packagedBase) ? args.packagedBase.relatedLists : undefined, list), key: 'label' }),
);
if (!hit || typeof hit.to !== 'string') return;
restored.push({ path: `relatedLists[${list}].label`, from: entry.label, to: hit.to });
entries ??= [...(item.relatedLists as unknown[])];
entries[index] = { ...entry, label: hit.to };
});
if (entries) {
nextItem = nextItem === item ? { ...item } : nextItem;
nextItem.relatedLists = entries;
}
}
const nextFields = new Map<string, Rec>();
for (const [name, field] of incoming) {
if (!sources.some((source) => source.fields.has(name))) continue;
let next: Rec = field;
for (const slot of FIELD_TEXT_SLOTS) {
const hit = sourceOf(field[slot], (_doc, fields) => {
const f = fields.get(name);
return f ? f[slot] : NOT_DECLARED;
}, () => ({ base: packagedFields.get(name), key: slot }));
if (!hit) continue;
restored.push({ path: `fields.${name}.${slot}`, from: field[slot] as string, to: hit.to as string | undefined });
next = next === field ? { ...field } : next;
if (hit.to === undefined) delete next[slot];
else next[slot] = hit.to;
}
if (Array.isArray(field.options)) {
let options: unknown[] | undefined;
field.options.forEach((option, index) => {
const key = optionKey(option);
if (key === undefined || !isRec(option)) return;
const hit = sourceOf(option.label, (_doc, fields) => {
const f = fields.get(name);
return f ? optionLabel(f.options, key) ?? NOT_DECLARED : NOT_DECLARED;
}, () => ({ base: optionOf(packagedFields.get(name)?.options, key), key: 'label' }));
if (!hit || typeof hit.to !== 'string') return;
restored.push({ path: `fields.${name}.options[${key}].label`, from: option.label as string, to: hit.to });
options ??= [...(field.options as unknown[])];
options[index] = { ...option, label: hit.to };
});
if (options) {
next = next === field ? { ...field } : next;
next.options = options;
}
}
if (next !== field) nextFields.set(name, next);
}
if (nextFields.size > 0) {
nextItem = nextItem === item ? { ...item } : nextItem;
const fields = item.fields;
nextItem.fields = Array.isArray(fields)
? fields.map((f) => (isRec(f) && typeof f.name === 'string' && nextFields.get(f.name)) || f)
: Object.fromEntries(Object.entries(fields as Rec).map(([name, f]) => [name, nextFields.get(name) ?? f]));
}
return { item: nextItem as T, restored };
}