@weasel-js/prefs-schema 0.0.0-stage → 2.0.0

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.
package/dist/index.js ADDED
@@ -0,0 +1,488 @@
1
+ import { isPrefCondition, prefConditionInGroup, prefLeavesHoldingIn, prefLeavesHolding, prefSchemaHolding, UNSEEN, prefConditionHolds, prefValueAt, isPrefSection, prefSectionLeaves, isPrefLeaf, prefConditionPaths, prefLeafAt, setOwnPrefValue, prefConditionReadsItem, prefAliasTarget } from './chunk-DVHFV6JS.js';
2
+ export { PREF_ELSE, PREF_KINDS, PX_UNIT, VERSION_RECORD, filterPrefSubtree, flattenPrefValues, isBuiltinPref, isPrefCondition, isPrefLeaf, isPrefLinearUnit, isPrefScope, isPrefSection, mapPrefConditionPaths, nestPrefValues, prefAliasName, prefAliasTarget, prefAliasedLeaf, prefConditionHolds, prefConditionInGroup, prefConditionPaths, prefConditionReadsItem, prefDisplayBounds, prefHoldsValue, prefIsEnabled, prefIsShown, prefLeafAt, prefLeaves, prefOverrideOptions, prefOverridesOf, prefPairRows, prefSchemaHolding, prefScopeOptionValue, prefScopeValue, prefScopedDefault, prefScopesOf, prefSectionLeaves, prefStoredSelectors, prefUnit, prefUnitAccepts, prefValueAt, resolvePrefUnit, visiblePrefSubtree } from './chunk-DVHFV6JS.js';
3
+ import { endlessAllows } from '@weasel-js/quantity';
4
+ import { createPart, setOwn, getOwn, isPlainObject, codePointFloor } from '@weasel-js/registry';
5
+ import { isPaint, tryParseColor } from '@weasel-js/paint';
6
+
7
+ function prefBoundedNumber(leaf2, read) {
8
+ const { minAt, maxAt, ...rest } = leaf2;
9
+ if (minAt === void 0 && maxAt === void 0) return leaf2;
10
+ const lo = minAt?.(read);
11
+ const hi = maxAt?.(read);
12
+ return {
13
+ ...rest,
14
+ ...lo === void 0 ? {} : { min: leaf2.min === void 0 ? lo : Math.max(leaf2.min, lo) },
15
+ ...hi === void 0 ? {} : { max: leaf2.max === void 0 ? hi : Math.min(leaf2.max, hi) }
16
+ };
17
+ }
18
+ function prefClampNumber(leaf2, n) {
19
+ if (endlessAllows(leaf2.endless, n)) return n;
20
+ let v = n;
21
+ if (leaf2.min !== void 0 && v < leaf2.min) v = leaf2.min;
22
+ if (leaf2.max !== void 0 && v > leaf2.max) v = leaf2.max;
23
+ return v;
24
+ }
25
+ var CONDITIONS = ["showIf", "enableIf", "activeIf"];
26
+ var isNode = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
27
+ var isGroup = (node) => !("kind" in node) && isNode(node.children);
28
+ var under = (path, key) => path === "" ? key : `${path}.${key}`;
29
+ function mapNodes(held, each) {
30
+ let out;
31
+ for (const [key, child] of Object.entries(held)) {
32
+ if (!isNode(child)) continue;
33
+ const next = each(key, child);
34
+ if (next === child) continue;
35
+ out ??= { ...held };
36
+ setOwn(out, key, next);
37
+ }
38
+ return out ?? held;
39
+ }
40
+ function resolveNode(node, group, path) {
41
+ let out;
42
+ const set = (key, value) => {
43
+ out ??= { ...node };
44
+ setOwn(out, key, value);
45
+ };
46
+ for (const key of CONDITIONS) {
47
+ const condition = node[key];
48
+ if (!isPrefCondition(condition)) continue;
49
+ const next = prefConditionInGroup(condition, group);
50
+ if (next !== condition) set(key, next);
51
+ }
52
+ const inGroup = (_key, child) => resolveNode(child, group, path);
53
+ if (isNode(node.children)) {
54
+ const children = isGroup(node) ? mapNodes(node.children, (key, child) => resolveNode(child, path, under(path, key))) : mapNodes(node.children, inGroup);
55
+ if (children !== node.children) set("children", children);
56
+ }
57
+ if (isNode(node.members)) {
58
+ const members = mapNodes(node.members, inGroup);
59
+ if (members !== node.members) set("members", members);
60
+ }
61
+ if (isNode(node.item)) {
62
+ const item = resolveNode(node.item, group, path);
63
+ if (item !== node.item) set("item", item);
64
+ }
65
+ if (isNode(node.variants)) {
66
+ const variants = mapNodes(node.variants, inGroup);
67
+ if (variants !== node.variants) set("variants", variants);
68
+ }
69
+ return out ?? node;
70
+ }
71
+ var resolvedOf = createPart((schema) => resolveNode(schema, "", ""));
72
+ function resolvePrefGroupConditions(schema) {
73
+ return resolvedOf(schema);
74
+ }
75
+ var variantOf = (value, tag) => typeof value === "object" && value !== null ? getOwn(value, tag) : void 0;
76
+ function prefDecodedValue(leaf2, stored, siblings) {
77
+ if (leaf2.kind !== "boolean" && leaf2.kind !== "enum") return stored;
78
+ const { encoding } = leaf2;
79
+ return encoding ? encoding.read(stored, siblings) : stored;
80
+ }
81
+ function prefShownReader(schema, read, item, over) {
82
+ return (path, from) => {
83
+ const stored = read(path, from);
84
+ const found = from === "item" ? item && prefLeavesHoldingIn(item, path) : prefLeavesHolding(prefSchemaHolding(schema, path, over), path);
85
+ if (found === void 0 || found === UNSEEN) return stored;
86
+ const held = found.find((each) => (each.variants ?? []).every((v) => variantOf(read(v.at, from), v.tag) === v.key));
87
+ if (held === void 0) return stored;
88
+ const object = held.within === void 0 ? void 0 : read(held.within, from);
89
+ return prefDecodedValue(held.leaf, stored, typeof object === "object" && object !== null ? object : void 0);
90
+ };
91
+ }
92
+
93
+ // src/judge.ts
94
+ var PREF_CONDITION_FIELDS = ["showIf", "enableIf", "activeIf"];
95
+ function entryValue({ item, value }, path) {
96
+ if (path === "") return value !== void 0 ? value : item.default;
97
+ const held = prefValueAt(value, path);
98
+ return held !== void 0 ? held : prefValueAt(item.default, path);
99
+ }
100
+ function prefJudge(schema, read, entry, over) {
101
+ const stored = (path, from) => from === "item" ? entry && entryValue(entry, path) : read(path);
102
+ const shown = prefShownReader(schema, stored, entry?.item, over);
103
+ return (condition) => prefConditionHolds(condition, shown);
104
+ }
105
+ function deferEntryConditions(holds) {
106
+ return (condition, field) => prefConditionReadsItem(condition) || holds(condition, field);
107
+ }
108
+ function prefReader(schema, values, over) {
109
+ const beyond = over && prefReader(over, values);
110
+ return (path) => {
111
+ const held = prefValueAt(values, path);
112
+ if (held !== void 0) return held;
113
+ if (beyond && prefSchemaHolding(schema, path, over) === over) return beyond(path);
114
+ if (isPrefSection(schema)) {
115
+ const leaves = prefSectionLeaves(schema.members);
116
+ const exact = leaves.find(([key]) => key === path);
117
+ if (exact) return exact[1].default;
118
+ const outer = leaves.find(([key]) => path.startsWith(`${key}.`));
119
+ return outer && prefValueAt(outer[1].default, path.slice(outer[0].length + 1));
120
+ }
121
+ const steps = path.split(".");
122
+ let node = schema;
123
+ for (let i = 0; i < steps.length; i++) {
124
+ if (!Object.hasOwn(node.children, steps[i])) return void 0;
125
+ const child = node.children[steps[i]];
126
+ if (!isPrefLeaf(child)) node = child;
127
+ else return i === steps.length - 1 ? child.default : prefValueAt(child.default, steps.slice(i + 1).join("."));
128
+ }
129
+ return void 0;
130
+ };
131
+ }
132
+
133
+ // src/statusAt.ts
134
+ var PREF_ENTRY = "*";
135
+ var prefSectionPath = (at, key) => at === "" ? `#${key}` : `${at}.#${key}`;
136
+ var ROOT_PREF_STATUS = Object.freeze({ shown: true, enabled: true });
137
+ function prefStatusUnder(above, node, holds) {
138
+ const shown = above.shown && (node.showIf === void 0 || holds(node.showIf, "showIf"));
139
+ const enabled = above.enabled && (node.enableIf === void 0 || holds(node.enableIf, "enableIf"));
140
+ return shown === above.shown && enabled === above.enabled ? above : { shown, enabled };
141
+ }
142
+ function prefStatusIn(owner, path, holds) {
143
+ const { node, status, above, entry } = owner;
144
+ if (status.enabled || !above.enabled || node.enableIf === void 0) return status;
145
+ const reads = prefConditionPaths(node.enableIf).includes(path) || entry !== void 0 && prefConditionPaths(node.enableIf, "item").some((p) => `${entry}.${p}` === path);
146
+ if (!reads) return status;
147
+ return prefStatusUnder(above, { showIf: node.showIf }, holds);
148
+ }
149
+ var under2 = (prefix, key) => prefix === "" ? key : `${prefix}.${key}`;
150
+ function statusAmong(held, prefix, rest, owner, holds) {
151
+ return statusMatching(held, prefix, rest, owner, holds, true) ?? statusMatching(held, prefix, rest, owner, holds, false);
152
+ }
153
+ function statusMatching(held, prefix, rest, owner, holds, exact) {
154
+ for (const [key, child] of Object.entries(held)) {
155
+ if (!isPrefLeaf(child)) {
156
+ const found = statusMatching(child.members, prefix, rest, { node: child, status: prefStatusUnder(owner.status, child, holds), above: owner.status }, holds, exact);
157
+ if (found) return found;
158
+ continue;
159
+ }
160
+ if (exact ? rest !== key : !rest.startsWith(`${key}.`)) continue;
161
+ const path = under2(prefix, key);
162
+ const sits = prefStatusIn(owner, path, holds);
163
+ const status = prefStatusUnder(sits, child, holds);
164
+ return rest === key ? status : statusInside(child, path, rest.slice(key.length + 1), { node: child, status, above: sits }, holds);
165
+ }
166
+ return void 0;
167
+ }
168
+ function statusInside(leaf2, path, rest, owner, holds) {
169
+ const { status } = owner;
170
+ if (leaf2.kind === "object") return statusAmong(leaf2.children, path, rest, owner, holds) ?? status;
171
+ if (leaf2.kind === "list" || leaf2.kind === "map") {
172
+ const { item } = leaf2;
173
+ if (item === void 0) return status;
174
+ const inItem = deferEntryConditions(holds);
175
+ const entry = prefStatusUnder(status, { enableIf: item.enableIf }, inItem);
176
+ const dot = rest.indexOf(".");
177
+ if (dot < 0) return entry;
178
+ const at = `${path}.${rest.slice(0, dot)}`;
179
+ return statusInside(item, at, rest.slice(dot + 1), { node: { enableIf: item.enableIf }, status: entry, above: status }, inItem);
180
+ }
181
+ if (leaf2.kind !== "union") return status;
182
+ for (const variant of Object.values(leaf2.variants)) {
183
+ const found = statusAmong(variant.children, path, rest, owner, holds);
184
+ if (found) return found;
185
+ }
186
+ return status;
187
+ }
188
+ function statusAt(schema, path, holds) {
189
+ let owner = { node: schema, status: prefStatusUnder(ROOT_PREF_STATUS, schema, holds), above: ROOT_PREF_STATUS };
190
+ if (path === "") return owner.status;
191
+ if (isPrefSection(schema)) return statusAmong(schema.members, "", path, owner, holds) ?? owner.status;
192
+ let node = schema;
193
+ const steps = path.split(".");
194
+ for (let i = 0; i < steps.length; i++) {
195
+ if (!Object.hasOwn(node.children, steps[i])) return owner.status;
196
+ const child = node.children[steps[i]];
197
+ const at = steps.slice(0, i + 1).join(".");
198
+ const sits = isPrefLeaf(child) ? prefStatusIn(owner, at, holds) : owner.status;
199
+ owner = { node: child, status: prefStatusUnder(sits, child, holds), above: sits };
200
+ if (isPrefLeaf(child)) return i === steps.length - 1 ? owner.status : statusInside(child, at, steps.slice(i + 1).join("."), owner, holds);
201
+ node = child;
202
+ }
203
+ return owner.status;
204
+ }
205
+ function prefAliasStatus(own, alias, overGiven, holds) {
206
+ const over = resolvePrefGroupConditions(overGiven);
207
+ const target = prefAliasTarget(alias, (path) => prefLeafAt(over, path));
208
+ if (target === void 0) return own;
209
+ const theirs = statusAt(over, target.path, holds);
210
+ const shown = own.shown && theirs.shown;
211
+ const enabled = own.enabled && theirs.enabled;
212
+ return shown === own.shown && enabled === own.enabled ? own : { shown, enabled };
213
+ }
214
+ function prefStatusAt(schema, path, holds, { aliasOver, aliasHolds } = {}) {
215
+ schema = resolvePrefGroupConditions(schema);
216
+ const own = statusAt(schema, path, holds);
217
+ const leaf2 = prefLeafAt(schema, path);
218
+ return leaf2?.kind === "alias" ? prefAliasStatus(own, leaf2, aliasOver ?? schema, aliasHolds ?? holds) : own;
219
+ }
220
+
221
+ // src/status.ts
222
+ var under3 = (prefix, key) => prefix === "" ? key : `${prefix}.${key}`;
223
+ function leafWalk({ keepEmpty, keepHidden, inItem, aliasOver, aliasHolds, answers }) {
224
+ const disabled = /* @__PURE__ */ new Set();
225
+ const hidden = /* @__PURE__ */ new Set();
226
+ const active = /* @__PURE__ */ new Set();
227
+ const leaf2 = (node, path, above, holds, entry) => {
228
+ const own = prefStatusUnder(above, node, holds);
229
+ const status = node.kind === "alias" && aliasOver ? prefAliasStatus(own, node, aliasOver, aliasHolds ?? holds) : own;
230
+ return status.shown || keepHidden ? inside(node, path, status, holds, above, entry) : null;
231
+ };
232
+ const inside = (node, path, status, holds, above = status, entry) => {
233
+ if (!status.shown) hidden.add(path);
234
+ if (!status.enabled) disabled.add(path);
235
+ const { activeIf } = node;
236
+ if (node.kind === "action" && activeIf !== void 0 && answers(activeIf, holds) && holds(activeIf, "activeIf")) active.add(path);
237
+ const owner = entry === void 0 ? { node, status, above } : { node, status, above, entry };
238
+ if (node.kind === "object") {
239
+ const children = members(node.children, path, owner, holds);
240
+ return children === node.children ? node : { ...node, children };
241
+ }
242
+ if (node.kind === "list" || node.kind === "map") {
243
+ const { item } = node;
244
+ if (item === void 0) return node;
245
+ const at = under3(path, PREF_ENTRY);
246
+ const entry2 = inside(item, at, prefStatusUnder(status, { enableIf: item.enableIf }, inItem), inItem, status, at);
247
+ return entry2 === item ? node : { ...node, item: entry2 };
248
+ }
249
+ if (node.kind !== "union") return node;
250
+ const { variants } = node;
251
+ let changed = false;
252
+ const next = {};
253
+ for (const [key, variant] of Object.entries(variants)) {
254
+ const children = members(variant.children, path, owner, holds);
255
+ changed ||= children !== variant.children;
256
+ setOwnPrefValue(next, key, children === variant.children ? variant : { ...variant, children });
257
+ }
258
+ return changed ? { ...node, variants: next } : node;
259
+ };
260
+ const members = (held, prefix, owner, holds, within = prefix) => {
261
+ let changed = false;
262
+ const out = {};
263
+ for (const [key, child] of Object.entries(held)) {
264
+ const at = under3(prefix, key);
265
+ const kept = isPrefLeaf(child) ? leaf2(child, at, prefStatusIn(owner, at, holds), holds, owner.entry) : section(child, prefix, prefSectionPath(within, key), owner.status, holds, owner.entry);
266
+ changed ||= kept !== child;
267
+ if (kept !== null) setOwnPrefValue(out, key, kept);
268
+ }
269
+ return changed ? out : held;
270
+ };
271
+ const section = (node, prefix, path, above, holds, entry) => {
272
+ const status = prefStatusUnder(above, node, holds);
273
+ if (!status.shown && !keepHidden) return null;
274
+ if (!status.shown) hidden.add(path);
275
+ if (!status.enabled) disabled.add(path);
276
+ const kept = members(node.members, prefix, entry === void 0 ? { node, status, above } : { node, status, above, entry }, holds, path);
277
+ if (kept === node.members) return node;
278
+ return Object.keys(kept).length === 0 && !keepEmpty ? null : { ...node, members: kept };
279
+ };
280
+ return { disabled, hidden, active, leaf: leaf2, inside, members };
281
+ }
282
+ function applyPrefConditions(schema, holds, { keepEmpty = false, keepHidden = false, aliasOver: overGiven, aliasHolds } = {}) {
283
+ schema = resolvePrefGroupConditions(schema);
284
+ const aliasOver = overGiven && resolvePrefGroupConditions(overGiven);
285
+ const inItem = deferEntryConditions(holds);
286
+ const answers = (condition, by) => by !== inItem || !prefConditionReadsItem(condition);
287
+ const walk = leafWalk({ keepEmpty, keepHidden, inItem, aliasOver: aliasOver ?? schema, aliasHolds, answers });
288
+ const { disabled, hidden, active } = walk;
289
+ const group = (node, path, above) => {
290
+ const status2 = prefStatusUnder(above, node, holds);
291
+ if (!status2.shown && !keepHidden) return null;
292
+ if (!status2.shown && path !== "") hidden.add(path);
293
+ if (!status2.enabled && path !== "") disabled.add(path);
294
+ let changed = false;
295
+ const children = {};
296
+ for (const [key, child] of Object.entries(node.children)) {
297
+ const at = under3(path, key);
298
+ const kept2 = isPrefLeaf(child) ? walk.leaf(child, at, prefStatusIn({ node, status: status2, above }, at, holds), holds) : group(child, at, status2);
299
+ changed ||= kept2 !== child;
300
+ if (kept2 !== null) setOwnPrefValue(children, key, kept2);
301
+ }
302
+ if (!changed) return node;
303
+ return Object.keys(children).length === 0 && !keepEmpty && path !== "" ? null : { ...node, children };
304
+ };
305
+ if (!isPrefSection(schema)) return { shown: group(schema, "", ROOT_PREF_STATUS), disabled, hidden, active };
306
+ const status = prefStatusUnder(ROOT_PREF_STATUS, schema, holds);
307
+ if (!status.shown && !keepHidden) return { shown: null, disabled, hidden, active };
308
+ const kept = walk.members(schema.members, "", { node: schema, status, above: ROOT_PREF_STATUS }, holds);
309
+ return { shown: kept === schema.members ? schema : { ...schema, members: kept }, disabled, hidden, active };
310
+ }
311
+ var ALWAYS = () => true;
312
+ function applyPrefEntryConditions(item, path, holds, { keepEmpty = false, keepHidden = false } = {}) {
313
+ const asked = (condition, field) => !prefConditionReadsItem(condition) || holds(condition, field);
314
+ const answers = (condition, by) => by === asked && prefConditionReadsItem(condition);
315
+ const walk = leafWalk({ keepEmpty, keepHidden, inItem: ALWAYS, answers });
316
+ const status = prefStatusUnder(ROOT_PREF_STATUS, { enableIf: item.enableIf }, asked);
317
+ return { item: walk.inside(item, path, status, asked, ROOT_PREF_STATUS, path), disabled: walk.disabled, hidden: walk.hidden, active: walk.active };
318
+ }
319
+
320
+ // src/leafValue.ts
321
+ function prefValueAbsent(leaf2, held) {
322
+ return held === void 0 || held === null && leaf2.kind !== "paint";
323
+ }
324
+ function prefValueOrDefault(leaf2, held, { unset = false } = {}) {
325
+ if (!prefValueAbsent(leaf2, held)) return held;
326
+ if (unset || leaf2.clearable === true) return void 0;
327
+ return leaf2.default;
328
+ }
329
+
330
+ // src/build.ts
331
+ var leaf = (kind, name, def, rest) => ({ kind, name, description: "", default: def, ...rest });
332
+ var labeled = (value) => value === "" ? value : value[0].toUpperCase() + value.slice(1);
333
+ var pref = {
334
+ /** A group: `children` nest under its key, which is a segment of their paths. */
335
+ group(name, children, options) {
336
+ return { name, ...options, children };
337
+ },
338
+ /** A section: a heading over `members`, whose key adds nothing to their paths. */
339
+ section(name, members, options) {
340
+ return { name, ...options, members };
341
+ },
342
+ number: (name, def, rest) => leaf("number", name, def, rest),
343
+ boolean: (name, def, rest) => leaf("boolean", name, def, rest),
344
+ string: (name, def, rest) => leaf("string", name, def, rest),
345
+ color: (name, def, rest) => leaf("color", name, def, rest),
346
+ /** A fixed set of choices. An option given as its value alone is labeled with that value, its first letter raised. */
347
+ enum(name, def, options, rest) {
348
+ const full = options.map((option) => typeof option === "string" ? { value: option, label: labeled(option) } : option);
349
+ return { kind: "enum", name, description: "", default: def, ...rest, options: full };
350
+ },
351
+ /** A button firing the registered action `action` (`id` or `id:variant`). Its name defaults to empty, which shows the action's own label. */
352
+ action: (action, rest) => leaf("action", "", void 0, { action, ...rest }),
353
+ /** Every action registered in the action group `group`. */
354
+ actions: (group, rest) => leaf("actions", "", void 0, { group, ...rest }),
355
+ label: (name, rest) => leaf("label", name, void 0, rest),
356
+ separator: () => leaf("separator", "", void 0, void 0)
357
+ };
358
+ var isObject = (v) => typeof v === "object" && v !== null;
359
+ function prefObjectBase(pref2, held) {
360
+ if (isObject(held)) return held;
361
+ const lifted = held === void 0 ? void 0 : pref2.fromScalar?.(held);
362
+ return lifted ?? (isObject(pref2.default) ? pref2.default : {});
363
+ }
364
+ function prefObjectField(held, key) {
365
+ let cursor = held;
366
+ for (const step of key.split(".")) {
367
+ if (!isObject(cursor)) return void 0;
368
+ cursor = getOwn(cursor, step);
369
+ }
370
+ return cursor;
371
+ }
372
+ function prefObjectWithField(pref2, held, key, value) {
373
+ return withField(prefObjectBase(pref2, held), key.split("."), value);
374
+ }
375
+ function withField(base, [step, ...rest], value) {
376
+ const held = getOwn(base, step);
377
+ const next = rest.length === 0 ? value : withField(isObject(held) ? held : {}, rest, value);
378
+ const drops = next === void 0 || rest.length > 0 && Object.keys(next).length === 0;
379
+ if (!drops) return { ...base, [step]: next };
380
+ const { [step]: _dropped, ...kept } = base;
381
+ return kept;
382
+ }
383
+ function prefVariantOf(leaf2, value) {
384
+ if (value === null || typeof value !== "object" || Array.isArray(value)) return void 0;
385
+ const key = getOwn(value, leaf2.tag);
386
+ if (typeof key !== "string" || !Object.hasOwn(leaf2.variants, key)) return void 0;
387
+ return [key, leaf2.variants[key]];
388
+ }
389
+ function prefVariantDefault(leaf2, key) {
390
+ const held = getOwn(leaf2.variants, key)?.default;
391
+ const fields = held !== null && typeof held === "object" ? held : {};
392
+ return { ...fields, [leaf2.tag]: key };
393
+ }
394
+
395
+ // src/groups.ts
396
+ function prefDrawsAsBox(as) {
397
+ return as === "panel" || as === "menu";
398
+ }
399
+ function prefGroupIsPage(group) {
400
+ return group.as === void 0 || group.as === "page";
401
+ }
402
+ function repairPrefValue(leaf2, stored, validators) {
403
+ if (leaf2.kind === "number") {
404
+ if (stored === "Infinity") stored = Infinity;
405
+ else if (stored === "-Infinity") stored = -Infinity;
406
+ }
407
+ const validate = validators && getOwn(validators, leaf2.kind);
408
+ if (validate) {
409
+ try {
410
+ const out = validate(stored, leaf2);
411
+ return out === void 0 ? leaf2.default : out;
412
+ } catch {
413
+ return leaf2.default;
414
+ }
415
+ }
416
+ switch (leaf2.kind) {
417
+ case "number":
418
+ return repairNumber(leaf2, stored);
419
+ case "boolean":
420
+ return leaf2.encoding || typeof stored === "boolean" ? stored : leaf2.default;
421
+ case "string": {
422
+ const { maxLength } = leaf2;
423
+ if (typeof stored !== "string") return leaf2.default;
424
+ return maxLength !== void 0 && stored.length > maxLength ? stored.slice(0, codePointFloor(stored, maxLength)) : stored;
425
+ }
426
+ case "field":
427
+ return typeof stored === "string" ? stored : leaf2.default;
428
+ case "color":
429
+ return typeof stored === "string" && tryParseColor(stored) !== null ? stored : leaf2.default;
430
+ case "paint":
431
+ return stored === null || isPaint(stored) ? stored : leaf2.default;
432
+ case "enum": {
433
+ const e = leaf2;
434
+ if (e.encoding) return stored;
435
+ return e.options.some((o) => o.value === stored) ? stored : leaf2.default;
436
+ }
437
+ case "object":
438
+ if (isPlainObject(stored)) return repairFields(leaf2, stored, validators);
439
+ return leaf2.fromScalar ? stored : leaf2.default;
440
+ case "map":
441
+ return repairMap(leaf2, stored, validators);
442
+ case "union": {
443
+ const variant = prefVariantOf(leaf2, stored);
444
+ return variant ? repairFields(variant[1], stored, validators) : leaf2.default;
445
+ }
446
+ case "list":
447
+ return repairList(leaf2, stored, validators);
448
+ default:
449
+ return stored;
450
+ }
451
+ }
452
+ function repairFields(leaf2, stored, validators) {
453
+ let out = stored;
454
+ for (const [key, field] of prefSectionLeaves(leaf2.children)) {
455
+ const held = prefObjectField(stored, key);
456
+ if (held === void 0) continue;
457
+ const read = repairPrefValue(field, held, validators);
458
+ if (read !== held) out = prefObjectWithField(leaf2, out, key, read);
459
+ }
460
+ return out;
461
+ }
462
+ function repairMap(leaf2, stored, validators) {
463
+ if (!isPlainObject(stored)) return leaf2.default;
464
+ const held = stored;
465
+ let out;
466
+ for (const [key, value] of Object.entries(held)) {
467
+ const read = repairPrefValue(leaf2.item, value, validators);
468
+ if (read !== value) setOwn(out ??= { ...held }, key, read);
469
+ }
470
+ return out ?? stored;
471
+ }
472
+ function repairList(leaf2, stored, validators) {
473
+ if (!Array.isArray(stored)) return leaf2.default;
474
+ if (leaf2.minItems !== void 0 && stored.length < leaf2.minItems) return leaf2.default;
475
+ const kept = leaf2.maxItems !== void 0 && stored.length > leaf2.maxItems ? stored.slice(0, leaf2.maxItems) : stored;
476
+ const read = kept.map((entry) => repairPrefValue(leaf2.item, entry, validators));
477
+ return kept === stored && read.every((entry, i) => entry === stored[i]) ? stored : read;
478
+ }
479
+ function repairNumber(leaf2, stored) {
480
+ if (typeof stored !== "number" || Number.isNaN(stored)) return leaf2.default;
481
+ if (endlessAllows(leaf2.endless, stored)) return stored;
482
+ const v = prefClampNumber(leaf2, stored);
483
+ return Number.isFinite(v) ? v : leaf2.default;
484
+ }
485
+
486
+ export { PREF_CONDITION_FIELDS, PREF_ENTRY, ROOT_PREF_STATUS, applyPrefConditions, applyPrefEntryConditions, pref, prefBoundedNumber, prefClampNumber, prefDecodedValue, prefDrawsAsBox, prefGroupIsPage, prefJudge, prefObjectBase, prefObjectField, prefObjectWithField, prefReader, prefSectionPath, prefShownReader, prefStatusAt, prefStatusIn, prefStatusUnder, prefValueAbsent, prefValueOrDefault, prefVariantDefault, prefVariantOf, repairPrefValue, resolvePrefGroupConditions };
487
+ //# sourceMappingURL=index.js.map
488
+ //# sourceMappingURL=index.js.map