@serene-pub/sdk 0.6.0-pr-1

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 (187) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +17 -0
  3. package/README.md +368 -0
  4. package/dist/actions.d.ts +572 -0
  5. package/dist/actions.js +829 -0
  6. package/dist/annexFields.d.ts +234 -0
  7. package/dist/annexFields.js +413 -0
  8. package/dist/announce.d.ts +464 -0
  9. package/dist/announce.js +426 -0
  10. package/dist/attributes.d.ts +670 -0
  11. package/dist/attributes.js +904 -0
  12. package/dist/bands.d.ts +125 -0
  13. package/dist/bands.js +256 -0
  14. package/dist/builder.d.ts +683 -0
  15. package/dist/builder.js +677 -0
  16. package/dist/candidates.d.ts +109 -0
  17. package/dist/candidates.js +118 -0
  18. package/dist/capabilities.d.ts +744 -0
  19. package/dist/capabilities.js +679 -0
  20. package/dist/channels.d.ts +106 -0
  21. package/dist/channels.js +129 -0
  22. package/dist/clauses.d.ts +40 -0
  23. package/dist/clauses.js +43 -0
  24. package/dist/component.d.ts +33 -0
  25. package/dist/component.js +27 -0
  26. package/dist/componentBuild.d.ts +48 -0
  27. package/dist/componentBuild.js +84 -0
  28. package/dist/componentClient.d.ts +100 -0
  29. package/dist/componentClient.js +13 -0
  30. package/dist/componentImports.d.ts +51 -0
  31. package/dist/componentImports.js +106 -0
  32. package/dist/componentModule.d.ts +8 -0
  33. package/dist/componentModule.js +270 -0
  34. package/dist/componentWire.d.ts +130 -0
  35. package/dist/componentWire.js +280 -0
  36. package/dist/config.d.ts +295 -0
  37. package/dist/config.js +231 -0
  38. package/dist/connections.d.ts +89 -0
  39. package/dist/connections.js +131 -0
  40. package/dist/declarations.d.ts +290 -0
  41. package/dist/declarations.js +794 -0
  42. package/dist/descriptors.d.ts +1774 -0
  43. package/dist/descriptors.js +1329 -0
  44. package/dist/dev.d.ts +136 -0
  45. package/dist/dev.js +161 -0
  46. package/dist/document.d.ts +202 -0
  47. package/dist/document.js +312 -0
  48. package/dist/engines.d.ts +117 -0
  49. package/dist/engines.js +203 -0
  50. package/dist/eventMapLaws.d.ts +63 -0
  51. package/dist/eventMapLaws.js +128 -0
  52. package/dist/events.d.ts +554 -0
  53. package/dist/events.js +633 -0
  54. package/dist/executor.d.ts +771 -0
  55. package/dist/executor.js +1829 -0
  56. package/dist/extension.d.ts +448 -0
  57. package/dist/extension.js +315 -0
  58. package/dist/facets.d.ts +53 -0
  59. package/dist/facets.js +60 -0
  60. package/dist/foldedSections.d.ts +74 -0
  61. package/dist/foldedSections.js +161 -0
  62. package/dist/genres.d.ts +507 -0
  63. package/dist/genres.js +633 -0
  64. package/dist/guardedConnection.d.ts +138 -0
  65. package/dist/guardedConnection.js +341 -0
  66. package/dist/handlebarsReach.d.ts +122 -0
  67. package/dist/handlebarsReach.js +222 -0
  68. package/dist/hash.d.ts +146 -0
  69. package/dist/hash.js +166 -0
  70. package/dist/hookGrants.d.ts +47 -0
  71. package/dist/hookGrants.js +68 -0
  72. package/dist/hooks.d.ts +290 -0
  73. package/dist/hooks.js +113 -0
  74. package/dist/hostElements.d.ts +447 -0
  75. package/dist/hostElements.js +702 -0
  76. package/dist/i18n.d.ts +94 -0
  77. package/dist/i18n.js +123 -0
  78. package/dist/identity.d.ts +93 -0
  79. package/dist/identity.js +135 -0
  80. package/dist/index.d.ts +85 -0
  81. package/dist/index.js +176 -0
  82. package/dist/layout.d.ts +194 -0
  83. package/dist/layout.js +40 -0
  84. package/dist/media.d.ts +202 -0
  85. package/dist/media.js +70 -0
  86. package/dist/messageBlocks.d.ts +487 -0
  87. package/dist/messageBlocks.js +683 -0
  88. package/dist/migration.d.ts +128 -0
  89. package/dist/migration.js +261 -0
  90. package/dist/names.d.ts +78 -0
  91. package/dist/names.js +136 -0
  92. package/dist/nodeInput.d.ts +399 -0
  93. package/dist/nodeInput.js +178 -0
  94. package/dist/participants.d.ts +291 -0
  95. package/dist/participants.js +252 -0
  96. package/dist/personGate.d.ts +62 -0
  97. package/dist/personGate.js +77 -0
  98. package/dist/pick.d.ts +84 -0
  99. package/dist/pick.js +103 -0
  100. package/dist/pluginHarness.d.ts +235 -0
  101. package/dist/pluginHarness.js +558 -0
  102. package/dist/pluginPermissions.d.ts +25 -0
  103. package/dist/pluginPermissions.js +97 -0
  104. package/dist/pluginRuleRef.d.ts +13 -0
  105. package/dist/pluginRuleRef.js +10 -0
  106. package/dist/predicates.d.ts +182 -0
  107. package/dist/predicates.js +249 -0
  108. package/dist/preview.d.ts +120 -0
  109. package/dist/preview.js +82 -0
  110. package/dist/promptBlocks.d.ts +130 -0
  111. package/dist/promptBlocks.js +197 -0
  112. package/dist/rankingDecisions.d.ts +60 -0
  113. package/dist/rankingDecisions.js +48 -0
  114. package/dist/receipt.d.ts +313 -0
  115. package/dist/receipt.js +60 -0
  116. package/dist/receiverRules.d.ts +24 -0
  117. package/dist/receiverRules.js +94 -0
  118. package/dist/refs.d.ts +117 -0
  119. package/dist/refs.js +137 -0
  120. package/dist/registry.d.ts +230 -0
  121. package/dist/registry.js +273 -0
  122. package/dist/review.d.ts +108 -0
  123. package/dist/review.js +97 -0
  124. package/dist/sampling.d.ts +152 -0
  125. package/dist/sampling.js +712 -0
  126. package/dist/scope.d.ts +65 -0
  127. package/dist/scope.js +128 -0
  128. package/dist/scripts.d.ts +243 -0
  129. package/dist/scripts.js +326 -0
  130. package/dist/sessionLayout.d.ts +339 -0
  131. package/dist/sessionLayout.js +503 -0
  132. package/dist/sessionSettings.d.ts +70 -0
  133. package/dist/sessionSettings.js +2 -0
  134. package/dist/settings.d.ts +501 -0
  135. package/dist/settings.js +604 -0
  136. package/dist/settingsSlot.d.ts +108 -0
  137. package/dist/settingsSlot.js +166 -0
  138. package/dist/shapes.d.ts +373 -0
  139. package/dist/shapes.js +408 -0
  140. package/dist/sheets.d.ts +165 -0
  141. package/dist/sheets.js +251 -0
  142. package/dist/statShapes.d.ts +96 -0
  143. package/dist/statShapes.js +112 -0
  144. package/dist/status.d.ts +103 -0
  145. package/dist/status.js +125 -0
  146. package/dist/storage.d.ts +145 -0
  147. package/dist/storage.js +33 -0
  148. package/dist/storyTime.d.ts +308 -0
  149. package/dist/storyTime.js +560 -0
  150. package/dist/surfaces.d.ts +501 -0
  151. package/dist/surfaces.js +292 -0
  152. package/dist/template.d.ts +312 -0
  153. package/dist/template.js +554 -0
  154. package/dist/templateCheck.d.ts +60 -0
  155. package/dist/templateCheck.js +516 -0
  156. package/dist/templateFit.d.ts +157 -0
  157. package/dist/templateFit.js +349 -0
  158. package/dist/templateIds.d.ts +138 -0
  159. package/dist/templateIds.js +144 -0
  160. package/dist/templateScopeAt.d.ts +131 -0
  161. package/dist/templateScopeAt.js +354 -0
  162. package/dist/testing.d.ts +177 -0
  163. package/dist/testing.js +383 -0
  164. package/dist/tokenizers.d.ts +113 -0
  165. package/dist/tokenizers.js +184 -0
  166. package/dist/turnLock.d.ts +96 -0
  167. package/dist/turnLock.js +79 -0
  168. package/dist/turnOrder.d.ts +143 -0
  169. package/dist/turnOrder.js +128 -0
  170. package/dist/validate.d.ts +74 -0
  171. package/dist/validate.js +1399 -0
  172. package/dist/values.d.ts +212 -0
  173. package/dist/values.js +390 -0
  174. package/dist/variables.d.ts +493 -0
  175. package/dist/variables.js +662 -0
  176. package/dist/verdicts.d.ts +275 -0
  177. package/dist/verdicts.js +348 -0
  178. package/dist/widgetDecls.d.ts +88 -0
  179. package/dist/widgetDecls.js +119 -0
  180. package/dist/widgets.d.ts +1527 -0
  181. package/dist/widgets.js +208 -0
  182. package/dist/wire.d.ts +126 -0
  183. package/dist/wire.js +140 -0
  184. package/dist/worldinfo.d.ts +150 -0
  185. package/dist/worldinfo.js +212 -0
  186. package/host-elements.d.ts +914 -0
  187. package/package.json +67 -0
