@orangecharts/wrun-chart-host 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (198) hide show
  1. package/README.md +72 -0
  2. package/dist/constants/governed-exchanges.constants.d.ts +11 -0
  3. package/dist/constants/governed-exchanges.constants.js +11 -0
  4. package/dist/constants/indicator-sets.constants.d.ts +8 -0
  5. package/dist/constants/indicator-sets.constants.js +8 -0
  6. package/dist/constants/interval.constants.d.ts +34 -0
  7. package/dist/constants/interval.constants.js +121 -0
  8. package/dist/constants/scriptEditor/technicalIndicatorScriptKeys.constants.d.ts +28 -0
  9. package/dist/constants/scriptEditor/technicalIndicatorScriptKeys.constants.js +65 -0
  10. package/dist/constants/tpo-color-schemes.constants.d.ts +30 -0
  11. package/dist/constants/tpo-color-schemes.constants.js +32 -0
  12. package/dist/host-env.d.ts +102 -0
  13. package/dist/host-env.js +26 -0
  14. package/dist/index.d.ts +93 -0
  15. package/dist/index.js +96 -0
  16. package/dist/indicators/controls/tpo-letter.util.d.ts +2 -0
  17. package/dist/indicators/controls/tpo-letter.util.js +13 -0
  18. package/dist/indicators/controls/tpo-session.util.d.ts +99 -0
  19. package/dist/indicators/controls/tpo-session.util.js +389 -0
  20. package/dist/interfaces/kscript-diagnostics.interface.d.ts +64 -0
  21. package/dist/interfaces/kscript-diagnostics.interface.js +1 -0
  22. package/dist/interfaces/om-panel-data.interface.d.ts +103 -0
  23. package/dist/interfaces/om-panel-data.interface.js +7 -0
  24. package/dist/interfaces/wrun-runtime.interface.d.ts +2659 -0
  25. package/dist/interfaces/wrun-runtime.interface.js +55 -0
  26. package/dist/utility/chart/calendar-interval.util.d.ts +24 -0
  27. package/dist/utility/chart/calendar-interval.util.js +81 -0
  28. package/dist/utility/chart/candle-bar-end.util.d.ts +15 -0
  29. package/dist/utility/chart/candle-bar-end.util.js +23 -0
  30. package/dist/utility/chart/cme-realtime.util.d.ts +24 -0
  31. package/dist/utility/chart/cme-realtime.util.js +53 -0
  32. package/dist/utility/chart/interval-ladder.util.d.ts +27 -0
  33. package/dist/utility/chart/interval-ladder.util.js +92 -0
  34. package/dist/utility/chart/kscript-drawing-prep.util.d.ts +40 -0
  35. package/dist/utility/chart/kscript-drawing-prep.util.js +184 -0
  36. package/dist/utility/chart/kscript-drawing-safe-area.util.d.ts +95 -0
  37. package/dist/utility/chart/kscript-drawing-safe-area.util.js +805 -0
  38. package/dist/utility/chart/kscript-legend-entries.util.d.ts +166 -0
  39. package/dist/utility/chart/kscript-legend-entries.util.js +538 -0
  40. package/dist/utility/chart/kscript-output-role.util.d.ts +32 -0
  41. package/dist/utility/chart/kscript-output-role.util.js +215 -0
  42. package/dist/utility/chart/kscript-provenance-keys.util.d.ts +22 -0
  43. package/dist/utility/chart/kscript-provenance-keys.util.js +56 -0
  44. package/dist/utility/chart/kscript-run-result-normalize.util.d.ts +53 -0
  45. package/dist/utility/chart/kscript-run-result-normalize.util.js +476 -0
  46. package/dist/utility/chart/kscript-run-result-shape.util.d.ts +14 -0
  47. package/dist/utility/chart/kscript-run-result-shape.util.js +66 -0
  48. package/dist/utility/chart/kscript-titancharts-container.util.d.ts +29 -0
  49. package/dist/utility/chart/kscript-titancharts-container.util.js +151 -0
  50. package/dist/utility/chart/kscript-viewport-overlays.util.d.ts +83 -0
  51. package/dist/utility/chart/kscript-viewport-overlays.util.js +443 -0
  52. package/dist/utility/chart/om-panel-push-style.util.d.ts +78 -0
  53. package/dist/utility/chart/om-panel-push-style.util.js +301 -0
  54. package/dist/utility/chart/om-panel.util.d.ts +166 -0
  55. package/dist/utility/chart/om-panel.util.js +815 -0
  56. package/dist/utility/chart/om-series.util.d.ts +42 -0
  57. package/dist/utility/chart/om-series.util.js +330 -0
  58. package/dist/utility/chart/om-target.util.d.ts +11 -0
  59. package/dist/utility/chart/om-target.util.js +24 -0
  60. package/dist/utility/chart/om-widget.util.d.ts +148 -0
  61. package/dist/utility/chart/om-widget.util.js +413 -0
  62. package/dist/utility/chart/overlay-output-budget.util.d.ts +16 -0
  63. package/dist/utility/chart/overlay-output-budget.util.js +109 -0
  64. package/dist/utility/chart/overlay-paint-flags.util.d.ts +24 -0
  65. package/dist/utility/chart/overlay-paint-flags.util.js +55 -0
  66. package/dist/utility/chart/profile-row-height.util.d.ts +21 -0
  67. package/dist/utility/chart/profile-row-height.util.js +110 -0
  68. package/dist/utility/chart/session-interval.util.d.ts +1 -0
  69. package/dist/utility/chart/session-interval.util.js +35 -0
  70. package/dist/utility/chart/technical-script-legend-value.util.d.ts +18 -0
  71. package/dist/utility/chart/technical-script-legend-value.util.js +53 -0
  72. package/dist/utility/chart/trading-day.util.d.ts +70 -0
  73. package/dist/utility/chart/trading-day.util.js +238 -0
  74. package/dist/utility/chart/wrun-legend-name.util.d.ts +25 -0
  75. package/dist/utility/chart/wrun-legend-name.util.js +87 -0
  76. package/dist/utility/chart/wrun-run-result.util.d.ts +977 -0
  77. package/dist/utility/chart/wrun-run-result.util.js +5179 -0
  78. package/dist/utility/chart/wrun-visibility-chart.util.d.ts +25 -0
  79. package/dist/utility/chart/wrun-visibility-chart.util.js +22 -0
  80. package/dist/utility/indicators/heatmap-sensitivity.util.d.ts +62 -0
  81. package/dist/utility/indicators/heatmap-sensitivity.util.js +82 -0
  82. package/dist/utility/number-format.util.d.ts +68 -0
  83. package/dist/utility/number-format.util.js +188 -0
  84. package/dist/utility/v2/opra-governed.util.d.ts +1 -0
  85. package/dist/utility/v2/opra-governed.util.js +13 -0
  86. package/dist/utility/wrun/wrun-auto-row.util.d.ts +54 -0
  87. package/dist/utility/wrun/wrun-auto-row.util.js +96 -0
  88. package/dist/utility/wrun/wrun-batched-marks.util.d.ts +341 -0
  89. package/dist/utility/wrun/wrun-batched-marks.util.js +938 -0
  90. package/dist/utility/wrun/wrun-batched-readout.util.d.ts +75 -0
  91. package/dist/utility/wrun/wrun-batched-readout.util.js +171 -0
  92. package/dist/utility/wrun/wrun-box-segment.util.d.ts +185 -0
  93. package/dist/utility/wrun/wrun-box-segment.util.js +570 -0
  94. package/dist/utility/wrun/wrun-build-memo.util.d.ts +133 -0
  95. package/dist/utility/wrun/wrun-build-memo.util.js +301 -0
  96. package/dist/utility/wrun/wrun-builtin-twin-ports.util.d.ts +25 -0
  97. package/dist/utility/wrun/wrun-builtin-twin-ports.util.js +75 -0
  98. package/dist/utility/wrun/wrun-builtin-twin.util.d.ts +9 -0
  99. package/dist/utility/wrun/wrun-builtin-twin.util.js +40 -0
  100. package/dist/utility/wrun/wrun-canvas-cache.util.d.ts +28 -0
  101. package/dist/utility/wrun/wrun-canvas-cache.util.js +56 -0
  102. package/dist/utility/wrun/wrun-canvas-readout.util.d.ts +69 -0
  103. package/dist/utility/wrun/wrun-canvas-readout.util.js +53 -0
  104. package/dist/utility/wrun/wrun-canvases.util.d.ts +295 -0
  105. package/dist/utility/wrun/wrun-canvases.util.js +1432 -0
  106. package/dist/utility/wrun/wrun-cap-notice.util.d.ts +77 -0
  107. package/dist/utility/wrun/wrun-cap-notice.util.js +296 -0
  108. package/dist/utility/wrun/wrun-card-style.util.d.ts +154 -0
  109. package/dist/utility/wrun/wrun-card-style.util.js +539 -0
  110. package/dist/utility/wrun/wrun-chart-context.util.d.ts +89 -0
  111. package/dist/utility/wrun/wrun-chart-context.util.js +213 -0
  112. package/dist/utility/wrun/wrun-chart-names.util.d.ts +23 -0
  113. package/dist/utility/wrun/wrun-chart-names.util.js +33 -0
  114. package/dist/utility/wrun/wrun-chart-time.util.d.ts +12 -0
  115. package/dist/utility/wrun/wrun-chart-time.util.js +23 -0
  116. package/dist/utility/wrun/wrun-child-overlay-id.util.d.ts +13 -0
  117. package/dist/utility/wrun/wrun-child-overlay-id.util.js +44 -0
  118. package/dist/utility/wrun/wrun-color-facts.util.d.ts +40 -0
  119. package/dist/utility/wrun/wrun-color-facts.util.js +88 -0
  120. package/dist/utility/wrun/wrun-color-settings.lazy.d.ts +26 -0
  121. package/dist/utility/wrun/wrun-color-settings.lazy.js +9 -0
  122. package/dist/utility/wrun/wrun-color-settings.util.d.ts +351 -0
  123. package/dist/utility/wrun/wrun-color-settings.util.js +1363 -0
  124. package/dist/utility/wrun/wrun-footprint-modes.util.d.ts +23 -0
  125. package/dist/utility/wrun/wrun-footprint-modes.util.js +24 -0
  126. package/dist/utility/wrun/wrun-frames.d.ts +217 -0
  127. package/dist/utility/wrun/wrun-frames.js +1021 -0
  128. package/dist/utility/wrun/wrun-handles.util.d.ts +792 -0
  129. package/dist/utility/wrun/wrun-handles.util.js +2050 -0
  130. package/dist/utility/wrun/wrun-heatmap-cell-budget.util.d.ts +156 -0
  131. package/dist/utility/wrun/wrun-heatmap-cell-budget.util.js +342 -0
  132. package/dist/utility/wrun/wrun-hosted-wire-caps.d.ts +218 -0
  133. package/dist/utility/wrun/wrun-hosted-wire-caps.js +667 -0
  134. package/dist/utility/wrun/wrun-hosted-wire.util.d.ts +40 -0
  135. package/dist/utility/wrun/wrun-hosted-wire.util.js +27 -0
  136. package/dist/utility/wrun/wrun-hover.util.d.ts +182 -0
  137. package/dist/utility/wrun/wrun-hover.util.js +534 -0
  138. package/dist/utility/wrun/wrun-hud-looks.util.d.ts +246 -0
  139. package/dist/utility/wrun/wrun-hud-looks.util.js +587 -0
  140. package/dist/utility/wrun/wrun-integration-budget.util.d.ts +47 -0
  141. package/dist/utility/wrun/wrun-integration-budget.util.js +122 -0
  142. package/dist/utility/wrun/wrun-latest-snapshot.d.ts +19 -0
  143. package/dist/utility/wrun/wrun-latest-snapshot.js +7 -0
  144. package/dist/utility/wrun/wrun-levels-child.util.d.ts +131 -0
  145. package/dist/utility/wrun/wrun-levels-child.util.js +514 -0
  146. package/dist/utility/wrun/wrun-lifecycle.contract.d.ts +75 -0
  147. package/dist/utility/wrun/wrun-lifecycle.contract.js +9 -0
  148. package/dist/utility/wrun/wrun-market-facts.util.d.ts +116 -0
  149. package/dist/utility/wrun/wrun-market-facts.util.js +238 -0
  150. package/dist/utility/wrun/wrun-output-style.util.d.ts +344 -0
  151. package/dist/utility/wrun/wrun-output-style.util.js +1239 -0
  152. package/dist/utility/wrun/wrun-overlay-identity.util.d.ts +84 -0
  153. package/dist/utility/wrun/wrun-overlay-identity.util.js +142 -0
  154. package/dist/utility/wrun/wrun-overlay-result.util.d.ts +120 -0
  155. package/dist/utility/wrun/wrun-overlay-result.util.js +219 -0
  156. package/dist/utility/wrun/wrun-pack.util.d.ts +70 -0
  157. package/dist/utility/wrun/wrun-pack.util.js +301 -0
  158. package/dist/utility/wrun/wrun-pane-format.util.d.ts +180 -0
  159. package/dist/utility/wrun/wrun-pane-format.util.js +299 -0
  160. package/dist/utility/wrun/wrun-panel-style.lazy.d.ts +25 -0
  161. package/dist/utility/wrun/wrun-panel-style.lazy.js +15 -0
  162. package/dist/utility/wrun/wrun-panel-style.util.d.ts +174 -0
  163. package/dist/utility/wrun/wrun-panel-style.util.js +726 -0
  164. package/dist/utility/wrun/wrun-param-values.util.d.ts +69 -0
  165. package/dist/utility/wrun/wrun-param-values.util.js +230 -0
  166. package/dist/utility/wrun/wrun-refusal.util.d.ts +263 -0
  167. package/dist/utility/wrun/wrun-refusal.util.js +1259 -0
  168. package/dist/utility/wrun/wrun-registry-errors.util.d.ts +199 -0
  169. package/dist/utility/wrun/wrun-registry-errors.util.js +257 -0
  170. package/dist/utility/wrun/wrun-render-select.util.d.ts +336 -0
  171. package/dist/utility/wrun/wrun-render-select.util.js +1157 -0
  172. package/dist/utility/wrun/wrun-script-inputs.util.d.ts +434 -0
  173. package/dist/utility/wrun/wrun-script-inputs.util.js +1971 -0
  174. package/dist/utility/wrun/wrun-setting-words.lazy.d.ts +20 -0
  175. package/dist/utility/wrun/wrun-setting-words.lazy.js +9 -0
  176. package/dist/utility/wrun/wrun-setting-words.util.d.ts +79 -0
  177. package/dist/utility/wrun/wrun-setting-words.util.js +176 -0
  178. package/dist/utility/wrun/wrun-socket-row-store.d.ts +98 -0
  179. package/dist/utility/wrun/wrun-socket-row-store.js +417 -0
  180. package/dist/utility/wrun/wrun-style-flags.util.d.ts +18 -0
  181. package/dist/utility/wrun/wrun-style-flags.util.js +21 -0
  182. package/dist/utility/wrun/wrun-style-words.util.d.ts +148 -0
  183. package/dist/utility/wrun/wrun-style-words.util.js +439 -0
  184. package/dist/utility/wrun/wrun-table-look.util.d.ts +125 -0
  185. package/dist/utility/wrun/wrun-table-look.util.js +225 -0
  186. package/dist/utility/wrun/wrun-text-look.util.d.ts +51 -0
  187. package/dist/utility/wrun/wrun-text-look.util.js +227 -0
  188. package/dist/utility/wrun/wrun-text-params.util.d.ts +106 -0
  189. package/dist/utility/wrun/wrun-text-params.util.js +171 -0
  190. package/dist/utility/wrun/wrun-tpo-rows.util.d.ts +71 -0
  191. package/dist/utility/wrun/wrun-tpo-rows.util.js +438 -0
  192. package/dist/utility/wrun/wrun-twin-compute-inputs.util.d.ts +14 -0
  193. package/dist/utility/wrun/wrun-twin-compute-inputs.util.js +58 -0
  194. package/dist/utility/wrun/wrun-visibility-table.d.ts +437 -0
  195. package/dist/utility/wrun/wrun-visibility-table.js +237 -0
  196. package/dist/utility/wrun/wrun-volume-profile.util.d.ts +37 -0
  197. package/dist/utility/wrun/wrun-volume-profile.util.js +278 -0
  198. package/package.json +38 -0
