@mark1russell7/stack-spend 0.0.0-stage → 0.1.6

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.
@@ -0,0 +1,421 @@
1
+ import { o as SPEND_SCHEMA } from "./reads-BNyUIVaY.js";
2
+ import { app, axes, each, evaluate, lit, ref, space } from "@mark1russell7/vex";
3
+ import { BoolDomain, NumDomain, TextDomain } from "@mark1russell7/vex-domains";
4
+ import { dayNumber, dayNumber as dayNumber$1, joinRecords, joinSetting, partsById } from "@mark1russell7/system-stack";
5
+ // src/formulas.ts
6
+ /**
7
+ * These are the kinds of the nodes of the spend tree.
8
+ *
9
+ * - `layer`: the root.
10
+ * - `service`: a child of the layer. It gives the cost of a service, or the cost before the end of its plan.
11
+ * - `limit`: a child of a service or of a `next` node.
12
+ * - `next`: a child of a service. It gives the cost after the end of the plan.
13
+ */
14
+ const NODE_KINDS = [
15
+ "layer",
16
+ "service",
17
+ "limit",
18
+ "next"
19
+ ];
20
+ /** This function gives the test of the kind of a node. */
21
+ function isKind(kind) {
22
+ return app("eq", ref("kind"), lit(kind));
23
+ }
24
+ /** This function gives the test of the field `limit` of a limit node: the kind of the limit. */
25
+ function isLimit(kind) {
26
+ return app("eq", ref("limit"), lit(kind));
27
+ }
28
+ /** This test tells if the balance of a prepaid limit gets money automatically. A limit without the field gives false. */
29
+ const RECHARGES = app("ifError", ref("recharge"), lit(false));
30
+ /**
31
+ * This test tells if a limit node is a hard limit: the service stops at its amount.
32
+ * A hard limit is a limit of the provider, a stop by code, or a prepaid balance with no automatic recharge.
33
+ */
34
+ const IS_HARD = app("or", app("or", isLimit("provider"), isLimit("code")), app("and", isLimit("prepaid"), app("not", RECHARGES)));
35
+ /** The limits of a plan: the limit nodes among the children of a service node or of a `next` node. */
36
+ const LIMITS = axes.where(axes.children, isKind("limit"));
37
+ /** The hard limits of a plan: its limit nodes that pass `IS_HARD`. */
38
+ const HARD_LIMITS = axes.where(LIMITS, IS_HARD);
39
+ /** The amount of each hard limit of a plan. */
40
+ const HARD_AMOUNTS = each(HARD_LIMITS, ref("amount"));
41
+ /** The count of the hard limits of a plan. */
42
+ const HARD_COUNT = app("count", HARD_AMOUNTS);
43
+ /**
44
+ * The hard limit of a plan for a month: the smallest amount of its hard limits.
45
+ * A plan with no hard limit gives `#N/A`. The program evaluates at a service node or at a `next` node.
46
+ */
47
+ const HARD_LIMIT = app("min", HARD_AMOUNTS);
48
+ /** This function gives the test of the billing of a plan. */
49
+ function isBilling(billing) {
50
+ return app("eq", ref("billing"), lit(billing));
51
+ }
52
+ /**
53
+ * The largest cost of one plan in a month. The program evaluates at a service node or at a `next` node.
54
+ *
55
+ * - `free`: 0.
56
+ * - `fixed`: the price. A plan with no price gives `#N/A`.
57
+ * - `metered`: the price, or 0, plus the hard limit. A plan with no hard limit gives `#N/A`: its cost has no bound.
58
+ */
59
+ const PLAN_MAX = app("if", isBilling("free"), lit(0), app("if", isBilling("fixed"), ref("price"), app("add", app("ifError", ref("price"), lit(0)), HARD_LIMIT)));
60
+ /** The plan of a service after the end of its plan: the `next` node among the children of the service. A service has one at most. */
61
+ const NEXT_PLANS = axes.where(axes.children, isKind("next"));
62
+ /** The count of the `next` nodes of a service: 0 or 1. The items are the literal 1, so a plan with no bound has a count too. */
63
+ const NEXT_COUNT = app("count", each(NEXT_PLANS, lit(1)));
64
+ /** The largest cost of a month after the end of the plan. A cost with no bound gives `#N/A`. */
65
+ const NEXT_MAX = app("nth", each(NEXT_PLANS, PLAN_MAX), lit(0));
66
+ /**
67
+ * The largest cost of a service in a month, before the end of its plan or after it.
68
+ *
69
+ * - A service with no plan after an end gives `PLAN_MAX`.
70
+ * - A service with a plan after an end gives the larger of the two costs. When one of the two has no bound, the result is `#N/A`.
71
+ */
72
+ const MONTH_MAX = app("if", app("eq", NEXT_COUNT, lit(0)), PLAN_MAX, app("max", PLAN_MAX, NEXT_MAX));
73
+ /** This test tells if the cost of a service in a month has a bound. */
74
+ const HAS_MAX = app("ifError", app("eq", MONTH_MAX, MONTH_MAX), lit(false));
75
+ /** The largest cost of each service of the layer in a month. The program evaluates at the layer node. */
76
+ const MONTH_MAXES = each(axes.children, MONTH_MAX);
77
+ /** The sum of the largest costs of the services that have a bound. The program evaluates at the layer node. */
78
+ const BOUNDED_TOTAL = app("sum", MONTH_MAXES);
79
+ /** The keys of the services whose cost in a month has no bound. The program evaluates at the layer node. */
80
+ const OPEN_SERVICES = app("keys", each(axes.where(axes.children, app("not", HAS_MAX)), ref("kind")));
81
+ // src/values.ts
82
+ /** This function gives the number of a result, or `undefined` for an error value. */
83
+ function numberOf(result) {
84
+ return result.ok && typeof result.value === "number" ? result.value : void 0;
85
+ }
86
+ /** This function gives the boolean of a result, or `undefined` for an error value. */
87
+ function booleanOf(result) {
88
+ return result.ok && typeof result.value === "boolean" ? result.value : void 0;
89
+ }
90
+ // src/states.ts
91
+ /** The key of the layer node: the root of the spend tree. */
92
+ const LAYER_KEY = "layer";
93
+ const SERVICE_PREFIX = "service:";
94
+ /** This function gives the key of the node of a service: the text `service:` and the `id` of its part. */
95
+ function serviceKey(part) {
96
+ return `${SERVICE_PREFIX}${part}`;
97
+ }
98
+ const NEXT_PREFIX = "next:";
99
+ /**
100
+ * This function gives the key of the node of the plan after the end of the plan of a service.
101
+ * The key is the text `next:` and the `id` of the part of the service.
102
+ */
103
+ function nextKey(part) {
104
+ return `${NEXT_PREFIX}${part}`;
105
+ }
106
+ /** This function gives the record of a limit node. */
107
+ function limitRecord(limit) {
108
+ return {
109
+ kind: "limit",
110
+ limit: limit.kind,
111
+ amount: limit.amount,
112
+ ...limit.recharge === void 0 ? {} : { recharge: limit.recharge },
113
+ ...limit.stop === void 0 ? {} : { stop: limit.stop }
114
+ };
115
+ }
116
+ /** This function gives the first record of each part of a layer, in the sequence of the records. */
117
+ function firstServices(layer) {
118
+ const seen = /* @__PURE__ */ new Set();
119
+ return layer.services.filter((service) => {
120
+ if (seen.has(service.part)) return false;
121
+ seen.add(service.part);
122
+ return true;
123
+ });
124
+ }
125
+ /**
126
+ * This function gives the tree of a layer, as a space of Vex.
127
+ *
128
+ * - The layer is the root, with the key `LAYER_KEY`.
129
+ * - A service is a child of the layer. Its fields are `billing` and `price`.
130
+ * - A limit in force is a child of its service. Its fields are `limit` (its kind), `amount`, `recharge` and `stop`.
131
+ * - The plan after the end of a plan is a child of its service, with the kind `next` and the key of `nextKey`. Its fields are `billing` and `price`.
132
+ * - A limit of the plan after the end is a child of that `next` node.
133
+ *
134
+ * A wanted limit is not in the tree, because it is not in force. The first record of a part is used.
135
+ */
136
+ function spendSpace(layer) {
137
+ const records = { [LAYER_KEY]: { kind: "layer" } };
138
+ const parents = { [LAYER_KEY]: null };
139
+ for (const service of firstServices(layer)) {
140
+ const key = serviceKey(service.part);
141
+ records[key] = {
142
+ kind: "service",
143
+ billing: service.billing,
144
+ ...service.price === void 0 ? {} : { price: service.price }
145
+ };
146
+ parents[key] = LAYER_KEY;
147
+ (service.limits ?? []).forEach((limit, index) => {
148
+ const child = `limit:${service.part}:${String(index)}`;
149
+ records[child] = limitRecord(limit);
150
+ parents[child] = key;
151
+ });
152
+ const next = service.ends?.next;
153
+ if (next === void 0) continue;
154
+ const after = nextKey(service.part);
155
+ records[after] = {
156
+ kind: "next",
157
+ billing: next.billing,
158
+ ...next.price === void 0 ? {} : { price: next.price }
159
+ };
160
+ parents[after] = key;
161
+ (next.limits ?? []).forEach((limit, index) => {
162
+ const child = `next-limit:${service.part}:${String(index)}`;
163
+ records[child] = limitRecord(limit);
164
+ parents[child] = after;
165
+ });
166
+ }
167
+ return space.tree(records, parents);
168
+ }
169
+ /** This function evaluates a program of `formulas.ts` at a node of a tree. It gives the value, or the error value. */
170
+ function run(program, tree, origin) {
171
+ return evaluate(program, {
172
+ space: tree,
173
+ origin,
174
+ domains: [
175
+ NumDomain,
176
+ BoolDomain,
177
+ TextDomain
178
+ ]
179
+ });
180
+ }
181
+ /**
182
+ * This function evaluates a program of `formulas.ts` at a service of a tree. It gives the value, or the error value.
183
+ * `tree` is the result of `spendSpace`, and `part` is the `id` of the part of the service.
184
+ */
185
+ function evaluateService(program, tree, part) {
186
+ return run(program, tree, serviceKey(part));
187
+ }
188
+ /**
189
+ * This function evaluates a program of `formulas.ts` at the plan of a service after the end of its plan.
190
+ * A service with no plan after an end gives an error value. `tree` is the result of `spendSpace`.
191
+ */
192
+ function evaluateNext(program, tree, part) {
193
+ return run(program, tree, nextKey(part));
194
+ }
195
+ /** This function gives the state of the cost of each service of a layer, in the sequence of the services. */
196
+ function serviceStates(layer) {
197
+ const tree = spendSpace(layer);
198
+ return firstServices(layer).map((service) => {
199
+ const hardLimit = numberOf(evaluateService(HARD_LIMIT, tree, service.part));
200
+ const nextLimit = service.ends?.next === void 0 ? void 0 : numberOf(evaluateNext(HARD_LIMIT, tree, service.part));
201
+ const monthMax = numberOf(evaluateService(MONTH_MAX, tree, service.part));
202
+ return {
203
+ part: service.part,
204
+ billing: service.billing,
205
+ ...hardLimit === void 0 ? {} : { hardLimit },
206
+ ...nextLimit === void 0 ? {} : { nextLimit },
207
+ ...monthMax === void 0 ? {} : { monthMax }
208
+ };
209
+ });
210
+ }
211
+ /**
212
+ * This function gives the largest cost of a layer in a month.
213
+ * The cost of the layer has a bound only when `open` is empty. Then `bounded` is that bound.
214
+ */
215
+ function monthTotal(layer) {
216
+ const tree = spendSpace(layer);
217
+ const keys = run(OPEN_SERVICES, tree, LAYER_KEY);
218
+ const open = keys.ok && Array.isArray(keys.value) ? keys.value.filter((key) => typeof key === "string").map((key) => key.slice(8)) : [];
219
+ return {
220
+ bounded: numberOf(run(BOUNDED_TOTAL, tree, "layer")) ?? 0,
221
+ open
222
+ };
223
+ }
224
+ // src/check.ts
225
+ /** These are the codes of the faults of a spend layer. */
226
+ const SPEND_CODES = [
227
+ "service-part",
228
+ "service-duplicate",
229
+ "part-no-service",
230
+ "price",
231
+ "amount",
232
+ "limit-none",
233
+ "limit-alert",
234
+ "limit-recharge",
235
+ "limit-stop",
236
+ "limit-wanted",
237
+ "end-date",
238
+ "end-past",
239
+ "end-soon",
240
+ "end-open",
241
+ "proof-none",
242
+ "proof-date",
243
+ "proof-old"
244
+ ];
245
+ /** This function tells if a value is a finite number, 0 or more. */
246
+ function isZeroOrMore(value) {
247
+ return Number.isFinite(value) && value >= 0;
248
+ }
249
+ /** This function gives the words for a count of days from the date of the check. */
250
+ function inDays(days) {
251
+ if (days === 0) return "on the date of the check";
252
+ return days === 1 ? "in 1 day" : `in ${String(days)} days`;
253
+ }
254
+ /**
255
+ * This function gives each fault of a spend layer for a stack. A list with no error tells that the gate passes.
256
+ *
257
+ * The records:
258
+ * - `service-part`: a record names no part of the stack.
259
+ * - `service-duplicate`: two records name one part.
260
+ * - `part-no-service`: a part of a kind of `kinds` has no record.
261
+ * - `price`: a price is less than 0 or not finite, or a `fixed` plan has no price.
262
+ * - `amount`: the amount of a limit is less than 0 or not finite.
263
+ *
264
+ * The limits:
265
+ * - `limit-none`: the cost of a `metered` service has no limit.
266
+ * - `limit-alert`: the only limits of a `metered` service are alerts. An alert stops nothing.
267
+ * - `limit-recharge`: a prepaid balance gets money automatically, so it is no limit.
268
+ * - `limit-stop`: a stop by code does not name the part of the stack that stops the use.
269
+ * - `limit-wanted`: a wanted limit is not in force.
270
+ *
271
+ * The end of a plan:
272
+ * - `end-date`: the date of an end is not a date. The date of its decision is not a date, or it is after `today`.
273
+ * - `end-past`: the plan ended before `today`. The record gives the cost before the end, so it is not correct.
274
+ * - `end-soon`: the plan ends in `endDays` days, or fewer. This fault is a warning: it does not stop the gate.
275
+ * Its message tells the decision of the owner, when the record has one.
276
+ * - `end-open`: the cost after the end grows with the use, and it has no hard limit.
277
+ *
278
+ * The proofs:
279
+ * - `proof-none`: a record has no proof.
280
+ * - `proof-date`: the date of a proof is not a date, or it is after `today`.
281
+ * - `proof-old`: a proof is older than `proofDays`.
282
+ *
283
+ * The faults `price`, `amount` and `limit-stop` examine the plan after an end too.
284
+ * The faults are in the sequence of the records, then of the parts with no record. The function is pure.
285
+ *
286
+ * @throws RangeError when `today` is not a date `YYYY-MM-DD`
287
+ */
288
+ function checkSpend(layer, stack, options) {
289
+ const today = dayNumber$1(options.today);
290
+ if (today === void 0) throw new RangeError(`The date "${options.today}" is not a date of the form YYYY-MM-DD.`);
291
+ const faults = [];
292
+ const push = (level, code, at, message, other) => {
293
+ faults.push({
294
+ level,
295
+ code,
296
+ at,
297
+ message,
298
+ ...other === void 0 ? {} : { other }
299
+ });
300
+ };
301
+ const add = (code, at, message, other) => {
302
+ push("error", code, at, message, other);
303
+ };
304
+ const parts = partsById(stack);
305
+ const examine = (limit, at, when) => {
306
+ if (!isZeroOrMore(limit.amount)) add("amount", at, `The amount ${String(limit.amount)} of a limit of "${at}"${when} is less than 0 or not finite.`);
307
+ if (limit.kind !== "code") return;
308
+ if (limit.stop === void 0) add("limit-stop", at, `A stop by code of "${at}"${when} does not name the part that stops the use.`);
309
+ else if (!parts.has(limit.stop)) add("limit-stop", at, `A stop by code of "${at}"${when} names "${limit.stop}", which is no part of the stack.`, limit.stop);
310
+ };
311
+ const states = new Map(serviceStates(layer).map((state) => [state.part, state]));
312
+ const endDays = layer.endDays ?? 30;
313
+ const seen = /* @__PURE__ */ new Set();
314
+ for (const service of layer.services) {
315
+ const at = service.part;
316
+ if (seen.has(at)) {
317
+ add("service-duplicate", at, `Two cost records name the part "${at}".`);
318
+ continue;
319
+ }
320
+ seen.add(at);
321
+ if (!parts.has(at)) add("service-part", at, `A cost record names "${at}", which is no part of the stack.`);
322
+ if (service.price !== void 0 && !isZeroOrMore(service.price)) add("price", at, `The price ${String(service.price)} of "${at}" is less than 0 or not finite.`);
323
+ if (service.billing === "fixed" && service.price === void 0) add("price", at, `The service "${at}" has a fixed price, and its record gives no price.`);
324
+ const limits = service.limits ?? [];
325
+ for (const limit of [...limits, ...service.wanted === void 0 ? [] : [service.wanted]]) examine(limit, at, "");
326
+ const recharges = limits.some((limit) => limit.kind === "prepaid" && limit.recharge === true);
327
+ if (recharges) add("limit-recharge", at, `The prepaid balance of "${at}" gets money automatically, so it does not limit the cost.`);
328
+ if (service.wanted !== void 0) add("limit-wanted", at, `The wanted limit of "${at}" (${service.wanted.kind}, ${String(service.wanted.amount)} ${layer.currency} for a month) is not in force.`);
329
+ else if (service.billing === "metered" && states.get(at)?.hardLimit === void 0 && !recharges) {
330
+ if (limits.some((limit) => limit.kind === "alert")) add("limit-alert", at, `The cost of "${at}" grows with the use, and its only limit is an alert. An alert stops nothing.`);
331
+ else add("limit-none", at, `The cost of "${at}" grows with the use, and it has no limit.`);
332
+ }
333
+ if (service.ends !== void 0) {
334
+ const { date, next, decided } = service.ends;
335
+ const end = dayNumber$1(date);
336
+ if (end === void 0) add("end-date", at, `The date "${date}" of the end of the plan of "${at}" is not a date of the form YYYY-MM-DD.`);
337
+ else if (end < today) add("end-past", at, `The plan of "${at}" ended on ${date}. The record gives the cost before the end.`);
338
+ else if (end - today <= endDays) {
339
+ const then = next === void 0 ? "Then the provider stops the service." : "Then the cost changes.";
340
+ const agreed = decided === void 0 ? "" : ` ${decided.by} decided this on ${decided.date}.`;
341
+ push("warning", "end-soon", at, `The plan of "${at}" ends on ${date}, ${inDays(end - today)}. ${then}${agreed}`);
342
+ }
343
+ if (decided !== void 0) {
344
+ const day = dayNumber$1(decided.date);
345
+ const about = `of the decision about the end of the plan of "${at}"`;
346
+ if (day === void 0) add("end-date", at, `The date "${decided.date}" ${about} is not a date of the form YYYY-MM-DD.`);
347
+ else if (day > today) add("end-date", at, `The date ${decided.date} ${about} is after the date of the check, ${options.today}.`);
348
+ }
349
+ if (next !== void 0) {
350
+ const when = " after the end of its plan";
351
+ if (next.price !== void 0 && !isZeroOrMore(next.price)) add("price", at, `The price ${String(next.price)} of "${at}"${when} is less than 0 or not finite.`);
352
+ if (next.billing === "fixed" && next.price === void 0) add("price", at, `The service "${at}" has a fixed price${when}, and its record gives no price.`);
353
+ for (const limit of next.limits ?? []) examine(limit, at, when);
354
+ if (next.billing === "metered" && states.get(at)?.nextLimit === void 0) add("end-open", at, `The plan of "${at}" ends on ${date}. After the end, the cost grows with the use, and it has no hard limit.`);
355
+ }
356
+ }
357
+ if (service.proof === void 0) {
358
+ add("proof-none", at, `The cost record of "${at}" has no proof.`);
359
+ continue;
360
+ }
361
+ const day = dayNumber$1(service.proof.date);
362
+ if (day === void 0) add("proof-date", at, `The date "${service.proof.date}" of the proof of "${at}" is not a date of the form YYYY-MM-DD.`);
363
+ else if (day > today) add("proof-date", at, `The date ${service.proof.date} of the proof of "${at}" is after the date of the check, ${options.today}.`);
364
+ else if (today - day > layer.proofDays) add("proof-old", at, `The proof of "${at}" is ${String(today - day)} days old. The largest age is ${String(layer.proofDays)} days.`);
365
+ }
366
+ const kinds = new Set(layer.kinds);
367
+ for (const part of stack.parts) if (kinds.has(part.kind) && !seen.has(part.id)) add("part-no-service", part.id, `The part "${part.id}" has the kind "${part.kind}", and it has no cost record.`);
368
+ return faults;
369
+ }
370
+ // src/join.ts
371
+ /**
372
+ * This function joins the spend layers of the fragments of a stack into one layer.
373
+ *
374
+ * - The cost records are in the order of the fragments. The record of a part is in one fragment.
375
+ * - A part with a record in two or more fragments is a fault, also when the records are equal.
376
+ * - The layer has one currency and one largest age of a proof. A fragment with a different value is a fault.
377
+ * - The count of days of warning before the end of a plan is one value too. A fragment with no value takes the value of the others.
378
+ * - The kinds are the kinds of each fragment, one time each, in the order of the fragments.
379
+ *
380
+ * The kinds of the joined layer hold for each part of the joined stack. Thus a kind of one fragment needs a cost
381
+ * record also for the parts of that kind in the other fragments. The function is pure, and it changes no layer.
382
+ * The gate `checkSpend` examines the joined layer afterward.
383
+ *
384
+ * @param pieces - The layers, with the name of the fragment of each one, in the order of the fragments.
385
+ * @returns The joined layer, or the faults of the join. An empty list of layers is a fault.
386
+ */
387
+ function joinSpend(pieces) {
388
+ const currency = joinSetting(pieces, (layer) => layer.currency, "currency");
389
+ const proofDays = joinSetting(pieces, (layer) => layer.proofDays, "largest age of a proof in days");
390
+ const endDays = joinSetting(pieces, (layer) => layer.endDays, "count of days of warning before the end of a plan");
391
+ const services = joinRecords(pieces, (layer) => layer.services, (service) => service.part, "cost record of the part");
392
+ if (currency.value === void 0 || proofDays.value === void 0) return {
393
+ ok: false,
394
+ errors: ["The join of spend layers got no layer."]
395
+ };
396
+ const errors = [
397
+ ...currency.errors,
398
+ ...proofDays.errors,
399
+ ...endDays.errors,
400
+ ...services.errors
401
+ ];
402
+ if (errors.length > 0) return {
403
+ ok: false,
404
+ errors
405
+ };
406
+ const kinds = [...new Set(pieces.flatMap((piece) => piece.layer.kinds))];
407
+ return {
408
+ ok: true,
409
+ layer: {
410
+ schema: SPEND_SCHEMA,
411
+ currency: currency.value,
412
+ kinds,
413
+ proofDays: proofDays.value,
414
+ ...endDays.value === void 0 ? {} : { endDays: endDays.value },
415
+ services: services.items
416
+ }
417
+ };
418
+ }
419
+ export { NODE_KINDS as C, NEXT_PLANS as S, PLAN_MAX as T, HARD_COUNT as _, LAYER_KEY as a, IS_HARD as b, firstServices as c, serviceKey as d, serviceStates as f, BOUNDED_TOTAL as g, numberOf as h, dayNumber as i, monthTotal as l, booleanOf as m, SPEND_CODES as n, evaluateNext as o, spendSpace as p, checkSpend as r, evaluateService as s, joinSpend as t, nextKey as u, HARD_LIMIT as v, OPEN_SERVICES as w, MONTH_MAX as x, HARD_LIMITS as y };
420
+
421
+ //# sourceMappingURL=join-98opeDSl.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"join-98opeDSl.js","names":["dayNumber"],"sources":["../src/formulas.ts","../src/values.ts","../src/states.ts","../src/check.ts","../src/join.ts"],"sourcesContent":["import { app, axes, each, lit, ref, type Axis, type Expr } from \"@mark1russell7/vex\";\n\n// The cost math, as Vex programs. A program is data: `explain` of Vex shows each step, and a host can keep it as JSON.\n// The programs evaluate on the tree that `spendSpace` makes from a layer.\n\n/**\n * These are the kinds of the nodes of the spend tree.\n *\n * - `layer`: the root.\n * - `service`: a child of the layer. It gives the cost of a service, or the cost before the end of its plan.\n * - `limit`: a child of a service or of a `next` node.\n * - `next`: a child of a service. It gives the cost after the end of the plan.\n */\nexport const NODE_KINDS: readonly [\"layer\", \"service\", \"limit\", \"next\"] = [\"layer\", \"service\", \"limit\", \"next\"];\n\n/** A kind of node of the spend tree. */\nexport type NodeKind = (typeof NODE_KINDS)[number];\n\n/** This function gives the test of the kind of a node. */\nfunction isKind(kind: NodeKind): Expr {\n return app(\"eq\", ref(\"kind\"), lit(kind));\n}\n\n/** This function gives the test of the field `limit` of a limit node: the kind of the limit. */\nfunction isLimit(kind: string): Expr {\n return app(\"eq\", ref(\"limit\"), lit(kind));\n}\n\n/** This test tells if the balance of a prepaid limit gets money automatically. A limit without the field gives false. */\nconst RECHARGES: Expr = app(\"ifError\", ref(\"recharge\"), lit(false));\n\n/**\n * This test tells if a limit node is a hard limit: the service stops at its amount.\n * A hard limit is a limit of the provider, a stop by code, or a prepaid balance with no automatic recharge.\n */\nexport const IS_HARD: Expr = app(\"or\", app(\"or\", isLimit(\"provider\"), isLimit(\"code\")), app(\"and\", isLimit(\"prepaid\"), app(\"not\", RECHARGES)));\n\n/** The limits of a plan: the limit nodes among the children of a service node or of a `next` node. */\nconst LIMITS: Axis = axes.where(axes.children, isKind(\"limit\"));\n\n/** The hard limits of a plan: its limit nodes that pass `IS_HARD`. */\nexport const HARD_LIMITS: Axis = axes.where(LIMITS, IS_HARD);\n\n/** The amount of each hard limit of a plan. */\nconst HARD_AMOUNTS: Expr = each(HARD_LIMITS, ref(\"amount\"));\n\n/** The count of the hard limits of a plan. */\nexport const HARD_COUNT: Expr = app(\"count\", HARD_AMOUNTS);\n\n/**\n * The hard limit of a plan for a month: the smallest amount of its hard limits.\n * A plan with no hard limit gives `#N/A`. The program evaluates at a service node or at a `next` node.\n */\nexport const HARD_LIMIT: Expr = app(\"min\", HARD_AMOUNTS);\n\n/** This function gives the test of the billing of a plan. */\nfunction isBilling(billing: string): Expr {\n return app(\"eq\", ref(\"billing\"), lit(billing));\n}\n\n/**\n * The largest cost of one plan in a month. The program evaluates at a service node or at a `next` node.\n *\n * - `free`: 0.\n * - `fixed`: the price. A plan with no price gives `#N/A`.\n * - `metered`: the price, or 0, plus the hard limit. A plan with no hard limit gives `#N/A`: its cost has no bound.\n */\nexport const PLAN_MAX: Expr = app(\"if\", isBilling(\"free\"), lit(0), app(\"if\", isBilling(\"fixed\"), ref(\"price\"), app(\"add\", app(\"ifError\", ref(\"price\"), lit(0)), HARD_LIMIT)));\n\n/** The plan of a service after the end of its plan: the `next` node among the children of the service. A service has one at most. */\nexport const NEXT_PLANS: Axis = axes.where(axes.children, isKind(\"next\"));\n\n/** The count of the `next` nodes of a service: 0 or 1. The items are the literal 1, so a plan with no bound has a count too. */\nconst NEXT_COUNT: Expr = app(\"count\", each(NEXT_PLANS, lit(1)));\n\n/** The largest cost of a month after the end of the plan. A cost with no bound gives `#N/A`. */\nconst NEXT_MAX: Expr = app(\"nth\", each(NEXT_PLANS, PLAN_MAX), lit(0));\n\n/**\n * The largest cost of a service in a month, before the end of its plan or after it.\n *\n * - A service with no plan after an end gives `PLAN_MAX`.\n * - A service with a plan after an end gives the larger of the two costs. When one of the two has no bound, the result is `#N/A`.\n */\nexport const MONTH_MAX: Expr = app(\"if\", app(\"eq\", NEXT_COUNT, lit(0)), PLAN_MAX, app(\"max\", PLAN_MAX, NEXT_MAX));\n\n/** This test tells if the cost of a service in a month has a bound. */\nconst HAS_MAX: Expr = app(\"ifError\", app(\"eq\", MONTH_MAX, MONTH_MAX), lit(false));\n\n/** The largest cost of each service of the layer in a month. The program evaluates at the layer node. */\nconst MONTH_MAXES: Expr = each(axes.children, MONTH_MAX);\n\n/** The sum of the largest costs of the services that have a bound. The program evaluates at the layer node. */\nexport const BOUNDED_TOTAL: Expr = app(\"sum\", MONTH_MAXES);\n\n/** The keys of the services whose cost in a month has no bound. The program evaluates at the layer node. */\nexport const OPEN_SERVICES: Expr = app(\"keys\", each(axes.where(axes.children, app(\"not\", HAS_MAX)), ref(\"kind\")));\n","import type { Result } from \"@mark1russell7/vex\";\n\n// The read of the result of a program. This module is internal: no entry exports it.\n\n/** This function gives the number of a result, or `undefined` for an error value. */\nexport function numberOf(result: Result<unknown>): number | undefined {\n return result.ok && typeof result.value === \"number\" ? result.value : undefined;\n}\n\n/** This function gives the boolean of a result, or `undefined` for an error value. */\nexport function booleanOf(result: Result<unknown>): boolean | undefined {\n return result.ok && typeof result.value === \"boolean\" ? result.value : undefined;\n}\n","import { evaluate, space, type Expr, type Result, type Space } from \"@mark1russell7/vex\";\nimport { BoolDomain, NumDomain, TextDomain } from \"@mark1russell7/vex-domains\";\nimport { BOUNDED_TOTAL, HARD_LIMIT, MONTH_MAX, OPEN_SERVICES } from \"./formulas.ts\";\nimport type { Billing, Limit, Service, SpendLayer } from \"./layer.ts\";\nimport { numberOf } from \"./values.ts\";\n\n// The state of the cost of each service of a layer. The layer becomes a tree, and the programs of `formulas.ts` evaluate on it.\n\n/** The key of the layer node: the root of the spend tree. */\nexport const LAYER_KEY = \"layer\";\n\nconst SERVICE_PREFIX = \"service:\";\n\n/** This function gives the key of the node of a service: the text `service:` and the `id` of its part. */\nexport function serviceKey(part: string): string {\n return `${SERVICE_PREFIX}${part}`;\n}\n\nconst NEXT_PREFIX = \"next:\";\n\n/**\n * This function gives the key of the node of the plan after the end of the plan of a service.\n * The key is the text `next:` and the `id` of the part of the service.\n */\nexport function nextKey(part: string): string {\n return `${NEXT_PREFIX}${part}`;\n}\n\n/** This function gives the record of a limit node. */\nfunction limitRecord(limit: Limit): Readonly<Record<string, unknown>> {\n return {\n kind: \"limit\",\n limit: limit.kind,\n amount: limit.amount,\n ...(limit.recharge === undefined ? {} : { recharge: limit.recharge }),\n ...(limit.stop === undefined ? {} : { stop: limit.stop }),\n };\n}\n\n/** This function gives the first record of each part of a layer, in the sequence of the records. */\nexport function firstServices(layer: SpendLayer): readonly Service[] {\n const seen = new Set<string>();\n return layer.services.filter((service) => {\n if (seen.has(service.part)) return false;\n seen.add(service.part);\n return true;\n });\n}\n\n/**\n * This function gives the tree of a layer, as a space of Vex.\n *\n * - The layer is the root, with the key `LAYER_KEY`.\n * - A service is a child of the layer. Its fields are `billing` and `price`.\n * - A limit in force is a child of its service. Its fields are `limit` (its kind), `amount`, `recharge` and `stop`.\n * - The plan after the end of a plan is a child of its service, with the kind `next` and the key of `nextKey`. Its fields are `billing` and `price`.\n * - A limit of the plan after the end is a child of that `next` node.\n *\n * A wanted limit is not in the tree, because it is not in force. The first record of a part is used.\n */\nexport function spendSpace(layer: SpendLayer): Space {\n const records: Record<string, Readonly<Record<string, unknown>>> = { [LAYER_KEY]: { kind: \"layer\" } };\n const parents: Record<string, string | null> = { [LAYER_KEY]: null };\n for (const service of firstServices(layer)) {\n const key = serviceKey(service.part);\n records[key] = { kind: \"service\", billing: service.billing, ...(service.price === undefined ? {} : { price: service.price }) };\n parents[key] = LAYER_KEY;\n (service.limits ?? []).forEach((limit, index) => {\n const child = `limit:${service.part}:${String(index)}`;\n records[child] = limitRecord(limit);\n parents[child] = key;\n });\n const next = service.ends?.next;\n if (next === undefined) continue;\n const after = nextKey(service.part);\n records[after] = { kind: \"next\", billing: next.billing, ...(next.price === undefined ? {} : { price: next.price }) };\n parents[after] = key;\n (next.limits ?? []).forEach((limit, index) => {\n const child = `next-limit:${service.part}:${String(index)}`;\n records[child] = limitRecord(limit);\n parents[child] = after;\n });\n }\n return space.tree(records, parents);\n}\n\n/** This function evaluates a program of `formulas.ts` at a node of a tree. It gives the value, or the error value. */\nfunction run(program: Expr, tree: Space, origin: string): Result<unknown> {\n return evaluate(program, { space: tree, origin, domains: [NumDomain, BoolDomain, TextDomain] });\n}\n\n/**\n * This function evaluates a program of `formulas.ts` at a service of a tree. It gives the value, or the error value.\n * `tree` is the result of `spendSpace`, and `part` is the `id` of the part of the service.\n */\nexport function evaluateService(program: Expr, tree: Space, part: string): Result<unknown> {\n return run(program, tree, serviceKey(part));\n}\n\n/**\n * This function evaluates a program of `formulas.ts` at the plan of a service after the end of its plan.\n * A service with no plan after an end gives an error value. `tree` is the result of `spendSpace`.\n */\nexport function evaluateNext(program: Expr, tree: Space, part: string): Result<unknown> {\n return run(program, tree, nextKey(part));\n}\n\n/** The state of the cost of one service of a layer. */\nexport interface ServiceState {\n /** The `id` of the part of the service. */\n readonly part: string;\n readonly billing: Billing;\n /** The hard limit of the service for a month: the smallest amount of its hard limits. A service with no hard limit has none. */\n readonly hardLimit?: number | undefined;\n /** The hard limit for a month after the end of the plan. A service has none with no plan after an end, or with no hard limit in that plan. */\n readonly nextLimit?: number | undefined;\n /**\n * The largest cost of the service in a month, before the end of its plan or after it.\n * A service has none when one of these costs has no bound.\n */\n readonly monthMax?: number | undefined;\n}\n\n/** This function gives the state of the cost of each service of a layer, in the sequence of the services. */\nexport function serviceStates(layer: SpendLayer): ServiceState[] {\n const tree = spendSpace(layer);\n return firstServices(layer).map((service) => {\n const hardLimit = numberOf(evaluateService(HARD_LIMIT, tree, service.part));\n const nextLimit = service.ends?.next === undefined ? undefined : numberOf(evaluateNext(HARD_LIMIT, tree, service.part));\n const monthMax = numberOf(evaluateService(MONTH_MAX, tree, service.part));\n return {\n part: service.part,\n billing: service.billing,\n ...(hardLimit === undefined ? {} : { hardLimit }),\n ...(nextLimit === undefined ? {} : { nextLimit }),\n ...(monthMax === undefined ? {} : { monthMax }),\n };\n });\n}\n\n/** The largest cost of a layer in a month. */\nexport interface MonthTotal {\n /** The sum of the largest costs of the services that have a bound, in the currency of the layer. */\n readonly bounded: number;\n /** The `id` of the part of each service whose cost has no bound. With one or more, the cost of the layer has no bound. */\n readonly open: readonly string[];\n}\n\n/**\n * This function gives the largest cost of a layer in a month.\n * The cost of the layer has a bound only when `open` is empty. Then `bounded` is that bound.\n */\nexport function monthTotal(layer: SpendLayer): MonthTotal {\n const tree = spendSpace(layer);\n const keys = run(OPEN_SERVICES, tree, LAYER_KEY);\n const open = keys.ok && Array.isArray(keys.value) ? keys.value.filter((key): key is string => typeof key === \"string\").map((key) => key.slice(SERVICE_PREFIX.length)) : [];\n return { bounded: numberOf(run(BOUNDED_TOTAL, tree, LAYER_KEY)) ?? 0, open };\n}\n","import { dayNumber, partsById, type Fault, type FaultLevel, type Stack } from \"@mark1russell7/system-stack\";\nimport { END_DAYS, type Limit, type SpendLayer } from \"./layer.ts\";\nimport { serviceStates } from \"./states.ts\";\n\n// The gate of the spend layer. A gate that passes tells that the cost of each part has a hard limit, with a fresh proof.\n\n/** These are the codes of the faults of a spend layer. */\nexport const SPEND_CODES: readonly [\n \"service-part\",\n \"service-duplicate\",\n \"part-no-service\",\n \"price\",\n \"amount\",\n \"limit-none\",\n \"limit-alert\",\n \"limit-recharge\",\n \"limit-stop\",\n \"limit-wanted\",\n \"end-date\",\n \"end-past\",\n \"end-soon\",\n \"end-open\",\n \"proof-none\",\n \"proof-date\",\n \"proof-old\",\n] = [\n \"service-part\",\n \"service-duplicate\",\n \"part-no-service\",\n \"price\",\n \"amount\",\n \"limit-none\",\n \"limit-alert\",\n \"limit-recharge\",\n \"limit-stop\",\n \"limit-wanted\",\n \"end-date\",\n \"end-past\",\n \"end-soon\",\n \"end-open\",\n \"proof-none\",\n \"proof-date\",\n \"proof-old\",\n];\n\n/** A code of a fault of a spend layer. */\nexport type SpendCode = (typeof SPEND_CODES)[number];\n\n/** A fault of a spend layer. Its field `at` is the `id` of the part. */\nexport interface SpendFault extends Fault {\n readonly code: SpendCode;\n}\n\n/** The options of the gate. */\nexport interface SpendOptions {\n /** The date of the check, as `YYYY-MM-DD`. The gate has no clock: the caller gives the date. */\n readonly today: string;\n}\n\n// The function `dayNumber` is in the core since 0.1.1. This entry keeps the name.\nexport { dayNumber } from \"@mark1russell7/system-stack\";\n\n/** This function tells if a value is a finite number, 0 or more. */\nfunction isZeroOrMore(value: number): boolean {\n return Number.isFinite(value) && value >= 0;\n}\n\n/** This function gives the words for a count of days from the date of the check. */\nfunction inDays(days: number): string {\n if (days === 0) return \"on the date of the check\";\n return days === 1 ? \"in 1 day\" : `in ${String(days)} days`;\n}\n\n/**\n * This function gives each fault of a spend layer for a stack. A list with no error tells that the gate passes.\n *\n * The records:\n * - `service-part`: a record names no part of the stack.\n * - `service-duplicate`: two records name one part.\n * - `part-no-service`: a part of a kind of `kinds` has no record.\n * - `price`: a price is less than 0 or not finite, or a `fixed` plan has no price.\n * - `amount`: the amount of a limit is less than 0 or not finite.\n *\n * The limits:\n * - `limit-none`: the cost of a `metered` service has no limit.\n * - `limit-alert`: the only limits of a `metered` service are alerts. An alert stops nothing.\n * - `limit-recharge`: a prepaid balance gets money automatically, so it is no limit.\n * - `limit-stop`: a stop by code does not name the part of the stack that stops the use.\n * - `limit-wanted`: a wanted limit is not in force.\n *\n * The end of a plan:\n * - `end-date`: the date of an end is not a date. The date of its decision is not a date, or it is after `today`.\n * - `end-past`: the plan ended before `today`. The record gives the cost before the end, so it is not correct.\n * - `end-soon`: the plan ends in `endDays` days, or fewer. This fault is a warning: it does not stop the gate.\n * Its message tells the decision of the owner, when the record has one.\n * - `end-open`: the cost after the end grows with the use, and it has no hard limit.\n *\n * The proofs:\n * - `proof-none`: a record has no proof.\n * - `proof-date`: the date of a proof is not a date, or it is after `today`.\n * - `proof-old`: a proof is older than `proofDays`.\n *\n * The faults `price`, `amount` and `limit-stop` examine the plan after an end too.\n * The faults are in the sequence of the records, then of the parts with no record. The function is pure.\n *\n * @throws RangeError when `today` is not a date `YYYY-MM-DD`\n */\nexport function checkSpend(layer: SpendLayer, stack: Stack, options: SpendOptions): SpendFault[] {\n const today = dayNumber(options.today);\n if (today === undefined) throw new RangeError(`The date \"${options.today}\" is not a date of the form YYYY-MM-DD.`);\n const faults: SpendFault[] = [];\n const push = (level: FaultLevel, code: SpendCode, at: string, message: string, other?: string): void => {\n faults.push({ level, code, at, message, ...(other === undefined ? {} : { other }) });\n };\n const add = (code: SpendCode, at: string, message: string, other?: string): void => {\n push(\"error\", code, at, message, other);\n };\n const parts = partsById(stack);\n // The faults of one limit. `when` is empty, or it tells that the limit is of the plan after an end.\n const examine = (limit: Limit, at: string, when: string): void => {\n if (!isZeroOrMore(limit.amount)) add(\"amount\", at, `The amount ${String(limit.amount)} of a limit of \"${at}\"${when} is less than 0 or not finite.`);\n if (limit.kind !== \"code\") return;\n if (limit.stop === undefined) add(\"limit-stop\", at, `A stop by code of \"${at}\"${when} does not name the part that stops the use.`);\n else if (!parts.has(limit.stop)) add(\"limit-stop\", at, `A stop by code of \"${at}\"${when} names \"${limit.stop}\", which is no part of the stack.`, limit.stop);\n };\n const states = new Map(serviceStates(layer).map((state) => [state.part, state]));\n const endDays = layer.endDays ?? END_DAYS;\n const seen = new Set<string>();\n for (const service of layer.services) {\n const at = service.part;\n if (seen.has(at)) {\n add(\"service-duplicate\", at, `Two cost records name the part \"${at}\".`);\n continue;\n }\n seen.add(at);\n if (!parts.has(at)) add(\"service-part\", at, `A cost record names \"${at}\", which is no part of the stack.`);\n if (service.price !== undefined && !isZeroOrMore(service.price)) add(\"price\", at, `The price ${String(service.price)} of \"${at}\" is less than 0 or not finite.`);\n if (service.billing === \"fixed\" && service.price === undefined) add(\"price\", at, `The service \"${at}\" has a fixed price, and its record gives no price.`);\n const limits: readonly Limit[] = service.limits ?? [];\n for (const limit of [...limits, ...(service.wanted === undefined ? [] : [service.wanted])]) examine(limit, at, \"\");\n const recharges = limits.some((limit) => limit.kind === \"prepaid\" && limit.recharge === true);\n if (recharges) add(\"limit-recharge\", at, `The prepaid balance of \"${at}\" gets money automatically, so it does not limit the cost.`);\n if (service.wanted !== undefined) {\n add(\"limit-wanted\", at, `The wanted limit of \"${at}\" (${service.wanted.kind}, ${String(service.wanted.amount)} ${layer.currency} for a month) is not in force.`);\n } else if (service.billing === \"metered\" && states.get(at)?.hardLimit === undefined && !recharges) {\n if (limits.some((limit) => limit.kind === \"alert\")) add(\"limit-alert\", at, `The cost of \"${at}\" grows with the use, and its only limit is an alert. An alert stops nothing.`);\n else add(\"limit-none\", at, `The cost of \"${at}\" grows with the use, and it has no limit.`);\n }\n if (service.ends !== undefined) {\n const { date, next, decided } = service.ends;\n const end = dayNumber(date);\n if (end === undefined) add(\"end-date\", at, `The date \"${date}\" of the end of the plan of \"${at}\" is not a date of the form YYYY-MM-DD.`);\n else if (end < today) add(\"end-past\", at, `The plan of \"${at}\" ended on ${date}. The record gives the cost before the end.`);\n else if (end - today <= endDays) {\n const then = next === undefined ? \"Then the provider stops the service.\" : \"Then the cost changes.\";\n const agreed = decided === undefined ? \"\" : ` ${decided.by} decided this on ${decided.date}.`;\n push(\"warning\", \"end-soon\", at, `The plan of \"${at}\" ends on ${date}, ${inDays(end - today)}. ${then}${agreed}`);\n }\n if (decided !== undefined) {\n const day = dayNumber(decided.date);\n const about = `of the decision about the end of the plan of \"${at}\"`;\n if (day === undefined) add(\"end-date\", at, `The date \"${decided.date}\" ${about} is not a date of the form YYYY-MM-DD.`);\n else if (day > today) add(\"end-date\", at, `The date ${decided.date} ${about} is after the date of the check, ${options.today}.`);\n }\n if (next !== undefined) {\n const when = \" after the end of its plan\";\n if (next.price !== undefined && !isZeroOrMore(next.price)) add(\"price\", at, `The price ${String(next.price)} of \"${at}\"${when} is less than 0 or not finite.`);\n if (next.billing === \"fixed\" && next.price === undefined) add(\"price\", at, `The service \"${at}\" has a fixed price${when}, and its record gives no price.`);\n for (const limit of next.limits ?? []) examine(limit, at, when);\n if (next.billing === \"metered\" && states.get(at)?.nextLimit === undefined) {\n add(\"end-open\", at, `The plan of \"${at}\" ends on ${date}. After the end, the cost grows with the use, and it has no hard limit.`);\n }\n }\n }\n if (service.proof === undefined) {\n add(\"proof-none\", at, `The cost record of \"${at}\" has no proof.`);\n continue;\n }\n const day = dayNumber(service.proof.date);\n if (day === undefined) add(\"proof-date\", at, `The date \"${service.proof.date}\" of the proof of \"${at}\" is not a date of the form YYYY-MM-DD.`);\n else if (day > today) add(\"proof-date\", at, `The date ${service.proof.date} of the proof of \"${at}\" is after the date of the check, ${options.today}.`);\n else if (today - day > layer.proofDays) {\n add(\"proof-old\", at, `The proof of \"${at}\" is ${String(today - day)} days old. The largest age is ${String(layer.proofDays)} days.`);\n }\n }\n const kinds = new Set(layer.kinds);\n for (const part of stack.parts) {\n if (kinds.has(part.kind) && !seen.has(part.id)) add(\"part-no-service\", part.id, `The part \"${part.id}\" has the kind \"${part.kind}\", and it has no cost record.`);\n }\n return faults;\n}\n","import { joinRecords, joinSetting, type LayerParse, type LayerPiece } from \"@mark1russell7/system-stack\";\nimport { SPEND_SCHEMA, type SpendLayer } from \"./layer.ts\";\n\n// The join of the spend layers of the fragments of a stack, for example one file of cost records for each owner.\n\n/**\n * This function joins the spend layers of the fragments of a stack into one layer.\n *\n * - The cost records are in the order of the fragments. The record of a part is in one fragment.\n * - A part with a record in two or more fragments is a fault, also when the records are equal.\n * - The layer has one currency and one largest age of a proof. A fragment with a different value is a fault.\n * - The count of days of warning before the end of a plan is one value too. A fragment with no value takes the value of the others.\n * - The kinds are the kinds of each fragment, one time each, in the order of the fragments.\n *\n * The kinds of the joined layer hold for each part of the joined stack. Thus a kind of one fragment needs a cost\n * record also for the parts of that kind in the other fragments. The function is pure, and it changes no layer.\n * The gate `checkSpend` examines the joined layer afterward.\n *\n * @param pieces - The layers, with the name of the fragment of each one, in the order of the fragments.\n * @returns The joined layer, or the faults of the join. An empty list of layers is a fault.\n */\nexport function joinSpend(pieces: readonly LayerPiece<SpendLayer>[]): LayerParse<SpendLayer> {\n const currency = joinSetting(pieces, (layer) => layer.currency, \"currency\");\n const proofDays = joinSetting(pieces, (layer) => layer.proofDays, \"largest age of a proof in days\");\n const endDays = joinSetting(pieces, (layer) => layer.endDays, \"count of days of warning before the end of a plan\");\n const services = joinRecords(\n pieces,\n (layer) => layer.services,\n (service) => service.part,\n \"cost record of the part\",\n );\n if (currency.value === undefined || proofDays.value === undefined) return { ok: false, errors: [\"The join of spend layers got no layer.\"] };\n const errors = [...currency.errors, ...proofDays.errors, ...endDays.errors, ...services.errors];\n if (errors.length > 0) return { ok: false, errors };\n const kinds = [...new Set(pieces.flatMap((piece) => piece.layer.kinds))];\n return {\n ok: true,\n layer: {\n schema: SPEND_SCHEMA,\n currency: currency.value,\n kinds,\n proofDays: proofDays.value,\n ...(endDays.value === undefined ? {} : { endDays: endDays.value }),\n services: services.items,\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;AAaA,MAAa,aAA6D;CAAC;CAAS;CAAW;CAAS;AAAM;;AAM9G,SAAS,OAAO,MAAsB;CACpC,OAAO,IAAI,MAAM,IAAI,MAAM,GAAG,IAAI,IAAI,CAAC;AACzC;;AAGA,SAAS,QAAQ,MAAoB;CACnC,OAAO,IAAI,MAAM,IAAI,OAAO,GAAG,IAAI,IAAI,CAAC;AAC1C;;AAGA,MAAM,YAAkB,IAAI,WAAW,IAAI,UAAU,GAAG,IAAI,KAAK,CAAC;;;;;AAMlE,MAAa,UAAgB,IAAI,MAAM,IAAI,MAAM,QAAQ,UAAU,GAAG,QAAQ,MAAM,CAAC,GAAG,IAAI,OAAO,QAAQ,SAAS,GAAG,IAAI,OAAO,SAAS,CAAC,CAAC;;AAG7I,MAAM,SAAe,KAAK,MAAM,KAAK,UAAU,OAAO,OAAO,CAAC;;AAG9D,MAAa,cAAoB,KAAK,MAAM,QAAQ,OAAO;;AAG3D,MAAM,eAAqB,KAAK,aAAa,IAAI,QAAQ,CAAC;;AAG1D,MAAa,aAAmB,IAAI,SAAS,YAAY;;;;;AAMzD,MAAa,aAAmB,IAAI,OAAO,YAAY;;AAGvD,SAAS,UAAU,SAAuB;CACxC,OAAO,IAAI,MAAM,IAAI,SAAS,GAAG,IAAI,OAAO,CAAC;AAC/C;;;;;;;;AASA,MAAa,WAAiB,IAAI,MAAM,UAAU,MAAM,GAAG,IAAI,CAAC,GAAG,IAAI,MAAM,UAAU,OAAO,GAAG,IAAI,OAAO,GAAG,IAAI,OAAO,IAAI,WAAW,IAAI,OAAO,GAAG,IAAI,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC;;AAG5K,MAAa,aAAmB,KAAK,MAAM,KAAK,UAAU,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U,YAAY,SAAS,UAAU,MAAM,SAAS,mDAAmD;CACjH,MAAM,WAAW,YACf,SACC,UAAU,MAAM,WAChB,YAAY,QAAQ,MACrB,yBACF;CACA,IAAI,SAAS,UAAU,KAAA,KAAa,UAAU,UAAU,KAAA,GAAW,OAAO;EAAE,IAAI;EAAO,QAAQ,CAAC,wCAAwC;CAAE;CAC1I,MAAM,SAAS;EAAC,GAAG,SAAS;EAAQ,GAAG,UAAU;EAAQ,GAAG,QAAQ;EAAQ,GAAG,SAAS;CAAM;CAC9F,IAAI,OAAO,SAAS,GAAG,OAAO;EAAE,IAAI;EAAO;CAAO;CAClD,MAAM,QAAQ,CAAC,GAAG,IAAI,IAAI,OAAO,SAAS,UAAU,MAAM,MAAM,KAAK,CAAC,CAAC;CACvE,OAAO;EACL,IAAI;EACJ,OAAO;GACL,QAAQ;GACR,UAAU,SAAS;GACnB;GACA,WAAW,UAAU;GACrB,GAAI,QAAQ,UAAU,KAAA,IAAY,CAAC,IAAI,EAAE,SAAS,QAAQ,MAAM;GAChE,UAAU,SAAS;EACrB;CACF;AACF"}
@@ -0,0 +1,116 @@
1
+ /** The name of the format of a spend layer. It is the key of the layer in `Stack.layers`. */
2
+ declare const SPEND_SCHEMA = "spend/1";
3
+ /**
4
+ * These are the kinds of billing of a service.
5
+ *
6
+ * - `free`: the provider cannot take money. The account has no payment method, or the plan has no paid use.
7
+ * - `fixed`: the price of a month is fixed, and the use does not change it. A price that was paid one time is a fixed price of 0.
8
+ * - `metered`: the cost grows with the use. A free allowance with a payment method on the account is `metered`.
9
+ */
10
+ declare const BILLINGS: readonly ["free", "fixed", "metered"];
11
+ /** A kind of billing of a service. */
12
+ type Billing = (typeof BILLINGS)[number];
13
+ /**
14
+ * These are the kinds of limit of the cost of a month.
15
+ *
16
+ * - `provider`: a hard limit of the provider. The provider stops the service at the amount.
17
+ * - `prepaid`: a prepaid balance. The provider stops the service when the balance is empty.
18
+ * - `code`: a stop by code. A part of the stack counts the cost and stops the use at the amount.
19
+ * - `alert`: a message at the amount. An alert stops nothing, so it is not a hard limit.
20
+ */
21
+ declare const LIMIT_KINDS: readonly ["provider", "prepaid", "code", "alert"];
22
+ /** A kind of limit of the cost of a month. */
23
+ type LimitKind = (typeof LIMIT_KINDS)[number];
24
+ /** A limit of the cost of a service in a month. */
25
+ interface Limit {
26
+ readonly kind: LimitKind;
27
+ /** The amount of the limit for a month, in the currency of the layer. For `prepaid`, it is the largest balance. */
28
+ readonly amount: number;
29
+ /** For `prepaid`: the provider adds money to the balance automatically. Such a balance is not a hard limit. */
30
+ readonly recharge?: boolean | undefined;
31
+ /** For `code`: the `id` of the part of the stack that stops the use. */
32
+ readonly stop?: string | undefined;
33
+ }
34
+ /** The proof of a record: who read the settings of the service, and on which date. */
35
+ interface Proof {
36
+ /** The date of the read, as `YYYY-MM-DD`. */
37
+ readonly date: string;
38
+ /** The reader of the settings, for example the owner of the account or the name of a program. */
39
+ readonly by: string;
40
+ /** The source of the read, for example the name of a page of the account. */
41
+ readonly source?: string | undefined;
42
+ }
43
+ /**
44
+ * The cost of a service after the end of its plan. It has the fields of a cost record that give a cost.
45
+ * The gate examines it with the rules of a cost record: a cost that grows with the use must have a hard limit.
46
+ */
47
+ interface NextPlan {
48
+ /** The name of the plan after the end. */
49
+ readonly plan?: string | undefined;
50
+ readonly billing: Billing;
51
+ /** The fixed price of a month after the end, in the currency of the layer. A `fixed` plan must have one. */
52
+ readonly price?: number | undefined;
53
+ /** The limits that are in force after the end. A `metered` plan must have one hard limit, or more. */
54
+ readonly limits?: readonly Limit[] | undefined;
55
+ }
56
+ /**
57
+ * The end of the plan of a service: a trial, a free credit or a term that ends on a date.
58
+ *
59
+ * - With no `next`, the provider stops the service after the date, and it takes no money. The part does not work after the date.
60
+ * - With a `next`, the service continues after the date, with the cost of `next`.
61
+ */
62
+ interface PlanEnd {
63
+ /** The last day of the plan, as `YYYY-MM-DD`. */
64
+ readonly date: string;
65
+ /** The cost of the service after the end. A service that stops has none. */
66
+ readonly next?: NextPlan | undefined;
67
+ /** The decision of the owner that the result after the end is wanted: the stop, or the cost of `next`. */
68
+ readonly decided?: EndDecision | undefined;
69
+ }
70
+ /**
71
+ * The decision of the owner about the end of a plan: the stop of the service, or the cost of its next plan, is wanted.
72
+ * An end with a decision gives no step to the owner. A cost with no hard limit after the end stays a fault.
73
+ */
74
+ interface EndDecision {
75
+ /** The date of the decision, as `YYYY-MM-DD`. */
76
+ readonly date: string;
77
+ /** The person who decided, for example the owner of the account. */
78
+ readonly by: string;
79
+ }
80
+ /** The cost record of one part of the stack. */
81
+ interface Service {
82
+ /** The `id` of the part of the stack with the cost. */
83
+ readonly part: string;
84
+ /** The name of the plan of the service. */
85
+ readonly plan?: string | undefined;
86
+ readonly billing: Billing;
87
+ /** The fixed price of a month, in the currency of the layer. A `fixed` service must have one. */
88
+ readonly price?: number | undefined;
89
+ /** The limits that are in force. A `metered` service must have one hard limit, or more. */
90
+ readonly limits?: readonly Limit[] | undefined;
91
+ /** A limit that is not in force: the owner must set it. The gate fails until it is in `limits`. */
92
+ readonly wanted?: Limit | undefined;
93
+ /** The end of the plan, for a plan that ends on a date. The fields `billing`, `price` and `limits` give the cost before the end. */
94
+ readonly ends?: PlanEnd | undefined;
95
+ /** The proof of the record. Each record must have one, and a proof becomes too old. */
96
+ readonly proof?: Proof | undefined;
97
+ /** The steps of the owner in the account of the service, to set or to read a limit. */
98
+ readonly steps?: readonly string[] | undefined;
99
+ }
100
+ /** The default count of days before the end of a plan from which the gate gives a warning. */
101
+ declare const END_DAYS = 30;
102
+ /** A spend layer: the cost records of the parts of a stack. */
103
+ interface SpendLayer {
104
+ readonly schema: typeof SPEND_SCHEMA;
105
+ /** The currency of each amount of the layer, as a code of ISO 4217, for example `USD`. */
106
+ readonly currency: string;
107
+ /** The kinds of the parts that can have a cost. Each part of one of these kinds must have a record. */
108
+ readonly kinds: readonly string[];
109
+ /** The largest age of a proof, in days. */
110
+ readonly proofDays: number;
111
+ /** The count of days before the end of a plan from which the gate gives a warning. The default is `END_DAYS`. */
112
+ readonly endDays?: number | undefined;
113
+ readonly services: readonly Service[];
114
+ }
115
+ export { LIMIT_KINDS as a, NextPlan as c, SPEND_SCHEMA as d, Service as f, EndDecision as i, PlanEnd as l, Billing as n, Limit as o, SpendLayer as p, END_DAYS as r, LimitKind as s, BILLINGS as t, Proof as u };
116
+ //# sourceMappingURL=layer-Dm7OM6CQ.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"layer-Dm7OM6CQ.d.ts","names":[],"sources":["../src/layer.ts"],"mappings":";cAIa;;;;;;;;cASA;;KAGD,kBAAkB;;;;;;;;;cAUjB;;KAGD,oBAAoB;;UAGf;WACN,MAAM;;WAEN;;WAEA;;WAEA;;;UAIM;;WAEN;;WAEA;;WAEA;;;;;;UAOM;;WAEN;WACA,SAAS;;WAET;;WAEA,kBAAkB;;;;;;;;UASZ;;WAEN;;WAEA,OAAO;;WAEP,UAAU;;;;;;UAOJ;;WAEN;;WAEA;;;UAIM;;WAEN;;WAEA;WACA,SAAS;;WAET;;WAEA,kBAAkB;;WAElB,SAAS;;WAET,OAAO;;WAEP,QAAQ;;WAER;;;cAIE;;UAGI;WACN,eAAe;;WAEf;;WAEA;;WAEA;;WAEA;WACA,mBAAmB"}