@n-uf/placement 26.11.24 → 26.11.26

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 (51) hide show
  1. package/CHANGELOG.md +134 -0
  2. package/README.md +143 -41
  3. package/dist/{chunk-LPMZ2FFR.js → chunk-3P6VOXA6.js} +2 -2
  4. package/dist/chunk-3P6VOXA6.js.map +1 -0
  5. package/dist/{chunk-CJA6LRHN.js → chunk-7ASCUAGV.js} +5 -7
  6. package/dist/chunk-7ASCUAGV.js.map +1 -0
  7. package/dist/chunk-CIGRI35P.js +1999 -0
  8. package/dist/chunk-CIGRI35P.js.map +1 -0
  9. package/dist/{chunk-RL3QB5SM.js → chunk-IHU6IOZH.js} +135 -19
  10. package/dist/chunk-IHU6IOZH.js.map +1 -0
  11. package/dist/{chunk-2H3CSPEH.js → chunk-OYGOJWAX.js} +2 -2
  12. package/dist/chunk-WM5FJ3IS.js +47 -0
  13. package/dist/chunk-WM5FJ3IS.js.map +1 -0
  14. package/dist/{constructions-VDK_mwmz.d.ts → constructions-DSEjvWMP.d.ts} +1 -1
  15. package/dist/desk.d.ts +3 -3
  16. package/dist/desk.js +3 -3
  17. package/dist/dock.d.ts +3 -3
  18. package/dist/dock.js +2 -2
  19. package/dist/float-fit-DRSOU_2y.d.ts +37 -0
  20. package/dist/geometry-CkZ6Fcfb.d.ts +48 -0
  21. package/dist/index.d.ts +3 -2
  22. package/dist/index.js +21 -3
  23. package/dist/index.js.map +1 -1
  24. package/dist/{model-DPTu2EtG.d.ts → model-Bd0oELzF.d.ts} +9 -1
  25. package/dist/{targets-DwwIWPpN.d.ts → model-Dz4R5ydE.d.ts} +36 -116
  26. package/dist/panels.d.ts +5 -4
  27. package/dist/panels.js +6 -5
  28. package/dist/scene-cases.d.ts +9 -5
  29. package/dist/scene-cases.js +18 -17
  30. package/dist/scene-cases.js.map +1 -1
  31. package/dist/scene.d.ts +69 -37
  32. package/dist/scene.js +6 -3
  33. package/dist/stack.d.ts +53 -33
  34. package/dist/stack.js +4 -98
  35. package/dist/stack.js.map +1 -1
  36. package/dist/{target-D3PiLg44.d.ts → target-CEVCfJd-.d.ts} +4 -2
  37. package/dist/{targets-B6eLC-52.d.ts → targets-BPG7Kf02.d.ts} +4 -6
  38. package/dist/targets.d.ts +4 -4
  39. package/dist/targets.js +6 -7
  40. package/dist/targets.js.map +1 -1
  41. package/dist/toolbar.d.ts +1 -1
  42. package/package.json +1 -1
  43. package/dist/chunk-CJA6LRHN.js.map +0 -1
  44. package/dist/chunk-LPMZ2FFR.js.map +0 -1
  45. package/dist/chunk-QMTNB2C3.js +0 -17
  46. package/dist/chunk-QMTNB2C3.js.map +0 -1
  47. package/dist/chunk-RL3QB5SM.js.map +0 -1
  48. package/dist/chunk-XUC5BU4N.js +0 -1669
  49. package/dist/chunk-XUC5BU4N.js.map +0 -1
  50. package/dist/geometry-mL-GTRti.d.ts +0 -28
  51. /package/dist/{chunk-2H3CSPEH.js.map → chunk-OYGOJWAX.js.map} +0 -0
