@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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 orochi235
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,31 @@
1
- # Temporary Holding Version
1
+ # @weasel-js/prefs-schema
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The declaration half of [`@weasel-js/prefs`](../prefs/README.md): the `pref`
4
+ builders, the leaf, group and section types, units, conditions and the status
5
+ they give a leaf, and repairing a stored value against a leaf. No store, no
6
+ storage, no React.
7
+
8
+ Part of [weasel](https://github.com/orochi235/weasel), a domain-agnostic
9
+ scene-graph engine for React. See the
10
+ [API reference](https://orochi235.github.io/weasel/#pkg/prefs-schema).
11
+
12
+ ```sh
13
+ npm install @weasel-js/prefs-schema
14
+ ```
15
+
16
+ ```ts
17
+ import { pref, type PrefGroup } from '@weasel-js/prefs-schema';
18
+
19
+ const params: PrefGroup = pref.group('Offset', {
20
+ distance: pref.number('Distance', 4, { min: 0, unit: { scale: 1, suffix: 'px' } }),
21
+ });
22
+ ```
23
+
24
+ A package that declares settings and leaves storing them to its host (an
25
+ action's `params`, a tool's options, a node's properties) depends on this one.
26
+ `@weasel-js/prefs` re-exports all of it beside the store, so an app that opens
27
+ a store imports from there and never needs this package by name.
28
+
29
+ - `@weasel-js/prefs-schema/internal` — not public API. The helpers
30
+ `@weasel-js/prefs` builds its store and edit model on; it changes without
31
+ notice.
@@ -0,0 +1,596 @@
1
+ import { createPart, setOwn, getOwn, matchQuery, isPlainObject } from '@weasel-js/registry';
2
+ import { unitSymbol, decimal, formatUnit, unitScale, decimalPlaces } from '@weasel-js/quantity';
3
+
4
+ // src/alias.ts
5
+ function prefAliasTarget(alias, leafAt) {
6
+ const passed = /* @__PURE__ */ new Set();
7
+ let path = alias.of;
8
+ for (; ; ) {
9
+ if (passed.has(path)) return void 0;
10
+ passed.add(path);
11
+ const leaf = leafAt(path);
12
+ if (leaf === void 0) return void 0;
13
+ if (leaf.kind !== "alias") return { path, leaf };
14
+ path = leaf.of;
15
+ }
16
+ }
17
+ function prefAliasName(alias, target) {
18
+ if (alias.name !== "") return alias.name;
19
+ return target === void 0 ? "Alias" : target.name;
20
+ }
21
+ function prefAliasedLeaf(alias, target) {
22
+ return {
23
+ ...target,
24
+ name: prefAliasName(alias, target),
25
+ description: alias.description === "" ? target.description : alias.description,
26
+ ...alias.access ? { access: alias.access } : {},
27
+ ...alias.tags ? { tags: [...target.tags ?? [], ...alias.tags] } : {}
28
+ };
29
+ }
30
+ function prefUnitAccepts(unit) {
31
+ return unit.suffix === void 0 ? { ...unit.accepts } : { [unit.suffix]: 1, ...unit.accepts };
32
+ }
33
+ function isPrefLinearUnit(unit) {
34
+ return typeof unit.toDisplay !== "function";
35
+ }
36
+ var SHOWN = "shown";
37
+ var DECIMAL_PLACES = 12;
38
+ function storedOf(shown, scale, offset) {
39
+ const raw = shown * scale + offset;
40
+ if (!Number.isFinite(raw)) return raw;
41
+ const places = Math.max(decimalPlaces(shown) + decimalPlaces(scale), decimalPlaces(offset));
42
+ return places > DECIMAL_PLACES ? raw : Number(raw.toFixed(places));
43
+ }
44
+ function resolvePrefUnit(unit) {
45
+ const suffix = unit.suffix === void 0 ? void 0 : unitSymbol(unit.suffix, { compact: unit.compact }).text;
46
+ if (!isPrefLinearUnit(unit)) {
47
+ return {
48
+ ...unit,
49
+ ...suffix === void 0 ? {} : { suffix },
50
+ toDisplayExact: unit.toDisplay,
51
+ format: unit.format ?? ((stored) => `${unit.toDisplay(stored)}${suffix ?? ""}`)
52
+ };
53
+ }
54
+ const { scale, offset = 0, places, accepts } = unit;
55
+ const system = { base: "stored", units: { [SHOWN]: { factor: scale, offset } } };
56
+ const toDisplayExact = (stored) => (stored - offset) / scale;
57
+ return {
58
+ toDisplay: (stored) => {
59
+ const shown = toDisplayExact(stored);
60
+ return places === void 0 ? shown : Number(shown.toFixed(places));
61
+ },
62
+ toDisplayExact,
63
+ fromDisplay: (shown) => storedOf(shown, scale, offset),
64
+ format: (stored) => `${formatUnit(stored, SHOWN, system, { places: places ?? 2, suffix: false, pad: places !== void 0 })}${suffix ?? ""}`,
65
+ ...places === void 0 ? {} : { places, display: decimal({ places, grouping: false }) },
66
+ ...suffix === void 0 ? {} : { suffix },
67
+ ...accepts === void 0 ? {} : { accepts }
68
+ };
69
+ }
70
+ function prefUnit(system, display, opts) {
71
+ let self;
72
+ try {
73
+ self = unitScale(system, display);
74
+ } catch (e) {
75
+ throw new Error(`prefUnit: ${e instanceof Error ? e.message : String(e)}`);
76
+ }
77
+ const suffix = opts?.suffix ?? display;
78
+ const accepts = { [suffix]: 1 };
79
+ for (const name of Object.keys(system.units)) {
80
+ const other = unitScale(system, name);
81
+ const factor = other.factor / self.factor;
82
+ const offset = (other.offset - self.offset) / self.factor;
83
+ setOwn(accepts, name, offset === 0 ? factor : { factor, offset });
84
+ }
85
+ return {
86
+ suffix,
87
+ scale: self.factor,
88
+ ...self.offset === 0 ? {} : { offset: self.offset },
89
+ accepts,
90
+ ...opts?.places === void 0 ? {} : { places: opts.places },
91
+ ...opts?.compact ? { compact: true } : {}
92
+ };
93
+ }
94
+ var PX_UNIT = { scale: 1, suffix: "px" };
95
+ function prefDisplayBounds(p) {
96
+ const ends = p.endless === void 0 ? {} : { endless: p.endless };
97
+ if (!p.unit) return { min: p.min, max: p.max, step: p.step ?? 1, ...ends, ...p.stops ? { stops: p.stops } : {} };
98
+ const { toDisplay, toDisplayExact, places } = resolvePrefUnit(p.unit);
99
+ const backward = toDisplayExact(1) < toDisplayExact(0);
100
+ const lo = p.min === void 0 ? void 0 : toDisplay(p.min);
101
+ const hi = p.max === void 0 ? void 0 : toDisplay(p.max);
102
+ const step = p.step === void 0 ? 1 : displayStep(Math.abs(toDisplayExact(p.step) - toDisplayExact(0)), places);
103
+ const flip = { min: "max", max: "min", both: "both" };
104
+ const endless = backward && p.endless !== void 0 ? flip[p.endless] : p.endless;
105
+ const stops = p.stops?.map(toDisplay);
106
+ return {
107
+ min: backward ? hi : lo,
108
+ max: backward ? lo : hi,
109
+ step,
110
+ ...endless === void 0 ? {} : { endless },
111
+ ...stops ? { stops } : {}
112
+ };
113
+ }
114
+ function displayStep(exact, places) {
115
+ if (places === void 0) return exact;
116
+ const quantum = 10 ** -places;
117
+ const quanta = Math.max(1, Math.ceil(Number((exact / quantum).toFixed(9))));
118
+ return Number((quanta * quantum).toFixed(places));
119
+ }
120
+ function isPrefLeaf(node) {
121
+ return "kind" in node;
122
+ }
123
+ var VALUELESS = /* @__PURE__ */ new Set(["action", "actions", "alias", "label", "separator"]);
124
+ function prefHoldsValue(leaf) {
125
+ return !VALUELESS.has(leaf.kind);
126
+ }
127
+ function isPrefSection(node) {
128
+ return !isPrefLeaf(node) && "members" in node;
129
+ }
130
+ function prefSectionLeaves(members) {
131
+ return Object.entries(members).flatMap(
132
+ ([key, child]) => isPrefLeaf(child) ? [[key, child]] : prefSectionLeaves(child.members)
133
+ );
134
+ }
135
+ function prefLeafAt(schema, path) {
136
+ if (isPrefSection(schema)) return prefSectionLeaves(schema.members).find(([key]) => key === path)?.[1];
137
+ let node = schema;
138
+ for (const seg of path.split(".")) {
139
+ if (isPrefLeaf(node) || !Object.hasOwn(node.children, seg)) return void 0;
140
+ node = node.children[seg];
141
+ }
142
+ return isPrefLeaf(node) ? node : void 0;
143
+ }
144
+ function prefValueAt(values, path) {
145
+ let cur = values;
146
+ for (const seg of path.split(".")) {
147
+ if (cur == null || typeof cur !== "object" || !Object.hasOwn(cur, seg)) return void 0;
148
+ cur = cur[seg];
149
+ }
150
+ return cur;
151
+ }
152
+ function visibleFields(fields, keepEmpty) {
153
+ const out = {};
154
+ for (const [key, child] of Object.entries(fields)) {
155
+ if (isPrefLeaf(child)) {
156
+ const kept = visiblePrefSubtree(child, false, keepEmpty);
157
+ if (kept) setOwnPrefValue(out, key, kept);
158
+ continue;
159
+ }
160
+ const members = visibleFields(child.members, keepEmpty);
161
+ if (Object.keys(members).length > 0 || keepEmpty) setOwnPrefValue(out, key, { ...child, members });
162
+ }
163
+ return out;
164
+ }
165
+ function visiblePrefSubtree(node, showHidden, keepEmpty = false) {
166
+ if (isPrefLeaf(node)) {
167
+ if (node.hidden && !showHidden) return null;
168
+ if (showHidden || node.kind !== "object") return node;
169
+ return { ...node, children: visibleFields(node.children, keepEmpty) };
170
+ }
171
+ const group = node;
172
+ const children = {};
173
+ for (const [key, child] of Object.entries(group.children)) {
174
+ const kept = visiblePrefSubtree(child, showHidden, keepEmpty);
175
+ if (kept) setOwnPrefValue(children, key, kept);
176
+ }
177
+ if (Object.keys(children).length === 0 && !keepEmpty) return null;
178
+ return { ...node, children };
179
+ }
180
+ var VERSION_RECORD = "$version";
181
+ function prefLeaves(schema) {
182
+ const out = /* @__PURE__ */ new Map();
183
+ const walk = (group, prefix) => {
184
+ for (const [key, child] of Object.entries(group.children)) {
185
+ if (key.includes(".")) {
186
+ throw new Error(`[prefs] schema key "${key}" contains "."; group keys are path segments`);
187
+ }
188
+ if (prefix === "" && key === VERSION_RECORD) {
189
+ throw new Error(`[prefs] schema key "${VERSION_RECORD}" is reserved`);
190
+ }
191
+ const path = prefix === "" ? key : `${prefix}.${key}`;
192
+ if (isPrefLeaf(child)) out.set(path, child);
193
+ else walk(child, path);
194
+ }
195
+ };
196
+ walk(schema, "");
197
+ return out;
198
+ }
199
+ function setOwnPrefValue(tree, key, value) {
200
+ setOwn(tree, key, value);
201
+ }
202
+ function assignPrefValueAt(root, path, value) {
203
+ const parts = path.split(".");
204
+ let cursor = root;
205
+ for (const part of parts.slice(0, -1)) {
206
+ const branch = getOwn(cursor, part);
207
+ if (typeof branch !== "object" || branch === null) setOwnPrefValue(cursor, part, {});
208
+ cursor = getOwn(cursor, part);
209
+ }
210
+ setOwnPrefValue(cursor, parts[parts.length - 1], value);
211
+ }
212
+ function nestPrefValues(values) {
213
+ const tree = {};
214
+ for (const [path, value] of Object.entries(values ?? {})) assignPrefValueAt(tree, path, value);
215
+ return tree;
216
+ }
217
+ function flattenPrefValues(schema, tree) {
218
+ const out = [];
219
+ for (const path of prefLeaves(schema).keys()) {
220
+ const value = prefValueAt(tree, path);
221
+ if (value !== void 0) out.push([path, value]);
222
+ }
223
+ return out;
224
+ }
225
+ function prefLeafMatches(pref, path, query) {
226
+ return matchQuery(query, { text: [pref.name, pref.description, path], tags: pref.tags }) !== null;
227
+ }
228
+ function filterPrefSubtree(node, query, path = "", over) {
229
+ const q = query.trim().toLowerCase();
230
+ if (q === "") return node;
231
+ const within = over ?? (isPrefLeaf(node) ? void 0 : node);
232
+ const find = (p) => within && prefLeafAt(within, p);
233
+ if (isPrefLeaf(node)) {
234
+ const target = node.kind === "alias" ? prefAliasTarget(node, find) : void 0;
235
+ const drawn = target ? prefAliasedLeaf(node, target.leaf) : node;
236
+ return prefLeafMatches(drawn, path, q) ? node : null;
237
+ }
238
+ const group = node;
239
+ if (matchQuery(q, { text: [group.name], tags: group.tags }) !== null) return node;
240
+ const children = {};
241
+ for (const [key, child] of Object.entries(group.children)) {
242
+ const kept = filterPrefSubtree(child, q, path === "" ? key : `${path}.${key}`, within);
243
+ if (kept) setOwnPrefValue(children, key, kept);
244
+ }
245
+ if (Object.keys(children).length === 0) return null;
246
+ return { ...node, children };
247
+ }
248
+ var ORDERINGS = ["above", "below", "atLeast", "atMost"];
249
+ var TEST_KEYS = /* @__PURE__ */ new Set(["path", "from", "is", "isNot", "in", ...ORDERINGS]);
250
+ var isValue = (v) => v === null || typeof v === "string" || typeof v === "number" || typeof v === "boolean";
251
+ function formOf(value) {
252
+ if (!isPlainObject(value)) return void 0;
253
+ const keys = Object.keys(value);
254
+ if (keys.length === 1 && (keys[0] === "all" || keys[0] === "any")) return Array.isArray(value[keys[0]]) ? keys[0] : void 0;
255
+ if (keys.length === 1 && keys[0] === "not") return "not";
256
+ if (typeof value.path !== "string" || !keys.every((key) => TEST_KEYS.has(key))) return void 0;
257
+ if (value.from !== void 0 && value.from !== "item" && value.from !== "group") return void 0;
258
+ if (value.is !== void 0 && !isValue(value.is)) return void 0;
259
+ if (value.isNot !== void 0 && !isValue(value.isNot)) return void 0;
260
+ if (value.in !== void 0 && !(Array.isArray(value.in) && value.in.every(isValue))) return void 0;
261
+ return ORDERINGS.every((op) => value[op] === void 0 || typeof value[op] === "number") ? "test" : void 0;
262
+ }
263
+ function partsOf(condition, form) {
264
+ const held = condition;
265
+ return form === "test" ? [] : form === "not" ? [held.not] : held[form];
266
+ }
267
+ function isPrefCondition(value) {
268
+ const form = formOf(value);
269
+ return form !== void 0 && partsOf(value, form).every(isPrefCondition);
270
+ }
271
+ var same = (held, value) => value === null ? held == null : held === value;
272
+ function isSet(held) {
273
+ if (Array.isArray(held)) return held.length > 0;
274
+ if (isPlainObject(held)) return Object.keys(held).length > 0;
275
+ return Boolean(held);
276
+ }
277
+ function testHolds(test, held) {
278
+ const ops = Object.keys(test).filter((key) => key !== "path" && key !== "from" && test[key] !== void 0);
279
+ if (ops.length === 0) return isSet(held);
280
+ if (test.is !== void 0 && !same(held, test.is)) return false;
281
+ if (test.isNot !== void 0 && same(held, test.isNot)) return false;
282
+ if (test.in !== void 0 && !test.in.some((value) => same(held, value))) return false;
283
+ if (!ORDERINGS.some((op) => test[op] !== void 0)) return true;
284
+ if (typeof held !== "number") return false;
285
+ return (test.above === void 0 || held > test.above) && (test.below === void 0 || held < test.below) && (test.atLeast === void 0 || held >= test.atLeast) && (test.atMost === void 0 || held <= test.atMost);
286
+ }
287
+ function prefConditionHolds(condition, read) {
288
+ if ("all" in condition) return condition.all.every((part) => prefConditionHolds(part, read));
289
+ if ("any" in condition) return condition.any.some((part) => prefConditionHolds(part, read));
290
+ if ("not" in condition) return !prefConditionHolds(condition.not, read);
291
+ if (condition.from === "group") warnUnresolved(condition.path);
292
+ return testHolds(condition, read(condition.path, condition.from));
293
+ }
294
+ var warned = /* @__PURE__ */ new Set();
295
+ function warnUnresolved(path) {
296
+ if (warned.has(path)) return;
297
+ warned.add(path);
298
+ console.warn(`weasel prefs: a condition reads "${path}" from its group, but nothing resolved it to a path from the root; resolve the schema with resolvePrefGroupConditions`);
299
+ }
300
+ function prefIsShown(node, read) {
301
+ return node.showIf === void 0 || prefConditionHolds(node.showIf, read);
302
+ }
303
+ function prefIsEnabled(node, read) {
304
+ return node.enableIf === void 0 || prefConditionHolds(node.enableIf, read);
305
+ }
306
+ function prefConditionPaths(condition, from) {
307
+ const out = /* @__PURE__ */ new Set();
308
+ const walk = (c) => {
309
+ if ("all" in c) c.all.forEach(walk);
310
+ else if ("any" in c) c.any.forEach(walk);
311
+ else if ("not" in c) walk(c.not);
312
+ else if (c.from === from) out.add(c.path);
313
+ };
314
+ walk(condition);
315
+ return [...out];
316
+ }
317
+ function prefConditionReadsItem(condition) {
318
+ if ("all" in condition) return condition.all.some(prefConditionReadsItem);
319
+ if ("any" in condition) return condition.any.some(prefConditionReadsItem);
320
+ if ("not" in condition) return prefConditionReadsItem(condition.not);
321
+ return condition.from === "item";
322
+ }
323
+ function mapPrefConditionPaths(condition, move) {
324
+ const each = (parts) => {
325
+ const next = parts.map((part) => mapPrefConditionPaths(part, move));
326
+ return next.every((part, i) => part === parts[i]) ? parts : next;
327
+ };
328
+ if ("all" in condition) {
329
+ const all = each(condition.all);
330
+ return all === condition.all ? condition : { all };
331
+ }
332
+ if ("any" in condition) {
333
+ const any = each(condition.any);
334
+ return any === condition.any ? condition : { any };
335
+ }
336
+ if ("not" in condition) {
337
+ const not = mapPrefConditionPaths(condition.not, move);
338
+ return not === condition.not ? condition : { not };
339
+ }
340
+ const path = move(condition.path, condition.from);
341
+ return path === condition.path ? condition : { ...condition, path };
342
+ }
343
+ function prefConditionInGroup(condition, group) {
344
+ const each = (parts) => {
345
+ const next = parts.map((part) => prefConditionInGroup(part, group));
346
+ return next.every((part, i) => part === parts[i]) ? parts : next;
347
+ };
348
+ if ("all" in condition) {
349
+ const all = each(condition.all);
350
+ return all === condition.all ? condition : { all };
351
+ }
352
+ if ("any" in condition) {
353
+ const any = each(condition.any);
354
+ return any === condition.any ? condition : { any };
355
+ }
356
+ if ("not" in condition) {
357
+ const not = prefConditionInGroup(condition.not, group);
358
+ return not === condition.not ? condition : { not };
359
+ }
360
+ if (condition.from !== "group") return condition;
361
+ const { from: _from, ...rest } = condition;
362
+ const path = group === "" ? condition.path : condition.path === "" ? group : `${group}.${condition.path}`;
363
+ return { ...rest, path };
364
+ }
365
+
366
+ // src/schema.ts
367
+ var PREF_KINDS = {
368
+ number: true,
369
+ boolean: true,
370
+ string: true,
371
+ enum: true,
372
+ color: true,
373
+ paint: true,
374
+ object: true,
375
+ list: true,
376
+ map: true,
377
+ union: true,
378
+ field: true,
379
+ action: true,
380
+ actions: true,
381
+ alias: true,
382
+ label: true,
383
+ separator: true
384
+ };
385
+ function prefPairRows(leaves) {
386
+ const rows = /* @__PURE__ */ new Map();
387
+ for (const [path, leaf] of leaves) {
388
+ if (!leaf.pair || rows.has(path)) continue;
389
+ const row = { key: path, label: leaf.pair.label ?? leaf.name };
390
+ rows.set(path, row);
391
+ const partners = typeof leaf.pair.with === "string" ? [leaf.pair.with] : leaf.pair.with;
392
+ for (const partner of partners) if (!rows.has(partner)) rows.set(partner, row);
393
+ }
394
+ return rows;
395
+ }
396
+ function isBuiltinPref(leaf) {
397
+ return Object.hasOwn(PREF_KINDS, leaf.kind);
398
+ }
399
+
400
+ // src/holding.ts
401
+ var UNSEEN = /* @__PURE__ */ Symbol("unseen");
402
+ var UNSEEN_LEAF = { kind: "unseen", name: "", description: "", default: void 0 };
403
+ var under = (at, key) => at === "" ? key : `${at}.${key}`;
404
+ function amongFields(fields, path, at) {
405
+ const exact = fields.find(([key]) => key === path);
406
+ if (exact) return [{ leaf: exact[1], ...at === void 0 ? {} : { within: at } }];
407
+ const outer = fields.find(([key]) => path.startsWith(`${key}.`));
408
+ return outer && insideLeaf(outer[1], path.slice(outer[0].length + 1), under(at ?? "", outer[0]));
409
+ }
410
+ function insideLeaf(leaf, rest, at) {
411
+ if (leaf.kind === "object") return amongFields(prefSectionLeaves(leaf.children), rest, at);
412
+ if (leaf.kind === "union") {
413
+ const { tag, variants } = leaf;
414
+ if (rest === tag) {
415
+ const options = Object.keys(variants).map((value) => ({ value, label: variants[value].name }));
416
+ return [{ leaf: { kind: "enum", name: tag, description: "", default: void 0, options }, within: at }];
417
+ }
418
+ const out = [];
419
+ for (const [key, variant] of Object.entries(variants)) {
420
+ const found = amongFields(prefSectionLeaves(variant.children), rest, at);
421
+ const held = found === UNSEEN ? [{ leaf: UNSEEN_LEAF }] : found ?? [];
422
+ for (const each of held) out.push({ ...each, variants: [{ at, tag, key }, ...each.variants ?? []] });
423
+ }
424
+ if (out.length && out.every((each) => each.leaf === UNSEEN_LEAF)) return UNSEEN;
425
+ return out.length ? out : void 0;
426
+ }
427
+ return !isBuiltinPref(leaf) || leaf.kind === "list" || leaf.kind === "map" || leaf.kind === "paint" ? UNSEEN : void 0;
428
+ }
429
+ function prefLeavesHolding(schema, path) {
430
+ if (isPrefSection(schema)) return amongFields(prefSectionLeaves(schema.members), path, void 0);
431
+ const steps = path.split(".");
432
+ let node = schema;
433
+ for (let i = 0; i < steps.length; i++) {
434
+ if (isPrefLeaf(node)) return insideLeaf(node, steps.slice(i).join("."), steps.slice(0, i).join("."));
435
+ if (!Object.hasOwn(node.children, steps[i])) return void 0;
436
+ node = node.children[steps[i]];
437
+ }
438
+ return isPrefLeaf(node) ? [{ leaf: node }] : void 0;
439
+ }
440
+ function prefLeavesHoldingIn(item, path) {
441
+ return path === "" ? [{ leaf: item }] : insideLeaf(item, path, "");
442
+ }
443
+ function prefSchemaHolding(schema, path, over) {
444
+ return over === void 0 || prefLeavesHolding(schema, path) !== void 0 ? schema : over;
445
+ }
446
+ var PREF_ELSE = "$else";
447
+ var named = (overrides) => Array.isArray(overrides);
448
+ function prefOverrideOptions(overrides) {
449
+ return named(overrides) ? overrides : Object.keys(overrides);
450
+ }
451
+ var hasRest = (scope) => Object.hasOwn(scope, PREF_ELSE);
452
+ var restOr = (scope, absent) => hasRest(scope) ? scope[PREF_ELSE] : absent;
453
+ var withRest = (scope, rest) => ({ ...scope, [PREF_ELSE]: rest });
454
+ function isPrefScope(value) {
455
+ if (!isPlainObject(value)) return false;
456
+ const keys = Object.keys(value).length - (Object.hasOwn(value, PREF_ELSE) ? 1 : 0);
457
+ return keys === 2 && typeof value.$by === "string" && isPlainObject(value.$in);
458
+ }
459
+ function upgradeScope(value) {
460
+ if (isPrefScope(value)) return mapMembers(value, upgradeScope);
461
+ const old = isPlainObject(value) && Object.keys(value).length === 2 && typeof value.$by === "string" && isPlainObject(value.$of);
462
+ return old ? mapMembers({ $by: value.$by, $in: value.$of }, upgradeScope) : value;
463
+ }
464
+ function selectionWeights(selection) {
465
+ return Object.fromEntries(Object.entries(selection).map(([path, option]) => [path, { [option]: 1 }]));
466
+ }
467
+ var shares = (scope, option, overridesOf) => {
468
+ const overrides = overridesOf?.(scope.$by);
469
+ return overrides !== void 0 && !prefOverrideOptions(overrides).includes(option);
470
+ };
471
+ function prefScopeOptionValue(scope, option, absent, overridesOf) {
472
+ if (option === void 0 || shares(scope, option, overridesOf)) return restOr(scope, absent);
473
+ if (Object.hasOwn(scope.$in, option)) return scope.$in[option];
474
+ const overrides = overridesOf?.(scope.$by);
475
+ return overrides && !named(overrides) && Object.hasOwn(overrides, option) ? overrides[option] : restOr(scope, absent);
476
+ }
477
+ function weighScope(stored, weights, absent, overridesOf) {
478
+ const out = [];
479
+ const walk = (value, weight) => {
480
+ if (!isPrefScope(value)) {
481
+ out.push({ value, weight });
482
+ return;
483
+ }
484
+ const given = getOwn(weights, value.$by);
485
+ if (given === void 0) {
486
+ if (hasRest(value)) walk(value[PREF_ELSE], weight);
487
+ return;
488
+ }
489
+ for (const [option, share] of Object.entries(given)) {
490
+ if (share > 0) walk(prefScopeOptionValue(value, option, absent, overridesOf), weight * share);
491
+ }
492
+ };
493
+ walk(stored, 1);
494
+ return out;
495
+ }
496
+ function prefScopeValue(stored, selection, absent, overridesOf) {
497
+ let value = stored;
498
+ while (isPrefScope(value)) value = prefScopeOptionValue(value, getOwn(selection, value.$by), absent, overridesOf);
499
+ return value;
500
+ }
501
+ function dropUnlisted(stored, overridesOf) {
502
+ if (!overridesOf || !isPrefScope(stored)) return stored;
503
+ const overrides = overridesOf(stored.$by);
504
+ const list = overrides && prefOverrideOptions(overrides);
505
+ const stale = list ? Object.keys(stored.$in).filter((option) => !list.includes(option)) : [];
506
+ const kept = stale.length ? { ...stored, $in: Object.fromEntries(Object.entries(stored.$in).filter(([option]) => list.includes(option))) } : stored;
507
+ return mapMembers(kept, (inner) => dropUnlisted(inner, overridesOf));
508
+ }
509
+ function writeDeclared(stored, selectors, under2, now, next, absent, overridesOf, held = true) {
510
+ const [by, ...inner] = selectors;
511
+ if (by === void 0) return next;
512
+ let scope;
513
+ if (isPrefScope(stored) && stored.$by === by) scope = stored;
514
+ else {
515
+ const shared = isPrefScope(stored) ? prefScopeValue(stored, now, absent, overridesOf) : stored;
516
+ scope = held ? { $by: by, $in: {}, [PREF_ELSE]: shared } : { $by: by, $in: {} };
517
+ }
518
+ const option = getOwn(under2, by);
519
+ if (option === void 0 || shares(scope, option, overridesOf)) {
520
+ return withRest(scope, writeDeclared(scope[PREF_ELSE], inner, under2, now, next, absent, overridesOf, hasRest(scope)));
521
+ }
522
+ const own = getOwn(scope.$in, option);
523
+ const kept = isPrefScope(own) && own.$by === inner[0] ? writeDeclared(own, inner, under2, now, next, absent, overridesOf) : next;
524
+ return { ...scope, $in: { ...scope.$in, [option]: kept } };
525
+ }
526
+ function mapMembers(scope, fn) {
527
+ let out;
528
+ for (const [option, value] of Object.entries(scope.$in)) {
529
+ const next = fn(value);
530
+ if (next !== value) setOwn(out ??= { ...scope.$in }, option, next);
531
+ }
532
+ const mapped = out ? { ...scope, $in: out } : scope;
533
+ if (!hasRest(scope)) return mapped;
534
+ const rest = fn(scope[PREF_ELSE]);
535
+ return rest === scope[PREF_ELSE] ? mapped : withRest(mapped, rest);
536
+ }
537
+ function mapScopeValues(stored, fn) {
538
+ return isPrefScope(stored) ? mapMembers(stored, (inner) => mapScopeValues(inner, fn)) : fn(stored);
539
+ }
540
+ function prefStoredSelectors(stored) {
541
+ const out = /* @__PURE__ */ new Set();
542
+ const walk = (value) => {
543
+ if (!isPrefScope(value)) return;
544
+ out.add(value.$by);
545
+ for (const inner of Object.values(value.$in)) walk(inner);
546
+ if (hasRest(value)) walk(value[PREF_ELSE]);
547
+ };
548
+ walk(stored);
549
+ return out;
550
+ }
551
+ function repairScope(stored, allowed, optionsOf, repairMember, overridesOf) {
552
+ const BAD = /* @__PURE__ */ Symbol("bad");
553
+ const walk = (value, met) => {
554
+ if (!isPrefScope(value)) return repairMember(value);
555
+ const options = allowed.includes(value.$by) && !met.includes(value.$by) ? optionsOf(value.$by) : void 0;
556
+ if (options === void 0) return BAD;
557
+ const inner = [...met, value.$by];
558
+ const kept = [];
559
+ for (const option of options ?? Object.keys(value.$in)) {
560
+ if (!Object.hasOwn(value.$in, option) || shares(value, option, overridesOf)) continue;
561
+ const read2 = walk(value.$in[option], inner);
562
+ if (read2 === BAD) return BAD;
563
+ kept.push([option, read2]);
564
+ }
565
+ const out = { $by: value.$by, $in: Object.fromEntries(kept) };
566
+ if (!hasRest(value)) return out;
567
+ const rest = walk(value[PREF_ELSE], inner);
568
+ return rest === BAD ? BAD : withRest(out, rest);
569
+ };
570
+ const read = walk(stored, []);
571
+ return read === BAD ? void 0 : read;
572
+ }
573
+ function prefScopesOf(pref) {
574
+ return pref.scopes ?? [];
575
+ }
576
+ function prefOverridesOf(pref, selector) {
577
+ return pref.scopes?.find((scope) => scope.selector === selector)?.overrides;
578
+ }
579
+ function declaredSelectorsOf(pref) {
580
+ return pref.scopes?.length ? pref.scopes.map((scope) => scope.selector) : void 0;
581
+ }
582
+ var prefOverridesLookup = createPart(
583
+ (pref) => pref.scopes?.some((scope) => scope.overrides !== void 0) ? (selector) => prefOverridesOf(pref, selector) : void 0
584
+ );
585
+ var withElse = (by, shared) => ({ $by: by, $in: {}, [PREF_ELSE]: shared });
586
+ function prefScopedDefault(pref) {
587
+ if (!pref.scopes?.length) return void 0;
588
+ if (pref.defaultScope !== void 0) return pref.defaultScope;
589
+ let inner;
590
+ for (const scope of [...pref.scopes].reverse()) inner = inner === void 0 ? { $by: scope.selector, $in: {} } : withElse(scope.selector, inner);
591
+ return inner;
592
+ }
593
+
594
+ export { ORDERINGS, PREF_ELSE, PREF_KINDS, PX_UNIT, UNSEEN, VERSION_RECORD, assignPrefValueAt, declaredSelectorsOf, dropUnlisted, filterPrefSubtree, flattenPrefValues, formOf, isBuiltinPref, isPrefCondition, isPrefLeaf, isPrefLinearUnit, isPrefScope, isPrefSection, mapPrefConditionPaths, mapScopeValues, nestPrefValues, partsOf, prefAliasName, prefAliasTarget, prefAliasedLeaf, prefConditionHolds, prefConditionInGroup, prefConditionPaths, prefConditionReadsItem, prefDisplayBounds, prefHoldsValue, prefIsEnabled, prefIsShown, prefLeafAt, prefLeaves, prefLeavesHolding, prefLeavesHoldingIn, prefOverrideOptions, prefOverridesLookup, prefOverridesOf, prefPairRows, prefSchemaHolding, prefScopeOptionValue, prefScopeValue, prefScopedDefault, prefScopesOf, prefSectionLeaves, prefStoredSelectors, prefUnit, prefUnitAccepts, prefValueAt, repairScope, resolvePrefUnit, selectionWeights, setOwnPrefValue, upgradeScope, visiblePrefSubtree, weighScope, writeDeclared };
595
+ //# sourceMappingURL=chunk-DVHFV6JS.js.map
596
+ //# sourceMappingURL=chunk-DVHFV6JS.js.map