@player-tools/dsl 0.5.0--canary.62.1176 → 0.5.0-next.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 (52) hide show
  1. package/dist/cjs/index.cjs +973 -0
  2. package/dist/cjs/index.cjs.map +1 -0
  3. package/dist/index.legacy-esm.js +910 -0
  4. package/dist/index.mjs +910 -0
  5. package/dist/index.mjs.map +1 -0
  6. package/package.json +27 -33
  7. package/src/__tests__/asset-api.test.tsx +354 -0
  8. package/src/__tests__/edge-cases.test.tsx +81 -0
  9. package/src/__tests__/helpers/asset-library.tsx +371 -0
  10. package/src/__tests__/helpers/mock-data-refs.ts +21 -0
  11. package/src/__tests__/json.test.ts +12 -0
  12. package/src/__tests__/jsx.test.tsx +138 -0
  13. package/src/__tests__/schema.test.tsx +378 -0
  14. package/src/__tests__/switch.test.tsx +251 -0
  15. package/src/__tests__/template.test.tsx +294 -0
  16. package/src/__tests__/view-api.test.tsx +46 -0
  17. package/src/auto-id.tsx +10 -10
  18. package/src/compiler/__tests__/compiler.test.tsx +264 -0
  19. package/src/compiler/__tests__/schema.test.ts +127 -0
  20. package/src/compiler/compiler.ts +50 -46
  21. package/src/compiler/index.ts +3 -3
  22. package/src/compiler/schema.ts +29 -25
  23. package/src/compiler/types.ts +15 -12
  24. package/src/compiler/utils.ts +7 -7
  25. package/src/components.tsx +17 -12
  26. package/src/index.ts +11 -11
  27. package/src/string-templates/__tests__/binding.test.ts +87 -0
  28. package/src/string-templates/__tests__/edge-cases.test.ts +6 -0
  29. package/src/string-templates/__tests__/expression.test.ts +9 -0
  30. package/src/string-templates/__tests__/react.test.tsx +75 -0
  31. package/src/string-templates/index.ts +40 -22
  32. package/src/switch.tsx +12 -12
  33. package/src/template.tsx +38 -34
  34. package/src/types.ts +5 -5
  35. package/src/utils.tsx +8 -8
  36. package/types/auto-id.d.ts +33 -0
  37. package/types/compiler/compiler.d.ts +27 -0
  38. package/types/compiler/index.d.ts +4 -0
  39. package/types/compiler/schema.d.ts +61 -0
  40. package/types/compiler/types.d.ts +55 -0
  41. package/types/compiler/utils.d.ts +4 -0
  42. package/types/components.d.ts +90 -0
  43. package/types/index.d.ts +12 -0
  44. package/types/string-templates/index.d.ts +50 -0
  45. package/types/switch.d.ts +19 -0
  46. package/types/template.d.ts +20 -0
  47. package/types/types.d.ts +42 -0
  48. package/types/utils.d.ts +33 -0
  49. package/README.md +0 -9
  50. package/dist/index.cjs.js +0 -990
  51. package/dist/index.d.ts +0 -404
  52. package/dist/index.esm.js +0 -923