@@ -0,0 +1,1999 @@
1
+ import {
2
+ fitFloat,
3
+ fitStack,
4
+ formCrossMin,
5
+ formMain,
6
+ formSetsCross,
7
+ wrapLines
8
+ } from "./chunk-IHU6IOZH.js";
9
+ import {
10
+ COLUMN_REACH,
11
+ EPS,
12
+ PLANE,
13
+ RAIL_EDGE,
14
+ area,
15
+ clampInto,
16
+ overlaps,
17
+ rect,
18
+ subtract
19
+ } from "./chunk-WM5FJ3IS.js";
20
+
21
+ // src/scene/model.ts
22
+ var SIDES = ["top", "right", "bottom", "left"];
23
+ var ZONES = ["start", "center", "end"];
24
+ var DEFAULT_STRIP = 24;
25
+ var DEFAULT_HIT_BAND = 24;
26
+ var DEFAULT_FLOAT_SIZE = { width: 280, height: 360 };
27
+
28
+ // src/scene/helpers.ts
29
+ var zoneTakes = (edge, zone, kind) => edge.accepts.includes(kind) && (edge.zoneAccepts === void 0 || !!edge.zoneAccepts[zone]?.includes(kind));
30
+ var zonesTaking = (edge, kind) => ZONES.filter((z) => zoneTakes(edge, z, kind));
31
+ var axisOf = (side) => side === "left" || side === "right" ? "column" : "row";
32
+ var zoneOf = (item) => item?.align === "center" || item?.align === "end" ? item.align : "start";
33
+ var formsOf = (item, axis) => item.forms[axis] ?? [];
34
+ function isOutsideSideBar(scene, e) {
35
+ return e.outsideInset === true && isSideBar(scene, e);
36
+ }
37
+ function isSideBar(scene, e) {
38
+ if (e.side !== "left" && e.side !== "right" || e.column === true || e.accepts.length === 0) return false;
39
+ const template = scene.columns?.[e.side];
40
+ const rail = scene.rails?.[e.side];
41
+ if (!template && !rail) return false;
42
+ const panelKinds = [...template?.accepts ?? [], ...rail?.accepts ?? []];
43
+ return !e.accepts.some((k) => panelKinds.includes(k));
44
+ }
45
+ var floatOrientOf = (item) => {
46
+ if (item.floatOrient === "row" || item.floatOrient === "column") return item.floatOrient;
47
+ for (const k of Object.keys(item.forms)) if ((k === "row" || k === "column") && formsOf(item, k).length > 0) return k;
48
+ return "column";
49
+ };
50
+ function floatSizeOf(item, orient2 = floatOrientOf(item)) {
51
+ if (item.floatSize) return item.floatSize;
52
+ const f = formsOf(item, orient2)[0];
53
+ if (!f || f.main.grows) return DEFAULT_FLOAT_SIZE;
54
+ const main = Number.isFinite(f.main.natural) ? Math.max(0, f.main.natural) : 0;
55
+ const cross = Number.isFinite(f.cross.natural) ? Math.max(0, f.cross.natural) : 0;
56
+ if (main <= 0 || cross <= 0) return DEFAULT_FLOAT_SIZE;
57
+ return orient2 === "row" ? { width: main, height: cross } : { width: cross, height: main };
58
+ }
59
+ function pastWrap(forms, from) {
60
+ for (let j = Math.max(0, Math.floor(from)); j < forms.length; j++) if (forms[j].kind !== "wrap" && !forms[j].wrap) return j;
61
+ return void 0;
62
+ }
63
+ function floatShape(item, f) {
64
+ const orient2 = f.orient ?? floatOrientOf(item);
65
+ const forms = formsOf(item, orient2);
66
+ const px2 = (v) => Number.isFinite(v) ? Math.max(0, v) : 0;
67
+ const w = px2(f.rect.width);
68
+ const h = px2(f.rect.height);
69
+ if (forms.length === 0) return { form: 0, width: w, height: h };
70
+ const asked = Math.min(Math.max(0, Math.floor(Number.isFinite(item.from) ? item.from : 0)), forms.length - 1);
71
+ const isWrap = (x) => x.kind === "wrap" || !!x.wrap;
72
+ const skipped = orient2 === "column" ? pastWrap(forms, asked) : asked;
73
+ if (skipped === void 0) return { form: asked, width: w, height: h, none: true };
74
+ const form = skipped;
75
+ const fm = forms[form];
76
+ if (orient2 === "column" && forms.slice(0, form + 1).some(isWrap)) return { form, width: px2(fm.cross.natural), height: px2(fm.main.natural) };
77
+ if (form > 0) return orient2 === "column" ? { form, width: w, height: px2(fm.main.natural) } : { form, width: px2(fm.main.natural), height: h };
78
+ return { form, width: w, height: h };
79
+ }
80
+
81
+ // src/scene/solve/order.ts
82
+ var finite = (x, fallback) => typeof x === "number" && Number.isFinite(x) ? x : fallback;
83
+ var px = (x) => Math.max(0, finite(x, 0));
84
+ var isSide = (e) => e.side === "left" || e.side === "right";
85
+ function cutOrder(edges) {
86
+ const phase = (e) => e.side === "left" || e.side === "right" ? 1 : e.span === "between" ? 2 : 0;
87
+ const sideRank = (e) => e.side === "top" || e.side === "left" ? 0 : 1;
88
+ return edges.map((e, i) => ({ e, i })).sort((a, b) => phase(a.e) - phase(b.e) || sideRank(a.e) - sideRank(b.e) || finite(a.e.ring, 0) - finite(b.e.ring, 0) || a.i - b.i).map((x) => x.e);
89
+ }
90
+ function layKeys(scene) {
91
+ const phase = (e) => e.mode === "overlay" ? 4 : isSide(e) ? 2 : e.span === "between" ? 3 : e.outsideInset === true ? 0 : 1;
92
+ return new Map(cutOrder(scene.edges).map((e, at) => [e.id, { phase: phase(e), side: e.side, at }]));
93
+ }
94
+ function pushes(a, b) {
95
+ if (a.phase === 4 || b.phase === 4) return false;
96
+ if (a.phase !== b.phase) return a.phase < b.phase;
97
+ return a.side === b.side && a.at < b.at;
98
+ }
99
+ var ZONE_RANK = { start: 0, center: 1, end: 2 };
100
+ function zoneSorted(scene, ids) {
101
+ return ids.map((id, i) => ({ id, i, z: ZONE_RANK[zoneOf(scene.items[id])] })).sort((a, b) => a.z - b.z || a.i - b.i).map((x) => x.id);
102
+ }
103
+
104
+ // src/scene/solve/draft.ts
105
+ function createDraft(given, options) {
106
+ const previous = options.previous;
107
+ const opened = /* @__PURE__ */ new Set();
108
+ if (previous) {
109
+ for (const e of given.edges) {
110
+ for (const id of e.items) {
111
+ const it = Object.hasOwn(given.items, id) ? given.items[id] : void 0;
112
+ const was = previous.items[id];
113
+ if (it && !it.closed && !it.fixed && (!was || was.closed)) opened.add(id);
114
+ }
115
+ }
116
+ }
117
+ const stayedOpen = (e) => e.items.some((id) => !opened.has(id) && Object.hasOwn(given.items, id) && !given.items[id].closed && !given.items[id].fixed);
118
+ const newest = Object.values(given.items).reduce((m, it) => Math.max(m, finite(it.used, 0)), 0) + 1;
119
+ const byAge = [...opened].filter((id) => given.edges.some((e) => e.items.includes(id) && stayedOpen(e))).sort((a, b) => finite(given.items[a].used, 0) - finite(given.items[b].used, 0) || (a < b ? -1 : 1));
120
+ const scene = byAge.length === 0 ? given : { ...given, items: { ...given.items, ...Object.fromEntries(byAge.map((id, k) => [id, { ...given.items[id], used: newest + k }])) } };
121
+ const fresh = new Set(options.fresh ?? []);
122
+ const prevEdge = (edge) => !previous || fresh.has(edge.id) || edge.items.some((id) => fresh.has(id)) ? void 0 : previous.edges[edge.id];
123
+ const notes = [];
124
+ const order = cutOrder(scene.edges);
125
+ const claimed = /* @__PURE__ */ new Set();
126
+ const claim = (id) => {
127
+ if (!Object.hasOwn(scene.items, id)) {
128
+ notes.push({ code: "unknown-item", id });
129
+ return false;
130
+ }
131
+ if (claimed.has(id)) {
132
+ notes.push({ code: "twice", id });
133
+ return false;
134
+ }
135
+ claimed.add(id);
136
+ return true;
137
+ };
138
+ const owned = /* @__PURE__ */ new Map();
139
+ for (const e of order) owned.set(e, e.items.filter(claim));
140
+ return { scene, previous, opened, fresh, order, owned, claim, prevEdge, notes, items: {}, edges: {}, rails: {}, railPlaces: {} };
141
+ }
142
+ function shapeOf(f, r, axis, cross) {
143
+ if (!f) return {};
144
+ if (f.wrap) {
145
+ const lead = Math.max(0, finite(f.wrap.lead ?? 0, 0));
146
+ const row = Math.max(0, finite(f.wrap.row, 0));
147
+ return { rows: wrapLines(f.wrap, cross).lines, grip: rect(r.x, r.y, lead, Math.min(row, r.height)) };
148
+ }
149
+ const g = typeof f.grip === "number" && Number.isFinite(f.grip) && f.grip > 0 ? f.grip : 0;
150
+ if (!g) return {};
151
+ return { grip: axis === "column" ? rect(r.x, r.y, r.width, Math.min(g, r.height)) : rect(r.x, r.y, Math.min(g, r.width), r.height) };
152
+ }
153
+ function zoneStarts(scene, fit, input) {
154
+ const column = fit.axis === "column";
155
+ const at = (r) => column ? r.y : r.x;
156
+ const out = /* @__PURE__ */ new Map();
157
+ const lengths = /* @__PURE__ */ new Map();
158
+ const drawn = fit.items.filter((p) => p.kind !== "menu");
159
+ for (const p of drawn) {
160
+ out.set(p.id, at(p.rect));
161
+ lengths.set(p.id, column ? p.rect.height : p.rect.width);
162
+ }
163
+ const len = (p) => lengths.get(p.id);
164
+ const gap = px(input.gap);
165
+ const start = px(input.pad?.start);
166
+ const end = px(input.pad?.end);
167
+ const menu = fit.menu ? (column ? fit.menu.rect.height : fit.menu.rect.width) + (drawn.length > 0 ? gap : 0) : 0;
168
+ const zone = (z) => drawn.filter((p) => zoneOf(scene.items[p.id]) === z);
169
+ const s = zone("start");
170
+ const c = zone("center");
171
+ const e = zone("end");
172
+ const title = column ? void 0 : s.find((p) => {
173
+ const it = scene.items[p.id];
174
+ const f = formsOf(it, "row")[p.form];
175
+ return it.fixed && f?.main.grows && len(p) > formMain(f, "natural", fit.cross) + EPS;
176
+ });
177
+ if (title) lengths.set(title.id, formMain(formsOf(scene.items[title.id], "row")[title.form], "natural", fit.cross));
178
+ const used = drawn.reduce((sum, p) => sum + len(p), 0) + Math.max(0, drawn.length - 1) * gap;
179
+ const spare = fit.length - start - end - menu - used;
180
+ if (spare <= EPS) return { starts: out, lengths };
181
+ const total = (ps) => ps.reduce((sum, p) => sum + len(p), 0) + Math.max(0, ps.length - 1) * gap;
182
+ const lay = (ps, from) => {
183
+ let x = from;
184
+ for (const p of ps) {
185
+ out.set(p.id, x);
186
+ x += len(p) + gap;
187
+ }
188
+ };
189
+ lay(s, start);
190
+ const endStart = fit.length - end - menu - total(e);
191
+ lay(e, endStart);
192
+ const lo = start + total(s) + (s.length > 0 ? gap : 0);
193
+ const hi = endStart - (e.length > 0 ? gap : 0);
194
+ const mid = (fit.length - total(c)) / 2;
195
+ const clear = mid >= lo - EPS && mid + total(c) <= hi + EPS;
196
+ lay(c, title && !clear ? Math.max(lo, (lo + hi - total(c)) / 2) : Math.max(lo, Math.min(mid, hi - total(c))));
197
+ if (title && s[s.length - 1] === title) {
198
+ const next = [...c, ...e].map((p) => out.get(p.id));
199
+ const until = next.length ? Math.min(...next) - gap : fit.length - end - menu;
200
+ lengths.set(title.id, Math.max(len(title), until - out.get(title.id)));
201
+ }
202
+ return { starts: out, lengths };
203
+ }
204
+ function emit(d, edge, r, solved, extra = {}) {
205
+ const { scene, notes, items, edges } = d;
206
+ const column = axisOf(edge.side) === "column";
207
+ const { stack } = solved;
208
+ const thick = column ? r.width : r.height;
209
+ const { starts, lengths } = zoneStarts(scene, stack, solved.input);
210
+ const toStage = (l, id) => {
211
+ const s = id !== void 0 ? starts.get(id) : void 0;
212
+ const n = id !== void 0 ? lengths.get(id) : void 0;
213
+ const x = column ? l.x : s ?? l.x;
214
+ const y = column ? s ?? l.y : l.y;
215
+ return rect(r.x + x, r.y + y, column ? l.width : n ?? l.width, column ? n ?? l.height : l.height);
216
+ };
217
+ const tabs = new Set(solved.tabs);
218
+ for (const id of solved.leftOut ?? []) notes.push({ code: "no-vertical-form", edge: edge.id, id });
219
+ for (const id of solved.leftOutIcons ?? []) notes.push({ code: "no-icons-form", edge: edge.id, id });
220
+ for (const p of stack.items) {
221
+ const it = scene.items[p.id];
222
+ const inMenu = p.kind === "menu";
223
+ const drawnRect = toStage(p.rect, inMenu ? void 0 : p.id);
224
+ const f = formsOf(it, column ? "column" : "row")[p.form];
225
+ const shape = inMenu ? {} : shapeOf(f, drawnRect, column ? "column" : "row", stack.cross);
226
+ items[p.id] = {
227
+ ...shape,
228
+ hidden: inMenu ? [] : f?.hides ?? [],
229
+ rect: drawnRect,
230
+ kind: it.kind,
231
+ form: p.form,
232
+ formKind: p.kind,
233
+ edge: edge.id,
234
+ zone: zoneOf(it),
235
+ ...inMenu ? { inMenu: true } : {},
236
+ ...tabs.has(p.id) ? { tab: true } : {},
237
+ ...solved.closedTabs.has(p.id) ? { closed: true } : {},
238
+ ...it.fixed ? { fixed: true } : {}
239
+ };
240
+ }
241
+ for (const n of stack.notes) notes.push({ edge: edge.id, ...n });
242
+ edges[edge.id] = {
243
+ side: edge.side,
244
+ ring: finite(edge.ring, 0),
245
+ rect: r,
246
+ cross: thick,
247
+ menu: stack.menu ? { rect: toStage(stack.menu.rect), items: stack.menu.items } : null,
248
+ tabs: solved.tabs,
249
+ stack,
250
+ free: solved.free,
251
+ ...extra
252
+ };
253
+ }
254
+
255
+ // src/scene/solve/edge.ts
256
+ var stackItemOf = (item, forms, from) => ({
257
+ id: item.id,
258
+ forms,
259
+ used: finite(item.used, 0),
260
+ ...item.kept ? { kept: true } : {},
261
+ ...from !== void 0 ? { from } : item.from !== void 0 ? { from: item.from } : {},
262
+ ...item.fixed ? { fixed: true } : {}
263
+ });
264
+ function hasSetter(fit, forms) {
265
+ return fit.items.some((p) => p.kind !== "menu" && formSetsCross(forms(p.id)[p.form]));
266
+ }
267
+ var TRAY_RANK = 100;
268
+ function compactEdge(f) {
269
+ const { scene, formsById } = f;
270
+ const iconsAt = (id) => {
271
+ const forms = formsById(id);
272
+ for (let j = Math.max(0, Math.floor(finite(scene.items[id].from, 0))); j < forms.length; j++) if (forms[j].kind === "icons") return j;
273
+ return forms.findIndex((x) => x.kind === "icons");
274
+ };
275
+ const shown = f.ordered.filter((id) => !scene.items[id].closed && iconsAt(id) >= 0);
276
+ const leftOutIcons = f.ordered.filter((id) => !scene.items[id].closed && iconsAt(id) < 0);
277
+ const compactInput = { ...f.base, ownCross: false, items: shown.map((id) => stackItemOf(scene.items[id], formsById(id), iconsAt(id))) };
278
+ const stack = fitStack(compactInput, f.previous ? { previous: f.previous.free } : {});
279
+ return { stack, free: stack, tabs: [], closedTabs: /* @__PURE__ */ new Set(), input: compactInput, leftOutIcons };
280
+ }
281
+ function railEdge(f, free, asStackItem) {
282
+ const { scene, edge, formsById } = f;
283
+ const railIndex = (item, forms) => {
284
+ if (item.fixed) return -1;
285
+ for (let j = Math.max(0, Math.floor(finite(item.from, 0))); j < forms.length; j++) if (forms[j].kind === "rail") return j;
286
+ return -1;
287
+ };
288
+ const wrapOnly = (id) => railIndex(scene.items[id], formsById(id)) < 0 && pastWrap(formsById(id), finite(scene.items[id].from, 0)) === void 0;
289
+ const shown = f.ordered.filter((id) => {
290
+ const it = scene.items[id];
291
+ if (!it.closed && wrapOnly(id)) return false;
292
+ return !it.closed || edge.railShowsClosed === true && railIndex(it, formsById(id)) >= 0;
293
+ });
294
+ const leftOut = f.ordered.filter((id) => !scene.items[id].closed && wrapOnly(id));
295
+ const anyRail = shown.some((id) => railIndex(scene.items[id], formsById(id)) >= 0);
296
+ if (!anyRail) return null;
297
+ const railInput = {
298
+ ...f.base,
299
+ items: shown.map((id) => {
300
+ const it = scene.items[id];
301
+ const j = railIndex(it, formsById(id));
302
+ const forms = asStackItem(id).forms;
303
+ return stackItemOf(it, forms, j >= 0 ? j : pastWrap(forms, finite(it.from, 0)));
304
+ })
305
+ };
306
+ const stack = fitStack(railInput, f.previous ? { previous: f.previous.stack } : {});
307
+ const tabs = stack.items.filter((p) => p.kind === "rail").map((p) => p.id);
308
+ const closedTabs = new Set(shown.filter((id) => scene.items[id].closed));
309
+ return { stack, free, tabs, closedTabs, input: railInput, leftOut };
310
+ }
311
+ function solveEdge(scene, edge, ids, length, room, previous) {
312
+ const axis = axisOf(edge.side);
313
+ const column = axis === "column";
314
+ const ordered = zoneSorted(scene, ids);
315
+ const priorityOf = (id) => finite(scene.items[id].priority ?? edge.priority ?? 0, 0);
316
+ const formsById = (id) => {
317
+ const forms = formsOf(scene.items[id], axis);
318
+ const p = priorityOf(id);
319
+ return p === 0 ? forms : forms.map((f) => ({ ...f, rank: finite(f.rank ?? 0, 0) + p }));
320
+ };
321
+ const maxOf = Number.isNaN(edge.cross.max) ? Infinity : edge.cross.max;
322
+ const cross = { min: Math.min(px(edge.cross.min), room), max: Math.min(maxOf, room), rail: Math.min(px(edge.cross.rail ?? 30), room) };
323
+ const base = {
324
+ axis,
325
+ length,
326
+ cross,
327
+ ...edge.menu ? { menu: edge.menu } : {},
328
+ ...edge.gap !== void 0 ? { gap: edge.gap } : {},
329
+ ...edge.pad ? { pad: edge.pad } : {},
330
+ ...column ? { ownCross: true } : {}
331
+ };
332
+ const frame = { scene, edge, column, ordered, formsById, base, previous };
333
+ if (edge.compact) return compactEdge(frame);
334
+ const open2 = ordered.filter((id) => !scene.items[id].closed);
335
+ const asStackItem = (id) => {
336
+ const it = scene.items[id];
337
+ const forms = formsById(id);
338
+ if (!column || !edge.zoneAccepts?.end || zoneOf(it) !== "end") return stackItemOf(it, forms);
339
+ const tray = (f) => f.kind === "wrap" || f.kind === "icons" || !!f.wrap;
340
+ let first = -1;
341
+ for (let j = Math.max(0, Math.floor(finite(it.from, 0))); j < forms.length && first < 0; j++) if (tray(forms[j])) first = j;
342
+ let last = -1;
343
+ forms.forEach((f, j) => {
344
+ if (tray(f)) last = j;
345
+ });
346
+ if (first < 0) return stackItemOf(it, forms);
347
+ return stackItemOf(it, forms.slice(0, last + 1).map((f) => ({ ...f, rank: finite(f.rank ?? 0, 0) + TRAY_RANK })), first);
348
+ };
349
+ const input = { ...base, items: open2.map(asStackItem) };
350
+ const free = fitStack(input, previous ? { previous: previous.free } : {});
351
+ const none = /* @__PURE__ */ new Set();
352
+ if (column && !hasSetter(free, formsById)) {
353
+ const rail = railEdge(frame, free, asStackItem);
354
+ if (rail) return rail;
355
+ }
356
+ const size = edge.size;
357
+ if (typeof size === "number" && Number.isFinite(size) && hasSetter(free, formsById)) {
358
+ const setters = free.items.filter((p) => p.kind !== "menu" && formSetsCross(formsById(p.id)[p.form]));
359
+ const lo = Math.max(px(edge.cross.min), ...setters.map((p) => formCrossMin(formsById(p.id)[p.form])));
360
+ const s = Math.min(Math.max(size, lo), Math.max(lo, maxOf), room);
361
+ if (Math.abs(s - free.cross) > EPS) {
362
+ const sized = { ...input, cross: { ...cross, min: s, max: s } };
363
+ const stack = fitStack(sized, previous ? { previous: previous.stack } : {});
364
+ return { stack, free, tabs: [], closedTabs: none, input: sized };
365
+ }
366
+ }
367
+ return { stack: free, free, tabs: [], closedTabs: none, input };
368
+ }
369
+
370
+ // src/scene/solve/bars.ts
371
+ function cutEdge(d, edge, area2, cut) {
372
+ const column = axisOf(edge.side) === "column";
373
+ const length = column ? area2.height : area2.width;
374
+ const room = Math.max(0, column ? area2.width : area2.height);
375
+ const solved = solveEdge(d.scene, edge, d.owned.get(edge), length, room, d.prevEdge(edge));
376
+ const thick = solved.stack.items.length === 0 ? 0 : Math.min(solved.stack.cross, room);
377
+ const r = edge.side === "top" ? rect(area2.x, area2.y, area2.width, thick) : edge.side === "bottom" ? rect(area2.x, area2.y + area2.height - thick, area2.width, thick) : edge.side === "left" ? rect(area2.x, area2.y, thick, area2.height) : rect(area2.x + area2.width - thick, area2.y, thick, area2.height);
378
+ emit(d, edge, r, solved);
379
+ if (!cut) return area2;
380
+ return edge.side === "top" ? rect(area2.x, area2.y + thick, area2.width, area2.height - thick) : edge.side === "bottom" ? rect(area2.x, area2.y, area2.width, area2.height - thick) : edge.side === "left" ? rect(area2.x + thick, area2.y, area2.width - thick, area2.height) : rect(area2.x, area2.y, area2.width - thick, area2.height);
381
+ }
382
+
383
+ // src/scene/solve/floats.ts
384
+ function anchored(content, w, h, anchor) {
385
+ const left = content.x;
386
+ const right = content.x + content.width - w;
387
+ const midX = content.x + (content.width - w) / 2;
388
+ const top = content.y;
389
+ const bottom = content.y + content.height - h;
390
+ const midY = content.y + (content.height - h) / 2;
391
+ const at = {
392
+ "content-top-left": [left, top],
393
+ "content-top": [midX, top],
394
+ "content-top-right": [right, top],
395
+ "content-right": [right, midY],
396
+ "content-bottom-right": [right, bottom],
397
+ "content-bottom": [midX, bottom],
398
+ "content-bottom-left": [left, bottom],
399
+ "content-left": [left, midY]
400
+ };
401
+ const [x, y] = at[anchor] ?? at["content-top-right"];
402
+ return rect(x, y, w, h);
403
+ }
404
+ function layFloats(d, stage, content, inset) {
405
+ const { scene, notes, items, edges } = d;
406
+ const avoid = [];
407
+ for (const l of Object.values(items)) avoid.push(l.rect);
408
+ for (const e of Object.values(edges)) {
409
+ if (axisOf(e.side) === "row" && e.cross > 0) avoid.push(e.rect);
410
+ if (e.menu) avoid.push(e.menu.rect);
411
+ }
412
+ for (const c of scene.chrome ?? []) avoid.push(c);
413
+ const floatRegion = rect(stage.x + px(inset.left), stage.y, Math.max(0, stage.width - px(inset.left) - px(inset.right)), stage.height);
414
+ const floats = [];
415
+ for (const f of scene.floats) {
416
+ if (!d.claim(f.id)) continue;
417
+ const it = scene.items[f.id];
418
+ if (it.closed) continue;
419
+ const shape = floatShape(it, f);
420
+ if (shape.none) {
421
+ notes.push({ code: "no-vertical-form", id: f.id });
422
+ continue;
423
+ }
424
+ const from = shape.form;
425
+ const fo = f.orient ?? floatOrientOf(it);
426
+ const form = formsOf(it, fo)[from];
427
+ const row = fo === "row";
428
+ const floor = {
429
+ width: it.floatMin ? px(it.floatMin.width) : Math.min(shape.width, px(row ? form?.main.min : form?.cross.min)),
430
+ height: it.floatMin ? px(it.floatMin.height) : Math.min(shape.height, px(row ? form?.cross.min : form?.main.min))
431
+ };
432
+ const w = Math.max(floor.width, Math.min(shape.width, floatRegion.width));
433
+ const folded = form?.kind === "header";
434
+ const h = folded ? shape.height : Math.max(floor.height, Math.min(shape.height, floatRegion.height));
435
+ const want = f.anchor ? anchored(content, w, h, f.anchor) : rect(finite(f.rect.x, stage.x), finite(f.rect.y, stage.y), w, h);
436
+ const fit0 = fitFloat(want, floatRegion, avoid);
437
+ const fit = w > floatRegion.width + EPS ? { ...fit0, rect: rect(floatRegion.x, fit0.rect.y, w, fit0.rect.height) } : fit0;
438
+ if (fit.clamped) notes.push({ code: "float-clamped", id: f.id });
439
+ if (fit.snapped) notes.push({ code: "float-moved", id: f.id });
440
+ if (fit.covers.length > 0) notes.push({ code: "float-covers", id: f.id });
441
+ items[f.id] = { ...shapeOf(form, fit.rect, fo, fit.rect.width), hidden: form?.hides ?? [], rect: fit.rect, kind: it.kind, form: from, formKind: form?.kind ?? "open", floating: true };
442
+ floats.push(f.id);
443
+ }
444
+ return floats;
445
+ }
446
+
447
+ // src/scene/solve/insets.ts
448
+ function insetOf(d) {
449
+ const { scene } = d;
450
+ const PANEL_FORMS = /* @__PURE__ */ new Set(["open", "header", "rail"]);
451
+ const isPanel = (id) => {
452
+ const it = scene.items[id];
453
+ return !it.closed && !it.fixed && PANEL_FORMS.has(formsOf(it, "column")[0]?.kind ?? "");
454
+ };
455
+ const docked = d.order.some((e) => isSide(e) && e.accepts.length > 0 && d.owned.get(e).some(isPanel));
456
+ const inset = scene.insetWhen === "docked" && !docked ? {} : scene.inset ?? {};
457
+ return { inset, docked };
458
+ }
459
+ var insideInset = (outer, inset) => rect(outer.x + px(inset.left), outer.y + px(inset.top), Math.max(0, outer.width - px(inset.left) - px(inset.right)), Math.max(0, outer.height - px(inset.top) - px(inset.bottom)));
460
+ function insetReach(area2, f) {
461
+ const up = area2.y <= f.outer.y + px(f.inset.top) + EPS ? px(f.inset.top) : 0;
462
+ const down = area2.y + area2.height >= f.outer.y + f.outer.height - px(f.inset.bottom) - EPS ? px(f.inset.bottom) : 0;
463
+ return { up, down };
464
+ }
465
+
466
+ // src/scene/solve/notes.ts
467
+ function contentNotes(d, content) {
468
+ const { scene, notes, previous } = d;
469
+ if (scene.contentMin) {
470
+ const minW = px(scene.contentMin.width);
471
+ const minH = px(scene.contentMin.height);
472
+ if (content.width < minW - EPS) notes.push({ code: "content-narrow", width: content.width, min: minW });
473
+ const bars = d.order.filter((e) => e.mode === "overlay" && !isSide(e)).reduce((s, e) => s + (d.edges[e.id]?.cross ?? 0), 0);
474
+ if (content.height - bars < minH - EPS) notes.push({ code: "content-short", height: content.height - bars, min: minH });
475
+ }
476
+ if (previous) {
477
+ const was = previous.content;
478
+ const moved = Math.abs(was.x - content.x) > EPS || Math.abs(was.y - content.y) > EPS || Math.abs(was.width - content.width) > EPS || Math.abs(was.height - content.height) > EPS;
479
+ if (moved) notes.push({ code: "content-changed", from: was, to: content });
480
+ }
481
+ }
482
+
483
+ // src/scene/solve/columns.ts
484
+ function columnsOf(d, sideEdges, area2, tall) {
485
+ const { scene } = d;
486
+ return sideEdges.map((edge) => {
487
+ const side = edge.side;
488
+ const solved = solveEdge(scene, edge, d.owned.get(edge), tall(edge), Math.max(0, area2.width), d.prevEdge(edge));
489
+ const width = solved.stack.items.length === 0 ? 0 : Math.min(solved.stack.cross, area2.width);
490
+ const visible = d.owned.get(edge).filter((id) => !scene.items[id].closed);
491
+ const dyn = (edge.column === true || edge.giveWay === true) && scene.rails?.[side] !== void 0 && visible.length > 0;
492
+ const forms = (id) => formsOf(scene.items[id], "column");
493
+ const setters = solved.free.items.filter((p) => p.kind !== "menu" && formSetsCross(forms(p.id)[p.form]));
494
+ const min = setters.length > 0 ? Math.min(width, Math.max(px(edge.cross.min), ...setters.map((p) => formCrossMin(forms(p.id)[p.form])))) : width;
495
+ return {
496
+ edge,
497
+ side,
498
+ solved,
499
+ width,
500
+ min,
501
+ visible,
502
+ dyn,
503
+ hand: dyn && edge.column === true && visible.every((id) => finite(scene.items[id].from, 0) >= 1),
504
+ kept: visible.some((id) => scene.items[id].kept === true),
505
+ age: Math.max(-Infinity, ...visible.map((id) => finite(scene.items[id].used, 0)))
506
+ };
507
+ });
508
+ }
509
+ function giveWay(d, cols, area2, gutter) {
510
+ const { scene, previous, fresh, opened } = d;
511
+ const ride = scene.handFold === "ride";
512
+ const cmin = scene.contentMin ? px(scene.contentMin.width) : void 0;
513
+ const foldOrder = cols.filter((c) => c.dyn && !c.hand && c.width > 0).sort((a, b) => Number(a.kept) - Number(b.kept) || a.age - b.age || finite(a.edge.ring, 0) - finite(b.edge.ring, 0) || (a.side === b.side ? 0 : a.side === "left" ? -1 : 1));
514
+ const shrunkBefore = (previous?.notes ?? []).flatMap((n) => n.code === "column-shrunk" ? [n.edge] : []);
515
+ const rankOf = (c) => shrunkBefore.includes(c.edge.id) ? shrunkBefore.indexOf(c.edge.id) : Infinity;
516
+ const shrinkOrder = foldOrder.map((c, i) => ({ c, i })).sort((a, b) => rankOf(a.c) === rankOf(b.c) ? a.i - b.i : rankOf(a.c) < rankOf(b.c) ? -1 : 1).map((x) => x.c);
517
+ const railW = (side) => px(scene.rails?.[side]?.width);
518
+ const evaluate = (auto2) => {
519
+ const openOn = (side) => cols.some((c) => c.side === side && c.dyn && c.edge.column === true && !c.hand && !auto2.has(c) && c.width > 0);
520
+ const riding = (c) => ride && c.hand && openOn(c.side);
521
+ const railed = (c) => c.dyn && (auto2.has(c) || c.hand && !riding(c));
522
+ const widths = new Map(cols.filter((c) => !railed(c) && !riding(c)).map((c) => [c, c.width]));
523
+ const railSides = ["left", "right"].filter((s) => cols.some((c) => c.side === s && railed(c)));
524
+ let used = 0;
525
+ for (const [c, w] of widths) used += w > 0 ? w + (c.edge.outsideInset === true ? 0 : gutter) : 0;
526
+ for (const s of railSides) used += railW(s) + gutter;
527
+ let excess = cmin === void 0 ? -Infinity : used + cmin - area2.width;
528
+ for (const c of shrinkOrder) {
529
+ if (excess <= EPS) break;
530
+ const w = widths.get(c);
531
+ if (w === void 0) continue;
532
+ const take = Math.min(excess, w - c.min);
533
+ if (take > 0) {
534
+ widths.set(c, w - take);
535
+ excess -= take;
536
+ }
537
+ }
538
+ return { auto: auto2, widths, excess, railed, riding, shrinkOrder };
539
+ };
540
+ let auto = /* @__PURE__ */ new Set();
541
+ if (cmin !== void 0) {
542
+ const unfold = () => {
543
+ for (const c of [...auto].reverse()) {
544
+ const fewer = new Set([...auto].filter((x) => x !== c));
545
+ if (evaluate(fewer).excess <= EPS) auto = fewer;
546
+ else break;
547
+ }
548
+ };
549
+ if (previous) {
550
+ const byId = new Map(foldOrder.map((c) => [c.edge.id, c]));
551
+ const anew = (c) => fresh.has(c.edge.id) || c.visible.some((id) => fresh.has(id)) || c.visible.some((id) => opened.has(id)) && c.visible.some((id) => !opened.has(id));
552
+ const was = previous.notes.flatMap((n) => (n.code === "column-folded" || n.code === "kept-folded") && byId.has(n.edge) && !anew(byId.get(n.edge)) ? [byId.get(n.edge)] : []);
553
+ auto = new Set(was);
554
+ if (evaluate(auto).excess <= EPS) unfold();
555
+ }
556
+ for (const c of foldOrder) {
557
+ if (evaluate(auto).excess <= EPS) break;
558
+ if (!auto.has(c)) auto = /* @__PURE__ */ new Set([...auto, c]);
559
+ }
560
+ for (let size = -1; size !== auto.size; ) {
561
+ size = auto.size;
562
+ unfold();
563
+ }
564
+ }
565
+ return evaluate(auto);
566
+ }
567
+ function columnRoom(d, cols, area2, f) {
568
+ const { scene } = d;
569
+ const cmin = scene.contentMin ? px(scene.contentMin.width) : void 0;
570
+ const openMin = (ids, least) => Math.max(
571
+ least,
572
+ ...ids.map((id) => {
573
+ const form = formsOf(scene.items[id], "column")[0];
574
+ return form && formSetsCross(form) ? formCrossMin(form) : 0;
575
+ })
576
+ );
577
+ const open2 = cols.filter((c) => c.edge.column === true && c.visible.length > 0).map((c) => c.hand ? openMin(c.visible, px(c.edge.cross.min)) : c.min);
578
+ const templates = ["left", "right"].flatMap((s) => {
579
+ const t = scene.columns?.[s];
580
+ if (!t) return [];
581
+ const takes = Object.values(scene.items).filter((it) => !it.fixed && !it.closed && t.accepts.includes(it.kind));
582
+ return [openMin(takes.map((it) => it.id), px(t.cross.min))];
583
+ });
584
+ const colMins = open2.length > 0 ? open2 : templates;
585
+ if (cmin === void 0 || colMins.length === 0) return;
586
+ const bars = cols.filter((c) => c.width > 0 && !c.dyn && isSideBar(scene, c.edge)).reduce((s, c) => s + c.width + (c.edge.outsideInset === true ? 0 : f.gutter), 0);
587
+ const width = area2.width - bars - (scene.insetWhen === "docked" && !f.docked ? px(scene.inset?.left) + px(scene.inset?.right) : 0);
588
+ const need = cmin + Math.min(...colMins) + f.gutter;
589
+ d.columnRoom = width >= need - EPS;
590
+ if (!d.columnRoom) d.notes.push({ code: "no-column-room", width, need });
591
+ }
592
+ function giveWayNotes(d, g, cols) {
593
+ const { scene, previous } = d;
594
+ const dormant = new Set(cols.filter((c) => c.visible.length === 0 && (c.edge.column === true || c.edge.giveWay === true) && scene.rails?.[c.side] !== void 0).map((c) => c.edge.id));
595
+ const before = previous?.notes ?? [];
596
+ const shrunk = /* @__PURE__ */ new Map();
597
+ for (const c of g.shrinkOrder) {
598
+ const w = g.widths.get(c);
599
+ if (w !== void 0 && w < c.width - EPS) shrunk.set(c.edge.id, { code: "column-shrunk", edge: c.edge.id, from: c.width, to: w });
600
+ }
601
+ const folded = new Map([...g.auto].map((c) => [c.edge.id, { code: c.kept ? "kept-folded" : "column-folded", edge: c.edge.id }]));
602
+ const merge = (now, kept) => {
603
+ const order = [];
604
+ for (const n of before) {
605
+ const edge = kept(n) && "edge" in n ? n.edge : void 0;
606
+ if (edge === void 0) continue;
607
+ if (dormant.has(edge)) order.push(n);
608
+ else if (now.has(edge)) {
609
+ order.push(now.get(edge));
610
+ now.delete(edge);
611
+ }
612
+ }
613
+ d.notes.push(...order, ...now.values());
614
+ };
615
+ d.notes.push(...shrunk.values());
616
+ merge(folded, (n) => n.code === "column-folded" || n.code === "kept-folded");
617
+ }
618
+
619
+ // src/scene/solve/rails.ts
620
+ function riderBars(d, c) {
621
+ const { scene } = d;
622
+ return zoneSorted(scene, c.visible).map((id) => {
623
+ const forms = formsOf(scene.items[id], "column");
624
+ const form = forms.length === 0 ? 0 : Math.min(Math.max(0, Math.floor(finite(scene.items[id].from, 0))), forms.length - 1);
625
+ return { id, form, f: forms[form], h: px(forms[form]?.main.natural) };
626
+ });
627
+ }
628
+ var ridersHeight = (d, riders) => riders.reduce((s, c) => s + riderBars(d, c).reduce((t, b) => t + b.h, 0), 0);
629
+ function layRiders(d, host, riders, top) {
630
+ const { scene, items, edges } = d;
631
+ const hr = edges[host.edge.id].rect;
632
+ let y = top;
633
+ for (const c of riders) {
634
+ const bars = riderBars(d, c);
635
+ const y0 = y;
636
+ for (const b of bars) {
637
+ const at = rect(hr.x, y, hr.width, b.h);
638
+ const it = scene.items[b.id];
639
+ items[b.id] = { rect: at, hidden: b.f?.hides ?? [], kind: it.kind, form: b.form, formKind: b.f?.kind ?? "header", edge: c.edge.id, zone: zoneOf(it), rides: host.edge.id, folded: "hand" };
640
+ y += b.h;
641
+ }
642
+ edges[c.edge.id] = { side: c.edge.side, ring: finite(c.edge.ring, 0), rect: rect(hr.x, y0, hr.width, y - y0), cross: 0, menu: null, tabs: [], stack: c.solved.stack, free: c.solved.free, rides: host.edge.id, folded: "hand" };
643
+ }
644
+ }
645
+ function layRail(d, side, railRect, railed, g) {
646
+ const { scene, items } = d;
647
+ const segments = [];
648
+ let y = railRect.y;
649
+ for (const c of railed) {
650
+ for (const id of zoneSorted(scene, c.visible)) {
651
+ const it = scene.items[id];
652
+ const forms = formsOf(it, "column");
653
+ const ri = forms.findIndex((f) => f.kind === "rail");
654
+ const form = ri >= 0 ? ri : Math.min(Math.max(0, Math.floor(finite(it.from, 0))), Math.max(0, forms.length - 1));
655
+ const h = ri >= 0 ? px(forms[ri].main.natural) : railRect.width;
656
+ const at = rect(railRect.x, y, railRect.width, h);
657
+ items[id] = { rect: at, hidden: forms[form]?.hides ?? [], kind: it.kind, form, formKind: "rail", edge: c.edge.id, zone: zoneOf(it), rail: side, folded: g.auto.has(c) ? "auto" : "hand" };
658
+ segments.push({ id, edge: c.edge.id, rect: at });
659
+ y += h;
660
+ }
661
+ }
662
+ d.rails[side] = { rect: railRect, segments };
663
+ }
664
+
665
+ // src/scene/solve/sides.ts
666
+ function laySides(d, sideEdges, area2, f) {
667
+ const H = area2.height;
668
+ const reach = insetReach(area2, f);
669
+ const tall = (edge) => H + (edge.outsideInset === true ? reach.up + reach.down : 0);
670
+ const cols = columnsOf(d, sideEdges, area2, tall);
671
+ const g = giveWay(d, cols, area2, f.gutter);
672
+ columnRoom(d, cols, area2, f);
673
+ giveWayNotes(d, g, cols);
674
+ const walk = { x0: area2.x, x1: area2.x + area2.width };
675
+ for (const side of ["left", "right"]) laySide(d, side, cols, g, area2, f, walk, reach.up, tall);
676
+ return rect(walk.x0, area2.y, Math.max(0, walk.x1 - walk.x0), H);
677
+ }
678
+ function forced(d, c, length, w) {
679
+ const edge = { ...c.edge, cross: { ...c.edge.cross, min: w, max: w } };
680
+ const { size: _s, ...plain } = edge;
681
+ return solveEdge(d.scene, plain, d.owned.get(c.edge), length, w, d.prevEdge(c.edge));
682
+ }
683
+ function laySide(d, side, cols, g, area2, f, walk, upInset, tall) {
684
+ const { scene, edges } = d;
685
+ const H = area2.height;
686
+ const outsideSideBar = (c) => c.width > 0 && isOutsideSideBar(scene, c.edge) && !g.railed(c) && !g.riding(c);
687
+ const all = cols.filter((c) => c.side === side);
688
+ const mine = [...all.filter(outsideSideBar), ...all.filter((c) => !outsideSideBar(c))];
689
+ const railed = mine.filter((c) => g.railed(c));
690
+ const riders = mine.filter((c) => g.riding(c));
691
+ const host = mine.find((c) => c.dyn && c.edge.column === true && !c.hand && !g.auto.has(c) && c.width > 0);
692
+ const ridersTop = ridersHeight(d, riders);
693
+ const railW = px(scene.rails?.[side]?.width);
694
+ let railRect = null;
695
+ let gap = px(side === "left" ? f.inset.left : f.inset.right);
696
+ let barLaid = false;
697
+ let railPlace = null;
698
+ const placeRail = () => {
699
+ const lead = barLaid ? gap : 0;
700
+ return side === "left" ? rect(walk.x0 - lead, area2.y, railW, H) : rect(walk.x1 + lead - railW, area2.y, railW, H);
701
+ };
702
+ for (const c of mine) {
703
+ if (!railPlace && !outsideSideBar(c)) {
704
+ railPlace = placeRail();
705
+ if (railed.length > 0) {
706
+ railRect = railPlace;
707
+ const lead = barLaid ? gap : 0;
708
+ const after = barLaid ? Math.max(gap, f.gutter) : f.gutter;
709
+ if (side === "left") walk.x0 += railPlace.width - lead + after;
710
+ else walk.x1 -= railPlace.width - lead + after;
711
+ gap = after;
712
+ }
713
+ }
714
+ if (outsideSideBar(c)) barLaid = true;
715
+ const base = { side: c.edge.side, ring: finite(c.edge.ring, 0), cross: 0, menu: null, tabs: [], stack: c.solved.stack, free: c.solved.free };
716
+ if (g.railed(c)) {
717
+ edges[c.edge.id] = { ...base, rect: rect(railRect.x, area2.y, 0, H), folded: g.auto.has(c) ? "auto" : "hand" };
718
+ continue;
719
+ }
720
+ if (g.riding(c)) continue;
721
+ const w = Math.min(g.widths.get(c), Math.max(0, walk.x1 - walk.x0));
722
+ const top = c === host ? ridersTop : 0;
723
+ let solved = top > 0 || Math.abs(w - c.width) > EPS ? forced(d, c, H - top, w) : c.solved;
724
+ let r;
725
+ if (c.edge.outsideInset === true) {
726
+ const y0 = area2.y - upInset;
727
+ const h = tall(c.edge);
728
+ if (side === "left") {
729
+ r = rect(walk.x0 - gap, y0, w, h);
730
+ walk.x0 += w;
731
+ } else {
732
+ r = rect(walk.x1 + gap - w, y0, w, h);
733
+ walk.x1 -= w;
734
+ }
735
+ if (Math.abs(h - H) > EPS || Math.abs(w - c.width) > EPS) solved = forced(d, c, h, w);
736
+ } else if (side === "left") {
737
+ r = rect(walk.x0, area2.y + top, w, H - top);
738
+ walk.x0 += w > 0 ? w + f.gutter : 0;
739
+ if (w > 0) gap = f.gutter;
740
+ } else {
741
+ walk.x1 -= w;
742
+ r = rect(walk.x1, area2.y + top, w, H - top);
743
+ walk.x1 -= w > 0 ? f.gutter : 0;
744
+ if (w > 0) gap = f.gutter;
745
+ }
746
+ emit(d, c.edge, r, solved, w < c.width - EPS ? { shrunk: true } : {});
747
+ }
748
+ if (host && riders.length > 0) layRiders(d, host, riders, area2.y);
749
+ if (railRect) layRail(d, side, railRect, railed, g);
750
+ if (scene.rails?.[side]) d.railPlaces[side] = railPlace ?? placeRail();
751
+ }
752
+
753
+ // src/scene/solve.ts
754
+ function solve(given, options = {}) {
755
+ const d = createDraft(given, options);
756
+ const { scene, order } = d;
757
+ const stage = rect(finite(scene.stage.x, 0), finite(scene.stage.y, 0), px(scene.stage.width), px(scene.stage.height));
758
+ const { inset, docked } = insetOf(d);
759
+ const reserved = order.filter((e) => e.mode !== "overlay");
760
+ const outsideBar = (e) => !isSide(e) && e.span !== "between" && e.outsideInset === true;
761
+ let outer = stage;
762
+ for (const e of reserved) if (outsideBar(e)) outer = cutEdge(d, e, outer, true);
763
+ let area2 = insideInset(outer, inset);
764
+ for (const e of reserved) if (!isSide(e) && e.span !== "between" && !outsideBar(e)) area2 = cutEdge(d, e, area2, true);
765
+ area2 = laySides(d, reserved.filter(isSide), area2, { inset, gutter: px(scene.gutter), docked, outer });
766
+ for (const e of reserved) if (!isSide(e) && e.span === "between") area2 = cutEdge(d, e, area2, true);
767
+ const content = area2;
768
+ let over = content;
769
+ for (const e of order) if (e.mode === "overlay") over = cutEdge(d, e, over, true);
770
+ contentNotes(d, content);
771
+ const floats = layFloats(d, stage, content, inset);
772
+ const { items, edges, rails, railPlaces, columnRoom: columnRoom2, notes } = d;
773
+ return { items, edges, content, floats, rails, railPlaces, ...columnRoom2 !== void 0 ? { columnRoom: columnRoom2 } : {}, notes };
774
+ }
775
+
776
+ // src/scene/changes.ts
777
+ var withItem = (scene, item) => ({ ...scene, items: { ...scene.items, [item.id]: item } });
778
+ var itemOf = (scene, id) => Object.hasOwn(scene.items, id) ? scene.items[id] : void 0;
779
+ function closeRings(edges, sides) {
780
+ const out = [...edges];
781
+ for (const side of sides) {
782
+ const idx = out.map((e, i) => ({ e, i })).filter((x) => x.e.side === side);
783
+ idx.sort((a, b) => a.e.ring === b.e.ring ? a.i - b.i : a.e.ring < b.e.ring ? -1 : 1);
784
+ idx.forEach((x, ring) => {
785
+ if (x.e.ring !== ring) out[x.i] = { ...x.e, ring };
786
+ });
787
+ }
788
+ return out;
789
+ }
790
+ function prune(scene) {
791
+ const gone = scene.edges.filter((e) => e.column === true && e.items.length === 0);
792
+ if (gone.length === 0) return scene;
793
+ const sides = new Set(gone.map((e) => e.side));
794
+ return { ...scene, edges: closeRings(scene.edges.filter((e) => !gone.includes(e)), sides) };
795
+ }
796
+ function lift(scene, id) {
797
+ const inEdge = scene.edges.some((e) => e.items.includes(id));
798
+ const inFloat = scene.floats.some((f) => f.id === id);
799
+ if (!inEdge && !inFloat) return scene;
800
+ return prune({
801
+ ...scene,
802
+ edges: inEdge ? scene.edges.map((e) => e.items.includes(id) ? { ...e, items: e.items.filter((x) => x !== id) } : e) : scene.edges,
803
+ floats: inFloat ? scene.floats.filter((f) => f.id !== id) : scene.floats
804
+ });
805
+ }
806
+ function newColumn(scene, side, kind, ring, items) {
807
+ const t = scene.columns?.[side];
808
+ if (!t || !t.accepts.includes(kind)) return null;
809
+ const taken = new Set(scene.edges.map((e) => e.id));
810
+ let n = 0;
811
+ while (taken.has(`${side}-${n}`)) n++;
812
+ return {
813
+ id: `${side}-${n}`,
814
+ side,
815
+ ring,
816
+ accepts: t.accepts,
817
+ items,
818
+ cross: t.cross,
819
+ ...t.gap !== void 0 ? { gap: t.gap } : {},
820
+ ...t.pad ? { pad: t.pad } : {},
821
+ ...t.menu ? { menu: t.menu } : {},
822
+ ...t.zoneAccepts ? { zoneAccepts: t.zoneAccepts } : {},
823
+ column: true
824
+ };
825
+ }
826
+ function sideRings(edges, side) {
827
+ return new Map(closeRings(edges, /* @__PURE__ */ new Set([side])).filter((e) => e.side === side).map((e) => [e.id, e.ring]));
828
+ }
829
+ function columnFloor(edges, side) {
830
+ const mine = edges.filter((e) => e.side === side).sort((a, b) => a.ring - b.ring);
831
+ let n = 0;
832
+ while (n < mine.length && mine[n].column !== true) n++;
833
+ return n;
834
+ }
835
+ function insertColumn(scene0, id, side, ring) {
836
+ const scene = { ...scene0, edges: closeRings(scene0.edges, /* @__PURE__ */ new Set([side])) };
837
+ const kind = scene.items[id].kind;
838
+ const mine = scene.edges.filter((e) => e.side === side);
839
+ const floor = columnFloor(scene.edges, side);
840
+ const r = Math.max(floor, Math.min(Number.isNaN(ring) ? Infinity : Math.floor(ring), mine.length === 0 ? 0 : Math.max(...mine.map((e) => e.ring)) + 1));
841
+ const col = newColumn(scene, side, kind, r, [id]);
842
+ if (!col) return null;
843
+ if (!zoneTakes(col, "start", kind)) return null;
844
+ const item = scene.items[id];
845
+ const zone = zoneTakes(col, zoneOf(item), kind) ? zoneOf(item) : zonesTaking(col, kind)[0] ?? "start";
846
+ const { align: _a, ...plain } = item;
847
+ const placed = withItem(scene, zone === "start" ? plain : { ...plain, align: zone });
848
+ const shifted = placed.edges.map((e) => e.side === side && e.ring >= r ? { ...e, ring: e.ring + 1 } : e);
849
+ return { ...placed, edges: closeRings([...shifted, col], /* @__PURE__ */ new Set([side])) };
850
+ }
851
+ function insertIndex(scene, edge, zone, at) {
852
+ const rank = { start: 0, center: 1, end: 2 };
853
+ const inZone = edge.items.map((id, i) => ({ id, i })).filter((x) => zoneOf(itemOf(scene, x.id)) === zone);
854
+ const k = Math.min(Math.max(0, Math.floor(Number.isFinite(at) ? at : inZone.length)), inZone.length);
855
+ if (k < inZone.length) return inZone[k].i;
856
+ if (inZone.length > 0) return inZone[inZone.length - 1].i + 1;
857
+ let idx = 0;
858
+ edge.items.forEach((id, i) => {
859
+ if (rank[zoneOf(itemOf(scene, id))] < rank[zone]) idx = i + 1;
860
+ });
861
+ return idx;
862
+ }
863
+ function moveTo(scene, id, place) {
864
+ const item = itemOf(scene, id);
865
+ if (!item || item.fixed) return scene;
866
+ if (place.kind === "float") {
867
+ const out = floatAt(scene, id, place.rect);
868
+ const anchor = place.anchor;
869
+ if (!anchor || out === scene) return out;
870
+ return { ...out, floats: out.floats.map((f) => f.id === id ? { ...f, anchor } : f) };
871
+ }
872
+ if (place.kind === "column" || place.kind === "rail") {
873
+ if (place.kind === "rail" && !scene.rails?.[place.side]?.accepts.includes(item.kind)) return scene;
874
+ const folded = place.kind === "rail" ? { ...item, from: Math.max(1, item.from ?? 0) } : item;
875
+ const made = insertColumn(withItem(lift(scene, id), folded), id, place.side, place.ring);
876
+ return made ?? scene;
877
+ }
878
+ const edge = scene.edges.find((e) => e.id === place.edge);
879
+ if (!edge || !edge.accepts.includes(item.kind)) return scene;
880
+ const zone = place.zone ?? (zoneTakes(edge, zoneOf(item), item.kind) ? zoneOf(item) : zonesTaking(edge, item.kind)[0]);
881
+ if (!zone || !zoneTakes(edge, zone, item.kind)) return scene;
882
+ const { align: _drop, ...rest } = item;
883
+ const moved = zone === "start" ? rest : { ...rest, align: zone };
884
+ const lifted = withItem(lift(scene, id), moved);
885
+ const target = lifted.edges.find((e) => e.id === place.edge);
886
+ if (!target) return scene;
887
+ const k = insertIndex(lifted, target, zone, place.at);
888
+ return { ...lifted, edges: lifted.edges.map((e) => e === target ? { ...e, items: [...e.items.slice(0, k), id, ...e.items.slice(k)] } : e) };
889
+ }
890
+ function floatAt(scene, id, at, orient2) {
891
+ const item = itemOf(scene, id);
892
+ if (!item || item.fixed || !scene.floatKinds.includes(item.kind)) return scene;
893
+ const was = scene.floats.find((f) => f.id === id);
894
+ const o = orient2 ?? was?.orient;
895
+ const size = floatSizeOf(item, o ?? floatOrientOf(item));
896
+ const float = at ? { id, rect: { x: at.x, y: at.y, width: at.width, height: at.height }, ...o ? { orient: o } : {} } : { id, rect: { x: 0, y: 0, width: size.width, height: size.height }, anchor: "content-top-right", ...o ? { orient: o } : {} };
897
+ const next = at && at.width > 0 && at.height > 0 ? { ...item, floatSize: { width: at.width, height: at.height } } : item;
898
+ const lifted = lift(withItem(scene, next), id);
899
+ return { ...lifted, floats: [...lifted.floats, float] };
900
+ }
901
+ function fold(scene, id, on) {
902
+ const item = itemOf(scene, id);
903
+ if (!item || item.fixed) return scene;
904
+ if (on) return (item.from ?? 0) >= 1 ? scene : withItem(scene, { ...item, from: 1 });
905
+ if (item.from === void 0) return scene;
906
+ const { from: _f, ...rest } = item;
907
+ return withItem(scene, rest);
908
+ }
909
+ var nextAge = (scene) => Object.values(scene.items).reduce((m, it) => Math.max(m, Number.isFinite(it.used) ? it.used : 0), 0) + 1;
910
+ function use(scene, id) {
911
+ const item = itemOf(scene, id);
912
+ if (!item) return scene;
913
+ return withItem(scene, { ...item, used: nextAge(scene) });
914
+ }
915
+ function keep(scene, id, on) {
916
+ const item = itemOf(scene, id);
917
+ if (!item || !!item.kept === on) return scene;
918
+ if (on) return withItem(scene, { ...item, kept: true });
919
+ const { kept: _k, ...rest } = item;
920
+ return withItem(scene, rest);
921
+ }
922
+ function open(scene, id) {
923
+ const item = itemOf(scene, id);
924
+ if (!item || !item.closed) return scene;
925
+ const { closed: _c, ...rest } = item;
926
+ return withItem(scene, rest);
927
+ }
928
+ function close(scene, id) {
929
+ const item = itemOf(scene, id);
930
+ if (!item || item.closed || item.fixed) return scene;
931
+ return withItem(scene, { ...item, closed: true });
932
+ }
933
+ function raise(scene, id) {
934
+ const i = scene.floats.findIndex((f) => f.id === id);
935
+ if (i < 0 || i === scene.floats.length - 1) return scene;
936
+ return { ...scene, floats: [...scene.floats.slice(0, i), ...scene.floats.slice(i + 1), scene.floats[i]] };
937
+ }
938
+ function orient(scene, id, to) {
939
+ const i = scene.floats.findIndex((f) => f.id === id);
940
+ const item = scene.items[id];
941
+ if (i < 0 || !item || (scene.floats[i].orient ?? floatOrientOf(item)) === to) return scene;
942
+ return { ...scene, floats: scene.floats.map((f, k) => k === i ? { ...f, orient: to } : f) };
943
+ }
944
+ function resize(scene, edgeId, size) {
945
+ const edge = scene.edges.find((e) => e.id === edgeId);
946
+ if (!edge) return scene;
947
+ if (!Number.isFinite(size)) {
948
+ if (edge.size === void 0) return scene;
949
+ const { size: _s, ...rest } = edge;
950
+ return { ...scene, edges: scene.edges.map((e) => e === edge ? rest : e) };
951
+ }
952
+ return { ...scene, edges: scene.edges.map((e) => e === edge ? { ...e, size } : e) };
953
+ }
954
+ function setSide(scene, edgeId, side) {
955
+ const edge = scene.edges.find((e) => e.id === edgeId);
956
+ if (!edge || edge.side === side || axisOf(edge.side) !== axisOf(side)) return scene;
957
+ const moved = { ...edge, side, ring: 0 };
958
+ const others = scene.edges.filter((e) => e !== edge).map((e) => e.side === side ? { ...e, ring: e.ring + 1 } : e);
959
+ return { ...scene, edges: closeRings([moved, ...others], /* @__PURE__ */ new Set([edge.side, side])) };
960
+ }
961
+ function homeFor(scene, id, start) {
962
+ for (const e of start.edges) {
963
+ const k = e.items.indexOf(id);
964
+ if (k < 0) continue;
965
+ const zone = zoneOf(itemOf(start, id) ?? itemOf(scene, id));
966
+ const found = scene.edges.find((x) => x.id === e.id);
967
+ const here = found && (e.column !== true || found.items.some((x) => e.items.includes(x))) ? found : void 0;
968
+ if (here) {
969
+ const now = here.items.filter((x) => x !== id && zoneOf(itemOf(scene, x)) === zone);
970
+ const next = e.items.slice(k + 1).find((x) => now.includes(x));
971
+ return { kind: "gap", edge: e.id, zone, at: next ? now.indexOf(next) : now.length };
972
+ }
973
+ if (e.column === true && (e.side === "left" || e.side === "right")) return { kind: "column", side: e.side, ring: sideRings(start.edges, e.side).get(e.id) ?? e.ring };
974
+ return null;
975
+ }
976
+ const f = start.floats.find((x) => x.id === id);
977
+ if (f) return { kind: "float", rect: f.rect, ...f.anchor ? { anchor: f.anchor } : {} };
978
+ return null;
979
+ }
980
+ function dockHome(scene, id, start) {
981
+ const place = homeFor(scene, id, start);
982
+ if (!place) return scene;
983
+ const moved = moveTo(scene, id, place);
984
+ const item = itemOf(moved, id);
985
+ if (!item) return scene;
986
+ const from = itemOf(start, id)?.from;
987
+ if (item.from === from) return moved;
988
+ const { from: _f, ...plain } = item;
989
+ return withItem(moved, from === void 0 ? plain : { ...plain, from });
990
+ }
991
+ function clampFloat(r, stage, drawn = r) {
992
+ const at = clampInto({ x: r.x, y: r.y, width: drawn.width, height: drawn.height }, stage);
993
+ return { x: at.x, y: at.y, width: r.width, height: r.height };
994
+ }
995
+ function drawnFloatSize(item, f) {
996
+ const s = floatShape(item, f);
997
+ return { width: s.width, height: s.height };
998
+ }
999
+
1000
+ // src/scene/store.ts
1001
+ var ANCHORS = ["content-top-left", "content-top", "content-top-right", "content-right", "content-bottom-right", "content-bottom", "content-bottom-left", "content-left"];
1002
+ var isNum = (x) => typeof x === "number" && Number.isFinite(x);
1003
+ var isSize = (x) => isNum(x) && x > 0;
1004
+ var isObject = (x) => typeof x === "object" && x !== null && !Array.isArray(x);
1005
+ var record = () => /* @__PURE__ */ Object.create(null);
1006
+ function storeScene(scene) {
1007
+ const items = record();
1008
+ for (const [id, it] of Object.entries(scene.items)) {
1009
+ if (id === "__proto__") continue;
1010
+ items[id] = {
1011
+ used: isNum(it.used) ? it.used : 0,
1012
+ ...it.kept ? { kept: true } : {},
1013
+ ...isNum(it.from) && it.from >= 1 ? { from: Math.floor(it.from) } : {},
1014
+ ...it.closed ? { closed: true } : {},
1015
+ ...it.align === "center" || it.align === "end" ? { align: it.align } : {},
1016
+ ...it.floatSize && isSize(it.floatSize.width) && isSize(it.floatSize.height) ? { floatSize: { width: it.floatSize.width, height: it.floatSize.height } } : {}
1017
+ };
1018
+ }
1019
+ return {
1020
+ v: 1,
1021
+ edges: scene.edges.map((e) => ({ id: e.id, side: e.side, ring: e.ring, items: [...e.items], ...isNum(e.size) ? { size: e.size } : {}, ...e.column === true ? { column: true } : {} })),
1022
+ floats: scene.floats.filter((f) => isNum(f.rect.x) && isNum(f.rect.y) && isSize(f.rect.width) && isSize(f.rect.height)).map((f) => ({ id: f.id, x: f.rect.x, y: f.rect.y, width: f.rect.width, height: f.rect.height, ...f.orient ? { orient: f.orient } : {}, ...f.anchor ? { anchor: f.anchor } : {} })),
1023
+ items
1024
+ };
1025
+ }
1026
+ function readScene(data, base) {
1027
+ try {
1028
+ return read(data, base);
1029
+ } catch (e) {
1030
+ return { scene: base, dropped: [], error: `unreadable: ${e instanceof Error ? e.message : typeof e}` };
1031
+ }
1032
+ }
1033
+ var words = (v) => typeof v === "number" ? String(v) : typeof v === "string" ? JSON.stringify(v.length > 24 ? `${v.slice(0, 24)}\u2026` : v) : typeof v;
1034
+ function withFixed(baseIds, stored, isFixed) {
1035
+ const out = [...stored];
1036
+ const missing = (id) => isFixed(id) && !stored.includes(id);
1037
+ let tail = baseIds.length;
1038
+ while (tail > 0 && isFixed(baseIds[tail - 1])) tail--;
1039
+ baseIds.forEach((id, i) => {
1040
+ if (missing(id) && i < tail) out.splice(Math.min(i, out.length), 0, id);
1041
+ });
1042
+ return [...out, ...baseIds.slice(tail).filter(missing)];
1043
+ }
1044
+ function read(data, base) {
1045
+ if (!isObject(data)) return { scene: base, dropped: [], error: "not an object" };
1046
+ if (data.v !== 1) return { scene: base, dropped: [], error: `unknown version ${words(data.v)} (this reads version 1)` };
1047
+ const dropped = [];
1048
+ const drop = (what) => {
1049
+ if (!dropped.includes(what)) dropped.push(what);
1050
+ };
1051
+ for (const k of Object.keys(data)) if (!["v", "edges", "floats", "items"].includes(k)) drop(k);
1052
+ const known = (id) => typeof id === "string" && id !== "__proto__" && Object.hasOwn(base.items, id);
1053
+ const isFixed = (id) => known(id) && base.items[id].fixed === true;
1054
+ const items = { ...base.items };
1055
+ if (data.items !== void 0 && !isObject(data.items)) drop("items");
1056
+ else if (isObject(data.items)) {
1057
+ for (const id of Object.keys(data.items)) {
1058
+ const s = data.items[id];
1059
+ if (!known(id) || !isObject(s)) {
1060
+ drop(`item:${id}`);
1061
+ continue;
1062
+ }
1063
+ const b = base.items[id];
1064
+ const { kept: _k, from: _f, closed: _c, align: _a, floatSize: _s, ...plain } = b;
1065
+ let it = { ...plain, used: b.used };
1066
+ for (const k of Object.keys(s)) if (!["used", "kept", "from", "closed", "align", "floatSize"].includes(k)) drop(`item:${id}:${k}`);
1067
+ if (isNum(s.used)) it = { ...it, used: s.used };
1068
+ else if (s.used !== void 0) drop(`item:${id}:used`);
1069
+ if (s.kept === true) it = { ...it, kept: true };
1070
+ else if (s.kept !== void 0 && s.kept !== false) drop(`item:${id}:kept`);
1071
+ if (isNum(s.from) && Number.isInteger(s.from) && s.from >= 1 && !b.fixed) it = { ...it, from: s.from };
1072
+ else if (s.from !== void 0) drop(`item:${id}:from`);
1073
+ if (b.fixed && b.from !== void 0) it = { ...it, from: b.from };
1074
+ if (s.closed === true && !b.fixed) it = { ...it, closed: true };
1075
+ else if (s.closed !== void 0 && s.closed !== false) drop(`item:${id}:closed`);
1076
+ if (s.align === "center" || s.align === "end") it = { ...it, align: s.align };
1077
+ else if (s.align !== void 0 && s.align !== "start") drop(`item:${id}:align`);
1078
+ if (b.fixed && b.align) it = { ...it, align: b.align };
1079
+ const fs = s.floatSize;
1080
+ if (isObject(fs) && isSize(fs.width) && isSize(fs.height)) it = { ...it, floatSize: { width: fs.width, height: fs.height } };
1081
+ else if (fs !== void 0) drop(`item:${id}:floatSize`);
1082
+ items[id] = it;
1083
+ }
1084
+ }
1085
+ const placed = /* @__PURE__ */ new Set();
1086
+ const order = /* @__PURE__ */ new Map();
1087
+ const sizes = /* @__PURE__ */ new Map();
1088
+ const made = [];
1089
+ const rings = /* @__PURE__ */ new Map();
1090
+ const moves = /* @__PURE__ */ new Map();
1091
+ if (data.edges !== void 0 && !Array.isArray(data.edges)) drop("edges");
1092
+ for (const e of Array.isArray(data.edges) ? data.edges : []) {
1093
+ if (!isObject(e) || typeof e.id !== "string") {
1094
+ drop("an edge with no id");
1095
+ continue;
1096
+ }
1097
+ let edge = base.edges.find((x) => x.id === e.id) ?? made.find((x) => x.id === e.id);
1098
+ if (!edge && e.column === true && (e.side === "left" || e.side === "right") && base.columns?.[e.side]) {
1099
+ const t = base.columns[e.side];
1100
+ edge = { id: e.id, side: e.side, ring: isNum(e.ring) ? e.ring : Infinity, accepts: t.accepts, items: [], cross: t.cross, ...t.gap !== void 0 ? { gap: t.gap } : {}, ...t.pad ? { pad: t.pad } : {}, ...t.menu ? { menu: t.menu } : {}, column: true };
1101
+ made.push(edge);
1102
+ }
1103
+ if (!edge || order.has(edge.id)) {
1104
+ drop(`edge:${e.id}`);
1105
+ continue;
1106
+ }
1107
+ if (edge.column === true && isNum(e.ring)) rings.set(edge.id, e.ring);
1108
+ if (e.side !== void 0 && !SIDES.includes(e.side)) drop(`edge:${e.id}:side`);
1109
+ else if (edge.column !== true && typeof e.side === "string" && e.side !== edge.side && axisOf(e.side) === axisOf(edge.side)) moves.set(edge.id, e.side);
1110
+ const ids = [];
1111
+ for (const id of Array.isArray(e.items) ? e.items : []) {
1112
+ if (!known(id) || placed.has(id)) drop(`edge:${edge.id}:${String(id)}`);
1113
+ else if (isFixed(id)) {
1114
+ if (edge.items.includes(id) && base.edges.some((x) => x.id === edge.id)) {
1115
+ ids.push(id);
1116
+ placed.add(id);
1117
+ } else drop(`edge:${edge.id}:${id}`);
1118
+ } else if (!edge.accepts.includes(items[id].kind)) drop(`edge:${edge.id}:${id}`);
1119
+ else {
1120
+ ids.push(id);
1121
+ placed.add(id);
1122
+ }
1123
+ }
1124
+ order.set(edge.id, ids);
1125
+ if (isNum(e.size)) sizes.set(edge.id, e.size);
1126
+ else if (e.size !== void 0) drop(`edge:${edge.id}:size`);
1127
+ else sizes.set(edge.id, void 0);
1128
+ }
1129
+ const floats = [];
1130
+ if (data.floats !== void 0 && !Array.isArray(data.floats)) drop("floats");
1131
+ for (const f of Array.isArray(data.floats) ? data.floats : []) {
1132
+ if (!isObject(f) || typeof f.id !== "string") {
1133
+ drop("a float with no id");
1134
+ continue;
1135
+ }
1136
+ const id = f.id;
1137
+ if (!known(id) || placed.has(id) || isFixed(id) || !base.floatKinds.includes(items[id].kind) || !isNum(f.x) || !isNum(f.y) || !isSize(f.width) || !isSize(f.height)) {
1138
+ drop(`float:${id}`);
1139
+ continue;
1140
+ }
1141
+ for (const k of Object.keys(f)) if (k === "pinned") drop(`pinned:${id}`);
1142
+ else if (!["id", "x", "y", "width", "height", "orient", "anchor"].includes(k)) drop(`float:${id}:${k}`);
1143
+ const o = f.orient === "row" || f.orient === "column" ? f.orient : void 0;
1144
+ if (f.orient !== void 0 && !o) drop(`float:${id}:orient`);
1145
+ const a = ANCHORS.includes(f.anchor) ? f.anchor : void 0;
1146
+ if (f.anchor !== void 0 && !a) drop(`float:${id}:anchor`);
1147
+ placed.add(id);
1148
+ const r = rect(f.x, f.y, f.width, f.height);
1149
+ const drawn = drawnFloatSize(items[id], { rect: r, ...o ? { orient: o } : {} });
1150
+ floats.push({ id, rect: clampFloat(r, base.stage, drawn), ...o ? { orient: o } : {}, ...a ? { anchor: a } : {} });
1151
+ }
1152
+ const edges = [...base.edges, ...made].map((edge) => {
1153
+ const stored = order.get(edge.id) ?? [];
1154
+ const rest = edge.items.filter((id) => !isFixed(id) && !placed.has(id));
1155
+ for (const id of rest) placed.add(id);
1156
+ let tail = stored.length;
1157
+ while (tail > 0 && isFixed(stored[tail - 1])) tail--;
1158
+ const listed = [...stored.slice(0, tail), ...rest, ...stored.slice(tail)];
1159
+ const items1 = withFixed(edge.items, listed, isFixed);
1160
+ const size = sizes.has(edge.id) ? sizes.get(edge.id) : edge.size;
1161
+ const { size: _s, ...plain } = edge;
1162
+ return { ...plain, ring: rings.get(edge.id) ?? edge.ring, items: items1, ...size !== void 0 ? { size } : {} };
1163
+ });
1164
+ for (const f of base.floats) if (!placed.has(f.id)) floats.push(f);
1165
+ let scene = prune({ ...base, items, edges: closeRings(edges, new Set(made.length > 0 || rings.size > 0 ? ["left", "right"] : [])), floats });
1166
+ for (const [id, side] of moves) scene = setSide(scene, id, side);
1167
+ return { scene, dropped };
1168
+ }
1169
+
1170
+ // src/scene/targets/kinds.ts
1171
+ var RAIL_REACH = 24;
1172
+ var EDGE_KINDS = {
1173
+ bar: { zoneExtent: "thirds", opens: "any", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: null, layer: 10, overreach: 30, priority: 2, reach: Infinity, virtual: false },
1174
+ sideBar: { zoneExtent: "thirds", opens: "any", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: null, layer: 11, overreach: 31, priority: 2, reach: Infinity, virtual: false },
1175
+ column: { zoneExtent: "solved", opens: "start", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: null, layer: 12, overreach: 32, priority: 2, reach: Infinity, virtual: false },
1176
+ barBetween: { zoneExtent: "thirds", opens: "any", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: null, layer: 13, overreach: 33, priority: 2, reach: Infinity, virtual: false },
1177
+ rail: { zoneExtent: "thirds", opens: "any", inward: RAIL_REACH, place: DEFAULT_HIT_BAND / 2, phase: null, layer: 14, overreach: 34, priority: 2, reach: Infinity, virtual: false },
1178
+ railStop: { zoneExtent: "whole", opens: "any", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: 2, layer: 9, overreach: 39, priority: 1, reach: Infinity, virtual: false },
1179
+ columnLine: { zoneExtent: "whole", opens: "any", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: 2, layer: 21, overreach: 41, priority: 2, reach: COLUMN_REACH, virtual: true }
1180
+ };
1181
+ function edgeKindOf(scene, e) {
1182
+ if (e.compact) return "rail";
1183
+ if (e.side === "top" || e.side === "bottom") return e.span === "between" ? "barBetween" : "bar";
1184
+ return isSideBar(scene, e) ? "sideBar" : "column";
1185
+ }
1186
+ var opensEmpty = (scene, edge, kind) => EDGE_KINDS[edgeKindOf(scene, edge)].opens === "any" || zoneTakes(edge, "start", kind);
1187
+
1188
+ // src/scene/targets/edges.ts
1189
+ function reservedRect(scene, l, id) {
1190
+ const it = l.items[id];
1191
+ const e = it.edge ? l.edges[it.edge] : void 0;
1192
+ const f = e ? formsOf(scene.items[id], axisOf(e.side))[it.form] : void 0;
1193
+ if (!e || !f?.main.grows) return it.rect;
1194
+ const column = axisOf(e.side) === "column";
1195
+ const len = column ? it.rect.height : it.rect.width;
1196
+ const keep2 = Math.min(len, Math.max(0, Number.isFinite(f.main.natural) ? f.main.natural : len));
1197
+ const shift = it.zone === "end" ? len - keep2 : it.zone === "center" ? (len - keep2) / 2 : 0;
1198
+ return column ? rect(it.rect.x, it.rect.y + shift, it.rect.width, keep2) : rect(it.rect.x + shift, it.rect.y, keep2, it.rect.height);
1199
+ }
1200
+ function slotAt(c, zone, line, id, place, lands, home = false) {
1201
+ const result = c.settle(use(moveTo(c.scene, c.dragged, place), c.dragged));
1202
+ const isHome = home || c.isHome(result);
1203
+ return { zone, line, id, place, result, home: isHome, ok: isHome || !!result.layout.items[c.dragged] && lands(result) && c.sound(result.layout) };
1204
+ }
1205
+ function distinctSlots(c, slots) {
1206
+ const landing = (s) => s.result.layout.items[c.dragged]?.rect;
1207
+ const sameMove = (a, b) => {
1208
+ if (a.zone !== b.zone) return false;
1209
+ if (a.home && b.home) return true;
1210
+ const [p, q] = [landing(a), landing(b)];
1211
+ return Math.abs(a.line - b.line) < EPS && (!p || !q ? p === q : Math.abs(p.x - q.x) + Math.abs(p.y - q.y) + Math.abs(p.width - q.width) + Math.abs(p.height - q.height) < 0.01);
1212
+ };
1213
+ return slots.filter((s, i) => !slots.slice(0, i).some((o) => sameMove(o, s)));
1214
+ }
1215
+ function zoneSpans(scene, base, edge, reach, probe) {
1216
+ const el = base.edges[edge.id];
1217
+ const column = axisOf(edge.side) === "column";
1218
+ const main0 = column ? el.rect.y : el.rect.x;
1219
+ const length = column ? el.rect.height : el.rect.width;
1220
+ if (EDGE_KINDS[edgeKindOf(scene, edge)].zoneExtent === "thirds")
1221
+ return ZONES.map((zone, k) => ({ zone, a: k === 0 ? main0 - reach : main0 + length * k / 3, b: k === 2 ? main0 + length + reach : main0 + length * (k + 1) / 3 }));
1222
+ const at = (r) => column ? [r.y, r.y + r.height] : [r.x, r.x + r.width];
1223
+ const declared = new Set(edge.zoneAccepts ? ZONES.filter((z) => edge.zoneAccepts[z] !== void 0) : ["start"]);
1224
+ const placed = /* @__PURE__ */ new Map();
1225
+ for (const [id, it] of Object.entries(base.items)) {
1226
+ if (it.edge !== edge.id || it.inMenu) continue;
1227
+ const z = zoneOf(scene.items[id]);
1228
+ const [a, b] = at(it.rect);
1229
+ const was = placed.get(z);
1230
+ placed.set(z, was ? [Math.min(was[0], a), Math.max(was[1], b)] : [a, b]);
1231
+ declared.add(z);
1232
+ }
1233
+ const anchor = { start: main0, center: main0 + length / 2, end: main0 + length };
1234
+ const spans = ZONES.filter((z) => declared.has(z)).map((zone) => ({ zone, s: placed.get(zone) ?? probe(zone) ?? [anchor[zone], anchor[zone]] }));
1235
+ return spans.map(({ zone, s }, i) => ({
1236
+ zone,
1237
+ a: i === 0 ? main0 - reach : (spans[i - 1].s[1] + s[0]) / 2,
1238
+ b: i === spans.length - 1 ? main0 + length + reach : (s[1] + spans[i + 1].s[0]) / 2
1239
+ }));
1240
+ }
1241
+ function edgeSlots(c, edge, accepts) {
1242
+ const { scene, rest, base, dragged, kind } = c;
1243
+ const el = base.edges[edge.id];
1244
+ const column = axisOf(edge.side) === "column";
1245
+ const at = (r) => column ? r.y : r.x;
1246
+ const len = (r) => column ? r.height : r.width;
1247
+ const main0 = at(el.rect);
1248
+ const length = len(el.rect);
1249
+ const restEdge = rest.edges.find((e) => e.id === edge.id);
1250
+ const slotId = (zone, k) => restEdge.items.length === 0 ? `strip:${edge.id}:${zone}` : `gap:${edge.id}:${zone}:${k}`;
1251
+ const firstKind = formsOf(scene.items[dragged], axisOf(edge.side))[0]?.kind;
1252
+ const titles = edge.items.filter((id) => base.items[id]?.edge === edge.id && base.items[id].fixed && !!formsOf(scene.items[id], axisOf(edge.side))[base.items[id].form]?.main.grows);
1253
+ const lands = ({ layout: after }) => {
1254
+ const me = after.items[dragged];
1255
+ if (me.edge !== edge.id || me.inMenu) return false;
1256
+ if (titles.length === 0) return true;
1257
+ if (formsOf(scene.items[dragged], axisOf(edge.side))[me.form]?.kind !== firstKind) return false;
1258
+ return edge.items.every((id) => {
1259
+ const a = after.items[id];
1260
+ const b = base.items[id];
1261
+ if (!a || !b || b.edge !== edge.id || id === dragged) return true;
1262
+ if (!titles.includes(id)) return a.form === b.form && !!a.inMenu === !!b.inMenu;
1263
+ const f = formsOf(scene.items[id], axisOf(edge.side))[a.form];
1264
+ return !f || (column ? a.rect.height : a.rect.width) >= formMain(f, "natural", after.edges[edge.id].cross) - 0.5;
1265
+ });
1266
+ };
1267
+ const pad = { start: Math.max(0, edge.pad?.start ?? 0), end: Math.max(0, edge.pad?.end ?? 0) };
1268
+ const menuLen = el.menu ? len(el.menu.rect) : 0;
1269
+ const occupied = (id) => base.items[id]?.fixed ? reservedRect(scene, base, id) : base.items[id].rect;
1270
+ const shown = (id) => base.items[id]?.edge === edge.id && !base.items[id].inMenu;
1271
+ const drawnOf = (z) => restEdge.items.filter((id) => zoneOf(rest.items[id]) === z && shown(id)).map(occupied);
1272
+ const startEnd = (() => {
1273
+ const s = drawnOf("start");
1274
+ return s.length ? at(s[s.length - 1]) + len(s[s.length - 1]) : main0 + pad.start;
1275
+ })();
1276
+ const endStart = (() => {
1277
+ const e = drawnOf("end");
1278
+ return e.length ? at(e[0]) : main0 + length - pad.end - menuLen;
1279
+ })();
1280
+ const anchorLine = { start: main0 + pad.start, center: Math.min(Math.max(main0 + length / 2, startEnd), Math.max(startEnd, endStart)), end: main0 + length - pad.end - menuLen };
1281
+ const slots = [];
1282
+ for (const zone of ZONES) {
1283
+ if (!accepts || !zoneTakes(edge, zone, kind)) continue;
1284
+ const members = restEdge.items.filter((id) => zoneOf(rest.items[id]) === zone);
1285
+ for (let k = 0; k <= members.length; k++) {
1286
+ const next = members.slice(k).find(shown);
1287
+ const before = members.slice(0, k).reverse().find(shown);
1288
+ const line = next ? at(occupied(next)) : before ? at(occupied(before)) + len(occupied(before)) : anchorLine[zone];
1289
+ const s = slotAt(c, zone, line, slotId(zone, k), { kind: "gap", edge: edge.id, zone, at: k }, lands);
1290
+ const movesFixed = restEdge.items.some((id) => {
1291
+ const b = base.items[id];
1292
+ const a = s.result.layout.items[id];
1293
+ return !!b?.fixed && b.edge === edge.id && (!a || Math.abs(a.rect.x - b.rect.x) > 0.5 || Math.abs(a.rect.y - b.rect.y) > 0.5);
1294
+ });
1295
+ if (!movesFixed) slots.push(s);
1296
+ }
1297
+ }
1298
+ return slots;
1299
+ }
1300
+ function edgeSpec(c, edge) {
1301
+ const { scene, rest, now, base, dragged, kind } = c;
1302
+ const el = base.edges[edge.id];
1303
+ if (!el || el.folded || el.rides) return null;
1304
+ const edgeKind = edgeKindOf(rest, edge);
1305
+ const data = EDGE_KINDS[edgeKind];
1306
+ const column = axisOf(edge.side) === "column";
1307
+ const at = (r2) => column ? r2.y : r2.x;
1308
+ const len = (r2) => column ? r2.height : r2.width;
1309
+ const empty = el.stack.items.every((p) => p.kind === "menu") && !el.menu;
1310
+ const accepts = edge.accepts.includes(kind);
1311
+ if (!accepts && (empty || area(el.rect) <= EPS)) return null;
1312
+ if (accepts && empty && !opensEmpty(rest, edge, kind)) return null;
1313
+ const ownNow = now.items[dragged];
1314
+ const ownHere = ownNow && ownNow.edge === edge.id && !ownNow.floating && !ownNow.inMenu ? ownNow.rect : null;
1315
+ const slots = edgeSlots(c, edge, accepts);
1316
+ const spanOf = (r2) => [at(r2), at(r2) + len(r2)];
1317
+ const grows = (id) => !!formsOf(scene.items[id], axisOf(edge.side))[base.items[id].form]?.main.grows;
1318
+ const reserved = Object.entries(base.items).filter(([id, p]) => p.edge === edge.id && p.fixed && !p.inMenu && !grows(id)).map(([, p]) => spanOf(p.rect));
1319
+ const zones = zoneSpans(rest, base, edge, c.reach, (zone) => {
1320
+ const s = slots.find((x) => x.zone === zone && x.place.kind === "gap" && x.place.at === 0);
1321
+ const me = s?.result.layout.items[dragged];
1322
+ return me && me.edge === edge.id && me.zone === zone ? spanOf(me.rect) : null;
1323
+ });
1324
+ const r = el.rect;
1325
+ const strip = Math.max(0, Number.isFinite(edge.strip) ? edge.strip : DEFAULT_STRIP);
1326
+ const nowCross = now.edges[edge.id]?.cross ?? 0;
1327
+ const outer = edge.side === "left" ? r.x : edge.side === "right" ? r.x + r.width : edge.side === "top" ? r.y : r.y + r.height;
1328
+ const inward = (depth) => edge.side === "left" || edge.side === "top" ? [outer, outer + depth] : [outer - depth, outer];
1329
+ const across = (after) => inward((el.cross > 0 || nowCross > 0 ? Math.max(el.cross, nowCross, after?.edges[edge.id]?.cross ?? 0) : strip) + data.inward);
1330
+ const ownCross = ownNow?.edge === edge.id ? el.cross : Math.max(el.cross, nowCross);
1331
+ const drawn = ownCross > 0;
1332
+ const marker = (s) => {
1333
+ const slot = s.result.layout.items[dragged].rect;
1334
+ const e = s.result.layout.edges[edge.id];
1335
+ return column ? rect(e.rect.x, slot.y, e.cross, 0) : rect(slot.x, e.rect.y, 0, e.cross);
1336
+ };
1337
+ const takes = (zone) => accepts && zoneTakes(edge, zone, kind);
1338
+ return {
1339
+ id: edge.id,
1340
+ key: edge.id,
1341
+ kind: edgeKind,
1342
+ lay: c.lay.get(edge.id),
1343
+ vertical: column,
1344
+ zones,
1345
+ slots,
1346
+ refuse: (zone) => takes(zone) ? "no-room" : accepts || area(el.rect) > EPS ? "not-accepted" : null,
1347
+ own: ownHere,
1348
+ holds: !!ownNow && ownNow.edge === edge.id && !ownNow.floating,
1349
+ reserved,
1350
+ across,
1351
+ owned: inward(drawn ? ownCross : Math.min(strip, data.place)),
1352
+ ownedAlong: null,
1353
+ drawn,
1354
+ marker
1355
+ };
1356
+ }
1357
+
1358
+ // src/scene/targets/filter.ts
1359
+ function placeKey(s, id) {
1360
+ const it = Object.hasOwn(s.items, id) ? s.items[id] : void 0;
1361
+ const byZone = (items) => ZONES.flatMap((z) => items.filter((x) => zoneOf(s.items[x]) === z));
1362
+ return JSON.stringify({
1363
+ edges: s.edges.map((e) => JSON.stringify([e.side, e.ring, e.column === true ? "" : e.id, byZone(e.items), e.size ?? null])).sort(),
1364
+ floats: s.floats.map((f) => [f.id, f.rect, f.anchor ?? null, f.orient ?? null]),
1365
+ from: it?.from ?? 0,
1366
+ align: it?.align ?? "start"
1367
+ });
1368
+ }
1369
+ function soundness(scene, base, dragged) {
1370
+ const chrome = scene.chrome ?? [];
1371
+ const noteKey = (n) => "edge" in n && (n.code === "overflow" || n.code === "too-wide") ? `${n.edge}:${n.code}:${"id" in n ? n.id : ""}` : null;
1372
+ const hadNotes = new Set(base.notes.map(noteKey).filter((k) => k !== null));
1373
+ return (after) => {
1374
+ if (after.notes.some((n) => {
1375
+ const k = noteKey(n);
1376
+ return k !== null && !hadNotes.has(k);
1377
+ })) return false;
1378
+ for (const [id, b] of Object.entries(base.items)) {
1379
+ if (!b.fixed) continue;
1380
+ const a = after.items[id];
1381
+ const e = a?.edge ? after.edges[a.edge] : void 0;
1382
+ if (!a || !e || a.form !== b.form || a.formKind !== b.formKind) return false;
1383
+ const f = formsOf(scene.items[id], axisOf(e.side))[a.form];
1384
+ const len = axisOf(e.side) === "column" ? a.rect.height : a.rect.width;
1385
+ if (f && len < formMain(f, "min", e.cross) - 0.5) return false;
1386
+ }
1387
+ const me = after.items[dragged];
1388
+ if (!me) return true;
1389
+ const fixedAfter = Object.entries(after.items).filter(([id, it]) => it.fixed && id !== dragged).map(([id]) => reservedRect(scene, after, id));
1390
+ return !chrome.some((c) => overlaps(me.rect, c)) && !fixedAfter.some((f) => overlaps(me.rect, f));
1391
+ };
1392
+ }
1393
+ function context(scene, dragged, options) {
1394
+ const item = Object.hasOwn(scene.items, dragged) ? scene.items[dragged] : void 0;
1395
+ if (!item || item.fixed || item.closed) return null;
1396
+ const prev = { ...options.previous ? { previous: options.previous } : {}, ...options.fresh ? { fresh: options.fresh } : {} };
1397
+ const settle = (s) => ({ scene: s, layout: solve(s, prev) });
1398
+ const now = solve(scene, prev);
1399
+ const rest = lift(scene, dragged);
1400
+ const base = solve(rest, prev);
1401
+ const here = placeKey(scene, dragged);
1402
+ const c = {
1403
+ scene,
1404
+ rest,
1405
+ now,
1406
+ base,
1407
+ dragged,
1408
+ kind: item.kind,
1409
+ reach: Math.max(0, Number.isFinite(options.endReach) ? options.endReach : 0),
1410
+ settle,
1411
+ isHome: (r) => placeKey(r.scene, dragged) === here,
1412
+ sound: soundness(scene, base, dragged),
1413
+ lay: layKeys(scene)
1414
+ };
1415
+ return { c, item, prev };
1416
+ }
1417
+
1418
+ // src/scene/targets/model.ts
1419
+ var less = (spans, away) => away.reduce((acc, [ra, rb]) => acc.flatMap(([a, b]) => rb <= a || ra >= b ? [[a, b]] : [[a, Math.max(a, ra)], [Math.min(b, rb), b]].filter(([u, w]) => w - u > EPS)), [...spans]);
1420
+
1421
+ // src/scene/targets/ownership.ts
1422
+ var bandOf = (s) => {
1423
+ const a = Math.min(...s.zones.map((z) => z.a));
1424
+ const b = Math.max(...s.zones.map((z) => z.b));
1425
+ return s.vertical ? rect(s.owned[0], a, s.owned[1] - s.owned[0], b - a) : rect(a, s.owned[0], b - a, s.owned[1] - s.owned[0]);
1426
+ };
1427
+ var ownedRectOf = (s) => {
1428
+ const b = bandOf(s);
1429
+ if (!s.ownedAlong) return b;
1430
+ const [a0, a1] = s.ownedAlong;
1431
+ return s.vertical ? rect(b.x, a0, b.width, a1 - a0) : rect(a0, b.y, a1 - a0, b.height);
1432
+ };
1433
+ function ownedRectsOf(s, along) {
1434
+ if (along.length === 0) return [];
1435
+ const o = ownedRectOf(s);
1436
+ if (s.ownedAlong) return [o];
1437
+ return along.map(([a, b]) => s.vertical ? rect(o.x, a, o.width, b - a) : rect(a, o.y, b - a, o.height));
1438
+ }
1439
+ function seenOf(c) {
1440
+ const { now, base, dragged } = c;
1441
+ const ownEdge = now.items[dragged]?.edge;
1442
+ const lifted = Object.values(base.edges).filter((e) => area(e.rect) > EPS).map((e) => e.rect);
1443
+ return Object.entries(now.edges).filter(([id, e]) => id !== ownEdge && area(e.rect) > EPS && c.lay.has(id)).flatMap(([id, e]) => lifted.reduce((ps, r) => ps.flatMap((p) => subtract(p, r)), [e.rect]).map((r) => ({ edge: id, rect: r })));
1444
+ }
1445
+ function ownersOf(c, live, alongOf) {
1446
+ const { now, base, dragged } = c;
1447
+ const ownEdge = now.items[dragged]?.edge;
1448
+ const ownRect = now.items[dragged];
1449
+ const homeRect = (() => {
1450
+ if (!ownRect || ownRect.floating || !ownEdge) return null;
1451
+ const o = ownRect.rect;
1452
+ const s = live.find((x) => x.id === ownEdge);
1453
+ if (!s || ownRect.inMenu) return o;
1454
+ const [c0, c1] = s.across();
1455
+ return s.vertical ? rect(Math.min(c0, o.x), o.y, Math.max(c1, o.x + o.width) - Math.min(c0, o.x), o.height) : rect(o.x, Math.min(c0, o.y), o.width, Math.max(c1, o.y + o.height) - Math.min(c0, o.y));
1456
+ })();
1457
+ return [
1458
+ // Home first: its tile is its own, for every edge but its own (which tiles it as its own interval).
1459
+ ...homeRect && ownEdge ? [{ id: ownEdge, key: ownEdge, rect: homeRect, lay: { phase: -1, side: "", at: -Infinity }, place: false }] : [],
1460
+ // Every edge the tiler reads, along each zone with a slot the filter keeps: what it owns, drawn or a band of its
1461
+ // place (a column line's band about its line). A zone whose every slot is refused owns nothing against the others
1462
+ // (its drawn rect still counts, below).
1463
+ ...live.flatMap((s) => ownedRectsOf(s, alongOf(s)).map((r) => ({ id: s.id, key: s.key, rect: r, lay: s.lay, place: !s.drawn }))).filter((o) => area(o.rect) > EPS),
1464
+ // Every drawn rect with the item lifted, the layout the tiles lie in: the edges the tiler doesn't read (folded,
1465
+ // riding, taking nothing) among them.
1466
+ ...Object.entries(base.edges).filter(([id, e]) => area(e.rect) > EPS && c.lay.has(id)).map(([id, e]) => ({ id, key: id, rect: e.rect, lay: c.lay.get(id), place: false })),
1467
+ // And where each edge is seen, as it is now, and no edge lies once lifted (`seenOf`): a stretch is owned by what the
1468
+ // user sees there. An edge that moves when the item lifts never owns, where it lay before, a stretch another edge
1469
+ // lies in once lifted.
1470
+ ...seenOf(c).map(({ edge, rect: r }) => ({ id: edge, key: edge, rect: r, lay: c.lay.get(edge), place: false }))
1471
+ ];
1472
+ }
1473
+ function ownership(c, live, alongOf) {
1474
+ const { scene, rest, now, base, dragged } = c;
1475
+ const owners = ownersOf(c, live, alongOf);
1476
+ const wins = (o, s) => {
1477
+ if (o.key === s.key) return false;
1478
+ if (pushes(o.lay, s.lay)) return true;
1479
+ if (pushes(s.lay, o.lay)) return false;
1480
+ const place = !s.drawn;
1481
+ return o.place === place ? !place || o.lay.at < s.lay.at : !o.place;
1482
+ };
1483
+ const taken = new Map(
1484
+ live.map((s) => [
1485
+ s.key,
1486
+ [
1487
+ ...owners.filter((o) => wins(o, s)).map((o) => o.rect),
1488
+ ...EDGE_KINDS[s.kind].virtual ? owners.filter((o) => o.key !== s.key && !wins(o, s)).flatMap((o) => subtract(o.rect, ownedRectOf(s))) : []
1489
+ ]
1490
+ ])
1491
+ );
1492
+ const bodySpans = (id) => {
1493
+ const e = rest.edges.find((x) => x.id === id);
1494
+ const bandOpt = e?.hitBand ?? DEFAULT_HIT_BAND;
1495
+ if (!e || bandOpt === "middle") return [];
1496
+ const column = axisOf(e.side) === "column";
1497
+ const half = Math.max(0, Number.isFinite(bandOpt) ? bandOpt : DEFAULT_HIT_BAND) / 2;
1498
+ return Object.entries(base.items).filter(([iid, p]) => p.edge === id && p.formKind === "open" && iid !== dragged).map(([, p]) => column ? [p.rect.y + half, p.rect.y + p.rect.height - half] : [p.rect.x + half, p.rect.x + p.rect.width - half]).filter(([a, b]) => b - a > EPS);
1499
+ };
1500
+ const cutsOf = (s) => {
1501
+ const b = bandOf(s);
1502
+ const along = (r) => s.vertical ? [r.y, r.y + r.height] : [r.x, r.x + r.width];
1503
+ const across = (r) => s.vertical ? [r.x, r.x + r.width] : [r.y, r.y + r.height];
1504
+ const rs = taken.get(s.key).filter((r) => overlaps(r, b));
1505
+ const ends = [.../* @__PURE__ */ new Set([...along(b), ...rs.flatMap(along)])].filter((x) => x >= along(b)[0] && x <= along(b)[1]).sort((p, q) => p - q);
1506
+ const owned = [];
1507
+ for (let k = 1; k < ends.length; k++) {
1508
+ const [u, w] = [ends[k - 1], ends[k]];
1509
+ const over = rs.filter((r) => along(r)[0] <= u + EPS && along(r)[1] >= w - EPS).map(across);
1510
+ if (w - u > EPS && less([across(b)], over).reduce((n, [p, q]) => n + q - p, 0) < 1) owned.push([u, w]);
1511
+ }
1512
+ return [...owned, ...bodySpans(s.id)];
1513
+ };
1514
+ const grows = (l, id) => {
1515
+ const it = l.items[id];
1516
+ const e = it.edge ? l.edges[it.edge] : void 0;
1517
+ return !!e && !!formsOf(scene.items[id], axisOf(e.side))[it.form]?.main.grows;
1518
+ };
1519
+ const solid = [.../* @__PURE__ */ new Set([now, base])].flatMap((l) => Object.entries(l.items).filter(([id, it]) => it.fixed && !grows(l, id)).map(([, it]) => it.rect));
1520
+ const bodies = (id) => {
1521
+ const e = rest.edges.find((x) => x.id === id);
1522
+ if (!e) return [];
1523
+ const column = axisOf(e.side) === "column";
1524
+ const across = (a, b) => column ? rect(-1e9, a, 2e9, b - a) : rect(a, -1e9, b - a, 2e9);
1525
+ const reserved = Object.entries(base.items).filter(([iid, p]) => p.edge === id && p.fixed && !p.inMenu && !grows(base, iid)).map(([, p]) => column ? across(p.rect.y, p.rect.y + p.rect.height) : across(p.rect.x, p.rect.x + p.rect.width));
1526
+ return [...reserved, ...bodySpans(id).map(([a, b]) => across(a, b))].filter((b) => area(b) > EPS);
1527
+ };
1528
+ const drawn = [...Object.entries(base.edges).filter(([, e]) => area(e.rect) > EPS).map(([id, e]) => ({ key: id, rect: e.rect })), ...seenOf(c).map((x) => ({ key: x.edge, rect: x.rect }))];
1529
+ const drawnByKey = /* @__PURE__ */ new Map();
1530
+ const drawnBesides = (key) => {
1531
+ let rs = drawnByKey.get(key);
1532
+ if (!rs) drawnByKey.set(key, rs = drawn.filter((d) => d.key !== key).map((d) => d.rect));
1533
+ return rs;
1534
+ };
1535
+ return { owners, taken, cutsOf, bodies, solid, drawnBesides };
1536
+ }
1537
+
1538
+ // src/scene/targets/tiles.ts
1539
+ function tilerOf(spec) {
1540
+ const data = EDGE_KINDS[spec.kind];
1541
+ const v = spec.vertical;
1542
+ const band = (a, b, after) => {
1543
+ const [c0, c1] = spec.across(after);
1544
+ return v ? rect(c0, a, c1 - c0, b - a) : rect(a, c0, b - a, c1 - c0);
1545
+ };
1546
+ const keepAt = (line, h) => v ? { x: h.x + h.width / 2, y: line } : { x: line, y: h.y + h.height / 2 };
1547
+ const tile = (zone, hit, more) => ({ edge: spec.id, key: spec.key, edgeKind: spec.kind, vertical: v, zone, hit, ...more });
1548
+ const split = (t) => {
1549
+ const h = t.hit;
1550
+ const [h0, h1] = v ? [h.x, h.x + h.width] : [h.y, h.y + h.height];
1551
+ const [c0, c1] = [Math.max(h0, spec.owned[0]), Math.min(h1, spec.owned[1])];
1552
+ const part = (a, b) => v ? rect(a, h.y, b - a, h.height) : rect(h.x, a, h.width, b - a);
1553
+ const beyond = less([[h0, h1]], [spec.owned]);
1554
+ if (beyond.length === 0) return [t];
1555
+ if (c1 - c0 <= EPS) return [{ ...t, order: data.overreach }];
1556
+ const core = { ...t, hit: part(c0, c1), keep: t.keep && keepAt(v ? t.keep.y : t.keep.x, part(c0, c1)) };
1557
+ return [core, ...beyond.map(([a, b]) => ({ ...t, kind: "reach", of: core, order: data.overreach, hit: part(a, b), keep: null }))];
1558
+ };
1559
+ return { band, keepAt, tile, split };
1560
+ }
1561
+ function linesIn(spec, z, mine, pieces, [pa, pb], own) {
1562
+ const reserved = spec.reserved;
1563
+ const lines = mine.map((s) => s.line);
1564
+ const lo = Math.min(z.a, ...lines);
1565
+ const hi = Math.max(z.b, ...lines);
1566
+ const free = less([[z.a, z.b]], reserved);
1567
+ const freeLen = free.reduce((n, [a, b]) => n + (b - a), 0);
1568
+ const spread = (x) => {
1569
+ if (hi - lo <= EPS || freeLen <= EPS) return x;
1570
+ let d = (x - lo) / (hi - lo) * freeLen;
1571
+ for (const [a, b] of free) {
1572
+ if (d <= b - a + EPS) return a + Math.min(d, b - a);
1573
+ d -= b - a;
1574
+ }
1575
+ return free[free.length - 1][1];
1576
+ };
1577
+ const past = lines.some((x) => x < z.a - EPS || x > z.b + EPS);
1578
+ const nearer = (p, [a, b]) => p > a && p < b ? p - a < b - p ? a : b : p;
1579
+ const proj = (x) => {
1580
+ const q = reserved.reduce((r, s) => nearer(r, s), past ? spread(x) : x);
1581
+ const p = own ? nearer(q, own) : q;
1582
+ if (pieces.some(([a, b]) => p >= a - EPS && p <= b + EPS)) return p;
1583
+ return pieces.map(([a, b]) => Math.min(Math.max(p, a), b)).reduce((m, y) => Math.abs(y - p) < Math.abs(m - p) ? y : m);
1584
+ };
1585
+ const inPiece = mine.filter((s) => proj(s.line) >= pa - EPS && proj(s.line) <= pb + EPS).sort((a, b) => proj(a.line) - proj(b.line) || a.line - b.line);
1586
+ const projected = inPiece.map((s) => proj(s.line));
1587
+ const at = projected.some((p, i) => i > 0 && p - projected[i - 1] <= EPS) ? inPiece.map((_, i) => pa + (i + 0.5) * (pb - pa) / inPiece.length) : projected;
1588
+ return { inPiece, at };
1589
+ }
1590
+ function tileEdge(c, spec, cuts, lost) {
1591
+ const data = EDGE_KINDS[spec.kind];
1592
+ const v = spec.vertical;
1593
+ const own = spec.own ? v ? [spec.own.y, spec.own.y + spec.own.height] : [spec.own.x, spec.own.x + spec.own.width] : null;
1594
+ const { band, keepAt, tile, split } = tilerOf(spec);
1595
+ const refused = (zone, hit, reason) => tile(zone, hit, { kind: "refuse", id: "", order: data.layer, priority: data.priority, keep: null, reason });
1596
+ const slots = distinctSlots(c, spec.slots);
1597
+ const out = [];
1598
+ for (const z of spec.zones) {
1599
+ const room = less([[z.a, z.b]], [...cuts, ...spec.reserved]).filter(([a, b]) => b - a > EPS);
1600
+ if (room.length === 0) continue;
1601
+ const pieces = own ? less(room, [own]) : room;
1602
+ const mine = slots.filter((s) => s.zone === z.zone && !(spec.holds && s.home) && pieces.some(([a, b]) => b > s.line - data.reach + EPS && a < s.line + data.reach - EPS));
1603
+ const reason = spec.refuse(z.zone);
1604
+ for (const [pa, pb] of pieces) {
1605
+ const { inPiece, at } = linesIn(spec, z, mine, pieces, [pa, pb], own);
1606
+ if (inPiece.length === 0) {
1607
+ const lone = reason === "no-room" && mine.length === 0 && slots.some((s) => s.zone === z.zone && s.home);
1608
+ const near = mine.length === 0 || mine.some((s) => pb > s.line - data.reach + EPS && pa < s.line + data.reach - EPS);
1609
+ if (reason && near) out.push(...split(refused(z.zone, band(pa, pb), lone ? "own-place" : reason)));
1610
+ continue;
1611
+ }
1612
+ inPiece.forEach((s, i) => {
1613
+ const p = at[i];
1614
+ const a = Math.max(i === 0 ? pa : (at[i - 1] + p) / 2, p - data.reach);
1615
+ const b = Math.min(i === inPiece.length - 1 ? pb : (p + at[i + 1]) / 2, p + data.reach);
1616
+ if (b - a <= EPS) {
1617
+ if (s.ok) lost.push(s.id);
1618
+ return;
1619
+ }
1620
+ const line = Math.min(Math.max(p, a), b);
1621
+ const hit = band(a, b, s.result.layout);
1622
+ if (!s.ok) out.push(...split(refused(z.zone, hit, "no-room")));
1623
+ else if (s.home) out.push(...split(tile(z.zone, hit, { kind: "home", id: s.id, order: data.layer, priority: data.priority, keep: keepAt(line, hit), place: { kind: "home" } })));
1624
+ else out.push(...split(tile(z.zone, hit, { kind: "slot", id: s.id, order: data.layer, priority: data.priority, keep: keepAt(line, hit), place: s.place, result: s.result, marker: spec.marker(s), slot: s.result.layout.items[c.dragged].rect })));
1625
+ });
1626
+ }
1627
+ }
1628
+ if (own && spec.own) {
1629
+ const home = spec.slots.find((s) => s.home);
1630
+ const [a, b] = own;
1631
+ const h = band(a, b);
1632
+ const o = spec.own;
1633
+ const hit = v ? rect(Math.min(h.x, o.x), a, Math.max(h.x + h.width, o.x + o.width) - Math.min(h.x, o.x), b - a) : rect(a, Math.min(h.y, o.y), b - a, Math.max(h.y + h.height, o.y + o.height) - Math.min(h.y, o.y));
1634
+ out.push(tile(home?.zone ?? zoneOf(c.scene.items[c.dragged]), hit, { kind: "home", id: home ? home.id : `home:${c.dragged}`, order: 0, priority: home ? data.priority : 1, keep: null, place: { kind: "home" } }));
1635
+ }
1636
+ return out;
1637
+ }
1638
+ var clip = (r, cuts) => cuts.reduce((ps, cut) => ps.flatMap((p) => subtract(p, cut)), [r]);
1639
+ var coverOf = (t) => t.hits ?? [t.hit];
1640
+ var largest = (ps) => ps.reduce((a, p) => area(p) > area(a) ? p : a);
1641
+ var withPieces = (t, ps) => {
1642
+ const { hits: _h, ...rest } = t;
1643
+ return { ...rest, hit: largest(ps), ...ps.length > 1 ? { hits: ps } : {} };
1644
+ };
1645
+ function placeReach(t, pieces, placed, landed, gone) {
1646
+ const of = t.of;
1647
+ const at = landed.get(of) ?? [];
1648
+ if (at.length === 0 && gone.has(of)) {
1649
+ const line = t.vertical ? of.keep.y : of.keep.x;
1650
+ const whole = pieces.filter((p) => p.width >= 1 && p.height >= 1);
1651
+ const on = whole.filter((p) => t.vertical ? p.y <= line + EPS && line <= p.y + p.height + EPS : p.x <= line + EPS && line <= p.x + p.width + EPS);
1652
+ const kept = (on.length > 0 ? on : whole).reduce((a, p) => !a || area(p) > area(a) ? p : a, null);
1653
+ if (kept) {
1654
+ placed.push({ ...of, hit: kept });
1655
+ gone.delete(of);
1656
+ }
1657
+ return;
1658
+ }
1659
+ for (const i of at) {
1660
+ const cover = coverOf(placed[i]);
1661
+ for (let k = 0; k < cover.length; k++) {
1662
+ const h = cover[k];
1663
+ const joint = pieces.find(
1664
+ (p) => t.vertical ? p.y <= h.y + EPS && p.y + p.height >= h.y + h.height - EPS && (Math.abs(p.x - h.x - h.width) < EPS || Math.abs(p.x + p.width - h.x) < EPS) : p.x <= h.x + EPS && p.x + p.width >= h.x + h.width - EPS && (Math.abs(p.y - h.y - h.height) < EPS || Math.abs(p.y + p.height - h.y) < EPS)
1665
+ );
1666
+ if (!joint) continue;
1667
+ const merged = t.vertical ? rect(Math.min(h.x, joint.x), h.y, Math.max(h.x + h.width, joint.x + joint.width) - Math.min(h.x, joint.x), h.height) : rect(h.x, Math.min(h.y, joint.y), h.width, Math.max(h.y + h.height, joint.y + joint.height) - Math.min(h.y, joint.y));
1668
+ placed[i] = cover.length > 1 ? withPieces(placed[i], cover.map((r, j) => j === k ? merged : r)) : { ...placed[i], hit: merged };
1669
+ return;
1670
+ }
1671
+ }
1672
+ }
1673
+ var isRefused = (t) => t.kind === "refuse" || t.kind === "reach" && t.of.kind === "refuse";
1674
+ function clipTiles(tiles, own, chrome, lost) {
1675
+ const { owners, taken, bodies, solid } = own;
1676
+ const byKey = /* @__PURE__ */ new Map();
1677
+ const others = (t) => {
1678
+ let rs = byKey.get(t.key);
1679
+ if (!rs) byKey.set(t.key, rs = owners.filter((o) => o.key !== t.key).map((o) => o.rect));
1680
+ return rs;
1681
+ };
1682
+ const placed = [];
1683
+ const later = [];
1684
+ const refused = tiles.filter(isRefused);
1685
+ const landed = /* @__PURE__ */ new Map();
1686
+ const gone = /* @__PURE__ */ new Set();
1687
+ const place = (t) => {
1688
+ if (t.order === 0) {
1689
+ placed.push(t);
1690
+ return;
1691
+ }
1692
+ const data = EDGE_KINDS[t.edgeKind];
1693
+ if (data.virtual && t.kind === "refuse") {
1694
+ later.push(t);
1695
+ return;
1696
+ }
1697
+ if (t.kind === "reach") {
1698
+ placeReach(t, clip(t.hit, [...placed.flatMap(coverOf), ...others(t), ...bodies(t.edge), ...solid, ...chrome]), placed, landed, gone);
1699
+ return;
1700
+ }
1701
+ const yields = t.kind === "refuse" ? own.drawnBesides(t.key) : [];
1702
+ const cuts = [...placed.flatMap(coverOf), ...taken.get(t.key) ?? [], ...yields, ...data.virtual ? [] : bodies(t.edge), ...solid, ...chrome];
1703
+ const pieces = clip(t.hit, cuts);
1704
+ if (pieces.length === 0) {
1705
+ if (t.kind !== "refuse") gone.add(t);
1706
+ return;
1707
+ }
1708
+ if (t.kind === "refuse") {
1709
+ landed.set(t, pieces.map((_, k) => placed.length + k));
1710
+ for (const p of pieces) placed.push({ ...t, hit: p });
1711
+ return;
1712
+ }
1713
+ const solidPieces = pieces.filter((p) => p.width >= 1 && p.height >= 1);
1714
+ for (const p of pieces) if (!solidPieces.includes(p)) (data.virtual ? later : refused).push({ ...t, kind: "refuse", reason: "no-room", hit: p, keep: null });
1715
+ if (solidPieces.length === 0) return;
1716
+ landed.set(t, [placed.length]);
1717
+ placed.push(withPieces(t, solidPieces));
1718
+ };
1719
+ const byOrder = (a, b) => a.order - b.order;
1720
+ for (const t of tiles.filter((x) => !isRefused(x)).sort(byOrder)) place(t);
1721
+ for (const t of refused.sort(byOrder)) place(t);
1722
+ for (const t of gone) lost.push(t.id);
1723
+ for (const t of later) for (const p of clip(t.hit, [...placed.flatMap(coverOf), ...own.drawnBesides(t.key), ...solid, ...chrome])) placed.push({ ...t, hit: p });
1724
+ return placed;
1725
+ }
1726
+ function seenRefusals(c, live, placed, own, chrome) {
1727
+ const out = [];
1728
+ const covered = placed.flatMap(coverOf);
1729
+ for (const { edge, rect: r } of seenOf(c)) {
1730
+ const e = c.scene.edges.find((x) => x.id === edge);
1731
+ if (!e) continue;
1732
+ const vertical = axisOf(e.side) === "column";
1733
+ const band = e.hitBand ?? DEFAULT_HIT_BAND;
1734
+ const half = band === "middle" ? 0 : Math.max(0, Number.isFinite(band) ? band : DEFAULT_HIT_BAND) / 2;
1735
+ const bodies = band === "middle" ? [] : Object.entries(c.now.items).filter(([id, p]) => p.edge === edge && p.formKind === "open" && id !== c.dragged).map(([, p]) => vertical ? rect(p.rect.x, p.rect.y + half, p.rect.width, p.rect.height - 2 * half) : rect(p.rect.x + half, p.rect.y, p.rect.width - 2 * half, p.rect.height)).filter((b) => b.width > EPS && b.height > EPS);
1736
+ const spec = live.find((s) => s.key === edge);
1737
+ const zones = spec?.zones ?? [{ zone: "start", a: -Infinity, b: Infinity }];
1738
+ const kind = edgeKindOf(c.rest, e);
1739
+ for (const p of clip(r, [...covered, ...bodies, ...own.solid, ...chrome]))
1740
+ for (const z of zones) {
1741
+ const [p0, p1] = vertical ? [p.y, p.y + p.height] : [p.x, p.x + p.width];
1742
+ const [a, b] = [Math.max(p0, z.a), Math.min(p1, z.b)];
1743
+ if (b - a <= EPS) continue;
1744
+ const hit = vertical ? rect(p.x, a, p.width, b - a) : rect(a, p.y, b - a, p.height);
1745
+ const reason = spec?.refuse(z.zone) ?? (zoneTakes(e, z.zone, c.kind) ? "no-room" : "not-accepted");
1746
+ out.push({ edge, key: edge, edgeKind: kind, vertical, zone: z.zone, order: 99, kind: "refuse", id: "", priority: EDGE_KINDS[kind].priority, hit, keep: null, reason });
1747
+ }
1748
+ }
1749
+ return out;
1750
+ }
1751
+
1752
+ // src/scene/targets/virtual.ts
1753
+ function columnLines(c, side) {
1754
+ const { rest, base, dragged } = c;
1755
+ const cols = rest.edges.filter((e) => e.side === side && e.column === true).map((edge) => ({ edge, l: base.edges[edge.id] })).filter((x) => x.l && !x.l.folded && !x.l.rides && x.l.cross > 0).sort((a, b) => a.edge.ring - b.edge.ring).map((x) => ({ edge: x.edge, r: x.l.rect }));
1756
+ const own = Object.values(base.edges).filter((e) => e.side === side && e.rect.height > 0);
1757
+ const span = own[0]?.rect ?? base.content;
1758
+ const rings = sideRings(rest.edges, side);
1759
+ const ringOf = (e) => rings.get(e.id) ?? e.ring;
1760
+ const lines = [];
1761
+ const outerX = (r) => side === "left" ? r.x : r.x + r.width;
1762
+ const innerX = (r) => side === "left" ? r.x + r.width : r.x;
1763
+ const atOf = (e) => c.lay.get(e.id)?.at ?? 0;
1764
+ cols.forEach((x) => lines.push({ x: outerX(x.r), ring: ringOf(x.edge), at: atOf(x.edge) - 0.5 }));
1765
+ if (cols.length > 0) lines.push({ x: innerX(cols[cols.length - 1].r), ring: ringOf(cols[cols.length - 1].edge) + 1, at: atOf(cols[cols.length - 1].edge) + 0.5 });
1766
+ else lines.push({ x: side === "left" ? base.content.x : base.content.x + base.content.width, ring: rings.size, at: Math.max(-1, ...rest.edges.filter((e) => e.side === side).map(atOf)) + 0.5 });
1767
+ const lands = (r) => {
1768
+ const me = r.layout.items[dragged];
1769
+ const el = me.edge ? r.layout.edges[me.edge] : void 0;
1770
+ return !!el && !me.inMenu && !el.folded && !el.rides && r.scene !== c.scene;
1771
+ };
1772
+ const { reach, place } = EDGE_KINDS.columnLine;
1773
+ return lines.map((l) => ({
1774
+ id: `column:${side}`,
1775
+ key: `column:${side}:${l.ring}`,
1776
+ kind: "columnLine",
1777
+ lay: { phase: EDGE_KINDS.columnLine.phase, side, at: l.at },
1778
+ vertical: false,
1779
+ zones: [{ zone: "start", a: l.x - reach, b: l.x + reach }],
1780
+ slots: [slotAt(c, "start", l.x, `column:${side}:${l.ring}`, { kind: "column", side, ring: l.ring }, lands)],
1781
+ refuse: () => "no-room",
1782
+ own: null,
1783
+ holds: false,
1784
+ reserved: [],
1785
+ across: () => [span.y, span.y + span.height],
1786
+ owned: [span.y, span.y + span.height],
1787
+ ownedAlong: [l.x - place / 2, l.x + place / 2],
1788
+ drawn: false,
1789
+ marker: (s) => rect(s.line, span.y, 0, span.height)
1790
+ }));
1791
+ }
1792
+ function railStops(c, side) {
1793
+ const { rest, base, now, dragged } = c;
1794
+ const rail = base.rails[side];
1795
+ const placeRect = base.railPlaces[side];
1796
+ if (!placeRect) return null;
1797
+ const rings = sideRings(rest.edges, side);
1798
+ const mine = rest.edges.filter((e) => e.side === side).sort((a, b) => (rings.get(a.id) ?? 0) - (rings.get(b.id) ?? 0));
1799
+ const ringOf = (id) => rings.get(id) ?? 0;
1800
+ const stops2 = [];
1801
+ let zone;
1802
+ if (rail && rail.segments.length > 0) {
1803
+ zone = rail.rect;
1804
+ const groups = [];
1805
+ for (const s of rail.segments) {
1806
+ const g = groups[groups.length - 1];
1807
+ if (g && g.edge === s.edge) g.y1 = s.rect.y + s.rect.height;
1808
+ else groups.push({ edge: s.edge, y0: s.rect.y, y1: s.rect.y + s.rect.height });
1809
+ }
1810
+ for (const g of groups) stops2.push({ y: g.y0, ring: ringOf(g.edge) });
1811
+ const last = groups[groups.length - 1];
1812
+ stops2.push({ y: last.y1, ring: ringOf(last.edge) + 1 });
1813
+ } else {
1814
+ const firstCol = mine.find((e) => e.column === true);
1815
+ zone = rect(side === "left" ? placeRect.x : placeRect.x + placeRect.width - RAIL_EDGE, placeRect.y, RAIL_EDGE, placeRect.height);
1816
+ stops2.push({ y: placeRect.y, ring: firstCol ? ringOf(firstCol.id) : mine.length });
1817
+ }
1818
+ const ownRail = now.items[dragged]?.rail === side;
1819
+ const lands = (r) => r.layout.items[dragged].rail === side && r.scene !== c.scene;
1820
+ const at = zone;
1821
+ return {
1822
+ id: `rail:${side}`,
1823
+ key: `rail:${side}`,
1824
+ kind: "railStop",
1825
+ // The rail lies outside every edge on its side: laid first among them.
1826
+ lay: { phase: EDGE_KINDS.railStop.phase, side, at: -1 },
1827
+ vertical: true,
1828
+ zones: [{ zone: "start", a: at.y, b: at.y + at.height }],
1829
+ slots: stops2.map((s) => slotAt(c, "start", s.y, `rail:${side}:${s.ring}`, { kind: "rail", side, ring: s.ring }, lands, ownRail)),
1830
+ refuse: () => "no-room",
1831
+ own: null,
1832
+ holds: false,
1833
+ reserved: [],
1834
+ across: () => [at.x, at.x + at.width],
1835
+ // A drawn rail owns its rect; where none shows, a thin band of its place at the side's outer boundary.
1836
+ owned: rail && rail.segments.length > 0 ? [at.x, at.x + at.width] : side === "left" ? [at.x, at.x + Math.min(at.width, EDGE_KINDS.railStop.place)] : [at.x + at.width - Math.min(at.width, EDGE_KINDS.railStop.place), at.x + at.width],
1837
+ ownedAlong: null,
1838
+ drawn: !!rail && rail.segments.length > 0,
1839
+ marker: (s) => rect(at.x, s.result.layout.items[dragged].rect.y, at.width, 0)
1840
+ };
1841
+ }
1842
+
1843
+ // src/scene/targets.ts
1844
+ function specsOf(c, kind) {
1845
+ const { scene, rest } = c;
1846
+ const specs = rest.edges.map((edge) => edgeSpec(c, edge));
1847
+ for (const side of ["left", "right"]) {
1848
+ const template = scene.columns?.[side];
1849
+ if (!template || !zoneTakes(template, "start", kind)) continue;
1850
+ specs.push(...columnLines(c, side));
1851
+ if (scene.rails?.[side]?.accepts.includes(kind)) specs.push(railStops(c, side));
1852
+ }
1853
+ return specs.filter((s) => s !== null);
1854
+ }
1855
+ function emit2(c, placed) {
1856
+ const { scene, now, dragged } = c;
1857
+ const targets2 = [];
1858
+ const refused = [];
1859
+ const homeAfter = () => ({ scene, layout: now });
1860
+ const keyOf = (t) => `refuse:${t.edge}:${t.zone}`;
1861
+ const pos = (t) => t.vertical ? [t.hit.y, t.hit.x] : [t.hit.x, t.hit.y];
1862
+ const rank = /* @__PURE__ */ new Map();
1863
+ const byKey = /* @__PURE__ */ new Map();
1864
+ for (const t of placed) if (t.kind === "refuse") byKey.set(keyOf(t), [...byKey.get(keyOf(t)) ?? [], t]);
1865
+ for (const group of byKey.values()) group.sort((a, b) => pos(a)[0] - pos(b)[0] || pos(a)[1] - pos(b)[1]).forEach((t, n) => rank.set(t, n));
1866
+ let ownHome = null;
1867
+ for (const t of placed) {
1868
+ if (t.kind === "refuse") {
1869
+ const n = rank.get(t);
1870
+ refused.push({ id: n === 0 ? keyOf(t) : `${keyOf(t)}:${n}`, edge: t.edge, zone: t.zone, hit: t.hit, reason: t.reason });
1871
+ continue;
1872
+ }
1873
+ if (t.kind === "home") {
1874
+ if (t.order === 0) ownHome = t.hit;
1875
+ if (t.id === `home:${dragged}`) continue;
1876
+ targets2.push({ id: t.id, priority: t.priority, hit: t.hit, ...t.hits ? { hits: t.hits } : {}, item: dragged, place: { kind: "home" }, after: homeAfter });
1877
+ continue;
1878
+ }
1879
+ const result = t.result;
1880
+ targets2.push({ id: t.id, priority: t.priority, hit: t.hit, ...t.hits ? { hits: t.hits } : {}, ...t.slot ? { slot: t.slot } : {}, ...t.marker ? { marker: t.marker } : {}, item: dragged, place: t.place, after: () => result });
1881
+ }
1882
+ return { targets: targets2, refused, ownHome };
1883
+ }
1884
+ function alongOfSlots(s, tiles) {
1885
+ const spans = tiles.filter((t) => t.kind === "slot").map((t) => s.vertical ? [t.hit.y, t.hit.y + t.hit.height] : [t.hit.x, t.hit.x + t.hit.width]).sort((a, b) => a[0] - b[0]);
1886
+ const out = [];
1887
+ for (const [a, b] of spans) {
1888
+ const last = out[out.length - 1];
1889
+ if (last && a <= last[1] + 1e-6) last[1] = Math.max(last[1], b);
1890
+ else out.push([a, b]);
1891
+ }
1892
+ return out;
1893
+ }
1894
+ function generate(scene, dragged, options) {
1895
+ const ctx = context(scene, dragged, options);
1896
+ if (!ctx) return { targets: [], refusals: [], lost: [] };
1897
+ const { c, item, prev } = ctx;
1898
+ const { now } = c;
1899
+ const live = specsOf(c, item.kind);
1900
+ const along = new Map(live.map((s) => [s.key, alongOfSlots(s, tileEdge(c, s, [], []))]));
1901
+ const own = ownership(c, live, (s) => along.get(s.key));
1902
+ const lost = [];
1903
+ const tiles = live.flatMap((spec) => tileEdge(c, spec, own.cutsOf(spec), lost));
1904
+ const ownRect = now.items[dragged];
1905
+ const homeEdge = scene.edges.find((e) => e.id === ownRect?.edge);
1906
+ if (ownRect && homeEdge && !ownRect.floating && !tiles.some((t) => t.order === 0))
1907
+ tiles.push({ edge: homeEdge.id, key: homeEdge.id, edgeKind: edgeKindOf(scene, homeEdge), vertical: axisOf(homeEdge.side) === "column", zone: zoneOf(item), order: 0, kind: "home", id: `home:${dragged}`, priority: 1, place: { kind: "home" }, hit: ownRect.rect, keep: null });
1908
+ const placed = clipTiles(tiles, own, scene.chrome ?? [], lost);
1909
+ placed.push(...seenRefusals(c, live, placed, own, scene.chrome ?? []));
1910
+ const { targets: targets2, refused, ownHome } = emit2(c, placed);
1911
+ const homeAfter = () => ({ scene, layout: now });
1912
+ if (scene.floatKinds.includes(item.kind)) {
1913
+ const was = scene.floats.find((f) => f.id === dragged);
1914
+ const fo = options.floatOrient ?? was?.orient ?? floatOrientOf(item);
1915
+ const size = options.floatSize ?? (was ? { width: was.rect.width, height: was.rect.height } : floatSizeOf(item, fo));
1916
+ targets2.push({
1917
+ id: `float:${dragged}`,
1918
+ priority: 3,
1919
+ free: true,
1920
+ hit: PLANE,
1921
+ item: dragged,
1922
+ place: { kind: "float" },
1923
+ after(point, grab) {
1924
+ const g = grab ?? { x: size.width / 2, y: size.height / 2 };
1925
+ const raw = rect(point.x - g.x, point.y - g.y, size.width, size.height);
1926
+ const first = solve(use(floatAt(scene, dragged, raw, options.floatOrient), dragged), prev);
1927
+ const at = first.items[dragged]?.rect ?? raw;
1928
+ return c.settle(use(floatAt(scene, dragged, rect(at.x, at.y, size.width, size.height), options.floatOrient), dragged));
1929
+ }
1930
+ });
1931
+ }
1932
+ (scene.blocked ?? []).forEach((b, i) => {
1933
+ targets2.push({ id: `none:${i}`, priority: 1, hit: rect(b.x, b.y, b.width, b.height), item: dragged, place: { kind: "none" }, after: homeAfter });
1934
+ });
1935
+ if (ownRect && !ownRect.floating) targets2.push({ id: `home:${dragged}`, priority: 1, hit: ownHome && !targets2.some((t) => t.place.kind === "home") ? ownHome : ownRect.rect, item: dragged, place: { kind: "home" }, after: homeAfter });
1936
+ return { targets: targets2, refusals: refused, lost };
1937
+ }
1938
+ function targets(scene, dragged, options = {}) {
1939
+ return generate(scene, dragged, options).targets;
1940
+ }
1941
+ function refusals(scene, dragged, options = {}) {
1942
+ return generate(scene, dragged, options).refusals;
1943
+ }
1944
+ var SIDE_RANK = { left: 0, top: 1, bottom: 2, right: 3 };
1945
+ function stops(scene, dragged, options = {}) {
1946
+ const list = targets(scene, dragged, options).filter((t) => t.place.kind !== "home" && t.place.kind !== "none");
1947
+ const sideOf = (t) => {
1948
+ const p = t.place;
1949
+ if (p.kind === "float") return "float";
1950
+ if (p.kind === "column" || p.kind === "rail") return p.side;
1951
+ if (p.kind === "gap") return scene.edges.find((e) => e.id === p.edge)?.side ?? "float";
1952
+ return "float";
1953
+ };
1954
+ const key = (t) => {
1955
+ const side = sideOf(t);
1956
+ const r = t.slot ?? t.hit;
1957
+ const cx = r.x + r.width / 2;
1958
+ const cy = r.y + r.height / 2;
1959
+ const rank = side === "float" ? 4 : SIDE_RANK[side];
1960
+ return side === "left" ? [rank, cx, cy] : side === "right" ? [rank, -cx, cy] : [rank, cy * (side === "bottom" ? -1 : 1), cx];
1961
+ };
1962
+ return list.map((t, i) => ({ t, i, k: key(t) })).sort((a, b) => a.k[0] - b.k[0] || a.k[1] - b.k[1] || a.k[2] - b.k[2] || a.i - b.i).map((x) => x.t);
1963
+ }
1964
+
1965
+ export {
1966
+ SIDES,
1967
+ ZONES,
1968
+ DEFAULT_STRIP,
1969
+ DEFAULT_HIT_BAND,
1970
+ DEFAULT_FLOAT_SIZE,
1971
+ zoneTakes,
1972
+ zonesTaking,
1973
+ isOutsideSideBar,
1974
+ isSideBar,
1975
+ floatOrientOf,
1976
+ floatSizeOf,
1977
+ solve,
1978
+ lift,
1979
+ moveTo,
1980
+ floatAt,
1981
+ fold,
1982
+ use,
1983
+ keep,
1984
+ open,
1985
+ close,
1986
+ raise,
1987
+ orient,
1988
+ resize,
1989
+ setSide,
1990
+ homeFor,
1991
+ dockHome,
1992
+ storeScene,
1993
+ readScene,
1994
+ reservedRect,
1995
+ targets,
1996
+ refusals,
1997
+ stops
1998
+ };
1999
+ //# sourceMappingURL=chunk-CIGRI35P.js.map