@10x-media/form-builder 0.1.0-beta.8 → 4.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (365) hide show
  1. package/CHANGELOG.md +185 -0
  2. package/README.md +11 -3
  3. package/dist/actions/assertNoBlockCollision.js +18 -0
  4. package/dist/actions/assertNoBlockCollision.js.map +1 -0
  5. package/dist/actions/body/serializeBody.d.ts +16 -14
  6. package/dist/actions/body/serializeBody.js +5 -8
  7. package/dist/actions/body/serializeBody.js.map +1 -1
  8. package/dist/actions/builtin/confirmation.js +2 -1
  9. package/dist/actions/builtin/confirmation.js.map +1 -1
  10. package/dist/actions/builtin/emailAction.d.ts +15 -3
  11. package/dist/actions/builtin/emailAction.js +72 -20
  12. package/dist/actions/builtin/emailAction.js.map +1 -1
  13. package/dist/actions/builtin/emailTeam.js +11 -5
  14. package/dist/actions/builtin/emailTeam.js.map +1 -1
  15. package/dist/actions/defineAction.d.ts +37 -6
  16. package/dist/actions/defineAction.js +14 -1
  17. package/dist/actions/defineAction.js.map +1 -1
  18. package/dist/actions/dispatch.d.ts +12 -0
  19. package/dist/actions/dispatch.js +103 -30
  20. package/dist/actions/dispatch.js.map +1 -1
  21. package/dist/actions/dispatchContext.js +19 -0
  22. package/dist/actions/dispatchContext.js.map +1 -0
  23. package/dist/actions/emailRecipients.js +38 -8
  24. package/dist/actions/emailRecipients.js.map +1 -1
  25. package/dist/actions/emailRender.d.ts +27 -0
  26. package/dist/actions/fromAddresses.d.ts +19 -1
  27. package/dist/actions/fromAddresses.js +66 -9
  28. package/dist/actions/fromAddresses.js.map +1 -1
  29. package/dist/actions/recipientSources.d.ts +23 -0
  30. package/dist/actions/recipientSources.js +11 -0
  31. package/dist/actions/recipientSources.js.map +1 -0
  32. package/dist/actions/registry.js +3 -1
  33. package/dist/actions/registry.js.map +1 -1
  34. package/dist/actions/runActions.d.ts +2 -0
  35. package/dist/actions/runActions.js +13 -8
  36. package/dist/actions/runActions.js.map +1 -1
  37. package/dist/actions/sign.js.map +1 -1
  38. package/dist/actions/submissionContext.d.ts +34 -0
  39. package/dist/actions/task.js +43 -16
  40. package/dist/actions/task.js.map +1 -1
  41. package/dist/aggregation/aggregateResponses.js +3 -4
  42. package/dist/aggregation/aggregateResponses.js.map +1 -1
  43. package/dist/aggregation/resolveResultsRequest.d.ts +6 -0
  44. package/dist/aggregation/resolveResultsRequest.js +69 -26
  45. package/dist/aggregation/resolveResultsRequest.js.map +1 -1
  46. package/dist/calc/computeCalcFields.d.ts +5 -3
  47. package/dist/calc/computeCalcFields.js +4 -3
  48. package/dist/calc/computeCalcFields.js.map +1 -1
  49. package/dist/calc/evaluate.d.ts +26 -3
  50. package/dist/calc/evaluate.js +33 -11
  51. package/dist/calc/evaluate.js.map +1 -1
  52. package/dist/calc/formatCalc.d.ts +11 -0
  53. package/dist/calc/formatCalc.js +35 -0
  54. package/dist/calc/formatCalc.js.map +1 -0
  55. package/dist/calc/formatCalcValue.d.ts +17 -0
  56. package/dist/calc/formatCalcValue.js +17 -0
  57. package/dist/calc/formatCalcValue.js.map +1 -0
  58. package/dist/calc/normalizeCalc.d.ts +17 -3
  59. package/dist/calc/normalizeCalc.js +39 -17
  60. package/dist/calc/normalizeCalc.js.map +1 -1
  61. package/dist/calc/registry.d.ts +43 -0
  62. package/dist/calc/registry.js +27 -0
  63. package/dist/calc/registry.js.map +1 -0
  64. package/dist/calc/resolveCalcContext.d.ts +36 -0
  65. package/dist/calc/resolveCalcContext.js +87 -0
  66. package/dist/calc/resolveCalcContext.js.map +1 -0
  67. package/dist/calc/types.d.ts +19 -4
  68. package/dist/calc/types.js +3 -2
  69. package/dist/calc/types.js.map +1 -1
  70. package/dist/client/ByteSizeField.js +11 -8
  71. package/dist/client/ByteSizeField.js.map +1 -1
  72. package/dist/client/CalcExpressionBuilder.d.ts +20 -0
  73. package/dist/client/CalcExpressionBuilder.js +888 -0
  74. package/dist/client/CalcExpressionBuilder.js.map +1 -0
  75. package/dist/client/ClosePollButton.js +3 -3
  76. package/dist/client/ClosePollButton.js.map +1 -1
  77. package/dist/client/CondensedArray.d.ts +2 -2
  78. package/dist/client/CondensedArray.js +5 -7
  79. package/dist/client/CondensedArray.js.map +1 -1
  80. package/dist/client/ConditionBuilder.js +5 -7
  81. package/dist/client/ConditionBuilder.js.map +1 -1
  82. package/dist/client/ConditionRow.js +3 -3
  83. package/dist/client/ConditionRow.js.map +1 -1
  84. package/dist/client/ConsentBlockLabel.js +2 -2
  85. package/dist/client/ConsentBlockLabel.js.map +1 -1
  86. package/dist/client/ConsentRetentionNotice.d.ts +11 -0
  87. package/dist/client/ConsentRetentionNotice.js +23 -0
  88. package/dist/client/ConsentRetentionNotice.js.map +1 -0
  89. package/dist/client/EndpointOptionsSelect.d.ts +17 -7
  90. package/dist/client/EndpointOptionsSelect.js +18 -14
  91. package/dist/client/EndpointOptionsSelect.js.map +1 -1
  92. package/dist/client/FieldBlockLabel.js +2 -2
  93. package/dist/client/FieldBlockLabel.js.map +1 -1
  94. package/dist/client/FieldNameSelect.d.ts +1 -0
  95. package/dist/client/FieldNameSelect.js +2 -4
  96. package/dist/client/FieldNameSelect.js.map +1 -1
  97. package/dist/client/FlowBuilder.js +31 -23
  98. package/dist/client/FlowBuilder.js.map +1 -1
  99. package/dist/client/FormConditionField.js +0 -1
  100. package/dist/client/FormConditionField.js.map +1 -1
  101. package/dist/client/RecipientsSelect.d.ts +9 -2
  102. package/dist/client/RecipientsSelect.js +31 -10
  103. package/dist/client/RecipientsSelect.js.map +1 -1
  104. package/dist/client/admin.css +345 -72
  105. package/dist/client/endpointOptions.d.ts +11 -0
  106. package/dist/client/endpointOptions.js +17 -8
  107. package/dist/client/endpointOptions.js.map +1 -1
  108. package/dist/collections/formSubmissions.js +145 -51
  109. package/dist/collections/formSubmissions.js.map +1 -1
  110. package/dist/collections/forms.js +170 -65
  111. package/dist/collections/forms.js.map +1 -1
  112. package/dist/collections/formsEndpoints.js +31 -26
  113. package/dist/collections/formsEndpoints.js.map +1 -1
  114. package/dist/collections/settingsFields.d.ts +32 -0
  115. package/dist/collections/settingsFields.js +55 -0
  116. package/dist/collections/settingsFields.js.map +1 -0
  117. package/dist/consent/captureConsent.d.ts +2 -1
  118. package/dist/consent/captureConsent.js +4 -1
  119. package/dist/consent/captureConsent.js.map +1 -1
  120. package/dist/consent/consentSourcesField.d.ts +7 -1
  121. package/dist/consent/consentSourcesField.js +15 -1
  122. package/dist/consent/consentSourcesField.js.map +1 -1
  123. package/dist/consent/effectiveStatement.js +16 -0
  124. package/dist/consent/effectiveStatement.js.map +1 -0
  125. package/dist/consent/resolveConsentSourcesRequest.js +2 -2
  126. package/dist/consent/resolveConsentSourcesRequest.js.map +1 -1
  127. package/dist/consent/resolveConsentStatements.js +3 -1
  128. package/dist/consent/resolveConsentStatements.js.map +1 -1
  129. package/dist/consent/resolvePublishedVersionRef.js +1 -0
  130. package/dist/consent/resolvePublishedVersionRef.js.map +1 -1
  131. package/dist/consent/types.d.ts +8 -0
  132. package/dist/context/formContext.d.ts +30 -0
  133. package/dist/context/formContext.js +54 -0
  134. package/dist/context/formContext.js.map +1 -0
  135. package/dist/email/departmentsField.d.ts +6 -1
  136. package/dist/email/departmentsField.js +6 -1
  137. package/dist/email/departmentsField.js.map +1 -1
  138. package/dist/exports/client.d.ts +3 -1
  139. package/dist/exports/client.js +3 -1
  140. package/dist/exports/i18n.d.ts +14 -2
  141. package/dist/exports/i18n.js +14 -2
  142. package/dist/exports/react.d.ts +23 -5
  143. package/dist/exports/react.js +17 -1
  144. package/dist/exports/rsc.d.ts +4 -1
  145. package/dist/exports/rsc.js +7 -4
  146. package/dist/exports/types.d.ts +4 -3
  147. package/dist/fields/buildFieldBlocks.js +2 -1
  148. package/dist/fields/buildFieldBlocks.js.map +1 -1
  149. package/dist/fields/builtin/calculation.d.ts +17 -0
  150. package/dist/fields/builtin/calculation.js +70 -127
  151. package/dist/fields/builtin/calculation.js.map +1 -1
  152. package/dist/fields/builtin/checkbox.js +15 -0
  153. package/dist/fields/builtin/checkbox.js.map +1 -1
  154. package/dist/fields/builtin/consent.js +21 -1
  155. package/dist/fields/builtin/consent.js.map +1 -1
  156. package/dist/fields/builtin/email.js +7 -0
  157. package/dist/fields/builtin/email.js.map +1 -1
  158. package/dist/fields/builtin/index.d.ts +6 -2
  159. package/dist/fields/builtin/index.js +9 -4
  160. package/dist/fields/builtin/index.js.map +1 -1
  161. package/dist/fields/builtin/select.js +20 -1
  162. package/dist/fields/builtin/select.js.map +1 -1
  163. package/dist/fields/builtin/text.js +7 -0
  164. package/dist/fields/builtin/text.js.map +1 -1
  165. package/dist/fields/instanceOptions.js +1 -1
  166. package/dist/fields/instanceOptions.js.map +1 -1
  167. package/dist/fields/sharedConfig.js +15 -11
  168. package/dist/fields/sharedConfig.js.map +1 -1
  169. package/dist/fields/types.d.ts +8 -2
  170. package/dist/form/findFormAtLocale.d.ts +30 -0
  171. package/dist/form/findFormAtLocale.js +88 -0
  172. package/dist/form/findFormAtLocale.js.map +1 -0
  173. package/dist/form/pollState.d.ts +2 -1
  174. package/dist/form/pollState.js.map +1 -1
  175. package/dist/form/toFormDocument.d.ts +8 -0
  176. package/dist/form/toFormDocument.js +3 -1
  177. package/dist/form/toFormDocument.js.map +1 -1
  178. package/dist/form/types.d.ts +9 -1
  179. package/dist/index.d.ts +22 -6
  180. package/dist/index.js +63 -17
  181. package/dist/index.js.map +1 -1
  182. package/dist/options.d.ts +113 -4
  183. package/dist/plugin/collectionSlugs.d.ts +28 -0
  184. package/dist/plugin/collectionSlugs.js +65 -0
  185. package/dist/plugin/collectionSlugs.js.map +1 -0
  186. package/dist/plugin/customState.js +1 -1
  187. package/dist/plugin/customState.js.map +1 -1
  188. package/dist/plugin/registerCollections.js +38 -21
  189. package/dist/plugin/registerCollections.js.map +1 -1
  190. package/dist/poll/closeJob.d.ts +4 -3
  191. package/dist/poll/closeJob.js +13 -10
  192. package/dist/poll/closeJob.js.map +1 -1
  193. package/dist/poll/outcomeBeforeChange.js +2 -2
  194. package/dist/poll/outcomeBeforeChange.js.map +1 -1
  195. package/dist/poll/pollTypeRegistry.js +1 -1
  196. package/dist/poll/resolvePollCloseRequest.js +5 -4
  197. package/dist/poll/resolvePollCloseRequest.js.map +1 -1
  198. package/dist/poll/resolvePollOptionsRequest.js +2 -2
  199. package/dist/poll/resolvePollOptionsRequest.js.map +1 -1
  200. package/dist/poll/resolvePollOutcome.d.ts +6 -0
  201. package/dist/poll/resolvePollOutcome.js +20 -6
  202. package/dist/poll/resolvePollOutcome.js.map +1 -1
  203. package/dist/poll/votes/aggregateFromVotes.d.ts +27 -0
  204. package/dist/poll/votes/aggregateFromVotes.js +55 -0
  205. package/dist/poll/votes/aggregateFromVotes.js.map +1 -0
  206. package/dist/poll/votes/answerValues.js +16 -0
  207. package/dist/poll/votes/answerValues.js.map +1 -0
  208. package/dist/poll/votes/bumpPollVote.js +91 -0
  209. package/dist/poll/votes/bumpPollVote.js.map +1 -0
  210. package/dist/poll/votes/recountPollVotes.d.ts +20 -0
  211. package/dist/poll/votes/recountPollVotes.js +63 -0
  212. package/dist/poll/votes/recountPollVotes.js.map +1 -0
  213. package/dist/poll/votes/voteTallyHook.js +76 -0
  214. package/dist/poll/votes/voteTallyHook.js.map +1 -0
  215. package/dist/poll/votes/votesCollection.d.ts +23 -0
  216. package/dist/poll/votes/votesCollection.js +96 -0
  217. package/dist/poll/votes/votesCollection.js.map +1 -0
  218. package/dist/react/Form.d.ts +49 -7
  219. package/dist/react/Form.js +183 -37
  220. package/dist/react/Form.js.map +1 -1
  221. package/dist/react/FormContext.d.ts +10 -2
  222. package/dist/react/FormContext.js.map +1 -1
  223. package/dist/react/FormResults.d.ts +4 -2
  224. package/dist/react/FormResults.js +21 -8
  225. package/dist/react/FormResults.js.map +1 -1
  226. package/dist/react/Poll.d.ts +25 -2
  227. package/dist/react/Poll.js +76 -25
  228. package/dist/react/Poll.js.map +1 -1
  229. package/dist/react/adapters.d.ts +35 -0
  230. package/dist/react/adapters.js +11 -0
  231. package/dist/react/adapters.js.map +1 -0
  232. package/dist/react/captcha/HcaptchaCaptcha.d.ts +4 -1
  233. package/dist/react/captcha/HcaptchaCaptcha.js +2 -2
  234. package/dist/react/captcha/HcaptchaCaptcha.js.map +1 -1
  235. package/dist/react/captcha/RecaptchaCaptcha.d.ts +4 -1
  236. package/dist/react/captcha/RecaptchaCaptcha.js +2 -2
  237. package/dist/react/captcha/RecaptchaCaptcha.js.map +1 -1
  238. package/dist/react/captcha/TurnstileCaptcha.d.ts +4 -1
  239. package/dist/react/captcha/TurnstileCaptcha.js +2 -2
  240. package/dist/react/captcha/TurnstileCaptcha.js.map +1 -1
  241. package/dist/react/captcha/useCaptchaScript.js +23 -5
  242. package/dist/react/captcha/useCaptchaScript.js.map +1 -1
  243. package/dist/react/fetchResults.d.ts +6 -3
  244. package/dist/react/fetchResults.js +10 -5
  245. package/dist/react/fetchResults.js.map +1 -1
  246. package/dist/react/primitives/Checkbox.d.ts +4 -2
  247. package/dist/react/primitives/Checkbox.js +3 -2
  248. package/dist/react/primitives/Checkbox.js.map +1 -1
  249. package/dist/react/primitives/ChoiceGroup.d.ts +40 -0
  250. package/dist/react/primitives/ChoiceGroup.js +33 -0
  251. package/dist/react/primitives/ChoiceGroup.js.map +1 -0
  252. package/dist/react/primitives/FieldShell.d.ts +11 -1
  253. package/dist/react/primitives/FieldShell.js +43 -33
  254. package/dist/react/primitives/FieldShell.js.map +1 -1
  255. package/dist/react/primitives/Input.d.ts +3 -1
  256. package/dist/react/primitives/Input.js +2 -1
  257. package/dist/react/primitives/Input.js.map +1 -1
  258. package/dist/react/renderers/calculation.js +6 -1
  259. package/dist/react/renderers/calculation.js.map +1 -1
  260. package/dist/react/renderers/checkbox.js +2 -1
  261. package/dist/react/renderers/checkbox.js.map +1 -1
  262. package/dist/react/renderers/consent.js +8 -3
  263. package/dist/react/renderers/consent.js.map +1 -1
  264. package/dist/react/renderers/email.js +3 -1
  265. package/dist/react/renderers/email.js.map +1 -1
  266. package/dist/react/renderers/repeater.js +5 -0
  267. package/dist/react/renderers/repeater.js.map +1 -1
  268. package/dist/react/renderers/select.js +20 -2
  269. package/dist/react/renderers/select.js.map +1 -1
  270. package/dist/react/renderers/text.js +3 -1
  271. package/dist/react/renderers/text.js.map +1 -1
  272. package/dist/react/state.d.ts +20 -1
  273. package/dist/react/state.js +57 -4
  274. package/dist/react/state.js.map +1 -1
  275. package/dist/react/submitForm.d.ts +20 -3
  276. package/dist/react/submitForm.js +8 -4
  277. package/dist/react/submitForm.js.map +1 -1
  278. package/dist/react/useCalcValues.d.ts +10 -0
  279. package/dist/react/useCalcValues.js +13 -0
  280. package/dist/react/useCalcValues.js.map +1 -0
  281. package/dist/react/useField.js +7 -5
  282. package/dist/react/useField.js.map +1 -1
  283. package/dist/recall/resolver.js +1 -1
  284. package/dist/recall/resolver.js.map +1 -1
  285. package/dist/spam/constants.js +7 -1
  286. package/dist/spam/constants.js.map +1 -1
  287. package/dist/spam/spamGuard.js +2 -1
  288. package/dist/spam/spamGuard.js.map +1 -1
  289. package/dist/submissions/SubmissionAnswersClient.js +14 -14
  290. package/dist/submissions/SubmissionAnswersClient.js.map +1 -1
  291. package/dist/submissions/createSubmission.d.ts +7 -1
  292. package/dist/submissions/createSubmission.js +10 -5
  293. package/dist/submissions/createSubmission.js.map +1 -1
  294. package/dist/submissions/formIdOf.js +13 -0
  295. package/dist/submissions/formIdOf.js.map +1 -0
  296. package/dist/submissions/resolveVotedSubmission.d.ts +27 -0
  297. package/dist/submissions/resolveVotedSubmission.js +51 -0
  298. package/dist/submissions/resolveVotedSubmission.js.map +1 -0
  299. package/dist/submissions/runSubmission.js +6 -5
  300. package/dist/submissions/runSubmission.js.map +1 -1
  301. package/dist/submissions/submissionLocale.js +39 -0
  302. package/dist/submissions/submissionLocale.js.map +1 -0
  303. package/dist/submissions/validateSubmission.js +125 -79
  304. package/dist/submissions/validateSubmission.js.map +1 -1
  305. package/dist/submissions/verifyContext.js +35 -0
  306. package/dist/submissions/verifyContext.js.map +1 -0
  307. package/dist/submissions/voteChange.js +21 -0
  308. package/dist/submissions/voteChange.js.map +1 -0
  309. package/dist/submissions/voteChangeEndpoint.js +110 -0
  310. package/dist/submissions/voteChangeEndpoint.js.map +1 -0
  311. package/dist/submissions/votedCookie.d.ts +10 -4
  312. package/dist/submissions/votedCookie.js +49 -11
  313. package/dist/submissions/votedCookie.js.map +1 -1
  314. package/dist/translations/ar.d.ts +7 -0
  315. package/dist/translations/ar.js +379 -0
  316. package/dist/translations/ar.js.map +1 -0
  317. package/dist/translations/clientRuntimeKeys.d.ts +17 -0
  318. package/dist/translations/clientRuntimeKeys.js +48 -0
  319. package/dist/translations/clientRuntimeKeys.js.map +1 -0
  320. package/dist/translations/de.d.ts +12 -0
  321. package/dist/translations/de.js +80 -3
  322. package/dist/translations/de.js.map +1 -1
  323. package/dist/translations/en.js +80 -3
  324. package/dist/translations/en.js.map +1 -1
  325. package/dist/translations/es.d.ts +7 -0
  326. package/dist/translations/es.js +379 -0
  327. package/dist/translations/es.js.map +1 -0
  328. package/dist/translations/fr.d.ts +7 -0
  329. package/dist/translations/fr.js +379 -0
  330. package/dist/translations/fr.js.map +1 -0
  331. package/dist/translations/id.d.ts +7 -0
  332. package/dist/translations/id.js +379 -0
  333. package/dist/translations/id.js.map +1 -0
  334. package/dist/translations/index.d.ts +23 -6
  335. package/dist/translations/index.js +31 -6
  336. package/dist/translations/index.js.map +1 -1
  337. package/dist/translations/keys.d.ts +78 -1
  338. package/dist/translations/keys.js +78 -1
  339. package/dist/translations/keys.js.map +1 -1
  340. package/dist/translations/ko.d.ts +7 -0
  341. package/dist/translations/ko.js +379 -0
  342. package/dist/translations/ko.js.map +1 -0
  343. package/dist/translations/makeTranslate.d.ts +7 -3
  344. package/dist/translations/makeTranslate.js +12 -3
  345. package/dist/translations/makeTranslate.js.map +1 -1
  346. package/dist/translations/pt.d.ts +7 -0
  347. package/dist/translations/pt.js +379 -0
  348. package/dist/translations/pt.js.map +1 -0
  349. package/dist/translations/ru.d.ts +7 -0
  350. package/dist/translations/ru.js +379 -0
  351. package/dist/translations/ru.js.map +1 -0
  352. package/dist/translations/uk.d.ts +7 -0
  353. package/dist/translations/uk.js +379 -0
  354. package/dist/translations/uk.js.map +1 -0
  355. package/dist/translations/zh.d.ts +7 -0
  356. package/dist/translations/zh.js +379 -0
  357. package/dist/translations/zh.js.map +1 -0
  358. package/dist/validation/builtin/index.js +2 -0
  359. package/dist/validation/builtin/index.js.map +1 -1
  360. package/dist/validation/builtin/integer.js +15 -0
  361. package/dist/validation/builtin/integer.js.map +1 -0
  362. package/dist/validation/builtin/notAlreadySubmitted.js +5 -3
  363. package/dist/validation/builtin/notAlreadySubmitted.js.map +1 -1
  364. package/package.json +23 -15
  365. package/styles/form-builder.css +84 -0