@@ -0,0 +1,1021 @@
1
+ import { isPanelClearFrame, parseSetPanelData } from '../chart/om-panel.util.js';
2
+ import { wrunChartHostEnv } from '../../host-env.js';
3
+ import { isWrunColorWord, WRUN_FONT_WEIGHTS } from './wrun-style-words.util.js';
4
+ import { wrunPlainText } from './wrun-text-params.util.js';
5
+ export const WRUN_FRAME_DEFAULT_MAX_BYTES = 98_304;
6
+ export const WRUN_FRAME_MAX_SLOTS = 8;
7
+ // ---------------------------------------------------------------------------
8
+ // Caps (the kit's, restated: this path never loads the kit bundle, so
9
+ // wrun-frame-host.kit-mirror.spec.ts pins every numeric export of this module
10
+ // to the installed kit, by its export or through the kit's own frame grammar)
11
+ // ---------------------------------------------------------------------------
12
+ /** Rows per levels frame on the pane dock, and per span (g2, g6). */
13
+ export const WRUN_MAX_LEVEL_ROWS = 512;
14
+ /** Stacked segments per row (DL-stacked). */
15
+ export const WRUN_MAX_LEVEL_SERIES = 8;
16
+ /** A frame row's own text (DL-row-text) and the POC tag (DL-poc). */
17
+ export const WRUN_MAX_LEVEL_LABEL_CHARS = 24;
18
+ /** A frame row's tooltip (DL-hover). */
19
+ export const WRUN_MAX_LEVEL_TOOLTIP_CHARS = 64;
20
+ /** Spans per levels frame under span "time", and rows across them (DL-time-anchored). */
21
+ export const WRUN_MAX_LEVEL_SPANS = 64;
22
+ export const WRUN_MAX_LEVEL_SPAN_ROWS = 4096;
23
+ /** Ladder frame rows (DW38, was 64) and feed lines. */
24
+ export const WRUN_MAX_LADDER_ROWS = 256;
25
+ export const WRUN_MAX_FEED_LINES = 50;
26
+ export const WRUN_MAX_FEED_LINE_CHARS = 80;
27
+ export const WRUN_MAX_LADDER_DIVIDER_CHARS = 24;
28
+ /** A ladder or feed frame title (DW15). */
29
+ export const WRUN_MAX_WIDGET_TITLE_CHARS = 40;
30
+ /** A matrix frame: prices x columns, the cell cap, a label or text's length (MX-price-matrix). */
31
+ export const WRUN_MAX_MATRIX_ROWS = 128;
32
+ export const WRUN_MAX_MATRIX_COLS = 12;
33
+ export const WRUN_MAX_MATRIX_TEXT = 16;
34
+ export const WRUN_MAX_MATRIX_CELLS = 1536;
35
+ export const WRUN_MAX_CANVAS_LABEL_CHARS = 40;
36
+ /** Panel frame chrome (SC17 and the kind fields of SC04 SC06 SC18 SC20 SC34 SC39). */
37
+ export const WRUN_PANEL_MAX_MARKERS = 8;
38
+ export const WRUN_PANEL_MAX_GUIDES = 8;
39
+ export const WRUN_PANEL_MAX_SUMMARY_ROWS = 5;
40
+ export const WRUN_PANEL_MAX_SUMMARY_VALUES = 2000;
41
+ export const WRUN_PANEL_MAX_TITLE_CHARS = 40;
42
+ export const WRUN_PANEL_MAX_CAPTION_CHARS = 64;
43
+ export const WRUN_PANEL_MAX_LABEL_CHARS = 24;
44
+ export const WRUN_PANEL_MAX_KEY_CHARS = 64;
45
+ export const WRUN_PANEL_MIN_MARKER_WIDTH = 0.5;
46
+ export const WRUN_PANEL_MAX_MARKER_WIDTH = 4;
47
+ /** The kit's colour-word refusal (wrun-style-words.ts `WRUN_COLOR_WORD_MESSAGE`). */
48
+ export const WRUN_COLOR_WORD_MESSAGE = 'a colour must be #rrggbb, #rrggbbaa or one of theme.up, theme.down, theme.text, theme.muted, theme.bg, theme.grid, theme.accent';
49
+ const LINE_STYLE_WORDS = ['solid', 'dashed', 'dotted'];
50
+ /** C3: "point" pins the label at (x, y) or on a declared series. */
51
+ const MARKER_VALIGN_WORDS = ['top', 'middle', 'point'];
52
+ const GUIDE_AXIS_WORDS = ['x', 'y'];
53
+ /** True for the spans shape of a parsed levels frame. */
54
+ export function isWrunLevelsSpansFrame(frame) {
55
+ return Array.isArray(frame.spans);
56
+ }
57
+ // ---------------------------------------------------------------------------
58
+ // Refusals and shape helpers
59
+ // ---------------------------------------------------------------------------
60
+ export function invalidWrunFrame(slot, reason) {
61
+ throw new Error(`wrun_frame_invalid: slot '${slot}': ${reason}`);
62
+ }
63
+ /** A refusal bound to one slot: a message raised at a path inside the frame
64
+ * prints `<path>: <message>` (the om runtime's form), an empty path prints
65
+ * the message alone. */
66
+ function refuser(slot) {
67
+ return (path, message) => invalidWrunFrame(slot, path.length > 0 ? `${path.join('.')}: ${message}` : message);
68
+ }
69
+ function record(value) {
70
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
71
+ }
72
+ function finite(value) {
73
+ return typeof value === 'number' && Number.isFinite(value);
74
+ }
75
+ function finiteOrNull(value) {
76
+ return value === null || finite(value);
77
+ }
78
+ function text(value, max, min = 1) {
79
+ return typeof value === 'string' && value.length >= min && value.length <= max;
80
+ }
81
+ function quoted(words) {
82
+ return words.map((word) => `'${word}'`).join(', ');
83
+ }
84
+ /** The first key of `frame` outside `allowed`, if any. */
85
+ function unknownKey(frame, allowed) {
86
+ return Object.keys(frame).find((key) => !allowed.includes(key));
87
+ }
88
+ /** The kit's monotonic rule: the direction comes from the first pair; the
89
+ * index of the first break, or -1. */
90
+ function monotonicBreak(prices) {
91
+ const direction = Math.sign((prices[1] ?? prices[0] ?? 0) - (prices[0] ?? 0));
92
+ for (let i = 1; i < prices.length; i += 1) {
93
+ if (direction === 0 || Math.sign(prices[i] - prices[i - 1]) !== direction)
94
+ return i;
95
+ }
96
+ return -1;
97
+ }
98
+ /** The no-disguises rule (guard G2) on every string a frame carries, after
99
+ * the JSON decode so an escaped `‮` is caught like a raw one: the bidi
100
+ * controls, zero-width and control characters go, newlines stay. Walks the
101
+ * parsed value in place; a frame is small (its byte cap) and shallow. */
102
+ function plainFrameStrings(value) {
103
+ if (typeof value === 'string')
104
+ return wrunPlainText(value, { multiline: true });
105
+ if (Array.isArray(value)) {
106
+ for (let at = 0; at < value.length; at += 1)
107
+ value[at] = plainFrameStrings(value[at]);
108
+ return value;
109
+ }
110
+ if (record(value)) {
111
+ for (const key of Object.keys(value))
112
+ value[key] = plainFrameStrings(value[key]);
113
+ }
114
+ return value;
115
+ }
116
+ /** The JSON envelope every frame shares: a UTF-8 JSON object. */
117
+ function decodeFrame(raw, slot) {
118
+ let value;
119
+ try {
120
+ value = JSON.parse(raw);
121
+ }
122
+ catch {
123
+ return invalidWrunFrame(slot, 'invalid JSON');
124
+ }
125
+ if (!record(value))
126
+ return invalidWrunFrame(slot, 'expected an object');
127
+ return plainFrameStrings(value);
128
+ }
129
+ /** The frame reader the mini-chart grid parser keeps (pinned against the
130
+ * kit's zod schema in wrun-legs-pin.spec): the decoded object with only the
131
+ * listed keys, strings as written. */
132
+ function frameObject(raw, slot, keys) {
133
+ let value;
134
+ try {
135
+ value = JSON.parse(raw);
136
+ }
137
+ catch {
138
+ return invalidWrunFrame(slot, 'invalid JSON');
139
+ }
140
+ if (!record(value))
141
+ return invalidWrunFrame(slot, 'expected an object');
142
+ if (Object.keys(value).some((key) => !keys.includes(key)))
143
+ return invalidWrunFrame(slot, 'unknown frame key');
144
+ return value;
145
+ }
146
+ // ---------------------------------------------------------------------------
147
+ // Levels frames (g2, g6 DL-time-anchored)
148
+ // ---------------------------------------------------------------------------
149
+ const LEVELS_PANE_KEYS = [
150
+ 'prices',
151
+ 'values',
152
+ 'colors',
153
+ 'labels_text',
154
+ 'series',
155
+ 'lows',
156
+ 'highs',
157
+ 'tooltips',
158
+ 'value_area',
159
+ 'poc_price',
160
+ 'poc_label_text',
161
+ 'scale_max',
162
+ ];
163
+ const LEVELS_SPANS_KEYS = ['spans'];
164
+ const LEVELS_SPAN_KEYS = ['start', 'end', 'prices', 'values', 'colors'];
165
+ /** `prices`: 1..512 finite numbers. */
166
+ function levelPrices(value, at, path) {
167
+ const message = `prices must list 1 to ${WRUN_MAX_LEVEL_ROWS} prices`;
168
+ if (!Array.isArray(value) || value.length < 1 || value.length > WRUN_MAX_LEVEL_ROWS)
169
+ return at(path, message);
170
+ return value.map((price, i) => (finite(price) ? price : at([...path, i], 'prices must list finite numbers')));
171
+ }
172
+ /** `values`: 1..512 finite numbers or nulls. */
173
+ function levelValues(value, at, path) {
174
+ const message = `values must list 1 to ${WRUN_MAX_LEVEL_ROWS} values`;
175
+ if (!Array.isArray(value) || value.length < 1 || value.length > WRUN_MAX_LEVEL_ROWS)
176
+ return at(path, message);
177
+ return value.map((entry, i) => finiteOrNull(entry) ? entry : at([...path, i], 'values must list finite numbers or null'));
178
+ }
179
+ /** A per-row list of finite numbers or nulls (`lows`, `highs`). */
180
+ function levelBand(value, key, at) {
181
+ if (!Array.isArray(value))
182
+ return at([key], `${key} must list a price or null per price`);
183
+ return value.map((entry, i) => finiteOrNull(entry) ? entry : at([key, i], `${key} must list finite numbers or null`));
184
+ }
185
+ /** A per-row list of bounded texts or nulls (`labels_text`, `tooltips`). */
186
+ function levelTexts(value, key, max, at) {
187
+ if (!Array.isArray(value))
188
+ return at([key], `${key} must list a text or null per price`);
189
+ return value.map((entry, i) => entry === null || text(entry, max, 0) ? entry : at([key, i], `a ${key} entry takes at most ${max} characters`));
190
+ }
191
+ function lengthMatchesPrices(key, list, rows, at) {
192
+ if (list !== undefined && list.length !== rows)
193
+ at([key], `${key} length must match prices`);
194
+ }
195
+ function parseLevelsPaneFrame(frame, at) {
196
+ const unknown = unknownKey(frame, LEVELS_PANE_KEYS);
197
+ if (unknown !== undefined)
198
+ at([], `unknown frame key '${unknown}'`);
199
+ const prices = levelPrices(frame.prices, at, ['prices']);
200
+ const values = levelValues(frame.values, at, ['values']);
201
+ const parsed = { prices, values };
202
+ if (frame.colors !== undefined) {
203
+ const colors = frame.colors;
204
+ if (!Array.isArray(colors) || colors.length < 1 || colors.length > WRUN_MAX_LEVEL_ROWS) {
205
+ at(['colors'], `colors must list 1 to ${WRUN_MAX_LEVEL_ROWS} colours`);
206
+ }
207
+ parsed.colors = colors.map((color, i) => typeof color === 'string' ? color : at(['colors', i], 'colors must list strings'));
208
+ }
209
+ if (frame.labels_text !== undefined) {
210
+ parsed.labels_text = levelTexts(frame.labels_text, 'labels_text', WRUN_MAX_LEVEL_LABEL_CHARS, at);
211
+ }
212
+ if (frame.series !== undefined) {
213
+ const series = frame.series;
214
+ if (!Array.isArray(series) || series.length < 1 || series.length > WRUN_MAX_LEVEL_SERIES) {
215
+ at(['series'], `series must list 1 to ${WRUN_MAX_LEVEL_SERIES} arrays`);
216
+ }
217
+ parsed.series = series.map((entry, k) => {
218
+ if (!Array.isArray(entry))
219
+ return at(['series', k], 'series must list arrays of finite numbers or null');
220
+ return entry.map((cell, i) => finiteOrNull(cell) ? cell : at(['series', k, i], 'series must list finite numbers or null'));
221
+ });
222
+ }
223
+ if (frame.lows !== undefined)
224
+ parsed.lows = levelBand(frame.lows, 'lows', at);
225
+ if (frame.highs !== undefined)
226
+ parsed.highs = levelBand(frame.highs, 'highs', at);
227
+ if (frame.tooltips !== undefined) {
228
+ parsed.tooltips = levelTexts(frame.tooltips, 'tooltips', WRUN_MAX_LEVEL_TOOLTIP_CHARS, at);
229
+ }
230
+ if (frame.value_area !== undefined) {
231
+ const area = frame.value_area;
232
+ if (!Array.isArray(area) || area.length !== 2 || !area.every(finite)) {
233
+ at(['value_area'], 'value_area must be [low, high], two prices');
234
+ }
235
+ parsed.value_area = [area[0], area[1]];
236
+ }
237
+ if (frame.poc_price !== undefined) {
238
+ if (!finite(frame.poc_price))
239
+ at(['poc_price'], 'poc_price must be a finite number');
240
+ parsed.poc_price = frame.poc_price;
241
+ }
242
+ if (frame.poc_label_text !== undefined) {
243
+ if (!text(frame.poc_label_text, WRUN_MAX_LEVEL_LABEL_CHARS)) {
244
+ at(['poc_label_text'], `poc_label_text takes 1 to ${WRUN_MAX_LEVEL_LABEL_CHARS} characters`);
245
+ }
246
+ parsed.poc_label_text = frame.poc_label_text;
247
+ }
248
+ if (frame.scale_max !== undefined) {
249
+ if (!finite(frame.scale_max) || frame.scale_max <= 0) {
250
+ at(['scale_max'], 'scale_max must be a number greater than 0');
251
+ }
252
+ parsed.scale_max = frame.scale_max;
253
+ }
254
+ // The cross-field rules, in the kit's order.
255
+ const rows = prices.length;
256
+ lengthMatchesPrices('values', values, rows, at);
257
+ lengthMatchesPrices('colors', parsed.colors, rows, at);
258
+ lengthMatchesPrices('labels_text', parsed.labels_text, rows, at);
259
+ lengthMatchesPrices('tooltips', parsed.tooltips, rows, at);
260
+ lengthMatchesPrices('lows', parsed.lows, rows, at);
261
+ lengthMatchesPrices('highs', parsed.highs, rows, at);
262
+ parsed.series?.forEach((entry, k) => {
263
+ if (entry.length !== rows)
264
+ at(['series', k], `series[${k}] length must match prices`);
265
+ });
266
+ const broken = monotonicBreak(prices);
267
+ if (broken >= 0)
268
+ at(['prices', broken], 'prices must be strictly monotonic');
269
+ if ((parsed.lows === undefined) !== (parsed.highs === undefined)) {
270
+ at([parsed.lows === undefined ? 'highs' : 'lows'], 'lows and highs come together');
271
+ }
272
+ else if (parsed.lows !== undefined && parsed.highs !== undefined) {
273
+ const { lows, highs } = parsed;
274
+ for (let i = 0; i < Math.min(lows.length, highs.length); i += 1) {
275
+ const low = lows[i];
276
+ const high = highs[i];
277
+ if ((low === null) !== (high === null)) {
278
+ at(['lows', i], `lows[${i}] and highs[${i}] must both be prices or both null`);
279
+ }
280
+ else if (low !== null && high !== null && low >= high) {
281
+ at(['lows', i], `lows[${i}] must be below highs[${i}]`);
282
+ }
283
+ }
284
+ }
285
+ if (parsed.value_area !== undefined && parsed.value_area[0] >= parsed.value_area[1]) {
286
+ at(['value_area'], 'value_area must be [low, high] with low below high');
287
+ }
288
+ return parsed;
289
+ }
290
+ function parseLevelsSpansFrame(frame, at) {
291
+ const unknown = unknownKey(frame, LEVELS_SPANS_KEYS);
292
+ if (unknown !== undefined)
293
+ at([], `unknown frame key '${unknown}'`);
294
+ const spans = frame.spans;
295
+ if (!Array.isArray(spans) || spans.length < 1 || spans.length > WRUN_MAX_LEVEL_SPANS) {
296
+ return at(['spans'], `spans must list 1 to ${WRUN_MAX_LEVEL_SPANS} spans`);
297
+ }
298
+ const parsed = spans.map((span, k) => {
299
+ const path = ['spans', k];
300
+ if (!record(span))
301
+ return at(path, 'a span is an object of start, end, prices and values');
302
+ const foreign = unknownKey(span, LEVELS_SPAN_KEYS);
303
+ if (foreign !== undefined)
304
+ at(path, `unknown span key '${foreign}'`);
305
+ const { start, end } = span;
306
+ if (!finite(start))
307
+ at([...path, 'start'], 'start must be a finite number');
308
+ if (!finite(end))
309
+ at([...path, 'end'], 'end must be a finite number');
310
+ const prices = levelPrices(span.prices, at, [...path, 'prices']);
311
+ const values = levelValues(span.values, at, [...path, 'values']);
312
+ const entry = { start, end, prices, values };
313
+ if (span.colors !== undefined) {
314
+ const colors = span.colors;
315
+ if (!Array.isArray(colors) || colors.length < 1 || colors.length > WRUN_MAX_LEVEL_ROWS) {
316
+ at([...path, 'colors'], `colors must list 1 to ${WRUN_MAX_LEVEL_ROWS} colours`);
317
+ }
318
+ entry.colors = colors.map((color, i) => isWrunColorWord(color) ? color : at([...path, 'colors', i], WRUN_COLOR_WORD_MESSAGE));
319
+ }
320
+ if (end <= start)
321
+ at([...path, 'end'], 'end must be after start');
322
+ if (values.length !== prices.length)
323
+ at([...path, 'values'], 'values length must match prices');
324
+ if (entry.colors !== undefined && entry.colors.length !== prices.length) {
325
+ at([...path, 'colors'], 'colors length must match prices');
326
+ }
327
+ const broken = monotonicBreak(prices);
328
+ if (broken >= 0)
329
+ at([...path, 'prices', broken], 'prices must be strictly monotonic');
330
+ return entry;
331
+ });
332
+ const rows = parsed.reduce((sum, span) => sum + span.prices.length, 0);
333
+ if (rows > WRUN_MAX_LEVEL_SPAN_ROWS) {
334
+ at(['spans'], `spans hold at most ${WRUN_MAX_LEVEL_SPAN_ROWS} rows in all (${rows} given)`);
335
+ }
336
+ return { spans: parsed };
337
+ }
338
+ /** Parses a levels frame: the pane shape (`prices`, `values`, ...) or the
339
+ * spans shape (`spans`), told apart by the `spans` key; shape rules only
340
+ * (the kit's `WrunLevelsFrameSchema`). With a declaration, the shape must
341
+ * match its `span` (the kit's `validateWrunLevelsFrame` message); the
342
+ * series and scale cross-checks are the selection's (`checkWrunLevelsFrame`). */
343
+ export function parseWrunLevelsFrame(raw, slot, declaration) {
344
+ const frame = decodeFrame(raw, slot);
345
+ const at = refuser(slot);
346
+ const spansShape = Array.isArray(frame.spans) || ('spans' in frame && !('prices' in frame));
347
+ const parsed = spansShape ? parseLevelsSpansFrame(frame, at) : parseLevelsPaneFrame(frame, at);
348
+ if (declaration !== undefined) {
349
+ const owner = `level '${declaration.name ?? '?'}'`;
350
+ const span = declaration.span === 'time' ? 'time' : 'pane';
351
+ if (spansShape && span !== 'time') {
352
+ at(['spans'], `${owner} has span 'pane': its frame takes prices and values, not spans`);
353
+ }
354
+ if (!spansShape && span === 'time') {
355
+ at(['spans'], `${owner} has span 'time': its frame takes spans, not prices and values`);
356
+ }
357
+ }
358
+ return parsed;
359
+ }
360
+ const ALL_PANEL_KINDS = [
361
+ 'bars',
362
+ 'line',
363
+ 'scatter',
364
+ 'histogram',
365
+ 'pie',
366
+ 'heatmap',
367
+ 'table',
368
+ 'tiles',
369
+ ];
370
+ const AXIS_PANEL_KINDS = ['line', 'bars', 'histogram', 'scatter'];
371
+ /** Which kinds take each frame key; x_min / x_max also need x "index" or
372
+ * "number" on a line, like the declared words. */
373
+ export const WRUN_PANEL_FRAME_WORD_KINDS = {
374
+ title: ALL_PANEL_KINDS,
375
+ caption: ALL_PANEL_KINDS,
376
+ badge: AXIS_PANEL_KINDS,
377
+ markers: ['line'],
378
+ x_min: ['line', 'scatter'],
379
+ x_max: ['line', 'scatter'],
380
+ y_min: AXIS_PANEL_KINDS,
381
+ y_max: AXIS_PANEL_KINDS,
382
+ guides: ['scatter'],
383
+ quadrants: ['scatter'],
384
+ min: ['heatmap'],
385
+ max: ['heatmap'],
386
+ highlight: ['heatmap'],
387
+ summary: ['heatmap'],
388
+ hole_text: ['pie'],
389
+ hole_caption: ['pie'],
390
+ };
391
+ const PANEL_FRAME_WORDS = Object.keys(WRUN_PANEL_FRAME_WORD_KINDS);
392
+ /** The frame keys this panel's kind accepts beside `rows`. */
393
+ export function wrunPanelFrameWords(kind) {
394
+ return PANEL_FRAME_WORDS.filter((word) => WRUN_PANEL_FRAME_WORD_KINDS[word].includes(kind));
395
+ }
396
+ const MARKER_KEYS = [
397
+ 'x',
398
+ 'label',
399
+ 'color',
400
+ 'badge',
401
+ 'wash',
402
+ 'valign',
403
+ 'y',
404
+ 'series',
405
+ 'show_value',
406
+ 'line_style',
407
+ 'width',
408
+ ];
409
+ const BADGE_KEYS = ['text', 'color', 'text_color'];
410
+ const GUIDE_KEYS = ['axis', 'value', 'label', 'color', 'line_style', 'width'];
411
+ const QUADRANTS_KEYS = ['x', 'y', 'colors', 'labels'];
412
+ const HIGHLIGHT_KEYS = ['row', 'col'];
413
+ const SUMMARY_ROW_KEYS = ['label', 'values'];
414
+ const RICH_CHROME_MESSAGE = "needs chrome 'grid' or 'none' on a time, category or stacked line";
415
+ function panelText(value, key, max, at, path) {
416
+ return text(value, max) ? value : at(path, `${key} must be 1 to ${max} characters`);
417
+ }
418
+ function panelNumber(value, key, at, path) {
419
+ return finite(value) ? value : at(path, `${key} must be a finite number`);
420
+ }
421
+ function panelFlag(value, key, at, path) {
422
+ return typeof value === 'boolean' ? value : at(path, `${key} must be true or false`);
423
+ }
424
+ function panelWord(value, key, words, at, path) {
425
+ return typeof value === 'string' && words.includes(value)
426
+ ? value
427
+ : at(path, `${key} must be one of ${quoted(words)}`);
428
+ }
429
+ function panelColor(value, at, path) {
430
+ return isWrunColorWord(value) ? value : at(path, WRUN_COLOR_WORD_MESSAGE);
431
+ }
432
+ function panelWidth(value, at, path) {
433
+ const ok = finite(value) && value >= WRUN_PANEL_MIN_MARKER_WIDTH && value <= WRUN_PANEL_MAX_MARKER_WIDTH;
434
+ return ok
435
+ ? value
436
+ : at(path, `width must be between ${WRUN_PANEL_MIN_MARKER_WIDTH} and ${WRUN_PANEL_MAX_MARKER_WIDTH}`);
437
+ }
438
+ function panelMarker(value, at, path) {
439
+ if (!record(value))
440
+ return at(path, 'a marker is an object with x');
441
+ const unknown = unknownKey(value, MARKER_KEYS);
442
+ if (unknown !== undefined)
443
+ at(path, `unknown marker key '${unknown}'`);
444
+ const x = value.x;
445
+ if (!finite(x) && !text(x, WRUN_PANEL_MAX_KEY_CHARS)) {
446
+ at([...path, 'x'], `x must be a number, 'spot' or a category key of 1 to ${WRUN_PANEL_MAX_KEY_CHARS} characters`);
447
+ }
448
+ const marker = { x };
449
+ if (value.label !== undefined) {
450
+ marker.label = panelText(value.label, 'label', WRUN_PANEL_MAX_LABEL_CHARS, at, [...path, 'label']);
451
+ }
452
+ if (value.color !== undefined)
453
+ marker.color = panelColor(value.color, at, [...path, 'color']);
454
+ if (value.badge !== undefined)
455
+ marker.badge = panelFlag(value.badge, 'badge', at, [...path, 'badge']);
456
+ if (value.wash !== undefined)
457
+ marker.wash = panelFlag(value.wash, 'wash', at, [...path, 'wash']);
458
+ if (value.valign !== undefined) {
459
+ marker.valign = panelWord(value.valign, 'valign', MARKER_VALIGN_WORDS, at, [...path, 'valign']);
460
+ }
461
+ // C3: the point anchor and its series ride valign "point" only (the
462
+ // declared-series check needs the declaration: refinePanelChrome).
463
+ if (value.y !== undefined) {
464
+ marker.y = panelNumber(value.y, 'y', at, [...path, 'y']);
465
+ if (marker.valign !== 'point')
466
+ at([...path, 'y'], "y needs valign 'point'");
467
+ }
468
+ if (value.series !== undefined) {
469
+ marker.series = panelText(value.series, 'series', WRUN_PANEL_MAX_LABEL_CHARS, at, [...path, 'series']);
470
+ if (marker.valign !== 'point')
471
+ at([...path, 'series'], "series needs valign 'point'");
472
+ }
473
+ if (value.show_value !== undefined) {
474
+ marker.show_value = panelFlag(value.show_value, 'show_value', at, [...path, 'show_value']);
475
+ }
476
+ if (value.line_style !== undefined) {
477
+ marker.line_style = panelWord(value.line_style, 'line_style', LINE_STYLE_WORDS, at, [...path, 'line_style']);
478
+ }
479
+ if (value.width !== undefined)
480
+ marker.width = panelWidth(value.width, at, [...path, 'width']);
481
+ return marker;
482
+ }
483
+ function panelMarkers(value, at) {
484
+ if (!Array.isArray(value))
485
+ return at(['markers'], 'markers must be an array of markers');
486
+ if (value.length > WRUN_PANEL_MAX_MARKERS)
487
+ at(['markers'], `markers allows at most ${WRUN_PANEL_MAX_MARKERS}`);
488
+ return value.map((marker, i) => panelMarker(marker, at, ['markers', i]));
489
+ }
490
+ /** The badge chip (C2): text 1..24 plus optional fill and ink colour words. */
491
+ function panelBadge(value, at) {
492
+ const path = ['badge'];
493
+ if (!record(value))
494
+ return at(path, 'badge is an object with text');
495
+ const unknown = unknownKey(value, BADGE_KEYS);
496
+ if (unknown !== undefined)
497
+ at(path, `unknown badge key '${unknown}'`);
498
+ const badge = {
499
+ text: panelText(value.text, 'text', WRUN_PANEL_MAX_LABEL_CHARS, at, [...path, 'text']),
500
+ };
501
+ if (value.color !== undefined)
502
+ badge.color = panelColor(value.color, at, [...path, 'color']);
503
+ if (value.text_color !== undefined)
504
+ badge.text_color = panelColor(value.text_color, at, [...path, 'text_color']);
505
+ return badge;
506
+ }
507
+ function panelGuide(value, at, path) {
508
+ if (!record(value))
509
+ return at(path, 'a guide is an object with axis and value');
510
+ const unknown = unknownKey(value, GUIDE_KEYS);
511
+ if (unknown !== undefined)
512
+ at(path, `unknown guide key '${unknown}'`);
513
+ const guide = {
514
+ axis: panelWord(value.axis, 'axis', GUIDE_AXIS_WORDS, at, [...path, 'axis']),
515
+ value: panelNumber(value.value, 'value', at, [...path, 'value']),
516
+ };
517
+ if (value.label !== undefined) {
518
+ guide.label = panelText(value.label, 'label', WRUN_PANEL_MAX_LABEL_CHARS, at, [...path, 'label']);
519
+ }
520
+ if (value.color !== undefined)
521
+ guide.color = panelColor(value.color, at, [...path, 'color']);
522
+ if (value.line_style !== undefined) {
523
+ guide.line_style = panelWord(value.line_style, 'line_style', LINE_STYLE_WORDS, at, [...path, 'line_style']);
524
+ }
525
+ if (value.width !== undefined)
526
+ guide.width = panelWidth(value.width, at, [...path, 'width']);
527
+ return guide;
528
+ }
529
+ function panelGuides(value, at) {
530
+ if (!Array.isArray(value))
531
+ return at(['guides'], 'guides must be an array of guides');
532
+ if (value.length > WRUN_PANEL_MAX_GUIDES)
533
+ at(['guides'], `guides allows at most ${WRUN_PANEL_MAX_GUIDES}`);
534
+ return value.map((guide, i) => panelGuide(guide, at, ['guides', i]));
535
+ }
536
+ function panelQuadrants(value, at) {
537
+ const path = ['quadrants'];
538
+ if (!record(value))
539
+ return at(path, 'quadrants is an object of x, y, colors and labels');
540
+ const unknown = unknownKey(value, QUADRANTS_KEYS);
541
+ if (unknown !== undefined)
542
+ at(path, `unknown quadrants key '${unknown}'`);
543
+ const x = panelNumber(value.x, 'x', at, [...path, 'x']);
544
+ const y = panelNumber(value.y, 'y', at, [...path, 'y']);
545
+ const colors = value.colors;
546
+ if (!Array.isArray(colors) || colors.length !== 4)
547
+ at([...path, 'colors'], 'quadrants.colors must list exactly 4 colours');
548
+ const [c0, c1, c2, c3] = colors.map((color, i) => panelColor(color, at, [...path, 'colors', i]));
549
+ const quadrants = { x, y, colors: [c0, c1, c2, c3] };
550
+ if (value.labels !== undefined) {
551
+ const labels = value.labels;
552
+ if (!Array.isArray(labels) || labels.length !== 4) {
553
+ at([...path, 'labels'], 'quadrants.labels must list exactly 4 labels');
554
+ }
555
+ const [l0, l1, l2, l3] = labels.map((label, i) => text(label, WRUN_PANEL_MAX_LABEL_CHARS)
556
+ ? label
557
+ : at([...path, 'labels', i], `labels entries must be 1 to ${WRUN_PANEL_MAX_LABEL_CHARS} characters`));
558
+ quadrants.labels = [l0, l1, l2, l3];
559
+ }
560
+ return quadrants;
561
+ }
562
+ function panelHighlight(value, at) {
563
+ const path = ['highlight'];
564
+ if (!record(value))
565
+ return at(path, 'highlight is an object of row and/or col');
566
+ const unknown = unknownKey(value, HIGHLIGHT_KEYS);
567
+ if (unknown !== undefined)
568
+ at(path, `unknown highlight key '${unknown}'`);
569
+ const highlight = {};
570
+ if (value.row !== undefined)
571
+ highlight.row = panelText(value.row, 'row', WRUN_PANEL_MAX_KEY_CHARS, at, [...path, 'row']);
572
+ if (value.col !== undefined)
573
+ highlight.col = panelText(value.col, 'col', WRUN_PANEL_MAX_KEY_CHARS, at, [...path, 'col']);
574
+ if (highlight.row === undefined && highlight.col === undefined)
575
+ at(path, 'highlight needs row or col');
576
+ return highlight;
577
+ }
578
+ function panelSummary(value, at) {
579
+ if (!Array.isArray(value))
580
+ return at(['summary'], 'summary must be an array of rows');
581
+ if (value.length > WRUN_PANEL_MAX_SUMMARY_ROWS) {
582
+ at(['summary'], `summary allows at most ${WRUN_PANEL_MAX_SUMMARY_ROWS} rows`);
583
+ }
584
+ return value.map((row, i) => {
585
+ const path = ['summary', i];
586
+ if (!record(row))
587
+ return at(path, 'a summary row is an object with label and values');
588
+ const unknown = unknownKey(row, SUMMARY_ROW_KEYS);
589
+ if (unknown !== undefined)
590
+ at(path, `unknown summary row key '${unknown}'`);
591
+ const label = panelText(row.label, 'label', WRUN_PANEL_MAX_LABEL_CHARS, at, [...path, 'label']);
592
+ const values = row.values;
593
+ if (!Array.isArray(values) || values.length < 1)
594
+ at([...path, 'values'], 'values must list at least 1 entry');
595
+ if (values.length > WRUN_PANEL_MAX_SUMMARY_VALUES) {
596
+ at([...path, 'values'], `values allows at most ${WRUN_PANEL_MAX_SUMMARY_VALUES}`);
597
+ }
598
+ return {
599
+ label,
600
+ values: values.map((entry, j) => finiteOrNull(entry) ? entry : at([...path, 'values', j], 'values must list finite numbers or null')),
601
+ };
602
+ });
603
+ }
604
+ /** A panel's effective chrome (SC02): the declared word, else "grid" for an
605
+ * unstacked line with x "index" or "number" (today's rich curve), else
606
+ * "box" (today's panel look). */
607
+ function effectivePanelChrome(panel) {
608
+ if (panel.chrome !== undefined)
609
+ return panel.chrome;
610
+ const numeric = panel.x === 'index' || panel.x === 'number';
611
+ return panel.kind === 'line' && numeric && panel.stacked !== true ? 'grid' : 'box';
612
+ }
613
+ /** The cross rules of a frame's chrome against its declaration: x_min /
614
+ * x_max on a numeric line or a scatter, the box-line rule on markers,
615
+ * category marker keys, a point marker's series among the declared ones
616
+ * (C3), the paired ranges (a frame half against the declared other half),
617
+ * hole text needs a hole. */
618
+ function refinePanelChrome(panel, chrome, at) {
619
+ const numeric = panel.x === 'index' || panel.x === 'number';
620
+ for (const word of ['x_min', 'x_max']) {
621
+ if (chrome[word] !== undefined && panel.kind !== 'scatter' && !numeric) {
622
+ at([word], `${word} is only valid for a line with x 'index' or 'number', and scatter`);
623
+ }
624
+ }
625
+ if (chrome.markers !== undefined) {
626
+ if (effectivePanelChrome(panel) === 'box')
627
+ at(['markers'], `markers ${RICH_CHROME_MESSAGE}`);
628
+ chrome.markers.forEach((marker, i) => {
629
+ if (typeof marker.x === 'string' && marker.x !== 'spot' && panel.x !== 'category') {
630
+ at(['markers', i, 'x'], `marker x '${marker.x}' names a category; this line's x is '${panel.x}'`);
631
+ }
632
+ if (marker.series !== undefined && !(panel.series ?? []).some((entry) => entry.name === marker.series)) {
633
+ at(['markers', i, 'series'], `marker series '${marker.series}' is not a declared series`);
634
+ }
635
+ });
636
+ }
637
+ const pairs = [
638
+ ['x_min', 'x_max'],
639
+ ['y_min', 'y_max'],
640
+ ['min', 'max'],
641
+ ];
642
+ for (const [lowKey, highKey] of pairs) {
643
+ if (chrome[lowKey] === undefined && chrome[highKey] === undefined)
644
+ continue;
645
+ const low = chrome[lowKey] ?? panel[lowKey];
646
+ const high = chrome[highKey] ?? panel[highKey];
647
+ if (low !== undefined && high !== undefined && low >= high) {
648
+ at([chrome[lowKey] !== undefined ? lowKey : highKey], `${lowKey} must be below ${highKey}`);
649
+ }
650
+ }
651
+ for (const word of ['hole_text', 'hole_caption']) {
652
+ if (chrome[word] !== undefined && !(typeof panel.hole === 'number' && panel.hole > 0)) {
653
+ at([word], `${word} needs hole > 0`);
654
+ }
655
+ }
656
+ }
657
+ /** The per-run chrome a decoded panel frame carries beside `rows` (SC17):
658
+ * any key outside the kind's words refuses by name, each value goes
659
+ * through its declared word's ranges, then the cross rules against the
660
+ * declaration. Returns the parsed chrome (empty when the frame carries
661
+ * only rows). */
662
+ export function parseWrunPanelChrome(frame, declaration, slot) {
663
+ const at = refuser(slot);
664
+ const allowed = wrunPanelFrameWords(declaration.kind);
665
+ for (const key of Object.keys(frame)) {
666
+ if (key !== 'rows' && !allowed.includes(key)) {
667
+ at([key], `unknown frame key '${key}' for panel.${declaration.kind}`);
668
+ }
669
+ }
670
+ const chrome = {};
671
+ for (const word of allowed) {
672
+ const value = frame[word];
673
+ if (value === undefined)
674
+ continue;
675
+ switch (word) {
676
+ case 'title':
677
+ chrome.title = panelText(value, word, WRUN_PANEL_MAX_TITLE_CHARS, at, [word]);
678
+ break;
679
+ case 'caption':
680
+ chrome.caption = panelText(value, word, WRUN_PANEL_MAX_CAPTION_CHARS, at, [word]);
681
+ break;
682
+ case 'badge':
683
+ chrome.badge = panelBadge(value, at);
684
+ break;
685
+ case 'markers':
686
+ chrome.markers = panelMarkers(value, at);
687
+ break;
688
+ case 'x_min':
689
+ case 'x_max':
690
+ case 'y_min':
691
+ case 'y_max':
692
+ case 'min':
693
+ case 'max':
694
+ chrome[word] = panelNumber(value, word, at, [word]);
695
+ break;
696
+ case 'guides':
697
+ chrome.guides = panelGuides(value, at);
698
+ break;
699
+ case 'quadrants':
700
+ chrome.quadrants = panelQuadrants(value, at);
701
+ break;
702
+ case 'highlight':
703
+ chrome.highlight = panelHighlight(value, at);
704
+ break;
705
+ case 'summary':
706
+ chrome.summary = panelSummary(value, at);
707
+ break;
708
+ case 'hole_text':
709
+ case 'hole_caption':
710
+ chrome[word] = panelText(value, word, WRUN_PANEL_MAX_LABEL_CHARS, at, [word]);
711
+ break;
712
+ }
713
+ }
714
+ refinePanelChrome(declaration, chrome, at);
715
+ return chrome;
716
+ }
717
+ /** Parses a panel frame: `rows` through the receive-lane validator (one row
718
+ * grammar on both lanes; the wrun lane carries the declaration's style
719
+ * words) plus the kind's chrome keys through {@link parseWrunPanelChrome},
720
+ * returned as `ParsedPanel.chrome` when the frame carries any. */
721
+ export function parseWrunPanelFrame(raw, slot, declaration) {
722
+ const frame = decodeFrame(raw, slot);
723
+ const chrome = parseWrunPanelChrome(frame, declaration, slot);
724
+ if (!('rows' in frame))
725
+ refuser(slot)(['rows'], 'a panel frame must be an object with rows');
726
+ const { name: _name, frame: _frame, ...panel } = declaration;
727
+ // The envelope id is host-owned; WRUN declaration names have their own grammar.
728
+ const result = parseSetPanelData({
729
+ chartIndex: 0,
730
+ payloadId: 'ompn:c0:wrun-frame',
731
+ revision: 0,
732
+ panel: { ...panel, id: 'wrun-frame' },
733
+ rows: frame.rows,
734
+ }, { lane: 'wrun' });
735
+ if (!result.ok)
736
+ return invalidWrunFrame(slot, result.rejectReason);
737
+ if (isPanelClearFrame(result.data))
738
+ return invalidWrunFrame(slot, 'unexpected clear frame');
739
+ return Object.keys(chrome).length > 0 ? { ...result.data, chrome } : result.data;
740
+ }
741
+ // ---------------------------------------------------------------------------
742
+ // Ladder and feed frames (g3 DW15, DW38)
743
+ // ---------------------------------------------------------------------------
744
+ const LADDER_KEYS = ['rows', 'divider', 'title'];
745
+ const FEED_KEYS = ['lines', 'title'];
746
+ const DIVIDER_KEYS = ['label', 'price'];
747
+ const WIDGET_TITLE_MESSAGE = `title takes 1 to ${WRUN_MAX_WIDGET_TITLE_CHARS} characters`;
748
+ function widgetTitle(value, at) {
749
+ return text(value, WRUN_MAX_WIDGET_TITLE_CHARS) ? value : at(['title'], WIDGET_TITLE_MESSAGE);
750
+ }
751
+ export function parseWrunLadderFrame(raw, slot) {
752
+ const frame = decodeFrame(raw, slot);
753
+ const at = refuser(slot);
754
+ const unknown = unknownKey(frame, LADDER_KEYS);
755
+ if (unknown !== undefined)
756
+ at([], `unknown frame key '${unknown}'`);
757
+ const { rows, divider, title } = frame;
758
+ if (!Array.isArray(rows) || rows.length < 1 || rows.length > WRUN_MAX_LADDER_ROWS) {
759
+ return at(['rows'], `rows must contain 1..${WRUN_MAX_LADDER_ROWS} ladder entries`);
760
+ }
761
+ const parsedRows = rows.map((row, k) => {
762
+ if (!Array.isArray(row) ||
763
+ row.length < 3 ||
764
+ row.length > 4 ||
765
+ !finite(row[0]) ||
766
+ !finite(row[1]) ||
767
+ row[1] < 0 ||
768
+ !finite(row[2]) ||
769
+ row[2] < 0 ||
770
+ row[2] > 1) {
771
+ return invalidWrunFrame(slot, 'invalid ladder row');
772
+ }
773
+ if (row.length === 4 && !isWrunColorWord(row[3]))
774
+ at(['rows', k, 3], WRUN_COLOR_WORD_MESSAGE);
775
+ return [...row];
776
+ });
777
+ if (divider !== undefined &&
778
+ (!record(divider) ||
779
+ unknownKey(divider, DIVIDER_KEYS) !== undefined ||
780
+ !text(divider.label, WRUN_MAX_LADDER_DIVIDER_CHARS) ||
781
+ !finite(divider.price))) {
782
+ return invalidWrunFrame(slot, 'divider needs a label of 1..24 characters and a finite price');
783
+ }
784
+ return {
785
+ rows: parsedRows,
786
+ ...(divider !== undefined ? { divider: { ...divider } } : {}),
787
+ ...(title !== undefined ? { title: widgetTitle(title, at) } : {}),
788
+ };
789
+ }
790
+ export function parseWrunFeedFrame(raw, slot) {
791
+ const frame = decodeFrame(raw, slot);
792
+ const at = refuser(slot);
793
+ const unknown = unknownKey(frame, FEED_KEYS);
794
+ if (unknown !== undefined)
795
+ at([], `unknown frame key '${unknown}'`);
796
+ const { lines, title } = frame;
797
+ if (!Array.isArray(lines) || lines.length < 1 || lines.length > WRUN_MAX_FEED_LINES) {
798
+ return invalidWrunFrame(slot, `lines must contain 1..${WRUN_MAX_FEED_LINES} feed entries`);
799
+ }
800
+ const parsedLines = lines.map((line, k) => {
801
+ if (!Array.isArray(line) ||
802
+ line.length < 2 ||
803
+ line.length > 3 ||
804
+ !finite(line[0]) ||
805
+ !Number.isSafeInteger(line[0]) ||
806
+ line[0] < 0 ||
807
+ !text(line[1], WRUN_MAX_FEED_LINE_CHARS)) {
808
+ return invalidWrunFrame(slot, 'invalid feed line');
809
+ }
810
+ if (line.length === 3 && !isWrunColorWord(line[2]))
811
+ at(['lines', k, 2], WRUN_COLOR_WORD_MESSAGE);
812
+ return [...line];
813
+ });
814
+ return { lines: parsedLines, ...(title !== undefined ? { title: widgetTitle(title, at) } : {}) };
815
+ }
816
+ // ---------------------------------------------------------------------------
817
+ // Matrix frames (g6 MX-price-matrix)
818
+ // ---------------------------------------------------------------------------
819
+ const MATRIX_KEYS = ['prices', 'cells', 'cols', 'title', 'highlight', 'range'];
820
+ const MATRIX_CELL_KEYS = ['value', 'text', 'color', 'text_color', 'font_weight'];
821
+ const MATRIX_HIGHLIGHT_KEYS = ['price', 'col'];
822
+ const MATRIX_RANGE_KEYS = ['min', 'max'];
823
+ const MATRIX_TEXT_MESSAGE = `a cell text takes at most ${WRUN_MAX_MATRIX_TEXT} characters`;
824
+ const MATRIX_CELL_MESSAGE = `a cell is null, a finite number, a text of at most ${WRUN_MAX_MATRIX_TEXT} characters or { value?, text?, color?, text_color?, font_weight? }`;
825
+ const MATRIX_COLS_MESSAGE = `cells rows must hold 1 to ${WRUN_MAX_MATRIX_COLS} columns`;
826
+ const MATRIX_LABEL_MESSAGE = `a column label takes 1 to ${WRUN_MAX_MATRIX_TEXT} characters`;
827
+ function matrixCell(cell, at, path) {
828
+ if (cell === null || finite(cell))
829
+ return cell;
830
+ if (typeof cell === 'string')
831
+ return cell.length <= WRUN_MAX_MATRIX_TEXT ? cell : at(path, MATRIX_TEXT_MESSAGE);
832
+ if (!record(cell))
833
+ return at(path, MATRIX_CELL_MESSAGE);
834
+ const unknown = unknownKey(cell, MATRIX_CELL_KEYS);
835
+ if (unknown !== undefined)
836
+ at(path, `unknown cell key '${unknown}'`);
837
+ const style = {};
838
+ if (cell.value !== undefined) {
839
+ style.value = finite(cell.value) ? cell.value : at([...path, 'value'], 'a cell value must be a finite number');
840
+ }
841
+ if (cell.text !== undefined) {
842
+ style.text = text(cell.text, WRUN_MAX_MATRIX_TEXT, 0) ? cell.text : at([...path, 'text'], MATRIX_TEXT_MESSAGE);
843
+ }
844
+ if (cell.color !== undefined)
845
+ style.color = panelColor(cell.color, at, [...path, 'color']);
846
+ if (cell.text_color !== undefined)
847
+ style.text_color = panelColor(cell.text_color, at, [...path, 'text_color']);
848
+ if (cell.font_weight !== undefined) {
849
+ style.font_weight = panelWord(cell.font_weight, 'font_weight', WRUN_FONT_WEIGHTS, at, [...path, 'font_weight']);
850
+ }
851
+ if (Object.keys(style).length === 0)
852
+ at(path, 'a cell object needs at least one field');
853
+ return style;
854
+ }
855
+ /** Parses a matrix frame through the kit's grammar (`WrunMatrixFrameSchema`:
856
+ * prices 1..128 strictly monotonic, cells one row per price of one width
857
+ * 1..12, at most 1536 cells, cols one label per column, highlight.col
858
+ * inside the width, texts 0..16, range.min below range.max), then the
859
+ * runtime's declaration check: a declared `columns` list must be as wide
860
+ * as the cells. */
861
+ export function parseWrunMatrixFrame(raw, slot, declaration) {
862
+ const frame = decodeFrame(raw, slot);
863
+ const at = refuser(slot);
864
+ const unknown = unknownKey(frame, MATRIX_KEYS);
865
+ if (unknown !== undefined)
866
+ at([], `unknown frame key '${unknown}'`);
867
+ const { prices, cells, cols, title, highlight, range } = frame;
868
+ if (!Array.isArray(prices) || prices.length < 1)
869
+ return at(['prices'], 'prices must list at least 1 price');
870
+ if (prices.length > WRUN_MAX_MATRIX_ROWS) {
871
+ at(['prices'], `prices must list at most ${WRUN_MAX_MATRIX_ROWS} prices`);
872
+ }
873
+ const parsedPrices = prices.map((price, i) => finite(price) ? price : at(['prices', i], 'prices must list finite numbers'));
874
+ if (!Array.isArray(cells))
875
+ return at(['cells'], 'cells must list one row of cells per price');
876
+ const parsedCells = cells.map((row, r) => {
877
+ if (!Array.isArray(row))
878
+ return at(['cells', r], 'a cells row must be an array of cells');
879
+ return row.map((cell, c) => matrixCell(cell, at, ['cells', r, c]));
880
+ });
881
+ const parsed = { prices: parsedPrices, cells: parsedCells };
882
+ if (cols !== undefined) {
883
+ if (!Array.isArray(cols))
884
+ return at(['cols'], 'cols must list one label per column');
885
+ parsed.cols = cols.map((label, i) => text(label, WRUN_MAX_MATRIX_TEXT) ? label : at(['cols', i], MATRIX_LABEL_MESSAGE));
886
+ }
887
+ if (title !== undefined) {
888
+ parsed.title = text(title, WRUN_MAX_CANVAS_LABEL_CHARS)
889
+ ? title
890
+ : at(['title'], `title takes 1 to ${WRUN_MAX_CANVAS_LABEL_CHARS} characters`);
891
+ }
892
+ if (highlight !== undefined) {
893
+ if (!record(highlight))
894
+ return at(['highlight'], 'highlight is an object of price and col');
895
+ const foreign = unknownKey(highlight, MATRIX_HIGHLIGHT_KEYS);
896
+ if (foreign !== undefined)
897
+ at(['highlight'], `unknown highlight key '${foreign}'`);
898
+ parsed.highlight = {};
899
+ if (highlight.price !== undefined) {
900
+ parsed.highlight.price = finite(highlight.price)
901
+ ? highlight.price
902
+ : at(['highlight', 'price'], 'highlight.price must be a finite number');
903
+ }
904
+ if (highlight.col !== undefined) {
905
+ const col = highlight.col;
906
+ parsed.highlight.col =
907
+ finite(col) && Number.isInteger(col) && col >= 0
908
+ ? col
909
+ : at(['highlight', 'col'], 'highlight.col must be a column index');
910
+ }
911
+ }
912
+ if (range !== undefined) {
913
+ if (!record(range))
914
+ return at(['range'], 'range is an object of min and max');
915
+ const foreign = unknownKey(range, MATRIX_RANGE_KEYS);
916
+ if (foreign !== undefined)
917
+ at(['range'], `unknown range key '${foreign}'`);
918
+ parsed.range = {};
919
+ for (const key of MATRIX_RANGE_KEYS) {
920
+ const bound = range[key];
921
+ if (bound === undefined)
922
+ continue;
923
+ parsed.range[key] = finite(bound) ? bound : at(['range', key], `range.${key} must be a finite number`);
924
+ }
925
+ }
926
+ // The cross-field rules, in the kit's order.
927
+ const rows = parsedPrices.length;
928
+ if (parsedCells.length !== rows) {
929
+ at(['cells'], `cells must hold one row per price (${rows} prices, ${parsedCells.length} rows)`);
930
+ }
931
+ const width = parsedCells[0]?.length ?? 0;
932
+ if (parsedCells.length > 0 && (width < 1 || width > WRUN_MAX_MATRIX_COLS)) {
933
+ at(['cells', 0], MATRIX_COLS_MESSAGE);
934
+ }
935
+ else {
936
+ for (let r = 1; r < parsedCells.length; r += 1) {
937
+ if (parsedCells[r].length !== width)
938
+ at(['cells', r], `cells rows must all hold ${width} columns`);
939
+ }
940
+ }
941
+ if (parsedCells.length * width > WRUN_MAX_MATRIX_CELLS) {
942
+ at(['cells'], `cells must hold at most ${WRUN_MAX_MATRIX_CELLS} cells`);
943
+ }
944
+ if (parsed.cols !== undefined && parsed.cols.length !== width) {
945
+ at(['cols'], `cols must list one label per column (${width})`);
946
+ }
947
+ const col = parsed.highlight?.col;
948
+ if (col !== undefined && width > 0 && col > width - 1) {
949
+ at(['highlight', 'col'], `highlight.col must be between 0 and ${width - 1}`);
950
+ }
951
+ const broken = monotonicBreak(parsedPrices);
952
+ if (broken >= 0)
953
+ at(['prices', broken], 'prices must be strictly monotonic');
954
+ const min = parsed.range?.min;
955
+ const max = parsed.range?.max;
956
+ if (min !== undefined && max !== undefined && min >= max) {
957
+ at(['range', 'min'], 'range.min must be less than range.max');
958
+ }
959
+ // The declared column labels must be as wide as the cells (the frame's own
960
+ // `cols` is checked above).
961
+ const columns = declaration?.columns;
962
+ if (columns !== undefined && columns.length !== width) {
963
+ at(['cells'], `rows are ${width} wide but columns declares ${columns.length} labels`);
964
+ }
965
+ return parsed;
966
+ }
967
+ function miniChartsBar(value) {
968
+ return (Array.isArray(value) &&
969
+ value.length >= 5 &&
970
+ value.length <= 6 &&
971
+ Number.isSafeInteger(value[0]) &&
972
+ finite(value[1]) &&
973
+ finite(value[2]) &&
974
+ finite(value[3]) &&
975
+ finite(value[4]) &&
976
+ (value.length === 5 || value[5] === null || finite(value[5])));
977
+ }
978
+ /**
979
+ * A minicharts frame (`draw.minicharts`) by om's WrunMiniChartsFrameSchema,
980
+ * rule for rule: `{ title?, panels }` and nothing else; the title a string,
981
+ * trimmed, at most 40 characters (empty = no title); 1..12 panels of exactly
982
+ * `{ label, bars }`, the label trimmed to 1..24 characters, at most 100 bars
983
+ * of `[time, open, high, low, close, volume?]` with time a safe integer (the
984
+ * candle's open in epoch ms), the prices finite and the volume finite, null
985
+ * or absent. The parsed frame carries the trimmed texts (zod's output), the
986
+ * shape the shared host's wrunMiniChartGridPayload maps.
987
+ */
988
+ export function parseWrunMiniChartsFrame(raw, slot) {
989
+ // The grammar's caps, the shared host's (a run loaded it before any frame reaches here).
990
+ const { WRUN_MINICHARTS_LABEL_MAX_CHARS, WRUN_MINICHARTS_MAX_BARS, WRUN_MINICHARTS_MAX_PANELS, WRUN_MINICHARTS_TITLE_MAX_CHARS, } = wrunChartHostEnv().sharedHost();
991
+ const frame = frameObject(raw, slot, ['title', 'panels']);
992
+ let title;
993
+ if (frame.title !== undefined) {
994
+ title = typeof frame.title === 'string' ? frame.title.trim() : undefined;
995
+ if (title === undefined || title.length > WRUN_MINICHARTS_TITLE_MAX_CHARS) {
996
+ return invalidWrunFrame(slot, `title must be text of at most ${WRUN_MINICHARTS_TITLE_MAX_CHARS} characters`);
997
+ }
998
+ }
999
+ const { panels } = frame;
1000
+ if (!Array.isArray(panels) || panels.length < 1 || panels.length > WRUN_MINICHARTS_MAX_PANELS) {
1001
+ return invalidWrunFrame(slot, `panels must contain 1..${WRUN_MINICHARTS_MAX_PANELS} panels`);
1002
+ }
1003
+ const parsed = panels.map((panel) => {
1004
+ if (!record(panel) || Object.keys(panel).some((key) => key !== 'label' && key !== 'bars')) {
1005
+ return invalidWrunFrame(slot, 'a panel is exactly { label, bars }');
1006
+ }
1007
+ const label = typeof panel.label === 'string' ? panel.label.trim() : '';
1008
+ if (label.length < 1 || label.length > WRUN_MINICHARTS_LABEL_MAX_CHARS) {
1009
+ return invalidWrunFrame(slot, `a panel label must be text of 1..${WRUN_MINICHARTS_LABEL_MAX_CHARS} characters`);
1010
+ }
1011
+ const { bars } = panel;
1012
+ if (!Array.isArray(bars) || bars.length > WRUN_MINICHARTS_MAX_BARS) {
1013
+ return invalidWrunFrame(slot, `panel '${label}' bars must contain at most ${WRUN_MINICHARTS_MAX_BARS} candles`);
1014
+ }
1015
+ if (!bars.every(miniChartsBar)) {
1016
+ return invalidWrunFrame(slot, `panel '${label}' has an invalid candle (time, open, high, low, close, volume?)`);
1017
+ }
1018
+ return { label, bars: bars.map((bar) => [...bar]) };
1019
+ });
1020
+ return { ...(title !== undefined ? { title } : {}), panels: parsed };
1021
+ }