@@ -0,0 +1,910 @@
1
+ // ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/dsl/src/components.tsx
2
+ import React4 from "react";
3
+ import mergeRefs from "react-merge-refs";
4
+
5
+ // ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/dsl/src/auto-id.tsx
6
+ import React from "react";
7
+ import { flattenNodes } from "react-json-reconciler";
8
+ var IDSuffixContext = React.createContext("root");
9
+ var IndexSuffixStopContext = React.createContext(false);
10
+ var useGetIdPrefix = () => {
11
+ return React.useContext(IDSuffixContext);
12
+ };
13
+ var IDSuffixProvider = (props) => {
14
+ const currentPrefix = useGetIdPrefix();
15
+ return /* @__PURE__ */ React.createElement(
16
+ IDSuffixContext.Provider,
17
+ {
18
+ value: [
19
+ currentPrefix === "root" ? void 0 : currentPrefix,
20
+ props.suffix
21
+ ].filter(Boolean).join("-")
22
+ },
23
+ props.children
24
+ );
25
+ };
26
+ var IDProvider = (props) => {
27
+ if (props.id) {
28
+ return /* @__PURE__ */ React.createElement(IDSuffixContext.Provider, { value: props.id }, props.children);
29
+ }
30
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, props.children);
31
+ };
32
+ var useIndexInSlot = (ref) => {
33
+ const [index, setIndex] = React.useState(-1);
34
+ const slotContext = React.useContext(SlotContext);
35
+ React.useEffect(() => {
36
+ if (!slotContext?.isArray) {
37
+ throw new Error("Cannot get index in non-array slot");
38
+ }
39
+ if (ref.current && slotContext?.ref.current?.valueNode?.type === "array") {
40
+ const allChildren = flattenNodes(
41
+ slotContext.ref.current.valueNode.children
42
+ );
43
+ const foundIndex = allChildren.indexOf(ref.current);
44
+ if (foundIndex !== index) {
45
+ setIndex(foundIndex);
46
+ }
47
+ }
48
+ }, [index, ref, slotContext?.isArray, slotContext?.ref]);
49
+ return index;
50
+ };
51
+ var IDSuffixIndexProvider = (props) => {
52
+ const slotIndex = useIndexInSlot(props.wrapperRef);
53
+ const stopIndex = React.useContext(IndexSuffixStopContext);
54
+ if (stopIndex) {
55
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, props.children);
56
+ }
57
+ return /* @__PURE__ */ React.createElement(IDSuffixProvider, { suffix: props.templateIndex ?? String(slotIndex) }, /* @__PURE__ */ React.createElement(IndexSuffixStopContext.Provider, { value: true }, props.children));
58
+ };
59
+ var OptionalIDSuffixProvider = (props) => {
60
+ const slotContext = React.useContext(SlotContext);
61
+ if (slotContext?.isArray) {
62
+ return /* @__PURE__ */ React.createElement(
63
+ IDSuffixIndexProvider,
64
+ {
65
+ wrapperRef: props.wrapperRef,
66
+ templateIndex: props.templateIndex
67
+ },
68
+ props.children
69
+ );
70
+ }
71
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, props.children);
72
+ };
73
+
74
+ // ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/dsl/src/utils.tsx
75
+ import * as React3 from "react";
76
+
77
+ // ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/dsl/src/string-templates/index.ts
78
+ import * as React2 from "react";
79
+ import { parseExpression } from "@player-ui/player";
80
+ var OpaqueIdentifier = Symbol("TemplateStringType");
81
+ var TemplateStringComponent = (props) => {
82
+ return React2.createElement(
83
+ "value",
84
+ {
85
+ value: props.value
86
+ },
87
+ null
88
+ );
89
+ };
90
+ var createTemplateInstance = (options) => {
91
+ const value = options.strings.reduce((sum, next, i) => {
92
+ const element2 = options.other[i];
93
+ if (typeof element2 === "string") {
94
+ return sum + next + element2;
95
+ }
96
+ return sum + next + (element2?.toRefString(options) ?? "");
97
+ }, "");
98
+ if (options.nestedContext === "expression") {
99
+ const parsedExpression = parseExpression(value, { strict: false });
100
+ if (parsedExpression.error) {
101
+ throw parsedExpression.error;
102
+ }
103
+ }
104
+ const toString = () => {
105
+ return options.toRefString({}, value);
106
+ };
107
+ const toValue = () => {
108
+ return value;
109
+ };
110
+ const element = React2.createElement(
111
+ TemplateStringComponent,
112
+ {
113
+ value: toString()
114
+ },
115
+ null
116
+ );
117
+ return {
118
+ ...element,
119
+ [OpaqueIdentifier]: true,
120
+ toString,
121
+ toValue,
122
+ toRefString: (refStringOptions) => {
123
+ return options.toRefString(refStringOptions, value);
124
+ }
125
+ };
126
+ };
127
+ var addBindingIndexes = (binding2) => {
128
+ let currentIndex = 0;
129
+ return binding2.replace(/_index_/g, () => {
130
+ const result = `_index${currentIndex > 0 ? currentIndex : ""}_`;
131
+ currentIndex += 1;
132
+ return result;
133
+ });
134
+ };
135
+ var createBindingTemplateInstance = (options) => {
136
+ const templateInstance = createTemplateInstance({
137
+ ...options,
138
+ strings: options.strings.map(
139
+ (element) => addBindingIndexes(element)
140
+ ),
141
+ other: options.other.map(
142
+ (element) => typeof element === "string" ? addBindingIndexes(element) : element
143
+ ),
144
+ toRefString: (context, value) => {
145
+ return `{{${value}}}`;
146
+ }
147
+ });
148
+ templateInstance.__type = "binding";
149
+ return templateInstance;
150
+ };
151
+ var createExpressionTemplateInstance = (options) => {
152
+ const templateInstance = createTemplateInstance({
153
+ ...options,
154
+ toRefString: (contextOptions, value) => {
155
+ if (contextOptions?.nestedContext === "expression") {
156
+ return value;
157
+ }
158
+ const inBinding = contextOptions?.nestedContext === "binding";
159
+ return `${inBinding ? "`" : "@["}${value}${inBinding ? "`" : "]@"}`;
160
+ }
161
+ });
162
+ templateInstance.__type = "expression";
163
+ return templateInstance;
164
+ };
165
+ var binding = (strings, ...nested) => {
166
+ return createBindingTemplateInstance({
167
+ strings,
168
+ other: nested,
169
+ nestedContext: "binding"
170
+ });
171
+ };
172
+ var expression = (strings, ...nested) => {
173
+ return createExpressionTemplateInstance({
174
+ strings,
175
+ other: nested,
176
+ nestedContext: "expression"
177
+ });
178
+ };
179
+ var isTemplateStringInstance = (val) => {
180
+ return val !== null && typeof val === "object" && val[OpaqueIdentifier] === true;
181
+ };
182
+
183
+ // ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/dsl/src/utils.tsx
184
+ function toArray(val) {
185
+ return Array.isArray(val) ? val : [val];
186
+ }
187
+ function toJsonElement(value, indexOrKey, options) {
188
+ const indexProp = typeof indexOrKey === "number" ? { key: indexOrKey } : null;
189
+ if (Array.isArray(value)) {
190
+ return /* @__PURE__ */ React3.createElement("array", { ...indexProp }, value.map((item, idx) => toJsonElement(item, idx, options)));
191
+ }
192
+ if (isTemplateStringInstance(value)) {
193
+ if (typeof indexOrKey === "string" && options?.propertiesToSkip?.includes(indexOrKey)) {
194
+ return /* @__PURE__ */ React3.createElement("value", { ...indexProp }, value.toValue());
195
+ }
196
+ return /* @__PURE__ */ React3.createElement("value", { ...indexProp }, value.toRefString());
197
+ }
198
+ if (typeof value === "object" && value !== null) {
199
+ return /* @__PURE__ */ React3.createElement("obj", { ...indexProp }, Object.keys(value).map((key) => /* @__PURE__ */ React3.createElement("property", { key, name: key }, toJsonElement(value[key], key, options))));
200
+ }
201
+ return /* @__PURE__ */ React3.createElement("value", { ...indexProp, value });
202
+ }
203
+ function toJsonProperties(value, options = { propertiesToSkip: ["applicability"] }) {
204
+ return Object.keys(value).map((key) => {
205
+ return /* @__PURE__ */ React3.createElement("property", { key, name: key }, toJsonElement(value[key], key, options));
206
+ });
207
+ }
208
+ function normalizeText(options) {
209
+ const { node, TextComp } = options;
210
+ const nodeArr = React3.Children.toArray(node);
211
+ if (nodeArr.every(
212
+ (n) => React3.isValidElement(n) && n.type !== TemplateStringComponent
213
+ )) {
214
+ return node;
215
+ }
216
+ if (TextComp) {
217
+ return /* @__PURE__ */ React3.createElement(TextComp, null, nodeArr);
218
+ }
219
+ throw new Error(
220
+ `Tried to convert node to Text Asset, but no Component was supplied.`
221
+ );
222
+ }
223
+ function normalizeToCollection(options) {
224
+ const { node, CollectionComp } = options;
225
+ if (React3.Children.count(node) > 1 && React3.Children.toArray(node).every((n) => typeof n !== "string")) {
226
+ if (!CollectionComp) {
227
+ throw new Error(
228
+ `Tried to convert array to a collection asset, but no Component was given.`
229
+ );
230
+ }
231
+ return /* @__PURE__ */ React3.createElement(CollectionComp, null, node);
232
+ }
233
+ return normalizeText({ ...options, node });
234
+ }
235
+ function flattenChildren(children) {
236
+ const childrenArray = React3.Children.toArray(children);
237
+ return childrenArray.reduce((flatChildren, child) => {
238
+ if (child.type === React3.Fragment) {
239
+ return flatChildren.concat(
240
+ flattenChildren(child.props.children)
241
+ );
242
+ }
243
+ flatChildren.push(child);
244
+ return flatChildren;
245
+ }, []);
246
+ }
247
+
248
+ // ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/dsl/src/components.tsx
249
+ var SlotContext = React4.createContext(void 0);
250
+ var AssetWrapper = React4.forwardRef(function AssetWrapper2(props, ref) {
251
+ const { children, ...rest } = props;
252
+ return /* @__PURE__ */ React4.createElement("obj", { ref }, toJsonProperties(rest), /* @__PURE__ */ React4.createElement("property", { name: "asset" }, children));
253
+ });
254
+ var GeneratedIDProperty = (props) => {
255
+ const currentPrefixId = useGetIdPrefix();
256
+ return /* @__PURE__ */ React4.createElement("property", { name: "id" }, props.id ?? currentPrefixId);
257
+ };
258
+ var Asset = React4.forwardRef((props, ref) => {
259
+ const { id, type, applicability, children, ...rest } = props;
260
+ const slotContext = React4.useContext(SlotContext);
261
+ const localRef = React4.useRef(null);
262
+ const Wrapper = slotContext?.wrapInAsset ? AssetWrapper : React4.Fragment;
263
+ return /* @__PURE__ */ React4.createElement(
264
+ Wrapper,
265
+ {
266
+ ref: slotContext?.wrapInAsset ? mergeRefs([ref, localRef]) : void 0,
267
+ ...slotContext?.wrapInAsset && slotContext?.additionalProperties ? slotContext?.additionalProperties : {}
268
+ },
269
+ /* @__PURE__ */ React4.createElement(OptionalIDSuffixProvider, { wrapperRef: localRef }, /* @__PURE__ */ React4.createElement(SlotContext.Provider, { value: void 0 }, /* @__PURE__ */ React4.createElement(IDProvider, { id }, /* @__PURE__ */ React4.createElement(
270
+ "obj",
271
+ {
272
+ ref: slotContext?.wrapInAsset ? void 0 : mergeRefs([ref, localRef])
273
+ },
274
+ /* @__PURE__ */ React4.createElement(GeneratedIDProperty, { id }),
275
+ /* @__PURE__ */ React4.createElement("property", { name: "type" }, type),
276
+ applicability !== void 0 && /* @__PURE__ */ React4.createElement("property", { name: "applicability" }, /* @__PURE__ */ React4.createElement(
277
+ "value",
278
+ {
279
+ value: typeof applicability === "boolean" ? applicability : applicability.toValue()
280
+ }
281
+ )),
282
+ toJsonProperties(rest),
283
+ children
284
+ ))))
285
+ );
286
+ });
287
+ Asset.displayName = "Asset";
288
+ Asset.defaultProps = {
289
+ id: void 0,
290
+ children: void 0
291
+ };
292
+ var View = React4.forwardRef(
293
+ (props, ref) => {
294
+ const { validation, children, ...rest } = props;
295
+ return /* @__PURE__ */ React4.createElement(Asset, { ref, ...rest }, validation && /* @__PURE__ */ React4.createElement("property", { key: "validation", name: "validation" }, toJsonElement(validation, "validation", {
296
+ propertiesToSkip: ["ref"]
297
+ })), children);
298
+ }
299
+ );
300
+ View.displayName = "View";
301
+ View.defaultProps = {
302
+ id: void 0,
303
+ children: void 0
304
+ };
305
+ var Slot = (props) => {
306
+ const { TextComp, CollectionComp } = props;
307
+ const children = flattenChildren(props.children);
308
+ const propRef = React4.useRef(null);
309
+ return /* @__PURE__ */ React4.createElement("property", { ref: propRef, name: props.name }, /* @__PURE__ */ React4.createElement(IDSuffixProvider, { suffix: props.name }, /* @__PURE__ */ React4.createElement(IndexSuffixStopContext.Provider, { value: false }, /* @__PURE__ */ React4.createElement(
310
+ SlotContext.Provider,
311
+ {
312
+ value: {
313
+ ref: propRef,
314
+ propertyName: props.name,
315
+ wrapInAsset: props.wrapInAsset ?? false,
316
+ isArray: props.isArray ?? false,
317
+ additionalProperties: props.additionalProperties,
318
+ TextComp,
319
+ CollectionComp
320
+ }
321
+ },
322
+ props.isArray && /* @__PURE__ */ React4.createElement("array", null, React4.Children.map(children, (child, index) => {
323
+ return (
324
+ // eslint-disable-next-line react/no-array-index-key
325
+ /* @__PURE__ */ React4.createElement(React4.Fragment, { key: `${props.name}-${index}` }, normalizeText({ node: child, TextComp }))
326
+ );
327
+ })),
328
+ !props.isArray && normalizeToCollection({
329
+ node: children,
330
+ TextComp,
331
+ CollectionComp
332
+ })
333
+ ))));
334
+ };
335
+ function createSlot(options) {
336
+ return (props) => {
337
+ const { children, ...other } = props;
338
+ return /* @__PURE__ */ React4.createElement(Slot, { ...options, additionalProperties: other }, children);
339
+ };
340
+ }
341
+
342
+ // ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/dsl/src/switch.tsx
343
+ import React5 from "react";
344
+ import { flattenNodes as flattenNodes2 } from "react-json-reconciler";
345
+ var SwitchContext = React5.createContext({});
346
+ var Switch = (props) => {
347
+ const slotContext = React5.useContext(SlotContext);
348
+ const propertyNode = React5.useRef(null);
349
+ return /* @__PURE__ */ React5.createElement("obj", { ref: propertyNode }, /* @__PURE__ */ React5.createElement(
350
+ SwitchContext.Provider,
351
+ {
352
+ value: {
353
+ ...props,
354
+ TextComp: slotContext?.TextComp,
355
+ CollectionComp: slotContext?.CollectionComp
356
+ }
357
+ },
358
+ /* @__PURE__ */ React5.createElement(OptionalIDSuffixProvider, { wrapperRef: propertyNode }, /* @__PURE__ */ React5.createElement("property", { name: props.isDynamic ? "dynamicSwitch" : "staticSwitch" }, /* @__PURE__ */ React5.createElement(SlotContext.Provider, { value: void 0 }, /* @__PURE__ */ React5.createElement("array", null, props.children))))
359
+ ), slotContext?.additionalProperties && toJsonProperties(slotContext.additionalProperties));
360
+ };
361
+ var findParentArray = (node) => {
362
+ if (node.type === "array") {
363
+ return node;
364
+ }
365
+ if (node.parent) {
366
+ return findParentArray(node.parent);
367
+ }
368
+ throw new Error("can't find parent array");
369
+ };
370
+ var findArrayIndex = (node) => {
371
+ const parentArray = findParentArray(node);
372
+ const allSearch = flattenNodes2(parentArray.children);
373
+ return allSearch.indexOf(node);
374
+ };
375
+ var Case = (props) => {
376
+ const slotContext = React5.useContext(SlotContext);
377
+ const switchContext = React5.useContext(SwitchContext);
378
+ const [caseIndex, setCaseIndex] = React5.useState(-1);
379
+ const caseNode = React5.useRef(null);
380
+ React5.useLayoutEffect(() => {
381
+ if (caseNode.current) {
382
+ const index = findArrayIndex(caseNode.current);
383
+ if (index !== caseIndex) {
384
+ setCaseIndex(index);
385
+ }
386
+ }
387
+ }, [caseIndex]);
388
+ let expValue = true;
389
+ if (props.exp !== void 0) {
390
+ expValue = isTemplateStringInstance(props.exp) ? props.exp.toValue() : props.exp;
391
+ }
392
+ return /* @__PURE__ */ React5.createElement("obj", { ref: caseNode }, /* @__PURE__ */ React5.createElement("property", { name: "case" }, /* @__PURE__ */ React5.createElement("value", { value: expValue })), /* @__PURE__ */ React5.createElement(
393
+ IDSuffixProvider,
394
+ {
395
+ suffix: `${switchContext.isDynamic ? "dynamicSwitch" : "staticSwitch"}-${caseIndex}`
396
+ },
397
+ /* @__PURE__ */ React5.createElement(
398
+ SlotContext.Provider,
399
+ {
400
+ value: slotContext ? { ...slotContext, wrapInAsset: false } : void 0
401
+ },
402
+ /* @__PURE__ */ React5.createElement("property", { name: "asset" }, normalizeToCollection({
403
+ node: props.children,
404
+ TextComp: switchContext?.TextComp,
405
+ CollectionComp: switchContext?.CollectionComp
406
+ }))
407
+ )
408
+ ));
409
+ };
410
+ Switch.Case = Case;
411
+
412
+ // ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/dsl/src/template.tsx
413
+ import React6 from "react";
414
+ import {
415
+ ArrayNode,
416
+ PropertyNode as PropertyNode2,
417
+ ValueNode,
418
+ createPortal,
419
+ ProxyNode,
420
+ toJSON
421
+ } from "react-json-reconciler";
422
+ var TemplateContext = React6.createContext({
423
+ depth: 0
424
+ });
425
+ function addTemplateToObject(obj, templateObj, templateParentNodeType) {
426
+ let templateProp = obj.properties.find(
427
+ (p) => p.keyNode.value === "template" && p.valueNode?.type === "array"
428
+ );
429
+ if (!templateProp) {
430
+ templateProp = new PropertyNode2(new ValueNode("template"), new ArrayNode());
431
+ templateProp.parent = obj;
432
+ obj.properties.push(templateProp);
433
+ }
434
+ const templateItems = templateProp.valueNode;
435
+ templateItems.items.push(templateObj);
436
+ templateObj.parent = templateItems;
437
+ const templateParentProp = obj.properties.find(
438
+ (p) => p.keyNode.value === templateParentNodeType && p.valueNode?.type === "array"
439
+ );
440
+ if (templateParentProp) {
441
+ const indexOfTemplateParent = obj.properties.indexOf(templateParentProp, 1);
442
+ const templateParentValueNode = obj.properties[indexOfTemplateParent]?.valueNode;
443
+ if (templateParentValueNode) {
444
+ const templateParentArray = toJSON(templateParentValueNode);
445
+ if (Array.isArray(templateParentArray) && templateParentArray.length === 0) {
446
+ obj.properties.splice(indexOfTemplateParent, 1);
447
+ }
448
+ }
449
+ }
450
+ return () => {
451
+ templateItems.items = templateItems.items.filter((t) => t !== templateObj);
452
+ if (templateItems.children.length === 0 && templateProp) {
453
+ obj.properties.splice(obj.properties.indexOf(templateProp, 1));
454
+ }
455
+ };
456
+ }
457
+ var TemplateProvider = (props) => {
458
+ const baseContext = React6.useContext(TemplateContext);
459
+ return /* @__PURE__ */ React6.createElement(TemplateContext.Provider, { value: { depth: baseContext.depth + 1 } }, props.children);
460
+ };
461
+ var getParentObject = (node) => {
462
+ if (node.type === "object") {
463
+ return node;
464
+ }
465
+ if (node.parent) {
466
+ return getParentObject(node.parent);
467
+ }
468
+ };
469
+ var getParentProperty = (node) => {
470
+ if (node.type === "property") {
471
+ return node;
472
+ }
473
+ if (node.parent) {
474
+ return getParentProperty(node.parent);
475
+ }
476
+ };
477
+ var Template = (props) => {
478
+ const baseContext = React6.useContext(TemplateContext);
479
+ const dynamicProp = props.dynamic ?? false;
480
+ const [outputProp, setOutputProp] = React6.useState(
481
+ props.output
482
+ );
483
+ const proxyRef = React6.useRef(null);
484
+ const valueRef = React6.useRef(null);
485
+ const outputElement = React6.useMemo(() => new ProxyNode(), []);
486
+ React6.useLayoutEffect(() => {
487
+ const propNode = proxyRef.current && getParentProperty(proxyRef.current);
488
+ if (outputProp === void 0 && propNode) {
489
+ setOutputProp(propNode.keyNode.value);
490
+ }
491
+ }, [proxyRef, outputProp]);
492
+ React6.useEffect(() => {
493
+ const templateObj = outputElement.items[0];
494
+ if (proxyRef.current) {
495
+ const parentObject = getParentObject(proxyRef.current);
496
+ if (!parentObject) {
497
+ throw new Error("Unable to find parent to add template to");
498
+ }
499
+ if (!outputProp) {
500
+ return;
501
+ }
502
+ return addTemplateToObject(parentObject, templateObj, outputProp);
503
+ }
504
+ }, [proxyRef, outputProp, outputElement.items]);
505
+ return /* @__PURE__ */ React6.createElement("proxy", { ref: proxyRef }, /* @__PURE__ */ React6.createElement(React6.Fragment, null, createPortal(
506
+ /* @__PURE__ */ React6.createElement(
507
+ OptionalIDSuffixProvider,
508
+ {
509
+ wrapperRef: valueRef,
510
+ templateIndex: `_index${baseContext.depth === 0 ? "" : baseContext.depth}_`
511
+ },
512
+ /* @__PURE__ */ React6.createElement(TemplateProvider, null, /* @__PURE__ */ React6.createElement("object", null, /* @__PURE__ */ React6.createElement("property", { name: "data" }, props.data.toValue()), /* @__PURE__ */ React6.createElement("property", { name: "output" }, outputProp), /* @__PURE__ */ React6.createElement("property", { name: "value" }, props.children), dynamicProp && /* @__PURE__ */ React6.createElement("property", { name: "dynamic" }, toJsonElement(dynamicProp))))
513
+ ),
514
+ outputElement
515
+ ), /* @__PURE__ */ React6.createElement("value", { ref: valueRef, value: void 0 })));
516
+ };
517
+
518
+ // ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/dsl/src/index.ts
519
+ export * from "react-json-reconciler";
520
+
521
+ // ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/dsl/src/compiler/schema.ts
522
+ import { dequal } from "dequal";
523
+ import { SyncWaterfallHook } from "tapable-ts";
524
+ var bindingSymbol = Symbol("binding");
525
+ var SchemaTypeName = Symbol("Schema Rename");
526
+ var isTypeDef = (property) => {
527
+ return property.type !== void 0;
528
+ };
529
+ var SchemaGenerator = class {
530
+ constructor(logger) {
531
+ this.hooks = {
532
+ createSchemaNode: new SyncWaterfallHook()
533
+ };
534
+ /**
535
+ * Converts an object to a `Schema.Schema` representation
536
+ * Note: uses iteration to prevent potentially very deep recursion on large objects
537
+ */
538
+ this.toSchema = (schema) => {
539
+ const newSchema = {
540
+ ROOT: {}
541
+ };
542
+ this.children = [];
543
+ this.generatedDataTypes.clear();
544
+ Object.keys(schema).forEach((property) => {
545
+ const subType = schema[property];
546
+ newSchema.ROOT[property] = this.hooks.createSchemaNode.call(
547
+ this.processChild(property, subType),
548
+ subType
549
+ );
550
+ });
551
+ while (this.children.length > 0) {
552
+ const c = this.children.pop();
553
+ if (c === void 0) {
554
+ break;
555
+ }
556
+ const { name, child } = c;
557
+ const typeDef = {};
558
+ Object.keys(child).forEach((property) => {
559
+ const subType = child[property];
560
+ typeDef[property] = this.hooks.createSchemaNode.call(
561
+ this.processChild(property, subType),
562
+ subType
563
+ );
564
+ });
565
+ newSchema[name] = typeDef;
566
+ }
567
+ return newSchema;
568
+ };
569
+ /**
570
+ * Make an intermediate `Schema.DataType` object given a name
571
+ */
572
+ this.makePlaceholderType = (typeName) => {
573
+ return {
574
+ type: `${typeName}Type`
575
+ };
576
+ };
577
+ this.children = [];
578
+ this.generatedDataTypes = /* @__PURE__ */ new Map();
579
+ this.logger = logger ?? console;
580
+ }
581
+ /**
582
+ * Processes the children of an object Node
583
+ * Newly discovered children get added to the provided array
584
+ */
585
+ processChild(property, subType) {
586
+ if (isTypeDef(subType)) {
587
+ return subType;
588
+ }
589
+ let intermediateType;
590
+ let child;
591
+ if (Array.isArray(subType)) {
592
+ if (subType.length > 1) {
593
+ this.logger.warn(
594
+ `Type ${property} has multiple types in array, should only contain one top level object type. Only taking first defined type`
595
+ );
596
+ }
597
+ const subTypeName = subType[0][SchemaTypeName] ?? property;
598
+ intermediateType = this.makePlaceholderArrayType(subTypeName);
599
+ [child] = subType;
600
+ } else {
601
+ const subTypeName = subType[SchemaTypeName] ?? property;
602
+ intermediateType = this.makePlaceholderType(subTypeName);
603
+ child = subType;
604
+ }
605
+ this.children.push({ name: intermediateType.type, child });
606
+ if (this.generatedDataTypes.has(intermediateType.type)) {
607
+ const generatedType = this.generatedDataTypes.get(
608
+ intermediateType.type
609
+ );
610
+ if (!dequal(
611
+ child,
612
+ this.generatedDataTypes.get(intermediateType.type)?.node
613
+ )) {
614
+ generatedType.count += 1;
615
+ const newIntermediateType = {
616
+ ...intermediateType,
617
+ type: `${intermediateType.type}${generatedType.count}`
618
+ };
619
+ this.logger.warn(
620
+ `WARNING: Generated two intermediate types with the name: ${intermediateType.type} that are of different shapes, using artificial type ${newIntermediateType.type}`
621
+ );
622
+ intermediateType = newIntermediateType;
623
+ this.children.pop();
624
+ this.children.push({ name: intermediateType.type, child });
625
+ }
626
+ }
627
+ this.generatedDataTypes.set(intermediateType.type, {
628
+ node: subType,
629
+ count: 1
630
+ });
631
+ return intermediateType;
632
+ }
633
+ /**
634
+ * Make an intermediate `Schema.DataType` object with array support given a name
635
+ */
636
+ makePlaceholderArrayType(typeName) {
637
+ return {
638
+ type: `${typeName}Type`,
639
+ isArray: true
640
+ };
641
+ }
642
+ };
643
+ function makeBindingsForObject(obj, arrayAccessorKeys = ["_index_"]) {
644
+ const accessor = (paths) => {
645
+ const bindingMap = /* @__PURE__ */ new WeakMap();
646
+ return {
647
+ ownKeys(target) {
648
+ return Reflect.ownKeys(target);
649
+ },
650
+ get(target, key) {
651
+ const bindingKeys = Object.keys(target);
652
+ if (Array.isArray(target[key]) && target[key].length > 0 && target[key].every((it) => typeof it !== "object")) {
653
+ return [...target[key]];
654
+ }
655
+ if (!bindingMap.has(target)) {
656
+ bindingMap.set(target, binding`${paths.join(".")}`);
657
+ }
658
+ if (key === bindingSymbol) {
659
+ return paths;
660
+ }
661
+ if (Array.isArray(target) && (arrayAccessorKeys.includes(key) || typeof key === "number")) {
662
+ return new Proxy(target[0], accessor(paths.concat([key])));
663
+ }
664
+ if (bindingKeys.includes(key) && typeof target[key] === "object") {
665
+ return new Proxy(target[key], accessor(paths.concat([key])));
666
+ }
667
+ const createdInstance = bindingMap.get(target);
668
+ return createdInstance?.[key];
669
+ }
670
+ };
671
+ };
672
+ return new Proxy(obj, accessor([]));
673
+ }
674
+ var getBindingFromObject = (obj) => {
675
+ const baseBindings = obj[bindingSymbol];
676
+ if (!Array.isArray(baseBindings) || baseBindings.length === 0) {
677
+ throw new Error(`Unable to get binding for ${obj}`);
678
+ }
679
+ return binding`${baseBindings.join(".")}`;
680
+ };
681
+ var getBindingStringFromObject = (obj) => {
682
+ return getBindingFromObject(obj).toString();
683
+ };
684
+ var getRefStringFromObject = (obj) => {
685
+ return getBindingFromObject(obj).toRefString();
686
+ };
687
+
688
+ // ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/dsl/src/compiler/compiler.ts
689
+ import React8 from "react";
690
+ import { SourceMapGenerator, SourceMapConsumer } from "source-map-js";
691
+ import { render } from "react-json-reconciler";
692
+ import {
693
+ AsyncSeriesHook,
694
+ AsyncSeriesWaterfallHook,
695
+ SyncHook
696
+ } from "tapable-ts";
697
+
698
+ // ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/dsl/src/compiler/utils.ts
699
+ import React7 from "react";
700
+ var fingerprintContent = (content, filename) => {
701
+ if (content !== null || content !== void 0) {
702
+ if (React7.isValidElement(content)) {
703
+ return "view";
704
+ }
705
+ if (typeof content === "object" && "navigation" in content) {
706
+ return "flow";
707
+ }
708
+ if (!filename || filename.includes("schema")) {
709
+ return "schema";
710
+ }
711
+ }
712
+ };
713
+
714
+ // ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/dsl/src/compiler/compiler.ts
715
+ var parseNavigationExpressions = (nav) => {
716
+ function replaceExpWithStr(obj) {
717
+ function convExp(value) {
718
+ return value && typeof value === "object" && value.__type === "expression" ? value.toValue() : replaceExpWithStr(value);
719
+ }
720
+ if (Array.isArray(obj)) {
721
+ return obj.map(convExp);
722
+ }
723
+ if (typeof obj === "object") {
724
+ const copy = { ...obj };
725
+ for (const [key, value] of Object.entries(copy)) {
726
+ copy[key] = convExp(value);
727
+ }
728
+ return copy;
729
+ }
730
+ return obj;
731
+ }
732
+ return replaceExpWithStr(nav);
733
+ };
734
+ var mergeSourceMaps = (sourceMaps, generated) => {
735
+ const generator = new SourceMapGenerator();
736
+ sourceMaps.forEach(({ sourceMap, offsetIndexSearch, source }) => {
737
+ const generatedLineOffset = generated.split("\n").findIndex((line) => line.includes(offsetIndexSearch));
738
+ const sourceLineOffset = source.split("\n").findIndex((line) => line.includes(offsetIndexSearch));
739
+ const lineOffset = generatedLineOffset - sourceLineOffset;
740
+ const generatedLine = generated.split("\n")[generatedLineOffset];
741
+ const sourceLine = source.split("\n")[sourceLineOffset];
742
+ const generatedColumn = generatedLine.indexOf(offsetIndexSearch);
743
+ const sourceColumn = sourceLine.indexOf(offsetIndexSearch);
744
+ const columnOffset = generatedColumn - sourceColumn;
745
+ const consumer = new SourceMapConsumer(JSON.parse(sourceMap));
746
+ consumer.eachMapping((mapping) => {
747
+ generator.addMapping({
748
+ generated: {
749
+ line: mapping.generatedLine + lineOffset,
750
+ column: mapping.generatedColumn + columnOffset
751
+ },
752
+ original: {
753
+ line: mapping.originalLine,
754
+ column: mapping.originalColumn
755
+ },
756
+ source: mapping.source
757
+ });
758
+ });
759
+ });
760
+ return generator.toString();
761
+ };
762
+ var DSLCompiler = class {
763
+ constructor(logger) {
764
+ this.hooks = {
765
+ // Hook to access the schema generator instance when initialized
766
+ schemaGenerator: new SyncHook(),
767
+ // Hook to access pre-compilation object
768
+ preProcessFlow: new AsyncSeriesWaterfallHook(),
769
+ // Hook to access post-compilation Flow before output is written
770
+ postProcessFlow: new AsyncSeriesWaterfallHook(),
771
+ // Hook called after all files are compiled. Revives the output directory
772
+ onEnd: new AsyncSeriesHook()
773
+ };
774
+ this.logger = logger ?? console;
775
+ }
776
+ /** Convert an object (flow, view, schema, etc) into it's JSON representation */
777
+ async serialize(value, context) {
778
+ if (typeof value !== "object" || value === null) {
779
+ throw new Error("Unable to serialize non-object");
780
+ }
781
+ const type = context?.type ? context.type : fingerprintContent(value);
782
+ if (!this.schemaGenerator) {
783
+ this.schemaGenerator = new SchemaGenerator(this.logger);
784
+ this.hooks.schemaGenerator.call(this.schemaGenerator);
785
+ }
786
+ if (type === "view") {
787
+ const { jsonValue, sourceMap } = await render(value, {
788
+ collectSourceMap: true
789
+ });
790
+ return {
791
+ value: jsonValue,
792
+ sourceMap
793
+ };
794
+ }
795
+ if (type === "flow") {
796
+ const allSourceMaps = [];
797
+ const copiedValue = {
798
+ ...value
799
+ };
800
+ copiedValue.views = await Promise.all(
801
+ copiedValue?.views?.map(async (node) => {
802
+ if (React8.isValidElement(node)) {
803
+ const { jsonValue, sourceMap, stringValue } = await render(node, {
804
+ collectSourceMap: true
805
+ });
806
+ if (sourceMap) {
807
+ const searchIdLine = stringValue.split("\n").find(
808
+ (line) => line.includes(
809
+ `"id": "${jsonValue.id}"`
810
+ )
811
+ );
812
+ if (searchIdLine) {
813
+ allSourceMaps.push({
814
+ sourceMap,
815
+ offsetIndexSearch: searchIdLine,
816
+ source: stringValue
817
+ });
818
+ }
819
+ }
820
+ return jsonValue;
821
+ }
822
+ return node;
823
+ }) ?? []
824
+ );
825
+ if ("navigation" in value) {
826
+ Object.entries(value.navigation).forEach(([navKey, node]) => {
827
+ if (typeof node === "object") {
828
+ Object.entries(node).forEach(([nodeKey, flowNode]) => {
829
+ if (flowNode && typeof flowNode === "object" && "state_type" in flowNode && flowNode.state_type === "VIEW" && React8.isValidElement(flowNode.ref)) {
830
+ const actualViewIndex = value.views?.indexOf?.(
831
+ flowNode.ref
832
+ );
833
+ if (actualViewIndex !== void 0 && actualViewIndex > -1) {
834
+ const actualId = copiedValue.views?.[actualViewIndex]?.id;
835
+ copiedValue.navigation[navKey][nodeKey].ref = actualId;
836
+ }
837
+ }
838
+ });
839
+ }
840
+ });
841
+ }
842
+ if ("schema" in copiedValue) {
843
+ copiedValue.schema = this.schemaGenerator.toSchema(copiedValue.schema);
844
+ }
845
+ copiedValue.navigation = parseNavigationExpressions(
846
+ copiedValue.navigation
847
+ );
848
+ if (value) {
849
+ const postProcessFlow = await this.hooks.postProcessFlow.call(
850
+ copiedValue
851
+ );
852
+ return {
853
+ value: postProcessFlow,
854
+ sourceMap: mergeSourceMaps(
855
+ allSourceMaps,
856
+ JSON.stringify(copiedValue, null, 2)
857
+ )
858
+ };
859
+ }
860
+ }
861
+ return {
862
+ value: this.schemaGenerator.toSchema(value)
863
+ };
864
+ }
865
+ };
866
+
867
+ // ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/dsl/src/compiler/types.ts
868
+ var DefaultCompilerContentTypes = ["view", "flow", "schema"];
869
+ function isDefaultCompilerContentType(t) {
870
+ return DefaultCompilerContentTypes.includes(t);
871
+ }
872
+ export {
873
+ Asset,
874
+ AssetWrapper,
875
+ DSLCompiler,
876
+ GeneratedIDProperty,
877
+ IDProvider,
878
+ IDSuffixIndexProvider,
879
+ IDSuffixProvider,
880
+ IndexSuffixStopContext,
881
+ OptionalIDSuffixProvider,
882
+ SchemaGenerator,
883
+ SchemaTypeName,
884
+ Slot,
885
+ SlotContext,
886
+ Switch,
887
+ Template,
888
+ TemplateContext,
889
+ TemplateStringComponent,
890
+ View,
891
+ binding,
892
+ createSlot,
893
+ expression,
894
+ fingerprintContent,
895
+ flattenChildren,
896
+ getBindingFromObject,
897
+ getBindingStringFromObject,
898
+ getRefStringFromObject,
899
+ isDefaultCompilerContentType,
900
+ isTemplateStringInstance,
901
+ makeBindingsForObject,
902
+ normalizeText,
903
+ normalizeToCollection,
904
+ toArray,
905
+ toJsonElement,
906
+ toJsonProperties,
907
+ useGetIdPrefix,
908
+ useIndexInSlot
909
+ };
910
+ //# sourceMappingURL=index.mjs.map