@@ -0,0 +1,888 @@
1
+ "use client";
2
+ import { keys } from "../translations/keys.js";
3
+ import { CALC_FNS, CALC_OPS, isCalcFn } from "../calc/types.js";
4
+ import { normalizeCalc } from "../calc/normalizeCalc.js";
5
+ import { formatCalc } from "../calc/formatCalc.js";
6
+ import { useTranslation as useTranslation$1 } from "../translations/useTranslation.js";
7
+ import { toStaticLabel } from "./toStaticLabel.js";
8
+ import { reduceFieldsToValues } from "payload/shared";
9
+ import { Button, CirclePlusIcon, FieldError, FieldLabel, LineIcon, ReactSelect, RenderCustomComponent, useField, useFormFields } from "@payloadcms/ui";
10
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
11
+ import { useMemo, useState } from "react";
12
+ import "./admin.css";
13
+ //#region src/client/CalcExpressionBuilder.tsx
14
+ /** The single-argument functions offered by the "Then apply" finisher; min/max are variadic and stay Function operands. */
15
+ const UNARY_FNS = CALC_FNS.filter((fn) => fn !== "min" && fn !== "max");
16
+ const seedOperand = (kind) => {
17
+ switch (kind) {
18
+ case "ref": return {
19
+ kind: "ref",
20
+ field: ""
21
+ };
22
+ case "lit": return {
23
+ kind: "lit",
24
+ value: 0
25
+ };
26
+ case "weight": return {
27
+ kind: "weight",
28
+ field: "",
29
+ weights: {}
30
+ };
31
+ }
32
+ };
33
+ const seedAst = (kind) => {
34
+ switch (kind) {
35
+ case "ref": return {
36
+ type: "ref",
37
+ field: ""
38
+ };
39
+ case "lit": return {
40
+ type: "lit",
41
+ value: 0
42
+ };
43
+ case "weight": return {
44
+ type: "weight",
45
+ field: "",
46
+ weights: {}
47
+ };
48
+ case "fn": return {
49
+ type: "fn",
50
+ fn: "min",
51
+ args: [{
52
+ type: "lit",
53
+ value: 0
54
+ }]
55
+ };
56
+ }
57
+ };
58
+ const astToOperand = (node) => {
59
+ switch (node.type) {
60
+ case "ref": return {
61
+ kind: "ref",
62
+ field: node.field
63
+ };
64
+ case "lit": return {
65
+ kind: "lit",
66
+ value: node.value
67
+ };
68
+ case "source": return {
69
+ kind: "source",
70
+ source: node.source
71
+ };
72
+ case "weight": return {
73
+ kind: "weight",
74
+ field: node.field,
75
+ ...node.weights !== void 0 ? { weights: node.weights } : {},
76
+ ...node.source !== void 0 ? { source: node.source } : {}
77
+ };
78
+ case "neg": return {
79
+ kind: "neg",
80
+ operand: astToOperand(node.operand)
81
+ };
82
+ case "fn": return {
83
+ kind: "fn",
84
+ fn: node.fn,
85
+ args: node.args.map((arg) => toChain(arg, false))
86
+ };
87
+ case "op": return {
88
+ kind: "group",
89
+ chain: toChain(node, false)
90
+ };
91
+ }
92
+ };
93
+ const toChain = (expr, allowFinish) => {
94
+ const arg = expr.type === "fn" ? expr.args[0] : void 0;
95
+ if (allowFinish && expr.type === "fn" && expr.args.length === 1 && arg !== void 0 && isCalcFn(expr.fn) && UNARY_FNS.includes(expr.fn)) return {
96
+ ...toChain(arg, false),
97
+ finish: expr.fn
98
+ };
99
+ const steps = [];
100
+ let node = expr;
101
+ while (node.type === "op") {
102
+ steps.unshift({
103
+ op: node.op,
104
+ operand: astToOperand(node.right)
105
+ });
106
+ node = node.left;
107
+ }
108
+ return {
109
+ first: astToOperand(node),
110
+ steps
111
+ };
112
+ };
113
+ /**
114
+ * AST -> chain: the left spine of binary ops flattens into first + steps (a nested op on the right
115
+ * becomes a `group`); a root unary fn wrapping the whole expression loads as `finish` (min/max stay
116
+ * fn operands so the finisher select never holds a value it cannot offer). Total: every valid
117
+ * `CalcExpression` loads without loss, and `chainToAst(astToChain(x))` is structurally `x`.
118
+ */
119
+ const astToChain = (expr) => toChain(expr, true);
120
+ const operandToAst = (operand) => {
121
+ switch (operand.kind) {
122
+ case "ref": return {
123
+ type: "ref",
124
+ field: operand.field
125
+ };
126
+ case "lit": return {
127
+ type: "lit",
128
+ value: operand.value
129
+ };
130
+ case "weight": return {
131
+ type: "weight",
132
+ field: operand.field,
133
+ ...operand.weights !== void 0 ? { weights: operand.weights } : {},
134
+ ...operand.source !== void 0 ? { source: operand.source } : {}
135
+ };
136
+ case "source": return {
137
+ type: "source",
138
+ source: operand.source
139
+ };
140
+ case "neg": return {
141
+ type: "neg",
142
+ operand: operandToAst(operand.operand)
143
+ };
144
+ case "fn": return {
145
+ type: "fn",
146
+ fn: operand.fn,
147
+ args: operand.args.map(chainBodyToAst)
148
+ };
149
+ case "group": return chainBodyToAst(operand.chain);
150
+ }
151
+ };
152
+ /** Folds first + steps only. `finish` is root-only by design: nested chains (groups, fn args) never carry one, so it is deliberately ignored here. */
153
+ const chainBodyToAst = (chain) => chain.steps.reduce((left, step) => ({
154
+ type: "op",
155
+ op: step.op,
156
+ left,
157
+ right: operandToAst(step.operand)
158
+ }), operandToAst(chain.first));
159
+ /** Chain -> AST: left-fold first + steps into the binary tree (left-assoc matches `-`/`/`/`%`), then wrap in `finish` when set. */
160
+ const chainToAst = (chain) => {
161
+ const body = chainBodyToAst(chain);
162
+ return chain.finish ? {
163
+ type: "fn",
164
+ fn: chain.finish,
165
+ args: [body]
166
+ } : body;
167
+ };
168
+ const optionRows = (options) => {
169
+ if (!Array.isArray(options)) return [];
170
+ return options.flatMap((option) => {
171
+ if (typeof option === "string") return option.length > 0 ? [{
172
+ label: option,
173
+ value: option
174
+ }] : [];
175
+ if (!option || typeof option !== "object") return [];
176
+ const { label, value } = option;
177
+ if (typeof value !== "string" || value.length === 0) return [];
178
+ return [{
179
+ label: typeof label === "string" && label.length > 0 ? label : value,
180
+ value
181
+ }];
182
+ });
183
+ };
184
+ const siblingsFromData = (data, selfIndex) => {
185
+ const rows = Array.isArray(data.fields) ? data.fields : [];
186
+ const siblings = {
187
+ numeric: [],
188
+ choices: [],
189
+ labels: {}
190
+ };
191
+ rows.forEach((row, index) => {
192
+ if (!row || typeof row !== "object") return;
193
+ const { blockType, name, label, options } = row;
194
+ const trimmed = typeof name === "string" ? name.trim() : "";
195
+ if (trimmed.length === 0) return;
196
+ const display = typeof label === "string" && label.length > 0 ? label : trimmed;
197
+ siblings.labels[trimmed] = display;
198
+ if (blockType === "number" || blockType === "calculation" && index < selfIndex) siblings.numeric.push({
199
+ label: display,
200
+ value: trimmed
201
+ });
202
+ if (blockType === "select") siblings.choices.push({
203
+ name: trimmed,
204
+ label: display,
205
+ options: optionRows(options)
206
+ });
207
+ });
208
+ return siblings;
209
+ };
210
+ const singleOption = (selected) => {
211
+ return (Array.isArray(selected) ? selected[0] : selected) ?? void 0;
212
+ };
213
+ /** Keeps a stored reference selectable even when it no longer matches any option, like FieldNameSelect. */
214
+ const withStored = (options, stored) => stored.length > 0 && !options.some((option) => option.value === stored) ? [...options, {
215
+ label: stored,
216
+ value: stored
217
+ }] : options;
218
+ /**
219
+ * Number input with a local draft: keystrokes commit only when the text parses to a finite number,
220
+ * an invalid draft reverts to the committed value on blur, and an external value change resyncs
221
+ * the draft.
222
+ */
223
+ const NumberInput = ({ value, onCommit, className, id, disabled, "aria-label": ariaLabel }) => {
224
+ const [draft, setDraft] = useState(value === void 0 ? "" : String(value));
225
+ const [lastValue, setLastValue] = useState(value);
226
+ if (value !== lastValue) {
227
+ setLastValue(value);
228
+ if (draft.trim() === "" || Number(draft) !== value) setDraft(value === void 0 ? "" : String(value));
229
+ }
230
+ return /* @__PURE__ */ jsx("input", {
231
+ type: "number",
232
+ className,
233
+ id,
234
+ disabled,
235
+ "aria-label": ariaLabel,
236
+ value: draft,
237
+ onChange: (event) => {
238
+ const text = event.target.value;
239
+ setDraft(text);
240
+ const parsed = Number(text);
241
+ if (text.trim() !== "" && Number.isFinite(parsed)) onCommit(parsed);
242
+ },
243
+ onBlur: () => {
244
+ const parsed = Number(draft);
245
+ if (draft.trim() === "" || !Number.isFinite(parsed)) setDraft(value === void 0 ? "" : String(value));
246
+ }
247
+ });
248
+ };
249
+ const OP_OPTIONS = CALC_OPS.map((op) => ({
250
+ label: op,
251
+ value: op
252
+ }));
253
+ const FN_OPTIONS = CALC_FNS.map((fn) => ({
254
+ label: fn,
255
+ value: fn
256
+ }));
257
+ const UNARY_OPTIONS = UNARY_FNS.map((fn) => ({
258
+ label: fn,
259
+ value: fn
260
+ }));
261
+ /** Source entries share kind/start selects with the fixed kinds, namespaced to avoid key collisions. */
262
+ const SOURCE_PREFIX = "source:";
263
+ const sourceOptionValue = (key) => `${SOURCE_PREFIX}${key}`;
264
+ const sourceKeyOf = (value) => value.startsWith(SOURCE_PREFIX) ? value.slice(7) : void 0;
265
+ /** A source label is a plain string or a per-locale record; resolve it for the admin UI language. */
266
+ const resolveSourceLabel = (label, language) => typeof label === "string" ? label : label[language] ?? label.en ?? Object.values(label)[0] ?? "";
267
+ /** Fixed kinds stay flat; sources join as a labeled group when any exist (react-select mixes both). */
268
+ const withSourceGroup = (base, sourceOptions, groupLabel) => sourceOptions.length > 0 ? [...base, {
269
+ label: groupLabel,
270
+ options: sourceOptions
271
+ }] : base;
272
+ const OperandCard = ({ operand, onChange, ctx, idPrefix }) => {
273
+ const { t, siblings, readOnly } = ctx;
274
+ if (operand.kind === "group") return /* @__PURE__ */ jsxs("div", {
275
+ className: "fb-calc-operand fb-calc-operand--group",
276
+ children: [/* @__PURE__ */ jsx("div", {
277
+ className: "fb-calc-operand__title",
278
+ children: t(keys.calcBuilderGroup)
279
+ }), /* @__PURE__ */ jsx(ChainEditor, {
280
+ chain: operand.chain,
281
+ onChange: (chain) => onChange({
282
+ ...operand,
283
+ chain
284
+ }),
285
+ ctx,
286
+ idPrefix: `${idPrefix}-g`
287
+ })]
288
+ });
289
+ if (operand.kind === "fn") {
290
+ const fnOptions = withStored([...FN_OPTIONS, ...ctx.customFnOptions], operand.fn);
291
+ return /* @__PURE__ */ jsxs("div", {
292
+ className: "fb-calc-operand fb-calc-operand--fn",
293
+ children: [/* @__PURE__ */ jsxs("div", {
294
+ className: "fb-calc-operand__header",
295
+ children: [/* @__PURE__ */ jsx("label", {
296
+ className: "fb-visually-hidden",
297
+ htmlFor: `${idPrefix}-fn`,
298
+ children: t(keys.calcBuilderFunction)
299
+ }), /* @__PURE__ */ jsx(ReactSelect, {
300
+ className: "fb-calc-operand__fn",
301
+ inputId: `${idPrefix}-fn`,
302
+ options: fnOptions,
303
+ value: fnOptions.find((option) => option.value === operand.fn),
304
+ isClearable: false,
305
+ disabled: readOnly,
306
+ onChange: (selected) => {
307
+ const chosen = singleOption(selected);
308
+ if (chosen) onChange({
309
+ ...operand,
310
+ fn: String(chosen.value)
311
+ });
312
+ }
313
+ })]
314
+ }), /* @__PURE__ */ jsxs("div", {
315
+ className: "fb-calc-fn__args",
316
+ children: [operand.args.map((arg, index) => /* @__PURE__ */ jsxs("div", {
317
+ className: "fb-calc-fn__arg",
318
+ children: [/* @__PURE__ */ jsx("div", {
319
+ className: "fb-calc-fn__arg-chain",
320
+ children: /* @__PURE__ */ jsx(ChainEditor, {
321
+ chain: arg,
322
+ onChange: (next) => onChange({
323
+ ...operand,
324
+ args: operand.args.map((a, i) => i === index ? next : a)
325
+ }),
326
+ ctx,
327
+ idPrefix: `${idPrefix}-a${index}`
328
+ })
329
+ }), operand.args.length > 1 ? /* @__PURE__ */ jsx("div", {
330
+ className: "fb-calc-fn__arg-remove",
331
+ children: /* @__PURE__ */ jsx(Button, {
332
+ buttonStyle: "ghost",
333
+ icon: /* @__PURE__ */ jsx(LineIcon, { size: 24 }),
334
+ "aria-label": t(keys.calcBuilderRemove),
335
+ margin: false,
336
+ disabled: readOnly,
337
+ onClick: () => onChange({
338
+ ...operand,
339
+ args: operand.args.filter((_, i) => i !== index)
340
+ })
341
+ })
342
+ }) : null]
343
+ }, index)), /* @__PURE__ */ jsx("div", {
344
+ className: "fb-calc-fn__add",
345
+ children: /* @__PURE__ */ jsx(Button, {
346
+ buttonStyle: "ghost",
347
+ icon: /* @__PURE__ */ jsx(CirclePlusIcon, { size: 24 }),
348
+ iconPosition: "left",
349
+ margin: false,
350
+ disabled: readOnly,
351
+ onClick: () => onChange({
352
+ ...operand,
353
+ args: [...operand.args, {
354
+ first: {
355
+ kind: "lit",
356
+ value: 0
357
+ },
358
+ steps: []
359
+ }]
360
+ }),
361
+ children: t(keys.calcBuilderAddArgument)
362
+ })
363
+ })]
364
+ })]
365
+ });
366
+ }
367
+ if (operand.kind === "neg") return /* @__PURE__ */ jsxs("div", {
368
+ className: "fb-calc-operand fb-calc-operand--neg",
369
+ children: [/* @__PURE__ */ jsx("div", {
370
+ className: "fb-calc-operand__title",
371
+ children: t(keys.calcBuilderNegate)
372
+ }), /* @__PURE__ */ jsx(OperandCard, {
373
+ operand: operand.operand,
374
+ onChange: (next) => onChange({
375
+ kind: "neg",
376
+ operand: next
377
+ }),
378
+ ctx,
379
+ idPrefix: `${idPrefix}-n`
380
+ })]
381
+ });
382
+ const leafKindOptions = [
383
+ {
384
+ label: t(keys.calcBuilderAnswer),
385
+ value: "ref"
386
+ },
387
+ {
388
+ label: t(keys.calcBuilderNumber),
389
+ value: "lit"
390
+ },
391
+ {
392
+ label: t(keys.calcBuilderWeights),
393
+ value: "weight"
394
+ }
395
+ ];
396
+ const kindValue = operand.kind === "source" ? sourceOptionValue(operand.source) : operand.kind;
397
+ const kindSourceOptions = operand.kind === "source" && !ctx.scalarSourceOptions.some((option) => option.value === sourceOptionValue(operand.source)) ? [...ctx.scalarSourceOptions, {
398
+ label: operand.source,
399
+ value: kindValue
400
+ }] : ctx.scalarSourceOptions;
401
+ const kindFlat = [...leafKindOptions, ...kindSourceOptions];
402
+ const refOptions = operand.kind === "ref" ? withStored(siblings.numeric, operand.field) : [];
403
+ const choiceOptions = operand.kind === "weight" ? withStored(siblings.choices.map((c) => ({
404
+ label: c.label,
405
+ value: c.name
406
+ })), operand.field) : [];
407
+ const choice = operand.kind === "weight" ? siblings.choices.find((c) => c.name === operand.field) : void 0;
408
+ const weightSource = operand.kind === "weight" ? operand.source : void 0;
409
+ const weightValuesOptions = [{
410
+ label: t(keys.calcBuilderWeightManual),
411
+ value: "manual"
412
+ }, ...weightSource !== void 0 && !ctx.weightSourceOptions.some((option) => option.value === sourceOptionValue(weightSource)) ? [...ctx.weightSourceOptions, {
413
+ label: weightSource,
414
+ value: sourceOptionValue(weightSource)
415
+ }] : ctx.weightSourceOptions];
416
+ return /* @__PURE__ */ jsxs("div", {
417
+ className: "fb-calc-operand",
418
+ children: [
419
+ /* @__PURE__ */ jsxs("div", {
420
+ className: "fb-calc-operand__header",
421
+ children: [
422
+ /* @__PURE__ */ jsx("label", {
423
+ className: "fb-visually-hidden",
424
+ htmlFor: `${idPrefix}-kind`,
425
+ children: t(keys.calcBuilderKind)
426
+ }),
427
+ /* @__PURE__ */ jsx(ReactSelect, {
428
+ className: "fb-calc-operand__kind",
429
+ inputId: `${idPrefix}-kind`,
430
+ options: withSourceGroup(leafKindOptions, kindSourceOptions, t(keys.calcBuilderSourcesGroup)),
431
+ value: kindFlat.find((option) => option.value === kindValue),
432
+ isClearable: false,
433
+ disabled: readOnly,
434
+ onChange: (selected) => {
435
+ const chosen = singleOption(selected);
436
+ if (!chosen || chosen.value === kindValue) return;
437
+ const sourceKey = sourceKeyOf(String(chosen.value));
438
+ onChange(sourceKey !== void 0 ? {
439
+ kind: "source",
440
+ source: sourceKey
441
+ } : seedOperand(chosen.value));
442
+ }
443
+ }),
444
+ operand.kind === "source" ? /* @__PURE__ */ jsx("span", {
445
+ className: "fb-calc-operand__source",
446
+ children: ctx.sourceLabelOf(operand.source)
447
+ }) : null,
448
+ operand.kind === "ref" ? refOptions.length === 0 ? /* @__PURE__ */ jsx("p", {
449
+ className: "fb-calc-hint",
450
+ children: t(keys.calcBuilderNoNumericFields)
451
+ }) : /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("label", {
452
+ className: "fb-visually-hidden",
453
+ htmlFor: `${idPrefix}-ref`,
454
+ children: t(keys.calcBuilderPickField)
455
+ }), /* @__PURE__ */ jsx(ReactSelect, {
456
+ className: "fb-calc-operand__ref",
457
+ inputId: `${idPrefix}-ref`,
458
+ options: refOptions,
459
+ value: refOptions.find((option) => option.value === operand.field),
460
+ isClearable: !readOnly,
461
+ disabled: readOnly,
462
+ placeholder: t(keys.calcBuilderPickField),
463
+ onChange: (selected) => {
464
+ const chosen = singleOption(selected);
465
+ onChange({
466
+ kind: "ref",
467
+ field: chosen ? String(chosen.value) : ""
468
+ });
469
+ }
470
+ })] }) : null,
471
+ operand.kind === "lit" ? /* @__PURE__ */ jsx(NumberInput, {
472
+ className: "form-input fb-calc-operand__number",
473
+ id: `${idPrefix}-number`,
474
+ "aria-label": t(keys.calcBuilderNumber),
475
+ disabled: readOnly,
476
+ value: operand.value,
477
+ onCommit: (value) => onChange({
478
+ kind: "lit",
479
+ value
480
+ })
481
+ }) : null,
482
+ operand.kind === "weight" ? choiceOptions.length === 0 ? /* @__PURE__ */ jsx("p", {
483
+ className: "fb-calc-hint",
484
+ children: t(keys.calcBuilderNoChoiceFields)
485
+ }) : /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("label", {
486
+ className: "fb-visually-hidden",
487
+ htmlFor: `${idPrefix}-weight-field`,
488
+ children: t(keys.calcBuilderPickField)
489
+ }), /* @__PURE__ */ jsx(ReactSelect, {
490
+ className: "fb-calc-operand__weight-field",
491
+ inputId: `${idPrefix}-weight-field`,
492
+ options: choiceOptions,
493
+ value: choiceOptions.find((option) => option.value === operand.field),
494
+ isClearable: !readOnly,
495
+ disabled: readOnly,
496
+ placeholder: t(keys.calcBuilderPickField),
497
+ onChange: (selected) => {
498
+ const chosen = singleOption(selected);
499
+ const field = chosen ? String(chosen.value) : "";
500
+ if (field !== operand.field) onChange(operand.source !== void 0 ? {
501
+ kind: "weight",
502
+ field,
503
+ source: operand.source
504
+ } : {
505
+ kind: "weight",
506
+ field,
507
+ weights: {}
508
+ });
509
+ }
510
+ })] }) : null
511
+ ]
512
+ }),
513
+ operand.kind === "weight" && ctx.weightSourceOptions.length > 0 ? /* @__PURE__ */ jsxs("div", {
514
+ className: "fb-calc-operand__weight-values-row",
515
+ children: [/* @__PURE__ */ jsx("label", {
516
+ className: "fb-calc-operand__weight-values-label",
517
+ htmlFor: `${idPrefix}-wv`,
518
+ children: t(keys.calcBuilderWeightValues)
519
+ }), /* @__PURE__ */ jsx(ReactSelect, {
520
+ className: "fb-calc-operand__weight-values",
521
+ inputId: `${idPrefix}-wv`,
522
+ options: weightValuesOptions,
523
+ value: weightValuesOptions.find((option) => option.value === (operand.source !== void 0 ? sourceOptionValue(operand.source) : "manual")),
524
+ isClearable: false,
525
+ disabled: readOnly,
526
+ onChange: (selected) => {
527
+ const chosen = singleOption(selected);
528
+ if (!chosen) return;
529
+ const sourceKey = sourceKeyOf(String(chosen.value));
530
+ if (sourceKey === void 0) {
531
+ if (operand.source !== void 0) onChange({
532
+ kind: "weight",
533
+ field: operand.field,
534
+ weights: operand.weights ?? {}
535
+ });
536
+ return;
537
+ }
538
+ if (sourceKey !== operand.source) onChange({
539
+ ...operand,
540
+ source: sourceKey
541
+ });
542
+ }
543
+ })]
544
+ }) : null,
545
+ operand.kind === "weight" && operand.source !== void 0 ? /* @__PURE__ */ jsx("p", {
546
+ className: "fb-calc-hint",
547
+ children: t(keys.calcBuilderWeightsFromSource)
548
+ }) : null,
549
+ operand.kind === "weight" && operand.source === void 0 && choice ? /* @__PURE__ */ jsx("div", {
550
+ className: "fb-calc-operand__weights",
551
+ children: choice.options.map((option) => /* @__PURE__ */ jsxs("div", {
552
+ className: "fb-calc-operand__weight-row",
553
+ children: [/* @__PURE__ */ jsx("label", {
554
+ className: "fb-calc-operand__weight-label",
555
+ htmlFor: `${idPrefix}-weight-${option.value}`,
556
+ children: option.label
557
+ }), /* @__PURE__ */ jsx(NumberInput, {
558
+ className: "form-input fb-calc-operand__weight",
559
+ id: `${idPrefix}-weight-${option.value}`,
560
+ disabled: readOnly,
561
+ value: operand.weights?.[option.value],
562
+ onCommit: (value) => onChange({
563
+ ...operand,
564
+ weights: {
565
+ ...operand.weights ?? {},
566
+ [option.value]: value
567
+ }
568
+ })
569
+ })]
570
+ }, option.value))
571
+ }) : null
572
+ ]
573
+ });
574
+ };
575
+ const ChainEditor = ({ chain, onChange, onEmptied, isRoot, ctx, idPrefix }) => {
576
+ const { t, readOnly } = ctx;
577
+ const removeRow = (row) => {
578
+ if (row === 0) {
579
+ const [promoted, ...rest] = chain.steps;
580
+ if (!promoted) {
581
+ onEmptied?.();
582
+ return;
583
+ }
584
+ onChange({
585
+ ...chain,
586
+ first: promoted.operand,
587
+ steps: rest
588
+ });
589
+ return;
590
+ }
591
+ onChange({
592
+ ...chain,
593
+ steps: chain.steps.filter((_, i) => i !== row - 1)
594
+ });
595
+ };
596
+ const rows = [{ operand: chain.first }, ...chain.steps.map((step) => ({
597
+ op: step.op,
598
+ operand: step.operand
599
+ }))];
600
+ return /* @__PURE__ */ jsxs("div", {
601
+ className: "fb-calc-chain",
602
+ children: [rows.map((row, index) => /* @__PURE__ */ jsxs("div", {
603
+ className: index > 0 ? "fb-calc-row fb-calc-row--divided" : "fb-calc-row",
604
+ children: [
605
+ index === 0 ? /* @__PURE__ */ jsx("div", {
606
+ className: "fb-calc-row__op-spacer",
607
+ "aria-hidden": "true"
608
+ }) : null,
609
+ index > 0 ? /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("label", {
610
+ className: "fb-visually-hidden",
611
+ htmlFor: `${idPrefix}-r${index}-op`,
612
+ children: t(keys.calcBuilderMath)
613
+ }), /* @__PURE__ */ jsx(ReactSelect, {
614
+ className: "fb-calc-row__op",
615
+ inputId: `${idPrefix}-r${index}-op`,
616
+ options: OP_OPTIONS,
617
+ value: OP_OPTIONS.find((option) => option.value === row.op),
618
+ isClearable: false,
619
+ disabled: readOnly,
620
+ onChange: (selected) => {
621
+ const chosen = singleOption(selected);
622
+ if (chosen) onChange({
623
+ ...chain,
624
+ steps: chain.steps.map((step, i) => i === index - 1 ? {
625
+ ...step,
626
+ op: chosen.value
627
+ } : step)
628
+ });
629
+ }
630
+ })] }) : null,
631
+ /* @__PURE__ */ jsx("div", {
632
+ className: "fb-calc-row__operand",
633
+ children: /* @__PURE__ */ jsx(OperandCard, {
634
+ operand: row.operand,
635
+ onChange: (operand) => index === 0 ? onChange({
636
+ ...chain,
637
+ first: operand
638
+ }) : onChange({
639
+ ...chain,
640
+ steps: chain.steps.map((step, i) => i === index - 1 ? {
641
+ ...step,
642
+ operand
643
+ } : step)
644
+ }),
645
+ ctx,
646
+ idPrefix: `${idPrefix}-r${index}`
647
+ })
648
+ }),
649
+ rows.length > 1 || isRoot ? /* @__PURE__ */ jsx("div", {
650
+ className: "fb-calc-row__remove",
651
+ children: /* @__PURE__ */ jsx(Button, {
652
+ buttonStyle: "ghost",
653
+ icon: /* @__PURE__ */ jsx(LineIcon, { size: 24 }),
654
+ "aria-label": t(keys.calcBuilderRemove),
655
+ margin: false,
656
+ disabled: readOnly,
657
+ onClick: () => removeRow(index)
658
+ })
659
+ }) : null
660
+ ]
661
+ }, index)), /* @__PURE__ */ jsx("div", {
662
+ className: "fb-calc-chain__add",
663
+ children: /* @__PURE__ */ jsx(Button, {
664
+ buttonStyle: "ghost",
665
+ icon: /* @__PURE__ */ jsx(CirclePlusIcon, { size: 24 }),
666
+ iconPosition: "left",
667
+ margin: false,
668
+ disabled: readOnly,
669
+ onClick: () => onChange({
670
+ ...chain,
671
+ steps: [...chain.steps, {
672
+ op: "*",
673
+ operand: seedOperand("ref")
674
+ }]
675
+ }),
676
+ children: t(keys.calcBuilderAddStep)
677
+ })
678
+ })]
679
+ });
680
+ };
681
+ /**
682
+ * Visual editor for the calculation field's `expression` AST, presented as a left-to-right chain.
683
+ * Storage shape is unchanged and `validateExpression` stays the server gate; the AST remains
684
+ * authorable via the API.
685
+ */
686
+ const CalcExpressionBuilder = (props) => {
687
+ const { customComponents: { Error: ErrorComponent, Label } = {}, disabled, path, setValue, showError, value } = useField({ path: props.path });
688
+ const { t, i18n } = useTranslation$1();
689
+ const label = toStaticLabel(props.field?.label);
690
+ const readOnly = props.readOnly === true || disabled === true;
691
+ const sources = useMemo(() => props.sources ?? [], [props.sources]);
692
+ const allowed = useMemo(() => ({
693
+ sources: new Set(sources.map((source) => source.key)),
694
+ functions: new Set(props.functions ?? [])
695
+ }), [sources, props.functions]);
696
+ const sourceLabelOf = (key) => {
697
+ const meta = sources.find((source) => source.key === key);
698
+ return meta ? resolveSourceLabel(meta.label, i18n.language) : key;
699
+ };
700
+ const scalarSourceOptions = sources.filter((source) => source.scalar).map((source) => ({
701
+ label: sourceLabelOf(source.key),
702
+ value: sourceOptionValue(source.key)
703
+ }));
704
+ const weightSourceOptions = sources.filter((source) => source.weights).map((source) => ({
705
+ label: sourceLabelOf(source.key),
706
+ value: sourceOptionValue(source.key)
707
+ }));
708
+ const customFnOptions = (props.functions ?? []).map((fn) => ({
709
+ label: fn,
710
+ value: fn
711
+ }));
712
+ const expression = useMemo(() => normalizeCalc(value, allowed), [value, allowed]);
713
+ const chain = useMemo(() => expression ? astToChain(expression) : void 0, [expression]);
714
+ const storedInvalid = expression === void 0 && value != null && value !== "";
715
+ const selfIndex = useMemo(() => {
716
+ const match = /^fields\.(\d+)\./.exec(path);
717
+ return match ? Number(match[1]) : Number.MAX_SAFE_INTEGER;
718
+ }, [path]);
719
+ const siblingsJson = useFormFields(([fields]) => JSON.stringify(siblingsFromData(reduceFieldsToValues(fields, true), selfIndex)));
720
+ const siblings = useMemo(() => JSON.parse(siblingsJson), [siblingsJson]);
721
+ const startOptions = [
722
+ {
723
+ label: t(keys.calcBuilderAnswer),
724
+ value: "ref"
725
+ },
726
+ {
727
+ label: t(keys.calcBuilderNumber),
728
+ value: "lit"
729
+ },
730
+ {
731
+ label: t(keys.calcBuilderWeights),
732
+ value: "weight"
733
+ },
734
+ {
735
+ label: t(keys.calcBuilderFunction),
736
+ value: "fn"
737
+ }
738
+ ];
739
+ const startDescriptions = [
740
+ {
741
+ key: "ref",
742
+ label: t(keys.calcBuilderAnswer),
743
+ description: t(keys.calcBuilderFieldDescription)
744
+ },
745
+ {
746
+ key: "lit",
747
+ label: t(keys.calcBuilderNumber),
748
+ description: t(keys.calcBuilderNumberDescription)
749
+ },
750
+ {
751
+ key: "weight",
752
+ label: t(keys.calcBuilderWeights),
753
+ description: t(keys.calcBuilderWeightsDescription)
754
+ },
755
+ {
756
+ key: "fn",
757
+ label: t(keys.calcBuilderFunction),
758
+ description: t(keys.calcBuilderFunctionDescription)
759
+ }
760
+ ];
761
+ const commitChain = (next) => {
762
+ if (!readOnly) setValue(chainToAst(next));
763
+ };
764
+ const clear = () => {
765
+ if (!readOnly) setValue(null);
766
+ };
767
+ const labelOf = (field) => field.trim() === "" ? "?" : siblings.labels[field] ?? field;
768
+ const idBase = `calc-${path.replace(/\./g, "__")}`;
769
+ const ctx = {
770
+ t,
771
+ siblings,
772
+ readOnly,
773
+ sourceLabelOf,
774
+ scalarSourceOptions,
775
+ weightSourceOptions,
776
+ customFnOptions
777
+ };
778
+ return /* @__PURE__ */ jsxs("div", {
779
+ className: [
780
+ "field-type",
781
+ "fb-calc-builder",
782
+ showError && "error",
783
+ readOnly && "read-only"
784
+ ].filter(Boolean).join(" "),
785
+ id: path ? `field-${path.replace(/\./g, "__")}` : void 0,
786
+ children: [/* @__PURE__ */ jsx(RenderCustomComponent, {
787
+ CustomComponent: Label,
788
+ Fallback: /* @__PURE__ */ jsx(FieldLabel, {
789
+ label,
790
+ path
791
+ })
792
+ }), /* @__PURE__ */ jsxs("div", {
793
+ className: "field-type__wrap",
794
+ children: [
795
+ /* @__PURE__ */ jsx(RenderCustomComponent, {
796
+ CustomComponent: ErrorComponent,
797
+ Fallback: /* @__PURE__ */ jsx(FieldError, {
798
+ path,
799
+ showError
800
+ })
801
+ }),
802
+ chain ? /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(ChainEditor, {
803
+ chain,
804
+ onChange: commitChain,
805
+ onEmptied: clear,
806
+ isRoot: true,
807
+ ctx,
808
+ idPrefix: idBase
809
+ }), /* @__PURE__ */ jsxs("div", {
810
+ className: "fb-calc-finish",
811
+ children: [/* @__PURE__ */ jsx("label", {
812
+ className: "fb-calc-finish__label",
813
+ htmlFor: `${idBase}-finish`,
814
+ title: t(keys.calcBuilderThenApply),
815
+ children: t(keys.calcBuilderThenApply)
816
+ }), /* @__PURE__ */ jsx(ReactSelect, {
817
+ className: "fb-calc-finish__select",
818
+ inputId: `${idBase}-finish`,
819
+ options: UNARY_OPTIONS,
820
+ value: UNARY_OPTIONS.find((option) => option.value === chain.finish),
821
+ isClearable: !readOnly,
822
+ disabled: readOnly,
823
+ onChange: (selected) => {
824
+ const chosen = singleOption(selected);
825
+ commitChain({
826
+ ...chain,
827
+ finish: chosen ? chosen.value : void 0
828
+ });
829
+ }
830
+ })]
831
+ })] }) : /* @__PURE__ */ jsxs("div", {
832
+ className: "fb-calc-builder__empty",
833
+ children: [
834
+ storedInvalid ? /* @__PURE__ */ jsx("p", {
835
+ className: "fb-calc-builder__warning",
836
+ children: t(keys.calcBuilderStoredInvalid)
837
+ }) : null,
838
+ /* @__PURE__ */ jsx("p", {
839
+ className: "fb-calc-builder__hint",
840
+ children: t(keys.calcBuilderAddExpression)
841
+ }),
842
+ /* @__PURE__ */ jsx("label", {
843
+ className: "fb-visually-hidden",
844
+ htmlFor: `${idBase}-seed`,
845
+ children: t(keys.calcBuilderStartWith)
846
+ }),
847
+ /* @__PURE__ */ jsx(ReactSelect, {
848
+ className: "fb-calc-builder__seed",
849
+ inputId: `${idBase}-seed`,
850
+ options: withSourceGroup(startOptions, scalarSourceOptions, t(keys.calcBuilderSourcesGroup)),
851
+ disabled: readOnly,
852
+ placeholder: t(keys.calcBuilderStartWith),
853
+ onChange: (selected) => {
854
+ const chosen = singleOption(selected);
855
+ if (!chosen || readOnly) return;
856
+ const sourceKey = sourceKeyOf(String(chosen.value));
857
+ setValue(sourceKey !== void 0 ? {
858
+ type: "source",
859
+ source: sourceKey
860
+ } : seedAst(chosen.value));
861
+ }
862
+ }),
863
+ /* @__PURE__ */ jsx("ul", {
864
+ className: "fb-calc-builder__kinds",
865
+ children: startDescriptions.map((entry) => /* @__PURE__ */ jsxs("li", { children: [
866
+ /* @__PURE__ */ jsx("span", {
867
+ className: "fb-calc-builder__kind-label",
868
+ children: entry.label
869
+ }),
870
+ " ",
871
+ entry.description
872
+ ] }, entry.key))
873
+ })
874
+ ]
875
+ }),
876
+ /* @__PURE__ */ jsx("p", {
877
+ className: "fb-calc-builder__preview",
878
+ "aria-live": "polite",
879
+ children: expression ? formatCalc(expression, labelOf, sourceLabelOf) : ""
880
+ })
881
+ ]
882
+ })]
883
+ });
884
+ };
885
+ //#endregion
886
+ export { CalcExpressionBuilder };
887
+
888
+ //# sourceMappingURL=CalcExpressionBuilder.js.map