@@ -0,0 +1,560 @@
1
+ /**
2
+ * Story time as a stored value — the free-form rung of the story calendar.
3
+ *
4
+ * A **story time** is a position on a book's own calendar: a year, and
5
+ * optionally a month, a day and a time of day. It is what a clock/date stat
6
+ * (`core:stat-shape/story-time@1`) holds, and what a birthdate slot is read
7
+ * as when age is derived.
8
+ *
9
+ * ## Why text, and why this one spelling
10
+ *
11
+ * A stored attribute value is a scalar (`SlotScalar`), so a story time is
12
+ * stored as one canonical line — `412-03-05 22:30` — and parsed back into its
13
+ * parts wherever it is read. The spelling is the one the host already read
14
+ * birthdates in (`412`, `412-03`, `412-03-05`), with a time of day added.
15
+ *
16
+ * Parts, validated beyond shape only against a book's **declared calendar**
17
+ * (`StoryCalendar`, below) when it has one; with none the book is
18
+ * **free-form** — no month lengths, no rollover, no origin. The story-time design (DESIGN-story-time) stores a signed
19
+ * **instant** once a book has a real calendar; when that rung is built the
20
+ * shape moves to `@2` rather than reinterpreting what `@1` stored.
21
+ *
22
+ * ## One comparator
23
+ *
24
+ * `compareStoryTimes` is THE ordering of story times — element-wise, so it is
25
+ * correct for any magnitude (a book numbering "day 250 of year 3" is ordinary
26
+ * free-form practice) and for negative years. Never pack the parts into one
27
+ * number to sort on: a radix packing collides once a part passes its radix,
28
+ * which is the bug the host's `compareDates` was rewritten to fix. The host's
29
+ * lorebook dates delegate here, so one calendar has one comparator.
30
+ */
31
+ /**
32
+ * The range each part of a story time takes on EVERY rung of the calendar —
33
+ * the free-form rule, and the one validity definition for a stored date.
34
+ *
35
+ * A declared calendar only ever narrows it (month count, month lengths, the
36
+ * leap day — `storyTimeProblem`); nothing widens it. `parseStoryTime`,
37
+ * `storyTimeProblem` and core's history entry type all read these, and the
38
+ * entry type's declared range is projected into the database's CHECK — so a
39
+ * date a book's calendar allows is a date the database holds (A15, 2026-09-30:
40
+ * a copied 1–12 / 1–31 once refused month 13 of a thirteen-month calendar and
41
+ * the day after a free-form day 31).
42
+ *
43
+ * ⚠ **Month and day have a floor and no ceiling, on purpose.** A free-form
44
+ * book numbers days of the year ("Year 3, day 250") and may count as many
45
+ * months as it likes; a declared calendar may have any number of months of up
46
+ * to 1000 days each. Those are facts about ONE book, checked against its
47
+ * calendar at entry — never a bound on every book. Zero is refused because it
48
+ * is what an absent part packs to. The year has no range at all: it is one
49
+ * signed count, negative before year 1.
50
+ *
51
+ * Not the calendar's own limits (`storyCalendarProblems`: a month's LENGTH is
52
+ * 1–1000 days) — those bound a calendar's shape, not a date. And ranges only:
53
+ * the narrowing rule (a day needs a month, a minute needs an hour) is
54
+ * `storyTimeProblem`'s, which reads these for every part and is the check
55
+ * every dated writer answers with.
56
+ * @experimental
57
+ */
58
+ export const STORY_TIME_PART_RANGES = {
59
+ month: { min: 1 },
60
+ day: { min: 1 },
61
+ hour: { min: 0, max: 23 },
62
+ minute: { min: 0, max: 59 },
63
+ };
64
+ const RANGE = STORY_TIME_PART_RANGES;
65
+ // `-12-03-05 06:30`: a signed year, then month and day, then an optional time.
66
+ // Month and day are unbounded above on purpose (`STORY_TIME_PART_RANGES`).
67
+ const STORY_TIME = /^\s*(-?\d+)(?:-(\d+))?(?:-(\d+))?(?:[ T](\d{1,2}):(\d{2}))?\s*$/;
68
+ /**
69
+ * The story time a stored value spells, or `null` when it spells none.
70
+ *
71
+ * A number is a bare year (the oldest spelling a birthdate was ever typed
72
+ * in); a string is the canonical line, trimmed. Anything else — and a month,
73
+ * day, hour or minute out of its range — is not a story time.
74
+ * @experimental
75
+ */
76
+ export function parseStoryTime(value) {
77
+ if (typeof value === 'number')
78
+ return Number.isInteger(value) ? { year: value } : null;
79
+ if (typeof value !== 'string')
80
+ return null;
81
+ const m = STORY_TIME.exec(value);
82
+ if (!m)
83
+ return null;
84
+ const out = { year: Number(m[1]) };
85
+ if (m[2] !== undefined) {
86
+ const month = Number(m[2]);
87
+ if (month < RANGE.month.min)
88
+ return null;
89
+ out.month = month;
90
+ }
91
+ if (m[3] !== undefined) {
92
+ const day = Number(m[3]);
93
+ if (day < RANGE.day.min)
94
+ return null;
95
+ out.day = day;
96
+ }
97
+ if (m[4] !== undefined) {
98
+ const hour = Number(m[4]);
99
+ const minute = Number(m[5]);
100
+ if (hour > RANGE.hour.max || minute > RANGE.minute.max)
101
+ return null;
102
+ out.hour = hour;
103
+ out.minute = minute;
104
+ }
105
+ return out;
106
+ }
107
+ const pad = (n) => String(n).padStart(2, '0');
108
+ const has = (n) => typeof n === 'number' && Number.isFinite(n);
109
+ /**
110
+ * The canonical line a story time is stored as — `412-03-05 22:30`.
111
+ *
112
+ * A day is only spelled beside a month (`412--05` is not a line anybody could
113
+ * read back), and a minute only beside an hour.
114
+ * @experimental
115
+ */
116
+ export function storyTimeText(time) {
117
+ let out = String(Math.trunc(time.year));
118
+ if (has(time.month)) {
119
+ out += `-${pad(time.month)}`;
120
+ if (has(time.day))
121
+ out += `-${pad(time.day)}`;
122
+ }
123
+ if (has(time.hour))
124
+ out += ` ${pad(time.hour)}:${pad(has(time.minute) ? time.minute : 0)}`;
125
+ return out;
126
+ }
127
+ /**
128
+ * Which of two story times comes first — **the ordering key; sort with this.**
129
+ *
130
+ * Element-wise. An absent month or day is 0 and an absent hour or minute is
131
+ * -1, so each sorts BEFORE a present one: "Year 2" is the whole year and
132
+ * comes before "Year 2, Mo. 5" inside it, and a date with no time comes
133
+ * before midnight on it.
134
+ * @experimental
135
+ */
136
+ export function compareStoryTimes(a, b) {
137
+ const part = (v, absent) => (has(v) ? v : absent);
138
+ return (a.year - b.year ||
139
+ part(a.month, 0) - part(b.month, 0) ||
140
+ part(a.day, 0) - part(b.day, 0) ||
141
+ part(a.hour, -1) - part(b.hour, -1) ||
142
+ part(a.minute, -1) - part(b.minute, -1));
143
+ }
144
+ /**
145
+ * A story time as a person reads it.
146
+ *
147
+ * With no calendar — **free-form**, the bottom rung and every book's default —
148
+ * `Year 412, Mo. 3, Day 5, 22:30`, exactly today's spelling (the story-time
149
+ * design's open decision #4, non-negotiable). With a declared calendar the same
150
+ * parts are spelled through it: `Moonday, 5 Thaw, Year 412 AR, 22:30`.
151
+ *
152
+ * ⚠ A date the calendar cannot place (month 13 of twelve) is spelled
153
+ * free-form rather than guessed at. Dates are validated at entry once a book
154
+ * declares a calendar, so this is the fallback for a row that predates it,
155
+ * never an ordinary path.
156
+ * @experimental
157
+ */
158
+ export function formatStoryTime(time, calendar) {
159
+ const clock = has(time.hour) ? `, ${pad(time.hour)}:${pad(has(time.minute) ? time.minute : 0)}` : '';
160
+ if (calendar && storyTimeProblem(time, calendar) === null) {
161
+ const year = storyYearText(time.year, calendar);
162
+ if (!has(time.month))
163
+ return year + clock;
164
+ const month = calendar.months[time.month - 1].name;
165
+ if (!has(time.day))
166
+ return `${month}, ${year}${clock}`;
167
+ const weekday = storyWeekdayOf(time, calendar);
168
+ return `${weekday ? `${weekday}, ` : ''}${time.day} ${month}, ${year}${clock}`;
169
+ }
170
+ let out = `Year ${Math.trunc(time.year)}`;
171
+ if (has(time.month) && time.month)
172
+ out += `, Mo. ${time.month}`;
173
+ if (has(time.day) && time.day)
174
+ out += `, Day ${time.day}`;
175
+ return out + clock;
176
+ }
177
+ const isInt = (n) => typeof n === 'number' && Number.isInteger(n);
178
+ const floorDiv = (a, b) => Math.floor(a / b);
179
+ const mod = (a, b) => ((a % b) + b) % b;
180
+ /**
181
+ * What is wrong with a calendar, one sentence per fault; empty when nothing is.
182
+ *
183
+ * Refuses rather than repairs: a calendar that silently dropped a nameless
184
+ * month would renumber every month after it.
185
+ * @experimental
186
+ */
187
+ export function storyCalendarProblems(calendar) {
188
+ const out = [];
189
+ const c = calendar;
190
+ if (!c || typeof c !== 'object')
191
+ return ['A calendar must be an object.'];
192
+ if (!Array.isArray(c.months) || c.months.length === 0)
193
+ out.push('A calendar needs at least one month.');
194
+ else
195
+ c.months.forEach((m, i) => {
196
+ if (!m || typeof m.name !== 'string' || !m.name.trim())
197
+ out.push(`Month ${i + 1} needs a name.`);
198
+ if (!m || !isInt(m.days) || m.days < 1 || m.days > 1000)
199
+ out.push(`Month ${i + 1} needs a length of 1 to 1000 days.`);
200
+ });
201
+ if (c.weekdays !== undefined) {
202
+ if (!Array.isArray(c.weekdays))
203
+ out.push('Weekdays must be a list of names.');
204
+ else if (c.weekdays.some((w) => typeof w !== 'string' || !w.trim()))
205
+ out.push('Every weekday needs a name.');
206
+ }
207
+ if (c.firstWeekday !== undefined) {
208
+ const n = Array.isArray(c.weekdays) ? c.weekdays.length : 0;
209
+ if (!isInt(c.firstWeekday) || c.firstWeekday < 0 || (n > 0 && c.firstWeekday >= n) || (n === 0 && c.firstWeekday !== 0))
210
+ out.push('The first weekday must be one of the weekdays.');
211
+ }
212
+ if (c.yearLabel !== undefined && (typeof c.yearLabel !== 'string' || c.yearLabel.length > 40))
213
+ out.push('The year label must be text of at most 40 characters.');
214
+ if (c.leap != null) {
215
+ const months = Array.isArray(c.months) ? c.months.length : 0;
216
+ if (!isInt(c.leap.every) || c.leap.every < 1)
217
+ out.push('A leap rule needs a whole number of years, 1 or more.');
218
+ if (!isInt(c.leap.month) || c.leap.month < 1 || c.leap.month > months)
219
+ out.push('A leap rule must name one of the months.');
220
+ }
221
+ if (c.eras !== undefined) {
222
+ if (!Array.isArray(c.eras))
223
+ out.push('Eras must be a list.');
224
+ else
225
+ c.eras.forEach((e, i) => {
226
+ if (!e || typeof e.name !== 'string' || !e.name.trim())
227
+ out.push(`Era ${i + 1} needs a name.`);
228
+ if (e?.start === null) {
229
+ if (i !== 0)
230
+ out.push(`Only the first era may have no start (era ${i + 1}).`);
231
+ }
232
+ else if (!isInt(e?.start))
233
+ out.push(`Era ${i + 1} needs a start year.`);
234
+ else if (i > 0) {
235
+ const prev = c.eras[i - 1]?.start;
236
+ if (isInt(prev) && e.start <= prev)
237
+ out.push(`Era ${i + 1} must start after era ${i}.`);
238
+ }
239
+ if (e?.backwards && i === c.eras.length - 1)
240
+ out.push(`Era ${i + 1} counts down, so another era must follow it.`);
241
+ });
242
+ }
243
+ return out;
244
+ }
245
+ /**
246
+ * A stored calendar value, checked: `null` for free-form, the calendar for a
247
+ * well-formed one, and a thrown error naming every fault otherwise. The copy
248
+ * carries only the declared keys.
249
+ * @experimental
250
+ */
251
+ export function parseStoryCalendar(value) {
252
+ if (value === null || value === undefined)
253
+ return null;
254
+ const problems = storyCalendarProblems(value);
255
+ if (problems.length)
256
+ throw new Error(problems.join(' '));
257
+ const c = value;
258
+ const out = { months: c.months.map((m) => ({ name: m.name.trim(), days: m.days })) };
259
+ if (c.weekdays !== undefined)
260
+ out.weekdays = c.weekdays.map((w) => w.trim());
261
+ if (c.firstWeekday !== undefined)
262
+ out.firstWeekday = c.firstWeekday;
263
+ if (c.yearLabel !== undefined)
264
+ out.yearLabel = c.yearLabel;
265
+ if (c.leap != null)
266
+ out.leap = { every: c.leap.every, month: c.leap.month };
267
+ if (c.eras !== undefined)
268
+ out.eras = c.eras.map((e) => ({ name: e.name.trim(), start: e.start, ...(e.backwards ? { backwards: true } : {}) }));
269
+ return out;
270
+ }
271
+ const isLeapYear = (year, calendar) => !!calendar.leap && mod(year, calendar.leap.every) === 0;
272
+ /**
273
+ * How many days a month has in a given year — its length, plus the leap day
274
+ * when the leap rule names it and the year is a leap year.
275
+ * @experimental
276
+ */
277
+ export function storyDaysInMonth(year, month, calendar) {
278
+ const base = calendar.months[month - 1]?.days ?? 0;
279
+ return base + (calendar.leap?.month === month && isLeapYear(year, calendar) ? 1 : 0);
280
+ }
281
+ /**
282
+ * Why a date does not land in a calendar, or `null` when it does.
283
+ *
284
+ * **Every rung** holds a story time to `STORY_TIME_PART_RANGES` and to the
285
+ * narrowing rule — a day needs a month, a minute needs an hour — so free-form
286
+ * (`null` calendar) takes any whole parts in range and no more. A declared
287
+ * calendar then narrows further: its month count, its month lengths, the leap
288
+ * day. This is the one check every dated writer answers with (history,
289
+ * amendments, fork dates, placements, clocks): one rule, one sentence.
290
+ * @experimental
291
+ */
292
+ export function storyTimeProblem(time, calendar) {
293
+ if (!isInt(time.year))
294
+ return 'A date needs a whole year.';
295
+ if (has(time.month) && (!isInt(time.month) || time.month < RANGE.month.min))
296
+ return `A month is a whole number, ${RANGE.month.min} or more.`;
297
+ if (has(time.day) && (!isInt(time.day) || time.day < RANGE.day.min))
298
+ return `A day is a whole number, ${RANGE.day.min} or more.`;
299
+ if (has(time.day) && !has(time.month))
300
+ return 'A day needs a month.';
301
+ if (has(time.hour) && (!isInt(time.hour) || time.hour < RANGE.hour.min || time.hour > RANGE.hour.max))
302
+ return `An hour is a whole number, ${RANGE.hour.min} to ${RANGE.hour.max}.`;
303
+ if (has(time.minute)) {
304
+ if (!has(time.hour))
305
+ return 'A minute needs an hour.';
306
+ if (!isInt(time.minute) || time.minute < RANGE.minute.min || time.minute > RANGE.minute.max)
307
+ return `A minute is a whole number, ${RANGE.minute.min} to ${RANGE.minute.max}.`;
308
+ }
309
+ if (!calendar)
310
+ return null;
311
+ if (has(time.month) && time.month > calendar.months.length)
312
+ return `There is no month ${time.month}: this calendar has ${calendar.months.length}.`;
313
+ if (has(time.day) && has(time.month)) {
314
+ const days = storyDaysInMonth(time.year, time.month, calendar);
315
+ if (time.day > days)
316
+ return `${calendar.months[time.month - 1].name} has ${days} days in year ${time.year}, not ${time.day}.`;
317
+ }
318
+ return null;
319
+ }
320
+ /**
321
+ * Days from Year 1, month 1, day 1 to this date — negative before it; `null`
322
+ * without a full date or for one that does not land.
323
+ *
324
+ * ⚠ A POSITION FOR ARITHMETIC (weekdays), not a stored key and not an
325
+ * ordering: the design's `day_index` column is P2 and not built.
326
+ * @experimental
327
+ */
328
+ export function storyDayIndex(time, calendar) {
329
+ if (!has(time.month) || !has(time.day) || storyTimeProblem(time, calendar))
330
+ return null;
331
+ const yearDays = calendar.months.reduce((n, m) => n + m.days, 0);
332
+ const y = time.year - 1;
333
+ let days = y * yearDays + (calendar.leap ? floorDiv(y, calendar.leap.every) : 0);
334
+ for (let m = 1; m < time.month; m++)
335
+ days += storyDaysInMonth(time.year, m, calendar);
336
+ return days + time.day - 1;
337
+ }
338
+ function storyWeekdayOf(time, calendar) {
339
+ const week = calendar.weekdays ?? [];
340
+ if (!week.length)
341
+ return null;
342
+ const index = storyDayIndex(time, calendar);
343
+ return index === null ? null : week[mod(index + (calendar.firstWeekday ?? 0), week.length)];
344
+ }
345
+ function storyYearText(year, calendar) {
346
+ const label = calendar.yearLabel ?? 'Year';
347
+ const eras = calendar.eras ?? [];
348
+ let spelled = String(year);
349
+ for (let i = eras.length - 1; i >= 0; i--) {
350
+ const era = eras[i];
351
+ if (era.start !== null && year < era.start)
352
+ continue;
353
+ const next = eras[i + 1]?.start;
354
+ const n = era.backwards && isInt(next) ? next - year : era.start === null ? year : year - era.start + 1;
355
+ spelled = `${n} ${era.name}`;
356
+ break;
357
+ }
358
+ return label ? `${label} ${spelled}` : spelled;
359
+ }
360
+ /**
361
+ * The date one step after `from` — what "the next date in sequence" is.
362
+ *
363
+ * The step is one more of the FINEST part given, and precision is kept: a
364
+ * year-and-month date steps a month, a year alone steps a year.
365
+ *
366
+ * - **Free-form** (no calendar): nothing rolls over. Day 31 of month 1 is
367
+ * followed by day 32 of month 1 — a book numbering days of the year is
368
+ * ordinary free-form practice.
369
+ * - **A declared calendar**: a day past its month's length (leap day
370
+ * included) rolls into day 1 of the next month, and a month past the last
371
+ * rolls into month 1 of the next year.
372
+ *
373
+ * Always sorts after `from` under `compareStoryTimes`.
374
+ * @experimental
375
+ */
376
+ export function nextStoryTime(from, calendar) {
377
+ const month = has(from.month) ? from.month : null;
378
+ const day = has(from.day) ? from.day : null;
379
+ if (!calendar) {
380
+ if (day !== null)
381
+ return { year: from.year, month, day: day + 1 };
382
+ if (month !== null)
383
+ return { year: from.year, month: month + 1, day: null };
384
+ return { year: from.year + 1, month: null, day: null };
385
+ }
386
+ const months = calendar.months.length;
387
+ if (day !== null && month !== null) {
388
+ if (day < storyDaysInMonth(from.year, month, calendar))
389
+ return { year: from.year, month, day: day + 1 };
390
+ if (month < months)
391
+ return { year: from.year, month: month + 1, day: 1 };
392
+ return { year: from.year + 1, month: 1, day: 1 };
393
+ }
394
+ if (month !== null) {
395
+ if (month < months)
396
+ return { year: from.year, month: month + 1, day: null };
397
+ return { year: from.year + 1, month: 1, day: null };
398
+ }
399
+ return { year: from.year + 1, month: null, day: null };
400
+ }
401
+ /**
402
+ * The date a day index names — the inverse of `storyDayIndex` (Year 1,
403
+ * month 1, day 1 is 0; negative before it).
404
+ * @experimental
405
+ */
406
+ export function storyTimeOfDayIndex(index, calendar) {
407
+ const yearDays = calendar.months.reduce((n, m) => n + m.days, 0);
408
+ const average = yearDays + (calendar.leap ? 1 / calendar.leap.every : 0);
409
+ const startOf = (year) => storyDayIndex({ year, month: 1, day: 1 }, calendar);
410
+ // An estimate by the average year, then corrected: at most a step or two.
411
+ let year = Math.floor(index / average) + 1;
412
+ while (startOf(year) > index)
413
+ year--;
414
+ while (startOf(year + 1) <= index)
415
+ year++;
416
+ let rest = index - startOf(year);
417
+ let month = 1;
418
+ while (rest >= storyDaysInMonth(year, month, calendar)) {
419
+ rest -= storyDaysInMonth(year, month, calendar);
420
+ month++;
421
+ }
422
+ return { year, month, day: rest + 1 };
423
+ }
424
+ /** @experimental */
425
+ export const STORY_TIME_UNITS = ['minutes', 'hours', 'days', 'months', 'years'];
426
+ /** A time with only the parts present — never a null key. */
427
+ function partsOf(t) {
428
+ const out = { year: t.year };
429
+ if (has(t.month))
430
+ out.month = t.month;
431
+ if (has(t.month) && has(t.day))
432
+ out.day = t.day;
433
+ if (has(t.hour)) {
434
+ out.hour = t.hour;
435
+ out.minute = has(t.minute) ? t.minute : 0;
436
+ }
437
+ return out;
438
+ }
439
+ /**
440
+ * Move a story time by `by` of `unit` — what a clock's `advance(n, unit)` is
441
+ * (DESIGN-story-time P3). Negative `by` goes back.
442
+ *
443
+ * **By a declared calendar** the step carries: minutes into hours into days
444
+ * (a 24-hour day), days through month lengths and the leap day, months into
445
+ * years. **Free-form** has no unit lengths, so a step changes only the part
446
+ * it names — the **smallest-part rule**: forty days on day 31 is day 71 of
447
+ * the same month, and nothing rolls into a month or a year. A time of day
448
+ * carries into the day in both, since a day is always 24 hours.
449
+ *
450
+ * Refused — with a sentence, never clamped — when `by` is not a whole number
451
+ * other than zero (a step of nothing is a silent no-op), when the time has
452
+ * no part for the unit to count (days on a year-only clock; a carry past
453
+ * midnight with no day to carry into), and when the result does not land
454
+ * (Bloom 31 plus a month is Ember 31; before day or month 1 in free-form).
455
+ * A missing time of day reads as the start of the day when stepping by hours
456
+ * or minutes, and stays missing otherwise.
457
+ * @experimental
458
+ */
459
+ export function advanceStoryTime(from, by, unit, calendar) {
460
+ if (!isInt(by) || by === 0)
461
+ return { problem: 'Advance by a whole number other than zero.' };
462
+ if (!STORY_TIME_UNITS.includes(unit))
463
+ return { problem: `Advance by one of ${STORY_TIME_UNITS.join(', ')} — not "${String(unit)}".` };
464
+ const start = partsOf(from);
465
+ const landing = storyTimeProblem(start, calendar);
466
+ if (landing)
467
+ return { problem: `The clock does not land: ${landing}` };
468
+ const spelled = formatStoryTime(start, calendar);
469
+ const out = { ...start };
470
+ const stepDays = (days) => {
471
+ if (days === 0)
472
+ return null;
473
+ if (!has(out.month) || !has(out.day))
474
+ return `The clock stands at ${spelled}, with no day to count days from.`;
475
+ if (calendar) {
476
+ const moved = storyTimeOfDayIndex(storyDayIndex(out, calendar) + days, calendar);
477
+ out.year = moved.year;
478
+ out.month = moved.month;
479
+ out.day = moved.day;
480
+ return null;
481
+ }
482
+ out.day = out.day + days;
483
+ return out.day < 1
484
+ ? 'That lands before day 1 of the month, and a free-form book has no month lengths to count back through.'
485
+ : null;
486
+ };
487
+ let problem = null;
488
+ if (unit === 'minutes' || unit === 'hours') {
489
+ const total = (has(out.hour) ? out.hour : 0) * 60 + (has(out.minute) ? out.minute : 0) + by * (unit === 'hours' ? 60 : 1);
490
+ out.hour = floorDiv(mod(total, 1440), 60);
491
+ out.minute = mod(total, 60);
492
+ problem = stepDays(floorDiv(total, 1440));
493
+ }
494
+ else if (unit === 'days')
495
+ problem = stepDays(by);
496
+ else if (unit === 'months') {
497
+ if (!has(out.month))
498
+ return { problem: `The clock stands at ${spelled}, with no month to count months from.` };
499
+ if (calendar) {
500
+ const count = calendar.months.length;
501
+ const index = out.year * count + (out.month - 1) + by;
502
+ out.year = floorDiv(index, count);
503
+ out.month = mod(index, count) + 1;
504
+ }
505
+ else {
506
+ out.month = out.month + by;
507
+ if (out.month < 1)
508
+ problem = 'That lands before month 1, and a free-form book has no year lengths to count back through.';
509
+ }
510
+ }
511
+ else
512
+ out.year = out.year + by;
513
+ if (problem)
514
+ return { problem };
515
+ const lands = storyTimeProblem(out, calendar);
516
+ if (lands)
517
+ return { problem: `${by > 0 ? 'Advancing' : 'Going back'} ${Math.abs(by)} ${unit} does not land: ${lands}` };
518
+ return { time: partsOf(out) };
519
+ }
520
+ const WEEK = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
521
+ /**
522
+ * The presets the calendar editor starts from (DESIGN-story-time P5). Each is
523
+ * an ordinary declared calendar — a starting point, never a live link.
524
+ * @experimental
525
+ */
526
+ export const STORY_CALENDAR_PRESETS = [
527
+ {
528
+ id: 'twelve-months',
529
+ name: 'Twelve months, 365 days',
530
+ calendar: {
531
+ months: [
532
+ { name: 'January', days: 31 },
533
+ { name: 'February', days: 28 },
534
+ { name: 'March', days: 31 },
535
+ { name: 'April', days: 30 },
536
+ { name: 'May', days: 31 },
537
+ { name: 'June', days: 30 },
538
+ { name: 'July', days: 31 },
539
+ { name: 'August', days: 31 },
540
+ { name: 'September', days: 30 },
541
+ { name: 'October', days: 31 },
542
+ { name: 'November', days: 30 },
543
+ { name: 'December', days: 31 },
544
+ ],
545
+ weekdays: WEEK,
546
+ firstWeekday: 0,
547
+ yearLabel: 'Year',
548
+ leap: { every: 4, month: 2 },
549
+ },
550
+ },
551
+ {
552
+ id: 'thirty-day-months',
553
+ name: 'Twelve months of thirty days',
554
+ calendar: {
555
+ months: Array.from({ length: 12 }, (_, i) => ({ name: `Month ${i + 1}`, days: 30 })),
556
+ yearLabel: 'Year',
557
+ },
558
+ },
559
+ ];
560
+ //# sourceMappingURL=storyTime.js.map