@azx-pbc/axiom-form-builder 0.0.0-stage → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +231 -2
  3. package/dist/bridge/index.d.ts +12 -0
  4. package/dist/bridge/index.d.ts.map +1 -0
  5. package/dist/bridge/index.js +10 -0
  6. package/dist/bridge/index.js.map +1 -0
  7. package/dist/bridge/inspector-commands.d.ts +55 -0
  8. package/dist/bridge/inspector-commands.d.ts.map +1 -0
  9. package/dist/bridge/inspector-commands.js +532 -0
  10. package/dist/bridge/inspector-commands.js.map +1 -0
  11. package/dist/bridge/rule-context.d.ts +57 -0
  12. package/dist/bridge/rule-context.d.ts.map +1 -0
  13. package/dist/bridge/rule-context.js +302 -0
  14. package/dist/bridge/rule-context.js.map +1 -0
  15. package/dist/index.d.ts +32 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +28 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/inspector.d.ts +125 -0
  20. package/dist/inspector.d.ts.map +1 -0
  21. package/dist/inspector.js +2 -0
  22. package/dist/inspector.js.map +1 -0
  23. package/dist/reference/ReferencePicker.d.ts +59 -0
  24. package/dist/reference/ReferencePicker.d.ts.map +1 -0
  25. package/dist/reference/ReferencePicker.js +145 -0
  26. package/dist/reference/ReferencePicker.js.map +1 -0
  27. package/dist/reference/index.d.ts +13 -0
  28. package/dist/reference/index.d.ts.map +1 -0
  29. package/dist/reference/index.js +11 -0
  30. package/dist/reference/index.js.map +1 -0
  31. package/dist/reference/token.d.ts +44 -0
  32. package/dist/reference/token.d.ts.map +1 -0
  33. package/dist/reference/token.js +141 -0
  34. package/dist/reference/token.js.map +1 -0
  35. package/dist/reference/types.d.ts +49 -0
  36. package/dist/reference/types.d.ts.map +1 -0
  37. package/dist/reference/types.js +2 -0
  38. package/dist/reference/types.js.map +1 -0
  39. package/dist/rules/ConditionEditor.d.ts +38 -0
  40. package/dist/rules/ConditionEditor.d.ts.map +1 -0
  41. package/dist/rules/ConditionEditor.js +79 -0
  42. package/dist/rules/ConditionEditor.js.map +1 -0
  43. package/dist/rules/RuleBuilder.d.ts +76 -0
  44. package/dist/rules/RuleBuilder.d.ts.map +1 -0
  45. package/dist/rules/RuleBuilder.js +336 -0
  46. package/dist/rules/RuleBuilder.js.map +1 -0
  47. package/dist/rules/RuleList.d.ts +45 -0
  48. package/dist/rules/RuleList.d.ts.map +1 -0
  49. package/dist/rules/RuleList.js +62 -0
  50. package/dist/rules/RuleList.js.map +1 -0
  51. package/dist/rules/RuleMessageEditor.d.ts +38 -0
  52. package/dist/rules/RuleMessageEditor.d.ts.map +1 -0
  53. package/dist/rules/RuleMessageEditor.js +38 -0
  54. package/dist/rules/RuleMessageEditor.js.map +1 -0
  55. package/dist/rules/ValidationEditor.d.ts +60 -0
  56. package/dist/rules/ValidationEditor.d.ts.map +1 -0
  57. package/dist/rules/ValidationEditor.js +299 -0
  58. package/dist/rules/ValidationEditor.js.map +1 -0
  59. package/dist/rules/condition.d.ts +12 -0
  60. package/dist/rules/condition.d.ts.map +1 -0
  61. package/dist/rules/condition.js +46 -0
  62. package/dist/rules/condition.js.map +1 -0
  63. package/dist/rules/expression.d.ts +57 -0
  64. package/dist/rules/expression.d.ts.map +1 -0
  65. package/dist/rules/expression.js +286 -0
  66. package/dist/rules/expression.js.map +1 -0
  67. package/dist/rules/index.d.ts +28 -0
  68. package/dist/rules/index.d.ts.map +1 -0
  69. package/dist/rules/index.js +19 -0
  70. package/dist/rules/index.js.map +1 -0
  71. package/dist/rules/messages.d.ts +64 -0
  72. package/dist/rules/messages.d.ts.map +1 -0
  73. package/dist/rules/messages.js +114 -0
  74. package/dist/rules/messages.js.map +1 -0
  75. package/dist/rules/operators.d.ts +25 -0
  76. package/dist/rules/operators.d.ts.map +1 -0
  77. package/dist/rules/operators.js +128 -0
  78. package/dist/rules/operators.js.map +1 -0
  79. package/dist/rules/sentence.d.ts +151 -0
  80. package/dist/rules/sentence.d.ts.map +1 -0
  81. package/dist/rules/sentence.js +527 -0
  82. package/dist/rules/sentence.js.map +1 -0
  83. package/dist/rules/templates.d.ts +163 -0
  84. package/dist/rules/templates.d.ts.map +1 -0
  85. package/dist/rules/templates.js +560 -0
  86. package/dist/rules/templates.js.map +1 -0
  87. package/dist/rules/types.d.ts +209 -0
  88. package/dist/rules/types.d.ts.map +1 -0
  89. package/dist/rules/types.js +2 -0
  90. package/dist/rules/types.js.map +1 -0
  91. package/dist/rules/validation-editor-api.d.ts +12 -0
  92. package/dist/rules/validation-editor-api.d.ts.map +1 -0
  93. package/dist/rules/validation-editor-api.js +12 -0
  94. package/dist/rules/validation-editor-api.js.map +1 -0
  95. package/dist/rules/validation.d.ts +27 -0
  96. package/dist/rules/validation.d.ts.map +1 -0
  97. package/dist/rules/validation.js +184 -0
  98. package/dist/rules/validation.js.map +1 -0
  99. package/dist/styles.css +900 -0
  100. package/package.json +65 -3
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ConditionEditor.d.ts","sourceRoot":"","sources":["../../src/rules/ConditionEditor.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAC1C,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,iCAAiC,CAAC;AAE7E,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAmB,MAAM,YAAY,CAAC;AAE/E,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAEpE,oDAAoD;AACpD,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAcD,yCAAyC;AACzC,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,KAAK,EAAE,cAAc,CAAC;IAC/B,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC;IAC9B,QAAQ,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,cAAc,KAAK,IAAI,CAAC;IACnD,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,qBAAqB,CAAC,GAAG,SAAS,CAAC;IAC7D,QAAQ,CAAC,gBAAgB,CAAC,EAAE,OAAO,CAAC,sBAAsB,CAAC,GAAG,SAAS,CAAC;IACxE,QAAQ,CAAC,qBAAqB,CAAC,EAAE,OAAO,CAAC,qBAAqB,CAAC,GAAG,SAAS,CAAC;IAC5E,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACxC,QAAQ,CAAC,EAAE,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACjC,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACzC;AAED;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,oBAAoB,GAAG,YAAY,CAgHzE"}
@@ -0,0 +1,79 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useId } from 'react';
3
+ import { defaultConditionDraft } from './condition.js';
4
+ import { ValidationEditor } from './ValidationEditor.js';
5
+ const DEFAULT_LABELS = {
6
+ condition: 'Condition',
7
+ mode: 'Combine',
8
+ predicateMode: 'A single check',
9
+ andMode: 'Both are true',
10
+ orMode: 'Either is true',
11
+ notMode: 'Is not true',
12
+ first: 'First',
13
+ second: 'Second',
14
+ nested: 'Nested condition',
15
+ };
16
+ /**
17
+ * A condition tree: a field check, `and`, `or`, or `not`.
18
+ *
19
+ * Switching a node's mode preserves what it meant where it can: `not` wraps the current node,
20
+ * and `and`/`or` keep it as the first operand and add a fresh predicate as the second. A predicate
21
+ * delegates to `ValidationEditor`, so the one operator vocabulary and the one expression builder
22
+ * serve both the validation editor and every condition in a rule.
23
+ */
24
+ export function ConditionEditor(props) {
25
+ const labels = { ...DEFAULT_LABELS, ...props.labels };
26
+ const reactId = useId();
27
+ const baseId = props.id ?? `axb-condition-${reactId}`;
28
+ const mode = props.value.kind;
29
+ const changeMode = (next) => {
30
+ if (next === mode)
31
+ return;
32
+ if (next === 'predicate') {
33
+ props.onChange({
34
+ kind: 'predicate',
35
+ predicate: props.value.kind === 'predicate' ? props.value.predicate : defaultPredicate(props.value),
36
+ });
37
+ return;
38
+ }
39
+ if (next === 'not') {
40
+ props.onChange({ kind: 'not', operand: props.value });
41
+ return;
42
+ }
43
+ props.onChange({
44
+ kind: next,
45
+ operands: [props.value, defaultConditionDraft(props.context)],
46
+ });
47
+ };
48
+ return (_jsxs("div", { className: ['axb-condition', props.className].filter(Boolean).join(' '), children: [_jsxs("div", { className: "axb-condition__head", children: [_jsx("span", { className: "axb-condition__label", children: labels.condition }), _jsxs("label", { className: "axb-field axb-field--inline", htmlFor: `${baseId}-mode`, children: [_jsx("span", { className: "axb-field__label", children: labels.mode }), _jsxs("select", { id: `${baseId}-mode`, className: "axb-select", value: mode, disabled: props.disabled === true, onChange: (event) => {
49
+ const next = event.currentTarget.value;
50
+ if (next === 'predicate' || next === 'and' || next === 'or' || next === 'not') {
51
+ changeMode(next);
52
+ }
53
+ }, children: [_jsx("option", { value: "predicate", children: labels.predicateMode }), _jsx("option", { value: "and", children: labels.andMode }), _jsx("option", { value: "or", children: labels.orMode }), _jsx("option", { value: "not", children: labels.notMode })] })] })] }), props.value.kind === 'predicate' ? (_jsx(ValidationEditor, { value: props.value.predicate, context: props.context, labels: props.validationLabels, referencePickerLabels: props.referencePickerLabels, disabled: props.disabled, showExpression: false, onChange: (predicate) => props.onChange({ kind: 'predicate', predicate }) })) : null, props.value.kind === 'and' || props.value.kind === 'or' ? (_jsxs("div", { className: "axb-condition__children", children: [_jsx(ConditionChild, { legend: labels.first, value: props.value.operands[0], context: props.context, labels: props.labels, validationLabels: props.validationLabels, referencePickerLabels: props.referencePickerLabels, disabled: props.disabled, onChange: (first) => {
54
+ if (props.value.kind !== 'and' && props.value.kind !== 'or')
55
+ return;
56
+ props.onChange({
57
+ kind: props.value.kind,
58
+ operands: [first, props.value.operands[1]],
59
+ });
60
+ } }), _jsx(ConditionChild, { legend: labels.second, value: props.value.operands[1], context: props.context, labels: props.labels, validationLabels: props.validationLabels, referencePickerLabels: props.referencePickerLabels, disabled: props.disabled, onChange: (second) => {
61
+ if (props.value.kind !== 'and' && props.value.kind !== 'or')
62
+ return;
63
+ props.onChange({
64
+ kind: props.value.kind,
65
+ operands: [props.value.operands[0], second],
66
+ });
67
+ } })] })) : null, props.value.kind === 'not' ? (_jsx(ConditionChild, { legend: labels.nested, value: props.value.operand, context: props.context, labels: props.labels, validationLabels: props.validationLabels, referencePickerLabels: props.referencePickerLabels, disabled: props.disabled, onChange: (operand) => props.onChange({ kind: 'not', operand }) })) : null] }));
68
+ }
69
+ function ConditionChild(props) {
70
+ return (_jsxs("fieldset", { className: "axb-condition__child", children: [_jsx("legend", { children: props.legend }), _jsx(ConditionEditor, { value: props.value, context: props.context, labels: props.labels, validationLabels: props.validationLabels, referencePickerLabels: props.referencePickerLabels, disabled: props.disabled, onChange: props.onChange })] }));
71
+ }
72
+ function defaultPredicate(value) {
73
+ if (value.kind === 'predicate')
74
+ return value.predicate;
75
+ if (value.kind === 'not')
76
+ return defaultPredicate(value.operand);
77
+ return defaultPredicate(value.operands[0]);
78
+ }
79
+ //# sourceMappingURL=ConditionEditor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ConditionEditor.js","sourceRoot":"","sources":["../../src/rules/ConditionEditor.ts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AAE,KAAK,CAAC,QAAQ,EACxB,QAAQ,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,GAC/D,CACH,CAAC,CAAC,CAAC,IAAI,IACJ,CACP,CAAC;AACJ,CAAC;AAED,SAAS,cAAc,CAAC,KASvB;IACC,OAAO,CACL,oBAAU,SAAS,EAAC,sBAAsB,aACxC,2BAAS,KAAK,CAAC,MAAM,GAAU,EAC/B,KAAC,eAAe,IACd,KAAK,EAAE,KAAK,CAAC,KAAK,EAClB,OAAO,EAAE,KAAK,CAAC,OAAO,EACtB,MAAM,EAAE,KAAK,CAAC,MAAM,EACpB,gBAAgB,EAAE,KAAK,CAAC,gBAAgB,EACxC,qBAAqB,EAAE,KAAK,CAAC,qBAAqB,EAClD,QAAQ,EAAE,KAAK,CAAC,QAAQ,EACxB,QAAQ,EAAE,KAAK,CAAC,QAAQ,GACxB,IACO,CACZ,CAAC;AACJ,CAAC;AAED,SAAS,gBAAgB,CAAC,KAAqB;IAC7C,IAAI,KAAK,CAAC,IAAI,KAAK,WAAW;QAAE,OAAO,KAAK,CAAC,SAAS,CAAC;IACvD,IAAI,KAAK,CAAC,IAAI,KAAK,KAAK;QAAE,OAAO,gBAAgB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IACjE,OAAO,gBAAgB,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;AAC7C,CAAC","sourcesContent":["import { useId } from 'react';\nimport type { ReactElement } from 'react';\nimport type { ReferencePickerLabels } from '../reference/ReferencePicker.js';\nimport { defaultConditionDraft } from './condition.js';\nimport type { ConditionDraft, RuleContext, ValidationDraft } from './types.js';\nimport { ValidationEditor } from './ValidationEditor.js';\nimport type { ValidationEditorLabels } from './ValidationEditor.js';\n\n/** The overridable chrome a condition tree uses. */\nexport interface ConditionEditorLabels {\n readonly condition: string;\n readonly mode: string;\n readonly predicateMode: string;\n readonly andMode: string;\n readonly orMode: string;\n readonly notMode: string;\n readonly first: string;\n readonly second: string;\n readonly nested: string;\n}\n\nconst DEFAULT_LABELS: ConditionEditorLabels = {\n condition: 'Condition',\n mode: 'Combine',\n predicateMode: 'A single check',\n andMode: 'Both are true',\n orMode: 'Either is true',\n notMode: 'Is not true',\n first: 'First',\n second: 'Second',\n nested: 'Nested condition',\n};\n\n/** The condition sub-builder's props. */\nexport interface ConditionEditorProps {\n readonly value: ConditionDraft;\n readonly context: RuleContext;\n readonly onChange: (draft: ConditionDraft) => void;\n readonly labels?: Partial<ConditionEditorLabels> | undefined;\n readonly validationLabels?: Partial<ValidationEditorLabels> | undefined;\n readonly referencePickerLabels?: Partial<ReferencePickerLabels> | undefined;\n readonly disabled?: boolean | undefined;\n readonly id?: string | undefined;\n readonly className?: string | undefined;\n}\n\n/**\n * A condition tree: a field check, `and`, `or`, or `not`.\n *\n * Switching a node's mode preserves what it meant where it can: `not` wraps the current node,\n * and `and`/`or` keep it as the first operand and add a fresh predicate as the second. A predicate\n * delegates to `ValidationEditor`, so the one operator vocabulary and the one expression builder\n * serve both the validation editor and every condition in a rule.\n */\nexport function ConditionEditor(props: ConditionEditorProps): ReactElement {\n const labels: ConditionEditorLabels = { ...DEFAULT_LABELS, ...props.labels };\n const reactId = useId();\n const baseId = props.id ?? `axb-condition-${reactId}`;\n const mode = props.value.kind;\n\n const changeMode = (next: 'predicate' | 'and' | 'or' | 'not'): void => {\n if (next === mode) return;\n if (next === 'predicate') {\n props.onChange({\n kind: 'predicate',\n predicate:\n props.value.kind === 'predicate' ? props.value.predicate : defaultPredicate(props.value),\n });\n return;\n }\n if (next === 'not') {\n props.onChange({ kind: 'not', operand: props.value });\n return;\n }\n props.onChange({\n kind: next,\n operands: [props.value, defaultConditionDraft(props.context)],\n });\n };\n\n return (\n <div className={['axb-condition', props.className].filter(Boolean).join(' ')}>\n <div className=\"axb-condition__head\">\n <span className=\"axb-condition__label\">{labels.condition}</span>\n <label className=\"axb-field axb-field--inline\" htmlFor={`${baseId}-mode`}>\n <span className=\"axb-field__label\">{labels.mode}</span>\n <select\n id={`${baseId}-mode`}\n className=\"axb-select\"\n value={mode}\n disabled={props.disabled === true}\n onChange={(event) => {\n const next = event.currentTarget.value;\n if (next === 'predicate' || next === 'and' || next === 'or' || next === 'not') {\n changeMode(next);\n }\n }}\n >\n <option value=\"predicate\">{labels.predicateMode}</option>\n <option value=\"and\">{labels.andMode}</option>\n <option value=\"or\">{labels.orMode}</option>\n <option value=\"not\">{labels.notMode}</option>\n </select>\n </label>\n </div>\n {props.value.kind === 'predicate' ? (\n <ValidationEditor\n value={props.value.predicate}\n context={props.context}\n labels={props.validationLabels}\n referencePickerLabels={props.referencePickerLabels}\n disabled={props.disabled}\n showExpression={false}\n onChange={(predicate) => props.onChange({ kind: 'predicate', predicate })}\n />\n ) : null}\n {props.value.kind === 'and' || props.value.kind === 'or' ? (\n <div className=\"axb-condition__children\">\n <ConditionChild\n legend={labels.first}\n value={props.value.operands[0]}\n context={props.context}\n labels={props.labels}\n validationLabels={props.validationLabels}\n referencePickerLabels={props.referencePickerLabels}\n disabled={props.disabled}\n onChange={(first) => {\n if (props.value.kind !== 'and' && props.value.kind !== 'or') return;\n props.onChange({\n kind: props.value.kind,\n operands: [first, props.value.operands[1]],\n });\n }}\n />\n <ConditionChild\n legend={labels.second}\n value={props.value.operands[1]}\n context={props.context}\n labels={props.labels}\n validationLabels={props.validationLabels}\n referencePickerLabels={props.referencePickerLabels}\n disabled={props.disabled}\n onChange={(second) => {\n if (props.value.kind !== 'and' && props.value.kind !== 'or') return;\n props.onChange({\n kind: props.value.kind,\n operands: [props.value.operands[0], second],\n });\n }}\n />\n </div>\n ) : null}\n {props.value.kind === 'not' ? (\n <ConditionChild\n legend={labels.nested}\n value={props.value.operand}\n context={props.context}\n labels={props.labels}\n validationLabels={props.validationLabels}\n referencePickerLabels={props.referencePickerLabels}\n disabled={props.disabled}\n onChange={(operand) => props.onChange({ kind: 'not', operand })}\n />\n ) : null}\n </div>\n );\n}\n\nfunction ConditionChild(props: {\n readonly legend: string;\n readonly value: ConditionDraft;\n readonly context: RuleContext;\n readonly labels: Partial<ConditionEditorLabels> | undefined;\n readonly validationLabels: Partial<ValidationEditorLabels> | undefined;\n readonly referencePickerLabels: Partial<ReferencePickerLabels> | undefined;\n readonly disabled: boolean | undefined;\n readonly onChange: (draft: ConditionDraft) => void;\n}): ReactElement {\n return (\n <fieldset className=\"axb-condition__child\">\n <legend>{props.legend}</legend>\n <ConditionEditor\n value={props.value}\n context={props.context}\n labels={props.labels}\n validationLabels={props.validationLabels}\n referencePickerLabels={props.referencePickerLabels}\n disabled={props.disabled}\n onChange={props.onChange}\n />\n </fieldset>\n );\n}\n\nfunction defaultPredicate(value: ConditionDraft): ValidationDraft {\n if (value.kind === 'predicate') return value.predicate;\n if (value.kind === 'not') return defaultPredicate(value.operand);\n return defaultPredicate(value.operands[0]);\n}\n"]}
@@ -0,0 +1,76 @@
1
+ import type { ReactElement } from 'react';
2
+ import type { ReferencePickerLabels } from '../reference/ReferencePicker.js';
3
+ import type { ConditionEditorLabels } from './ConditionEditor.js';
4
+ import type { RuleSentenceLabels } from './sentence.js';
5
+ import type { EmittedRule, RuleProblem, RuleTemplateDefinition, RuleTemplateDraft } from './templates.js';
6
+ import type { RuleContext } from './types.js';
7
+ import type { ValidationEditorLabels } from './ValidationEditor.js';
8
+ /** The overridable chrome the rule builder uses. */
9
+ export interface RuleBuilderLabels {
10
+ readonly template: string;
11
+ readonly ruleId: string;
12
+ readonly messageKey: string;
13
+ readonly field: string;
14
+ /** The numeric question a total is written into; distinct from a condition's question. */
15
+ readonly totalField: string;
16
+ readonly target: string;
17
+ readonly targetField: string;
18
+ readonly targetSection: string;
19
+ readonly show: string;
20
+ readonly hide: string;
21
+ readonly table: string;
22
+ readonly column: string;
23
+ readonly condition: string;
24
+ readonly formula: string;
25
+ readonly addReference: string;
26
+ readonly hard: string;
27
+ readonly preview: string;
28
+ readonly sentence: string;
29
+ readonly expression: string;
30
+ readonly advanced: string;
31
+ readonly commit: string;
32
+ readonly ready: string;
33
+ readonly notReady: string;
34
+ readonly noTables: string;
35
+ readonly keyField: string;
36
+ readonly outputColumn: string;
37
+ readonly chooseTable: string;
38
+ readonly noField: string;
39
+ readonly noColumn: string;
40
+ readonly templateLabel: (template: RuleTemplateDefinition) => string;
41
+ readonly problem: (problem: RuleProblem) => string;
42
+ }
43
+ /** The rule builder's props: a draft in, a changed draft out, and a complete rule on commit. */
44
+ export interface RuleBuilderProps {
45
+ readonly value: RuleTemplateDraft;
46
+ readonly context: RuleContext;
47
+ readonly onChange: (draft: RuleTemplateDraft) => void;
48
+ readonly onCommit?: ((rule: EmittedRule) => void) | undefined;
49
+ readonly labels?: Partial<RuleBuilderLabels> | undefined;
50
+ readonly validationLabels?: Partial<ValidationEditorLabels> | undefined;
51
+ readonly conditionLabels?: Partial<ConditionEditorLabels> | undefined;
52
+ readonly sentenceLabels?: Partial<RuleSentenceLabels> | undefined;
53
+ readonly referencePickerLabels?: Partial<ReferencePickerLabels> | undefined;
54
+ readonly disabled?: boolean | undefined;
55
+ readonly id?: string | undefined;
56
+ readonly className?: string | undefined;
57
+ /**
58
+ * The host's message slot for a template that raises a sentence.
59
+ *
60
+ * A host with its own message control supplies the key the store will mint (or any non-empty
61
+ * placeholder, since the store mints from the rule id) and the builder stops rendering its own
62
+ * key field; an empty string keeps the template incomplete, which is how the host says "no
63
+ * message yet". Absent means the author edits the key here, which is the default.
64
+ */
65
+ readonly messageKey?: string | undefined;
66
+ }
67
+ /**
68
+ * Sentence templates that compile to rules.
69
+ *
70
+ * The draft is controlled and the expression is built as a tree and printed by the compiler's
71
+ * printer on every change, so the source under the sentence is what will be stored. `onCommit`
72
+ * receives the `EmittedRule` only when the draft is complete and the compiler accepts it; a draft
73
+ * that is not ready still renders its sentence and the reason it is not ready.
74
+ */
75
+ export declare function RuleBuilder(props: RuleBuilderProps): ReactElement;
76
+ //# sourceMapp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ACzD,QAAQ,CAAC,gBAAgB,CAAC,EAAE,OAAO,CAAC,sBAAsB,CAAC,GAAG,SAAS,CAAC;IACxE,QAAQ,CAAC,eAAe,CAAC,EAAE,OAAO,CAAC,qBAAqB,CAAC,GAAG,SAAS,CAAC;IACtE,QAAQ,CAAC,cAAc,CAAC,EAAE,OAAO,CAAC,kBAAkB,CAAC,GAAG,SAAS,CAAC;IAClE,QAAQ,CAAC,qBAAqB,CAAC,EAAE,OAAO,CAAC,qBAAqB,CAAC,GAAG,SAAS,CAAC;IAC5E,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACxC,QAAQ,CAAC,EAAE,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACjC,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACxC;;;;;;;OAOG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC1C;AAED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,gBAAgB,GAAG,YAAY,CA6IjE"}
@@ -0,0 +1,336 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useId, useRef } from 'react';
3
+ import { ReferencePicker } from '../reference/ReferencePicker.js';
4
+ import { insertReferenceAtCaret } from '../reference/token.js';
5
+ import { ConditionEditor } from './ConditionEditor.js';
6
+ import { defaultConditionDraft } from './condition.js';
7
+ import { rowContext } from './expression.js';
8
+ import { DEFAULT_RULE_SENTENCE_LABELS, deriveRuleSentence, renderRuleSentence, } from './sentence.js';
9
+ import { defaultKeyFieldIds, defaultRuleTemplateDraft, ruleBuilderBuild, ruleTemplateNeedsMessage, templatesForContext, } from './templates.js';
10
+ import { ValidationEditor } from './ValidationEditor.js';
11
+ const DEFAULT_LABELS = {
12
+ template: 'What it does',
13
+ ruleId: 'Check name (optional)',
14
+ messageKey: 'Message name',
15
+ field: 'Question',
16
+ totalField: 'Question to total',
17
+ target: 'Applies to',
18
+ targetField: 'One question',
19
+ targetSection: 'A section',
20
+ show: 'Show',
21
+ hide: 'Hide',
22
+ table: 'Table',
23
+ column: 'Column',
24
+ condition: 'Condition',
25
+ formula: 'Formula',
26
+ addReference: 'Insert a question',
27
+ hard: 'A hard blocker cannot be overridden',
28
+ preview: 'What this check will say',
29
+ sentence: 'This check says',
30
+ expression: 'Condition',
31
+ advanced: 'Advanced',
32
+ commit: 'Add check',
33
+ ready: 'Ready to add.',
34
+ notReady: 'Not ready yet.',
35
+ noTables: 'Add a table question to use this template.',
36
+ keyField: 'Column that identifies a row',
37
+ outputColumn: 'Output column (optional)',
38
+ chooseTable: 'Choose a table',
39
+ noField: 'There is no question to choose.',
40
+ noColumn: 'This table has no columns.',
41
+ templateLabel: (template) => template.label,
42
+ problem: defaultProblem,
43
+ };
44
+ function defaultProblem(problem) {
45
+ switch (problem.code) {
46
+ case 'missing-field':
47
+ return 'Choose a question.';
48
+ case 'missing-table':
49
+ return 'Choose a table.';
50
+ case 'missing-column':
51
+ return 'Choose a column.';
52
+ case 'missing-section':
53
+ return 'Choose a section.';
54
+ case 'table-columns-missing':
55
+ return 'That table has no columns.';
56
+ case 'missing-message-key':
57
+ return 'Give this check a message.';
58
+ case 'missing-source':
59
+ return 'Write the formula this check calculates.';
60
+ case 'key-count':
61
+ return `This table takes ${String(problem.expected)} key field${problem.expected === 1 ? '' : 's'}, not ${String(problem.actual)}.`;
62
+ case 'expression':
63
+ switch (problem.problem.code) {
64
+ case 'missing-operand':
65
+ return 'Choose a value to compare with.';
66
+ case 'empty-list':
67
+ return 'Add at least one value to the list.';
68
+ case 'invalid-literal':
69
+ return problem.problem.detail;
70
+ case 'unprintable':
71
+ return `This condition cannot be shown: ${problem.problem.detail}`;
72
+ case 'missing-field':
73
+ return `There is no question or row field named ${problem.problem.fieldId}.`;
74
+ case 'missing-table':
75
+ return 'That table is no longer in the form.';
76
+ case 'unsupported-primitive':
77
+ return 'This answer cannot be typed here.';
78
+ }
79
+ }
80
+ }
81
+ /**
82
+ * Sentence templates that compile to rules.
83
+ *
84
+ * The draft is controlled and the expression is built as a tree and printed by the compiler's
85
+ * printer on every change, so the source under the sentence is what will be stored. `onCommit`
86
+ * receives the `EmittedRule` only when the draft is complete and the compiler accepts it; a draft
87
+ * that is not ready still renders its sentence and the reason it is not ready.
88
+ */
89
+ export function RuleBuilder(props) {
90
+ const labels = { ...DEFAULT_LABELS, ...props.labels };
91
+ const reactId = useId();
92
+ const baseId = props.id ?? `axb-rule-builder-${reactId}`;
93
+ // The host's key is part of the draft the template builds from, so readiness and the emitted
94
+ // sentence agree with the message the author wrote in the host's control.
95
+ const draft = props.messageKey === undefined ? props.value : { ...props.value, messageKey: props.messageKey };
96
+ const build = ruleBuilderBuild(draft, props.context);
97
+ const sentenceRule = build.rule === undefined ? undefined : { ...build.rule, ruleId: build.rule.ruleId ?? '' };
98
+ const sentence = sentenceRule === undefined
99
+ ? labels.notReady
100
+ : renderRuleSentence(deriveRuleSentence(sentenceRule, props.context), {
101
+ ...DEFAULT_RULE_SENTENCE_LABELS,
102
+ ...props.sentenceLabels,
103
+ });
104
+ const expression = displayExpression(build);
105
+ const templates = templatesForContext(props.context);
106
+ return (_jsxs("div", { className: ['axb-rule-builder', props.className].filter(Boolean).join(' '), children: [_jsxs("div", { className: "axb-validation__row", children: [_jsxs("label", { className: "axb-field", htmlFor: `${baseId}-template`, children: [_jsx("span", { className: "axb-field__label", children: labels.template }), _jsx("select", { id: `${baseId}-template`, className: "axb-select", value: props.value.template, disabled: props.disabled === true, onChange: (event) => {
107
+ const template = event.currentTarget.value;
108
+ const known = templates.find((definition) => definition.id === template);
109
+ if (known === undefined)
110
+ return;
111
+ props.onChange(defaultRuleTemplateDraft(known.id, props.context));
112
+ }, children: templates.map((template) => (_jsx("option", { value: template.id, children: labels.templateLabel(template) }, template.id))) })] }), _jsxs("label", { className: "axb-field", htmlFor: `${baseId}-rule-id`, children: [_jsx("span", { className: "axb-field__label", children: labels.ruleId }), _jsx("input", { id: `${baseId}-rule-id`, className: "axb-input axb-input--mono", type: "text", value: props.value.ruleId ?? '', disabled: props.disabled === true, onChange: (event) => {
113
+ const value = event.currentTarget.value;
114
+ props.onChange(setRuleId(props.value, value));
115
+ } })] })] }), _jsx(TemplateFields, { draft: props.value, context: props.context, labels: labels, explicitAddReferenceLabel: props.labels?.addReference, validationLabels: props.validationLabels, conditionLabels: props.conditionLabels, referencePickerLabels: props.referencePickerLabels, disabled: props.disabled === true, baseId: baseId, onChange: props.onChange }), props.messageKey === undefined && needsMessage(props.value) ? (_jsxs("label", { className: "axb-field", children: [_jsx("span", { className: "axb-field__label", children: labels.messageKey }), _jsx("input", { className: "axb-input axb-input--mono", type: "text", value: props.value.messageKey ?? '', disabled: props.disabled === true, onChange: (event) => {
116
+ const value = event.currentTarget.value;
117
+ props.onChange(setMessageKey(props.value, value));
118
+ } })] })) : null, _jsxs("div", { className: "axb-rule-preview", children: [_jsxs("p", { className: "axb-rule-preview__sentence", children: [_jsx("span", { className: "axb-rule-preview__label", children: labels.sentence }), " ", sentence] }), expression === undefined ? null : (_jsxs("details", { className: "axb-advanced", children: [_jsx("summary", { children: labels.advanced }), _jsxs("p", { className: "axb-rule-preview__expression", children: [_jsx("span", { className: "axb-rule-preview__label", children: labels.expression }), ' ', _jsx("code", { children: expression })] })] })), build.problems.length === 0 ? null : (_jsx("ul", { className: "axb-problems", "aria-label": labels.notReady, children: build.problems.map((problem, index) => (_jsx("li", { className: "axb-problem", children: labels.problem(problem) }, `${problem.code}-${String(index)}`))) })), build.diagnostics.length === 0 ? null : (_jsx("ul", { className: "axb-problems", "aria-label": labels.notReady, children: build.diagnostics.map((diagnostic) => (_jsxs("li", { className: "axb-problem", children: [_jsx("code", { children: diagnostic.code }), " ", diagnostic.detail] }, `${diagnostic.code}-${String(diagnostic.offset)}`))) })), _jsx("p", { className: build.ready ? 'axb-ready' : 'axb-hint', role: "status", "aria-live": "polite", children: build.ready ? labels.ready : labels.notReady })] }), props.onCommit === undefined ? null : (_jsx("button", { type: "button", className: "axb-button axb-button--primary", disabled: props.disabled === true || !build.ready || build.rule === undefined, onClick: () => {
119
+ if (build.rule !== undefined)
120
+ props.onCommit?.(build.rule);
121
+ }, children: labels.commit }))] }));
122
+ }
123
+ function TemplateFields(props) {
124
+ const draft = props.draft;
125
+ switch (draft.template) {
126
+ case 'require':
127
+ return (_jsxs("div", { className: "axb-template-fields", children: [_jsx(FieldSelect, { id: `${props.baseId}-require-field`, label: props.labels.field, emptyText: props.labels.noField, fields: props.context.fields.filter((field) => field.primitive !== 'array'), value: draft.fieldId, disabled: props.disabled, onChange: (fieldId) => props.onChange({ ...draft, fieldId }) }), _jsxs("label", { className: "axb-check", children: [_jsx("input", { type: "checkbox", checked: draft.condition !== undefined, disabled: props.disabled, onChange: (event) => {
128
+ if (event.currentTarget.checked) {
129
+ props.onChange({
130
+ ...draft,
131
+ condition: defaultConditionDraft(props.context),
132
+ });
133
+ }
134
+ else {
135
+ const { condition: _condition, ...rest } = draft;
136
+ props.onChange(rest);
137
+ }
138
+ } }), _jsx("span", { children: props.labels.condition })] }), draft.condition === undefined ? null : (_jsx(ConditionEditor, { value: draft.condition, context: props.context, labels: props.conditionLabels, validationLabels: props.validationLabels, referencePickerLabels: props.referencePickerLabels, disabled: props.disabled, onChange: (condition) => props.onChange({ ...draft, condition }) }))] }));
139
+ case 'show-hide':
140
+ return (_jsxs("div", { className: "axb-template-fields", children: [_jsxs("label", { className: "axb-field", children: [_jsx("span", { className: "axb-field__label", children: props.labels.target }), _jsxs("select", { className: "axb-select", value: draft.target.kind, disabled: props.disabled, onChange: (event) => {
141
+ const kind = event.currentTarget.value;
142
+ if (kind === 'section') {
143
+ const section = props.context.sections?.[0];
144
+ props.onChange({
145
+ ...draft,
146
+ target: { kind: 'section', sectionId: section?.sectionId ?? '' },
147
+ });
148
+ }
149
+ else {
150
+ props.onChange({
151
+ ...draft,
152
+ target: { kind: 'field', fieldId: firstFieldId(props.context) },
153
+ });
154
+ }
155
+ }, children: [_jsx("option", { value: "field", children: props.labels.targetField }), _jsx("option", { value: "section", children: props.labels.targetSection })] })] }), draft.target.kind === 'field' ? (_jsx(FieldSelect, { id: `${props.baseId}-show-field`, label: props.labels.field, emptyText: props.labels.noField, fields: props.context.fields, value: draft.target.fieldId, disabled: props.disabled, onChange: (fieldId) => props.onChange({ ...draft, target: { kind: 'field', fieldId } }) })) : (_jsxs("label", { className: "axb-field", children: [_jsx("span", { className: "axb-field__label", children: props.labels.targetSection }), _jsx("select", { className: "axb-select", value: draft.target.sectionId, disabled: props.disabled, onChange: (event) => props.onChange({
156
+ ...draft,
157
+ target: { kind: 'section', sectionId: event.currentTarget.value },
158
+ }), children: (props.context.sections ?? []).map((section) => (_jsx("option", { value: section.sectionId, children: section.label }, section.sectionId))) })] })), _jsxs("label", { className: "axb-field", children: [_jsx("span", { className: "axb-field__label", children: props.labels.show }), _jsxs("select", { className: "axb-select", value: draft.mode, disabled: props.disabled, onChange: (event) => {
159
+ const mode = event.currentTarget.value;
160
+ if (mode === 'show' || mode === 'hide') {
161
+ props.onChange({ ...draft, mode });
162
+ }
163
+ }, children: [_jsx("option", { value: "show", children: props.labels.show }), _jsx("option", { value: "hide", children: props.labels.hide })] })] }), _jsx(ConditionEditor, { value: draft.condition, context: props.context, labels: props.conditionLabels, validationLabels: props.validationLabels, referencePickerLabels: props.referencePickerLabels, disabled: props.disabled, onChange: (condition) => props.onChange({ ...draft, condition }) })] }));
164
+ case 'calculate-total':
165
+ return (_jsxs("div", { className: "axb-template-fields", children: [_jsx(FieldSelect, { id: `${props.baseId}-total-target`, label: props.labels.totalField, emptyText: props.labels.noField, fields: targetFields(props.context.fields, 'number'), value: draft.fieldId, disabled: props.disabled, onChange: (fieldId) => props.onChange({ ...draft, fieldId }) }), _jsx(TableFieldSelect, { id: `${props.baseId}-total-table`, label: props.labels.table, fields: props.context.fields, value: draft.tableFieldId, disabled: props.disabled, labels: props.labels, onChange: (tableFieldId) => {
166
+ const table = props.context.fields.find((field) => field.fieldId === tableFieldId);
167
+ props.onChange({
168
+ ...draft,
169
+ tableFieldId,
170
+ columnFieldId: table?.columns?.[0]?.fieldId ?? '',
171
+ });
172
+ } }), _jsx(ColumnSelect, { id: `${props.baseId}-total-column`, label: props.labels.column, emptyText: props.labels.noColumn, table: props.context.fields.find((field) => field.fieldId === draft.tableFieldId), value: draft.columnFieldId, disabled: props.disabled, onChange: (columnFieldId) => props.onChange({ ...draft, columnFieldId }) })] }));
173
+ case 'calculate-count':
174
+ case 'calculate-any':
175
+ case 'calculate-all': {
176
+ const table = props.context.fields.find((field) => field.fieldId === draft.tableFieldId);
177
+ const row = table === undefined ? props.context : rowContext(table, props.context.tables);
178
+ return (_jsxs("div", { className: "axb-template-fields", children: [_jsx(FieldSelect, { id: `${props.baseId}-aggregate-target`, label: props.labels.field, emptyText: props.labels.noField, fields: targetFields(props.context.fields, draft.template === 'calculate-count' ? 'integer' : 'boolean'), value: draft.fieldId, disabled: props.disabled, onChange: (fieldId) => props.onChange({ ...draft, fieldId }) }), _jsx(TableFieldSelect, { id: `${props.baseId}-aggregate-table`, label: props.labels.table, fields: props.context.fields, value: draft.tableFieldId, disabled: props.disabled, labels: props.labels, onChange: (tableFieldId) => {
179
+ const next = props.context.fields.find((field) => field.fieldId === tableFieldId);
180
+ const nextRow = next === undefined ? props.context : rowContext(next, props.context.tables);
181
+ props.onChange({
182
+ ...draft,
183
+ tableFieldId,
184
+ condition: defaultConditionDraft(nextRow),
185
+ });
186
+ } }), _jsx(ConditionEditor, { value: draft.condition, context: row, labels: props.conditionLabels, validationLabels: props.validationLabels, referencePickerLabels: props.referencePickerLabels, disabled: props.disabled, onChange: (condition) => props.onChange({ ...draft, condition }) })] }));
187
+ }
188
+ case 'calculate-formula':
189
+ return (_jsxs("div", { className: "axb-template-fields", children: [_jsx(FieldSelect, { id: `${props.baseId}-formula-target`, label: props.labels.field, emptyText: props.labels.noField, fields: props.context.fields.filter((field) => field.primitive !== 'array'), value: draft.fieldId, disabled: props.disabled, onChange: (fieldId) => props.onChange({ ...draft, fieldId }) }), _jsx(FormulaField, { draft: draft, context: props.context, labels: props.labels, explicitAddReferenceLabel: props.explicitAddReferenceLabel, referencePickerLabels: props.referencePickerLabels, disabled: props.disabled, onChange: props.onChange })] }));
190
+ case 'check':
191
+ return (_jsx(ValidationEditor, { value: draft.validation, context: props.context, labels: props.validationLabels, referencePickerLabels: props.referencePickerLabels, disabled: props.disabled,
192
+ // The builder's own preview already states the expression, so the embedded editor keeps
193
+ // its controls and does not mount a second live region beside the builder's.
194
+ showExpression: false, onChange: (validation) => props.onChange({ ...draft, validation }) }));
195
+ case 'no-duplicate':
196
+ return (_jsxs("div", { className: "axb-template-fields", children: [_jsx(TableFieldSelect, { id: `${props.baseId}-unique-table`, label: props.labels.table, fields: props.context.fields, value: draft.tableFieldId, disabled: props.disabled, labels: props.labels, onChange: (tableFieldId) => {
197
+ const table = props.context.fields.find((field) => field.fieldId === tableFieldId);
198
+ props.onChange({
199
+ ...draft,
200
+ tableFieldId,
201
+ columnFieldId: table?.columns?.[0]?.fieldId ?? '',
202
+ });
203
+ } }), _jsx(ColumnSelect, { id: `${props.baseId}-unique-column`, label: props.labels.column, emptyText: props.labels.noColumn, table: props.context.fields.find((field) => field.fieldId === draft.tableFieldId), value: draft.columnFieldId, disabled: props.disabled, onChange: (columnFieldId) => props.onChange({ ...draft, columnFieldId }) })] }));
204
+ case 'block-submission':
205
+ return (_jsxs("div", { className: "axb-template-fields", children: [_jsx(ConditionEditor, { value: draft.condition, context: props.context, labels: props.conditionLabels, validationLabels: props.validationLabels, referencePickerLabels: props.referencePickerLabels, disabled: props.disabled, onChange: (condition) => props.onChange({ ...draft, condition }) }), _jsxs("label", { className: "axb-check", children: [_jsx("input", { type: "checkbox", checked: draft.hard, disabled: props.disabled, onChange: (event) => props.onChange({ ...draft, hard: event.currentTarget.checked }) }), _jsx("span", { children: props.labels.hard })] })] }));
206
+ case 'choose-from-table': {
207
+ const table = props.context.tables?.find((candidate) => candidate.tableId === draft.tableId);
208
+ const keyColumns = table?.columns?.filter((column) => column.role === 'key') ?? [];
209
+ return (_jsxs("div", { className: "axb-template-fields", children: [_jsx(FieldSelect, { id: `${props.baseId}-choose-target`, label: props.labels.field, emptyText: props.labels.noField, fields: props.context.fields.filter((field) => field.primitive !== 'array'), value: draft.fieldId, disabled: props.disabled, onChange: (fieldId) => props.onChange({ ...draft, fieldId }) }), _jsxs("label", { className: "axb-field", children: [_jsx("span", { className: "axb-field__label", children: props.labels.table }), _jsx("select", { className: "axb-select", value: draft.tableId, disabled: props.disabled, onChange: (event) => {
210
+ const next = props.context.tables?.find((candidate) => candidate.tableId === event.currentTarget.value);
211
+ props.onChange({
212
+ ...draft,
213
+ tableId: event.currentTarget.value,
214
+ keyFieldIds: defaultKeyFieldIds(next, props.context.fields),
215
+ });
216
+ }, children: (props.context.tables ?? [])
217
+ .filter((candidate) => candidate.kind === 'decision_table')
218
+ .map((candidate) => (_jsx("option", { value: candidate.tableId, children: candidate.label }, candidate.tableId))) })] }), keyColumns.map((column, index) => (_jsx(FieldSelect, { id: `${props.baseId}-key-${String(index)}`, label: `${props.labels.keyField}: ${column.label}`, emptyText: props.labels.noField, fields: props.context.fields.filter((field) => field.primitive === column.primitive), value: draft.keyFieldIds[index] ?? '', disabled: props.disabled, onChange: (fieldId) => {
219
+ const keys = [...draft.keyFieldIds];
220
+ keys[index] = fieldId;
221
+ props.onChange({ ...draft, keyFieldIds: keys });
222
+ } }, column.columnId))), _jsxs("label", { className: "axb-field", children: [_jsx("span", { className: "axb-field__label", children: props.labels.outputColumn }), _jsxs("select", { className: "axb-select", value: draft.outputColumnId ?? '', disabled: props.disabled, onChange: (event) => {
223
+ const value = event.currentTarget.value;
224
+ if (value.length === 0) {
225
+ const { outputColumnId: _output, ...rest } = draft;
226
+ props.onChange(rest);
227
+ }
228
+ else {
229
+ props.onChange({ ...draft, outputColumnId: value });
230
+ }
231
+ }, children: [_jsx("option", { value: "", children: "\u2014" }), (table?.columns ?? [])
232
+ .filter((column) => column.role === 'output')
233
+ .map((column) => (_jsx("option", { value: column.columnId, children: column.label }, column.columnId)))] })] })] }));
234
+ }
235
+ }
236
+ }
237
+ function FormulaField(props) {
238
+ const textarea = useRef(null);
239
+ const pickerLabels = {
240
+ ...props.referencePickerLabels,
241
+ label: props.explicitAddReferenceLabel ??
242
+ props.referencePickerLabels?.label ??
243
+ props.labels.addReference,
244
+ };
245
+ return (_jsxs("div", { className: "axb-formula", children: [_jsxs("label", { className: "axb-field", children: [_jsx("span", { className: "axb-field__label", children: props.labels.formula }), _jsx("textarea", { ref: textarea, className: "axb-textarea axb-input--mono", rows: 3, value: props.draft.source, disabled: props.disabled, onChange: (event) => props.onChange({ ...props.draft, source: event.currentTarget.value }) })] }), _jsx(ReferencePicker, { fields: props.context.fields, disabled: props.disabled, labels: pickerLabels, onPick: (reference) => {
246
+ const element = textarea.current;
247
+ const source = props.draft.source;
248
+ const start = element?.selectionStart ?? source.length;
249
+ const end = element?.selectionEnd ?? start;
250
+ const inserted = insertReferenceAtCaret(source, start, end, reference.fieldId);
251
+ props.onChange({ ...props.draft, source: inserted.value });
252
+ // React commits a discrete event's update before the microtask, so the textarea holds
253
+ // the new source when the caret is placed after the inserted id.
254
+ queueMicrotask(() => {
255
+ const element = textarea.current;
256
+ if (element === null)
257
+ return;
258
+ element.focus();
259
+ element.setSelectionRange(inserted.caret, inserted.caret);
260
+ });
261
+ } })] }));
262
+ }
263
+ function needsMessage(draft) {
264
+ return ruleTemplateNeedsMessage(draft.template);
265
+ }
266
+ function setRuleId(draft, value) {
267
+ if (value.length === 0) {
268
+ const { ruleId: _ruleId, ...rest } = draft;
269
+ return rest;
270
+ }
271
+ return { ...draft, ruleId: value };
272
+ }
273
+ function setMessageKey(draft, value) {
274
+ if (value.length === 0) {
275
+ const { messageKey: _messageKey, ...rest } = draft;
276
+ return rest;
277
+ }
278
+ return { ...draft, messageKey: value };
279
+ }
280
+ function displayExpression(build) {
281
+ const rule = build.rule;
282
+ if (rule === undefined)
283
+ return build.source;
284
+ if (rule.gate !== undefined)
285
+ return rule.gate.items[0]?.expr;
286
+ return rule.expr;
287
+ }
288
+ function firstFieldId(context) {
289
+ return context.fields.find((field) => field.primitive !== 'array')?.fieldId ?? '';
290
+ }
291
+ function targetFields(fields, target) {
292
+ return fields.filter((field) => {
293
+ switch (target) {
294
+ case 'number':
295
+ return (field.primitive === 'integer' ||
296
+ field.primitive === 'decimal' ||
297
+ field.primitive === 'money');
298
+ case 'integer':
299
+ return field.primitive === 'integer';
300
+ case 'boolean':
301
+ return field.primitive === 'boolean';
302
+ }
303
+ });
304
+ }
305
+ function FieldSelect(props) {
306
+ const groups = new Map();
307
+ for (const field of props.fields) {
308
+ const key = `${field.pageLabel ?? ''}\u0000${field.sectionLabel ?? ''}`;
309
+ const group = groups.get(key);
310
+ if (group === undefined)
311
+ groups.set(key, [field]);
312
+ else
313
+ group.push(field);
314
+ }
315
+ if (props.fields.length === 0) {
316
+ return _jsx("p", { className: "axb-hint", children: props.emptyText });
317
+ }
318
+ return (_jsxs("label", { className: "axb-field", htmlFor: props.id, children: [_jsx("span", { className: "axb-field__label", children: props.label }), _jsx("select", { id: props.id, className: "axb-select", value: props.value, disabled: props.disabled, onChange: (event) => props.onChange(event.currentTarget.value), children: [...groups].map(([key, fields]) => {
319
+ const [first] = fields;
320
+ return (_jsx("optgroup", { label: first?.sectionLabel ?? first?.pageLabel ?? '', children: fields.map((field) => (_jsx("option", { value: field.fieldId, children: field.label }, field.fieldId))) }, key));
321
+ }) })] }));
322
+ }
323
+ function TableFieldSelect(props) {
324
+ const tables = props.fields.filter((field) => field.primitive === 'array');
325
+ if (tables.length === 0) {
326
+ return _jsx("p", { className: "axb-hint", children: props.labels.noTables });
327
+ }
328
+ return (_jsxs("label", { className: "axb-field", htmlFor: props.id, children: [_jsx("span", { className: "axb-field__label", children: props.label }), _jsx("select", { id: props.id, className: "axb-select", value: props.value, disabled: props.disabled, onChange: (event) => props.onChange(event.currentTarget.value), children: tables.map((field) => (_jsx("option", { value: field.fieldId, children: field.label }, field.fieldId))) })] }));
329
+ }
330
+ function ColumnSelect(props) {
331
+ const columns = props.table?.columns ?? [];
332
+ if (columns.length === 0)
333
+ return _jsx("p", { className: "axb-hint", children: props.emptyText });
334
+ return (_jsxs("label", { className: "axb-field", htmlFor: props.id, children: [_jsx("span", { className: "axb-field__label", children: props.label }), _jsx("select", { id: props.id, className: "axb-select", value: props.value, disabled: props.disabled, onChange: (event) => props.onChange(event.currentTarget.value), children: columns.map((column) => (_jsx("option", { value: column.fieldId, children: column.label }, column.fieldId))) })] }));
335
+ }
336
+ //# sourceMappingURL=RuleBuilder.js.map