@ram_28/kf-ai-sdk 1.0.11 → 1.0.12

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 (193) hide show
  1. package/README.md +55 -48
  2. package/dist/api/client.d.ts +14 -14
  3. package/dist/api/client.d.ts.map +1 -1
  4. package/dist/api/datetime.d.ts +5 -5
  5. package/dist/api/datetime.d.ts.map +1 -1
  6. package/dist/api/index.d.ts +1 -1
  7. package/dist/api/index.d.ts.map +1 -1
  8. package/dist/api/metadata.d.ts +2 -2
  9. package/dist/api/metadata.d.ts.map +1 -1
  10. package/dist/api.cjs +1 -0
  11. package/dist/api.d.ts +4 -0
  12. package/dist/api.d.ts.map +1 -0
  13. package/dist/api.mjs +27 -0
  14. package/dist/api.types.cjs +1 -0
  15. package/dist/api.types.d.ts +4 -0
  16. package/dist/api.types.d.ts.map +1 -0
  17. package/dist/api.types.mjs +1 -0
  18. package/dist/auth/AuthProvider.d.ts +3 -3
  19. package/dist/auth/AuthProvider.d.ts.map +1 -1
  20. package/dist/auth/authClient.d.ts +4 -4
  21. package/dist/auth/authClient.d.ts.map +1 -1
  22. package/dist/auth/authConfig.d.ts +5 -5
  23. package/dist/auth/authConfig.d.ts.map +1 -1
  24. package/dist/auth/index.d.ts +1 -1
  25. package/dist/auth/index.d.ts.map +1 -1
  26. package/dist/auth/types.d.ts +21 -21
  27. package/dist/auth/types.d.ts.map +1 -1
  28. package/dist/auth/useAuth.d.ts +2 -2
  29. package/dist/auth/useAuth.d.ts.map +1 -1
  30. package/dist/auth.cjs +1 -0
  31. package/dist/auth.d.ts +5 -0
  32. package/dist/auth.d.ts.map +1 -0
  33. package/dist/auth.mjs +280 -0
  34. package/dist/auth.types.cjs +1 -0
  35. package/dist/auth.types.d.ts +2 -0
  36. package/dist/auth.types.d.ts.map +1 -0
  37. package/dist/auth.types.mjs +1 -0
  38. package/dist/base-types.d.ts +2 -0
  39. package/dist/base-types.d.ts.map +1 -0
  40. package/dist/client-C15j4O5B.cjs +1 -0
  41. package/dist/client-CfvLiGfP.js +233 -0
  42. package/dist/cn-Dr87sK3i.js +2775 -0
  43. package/dist/cn-mXUIg4vp.cjs +1 -0
  44. package/dist/components/hooks/index.d.ts +3 -3
  45. package/dist/components/hooks/index.d.ts.map +1 -1
  46. package/dist/components/hooks/useFilter/index.d.ts +2 -2
  47. package/dist/components/hooks/useFilter/index.d.ts.map +1 -1
  48. package/dist/components/hooks/useFilter/types.d.ts +19 -31
  49. package/dist/components/hooks/useFilter/types.d.ts.map +1 -1
  50. package/dist/components/hooks/useFilter/useFilter.d.ts +2 -2
  51. package/dist/components/hooks/useFilter/useFilter.d.ts.map +1 -1
  52. package/dist/components/hooks/useForm/apiClient.d.ts +6 -6
  53. package/dist/components/hooks/useForm/apiClient.d.ts.map +1 -1
  54. package/dist/components/hooks/useForm/expressionValidator.utils.d.ts +7 -7
  55. package/dist/components/hooks/useForm/expressionValidator.utils.d.ts.map +1 -1
  56. package/dist/components/hooks/useForm/index.d.ts +5 -5
  57. package/dist/components/hooks/useForm/index.d.ts.map +1 -1
  58. package/dist/components/hooks/useForm/optimizedExpressionValidator.utils.d.ts +10 -10
  59. package/dist/components/hooks/useForm/optimizedExpressionValidator.utils.d.ts.map +1 -1
  60. package/dist/components/hooks/useForm/ruleClassifier.utils.d.ts +7 -12
  61. package/dist/components/hooks/useForm/ruleClassifier.utils.d.ts.map +1 -1
  62. package/dist/components/hooks/useForm/schemaParser.utils.d.ts +7 -7
  63. package/dist/components/hooks/useForm/schemaParser.utils.d.ts.map +1 -1
  64. package/dist/components/hooks/useForm/types.d.ts +67 -76
  65. package/dist/components/hooks/useForm/types.d.ts.map +1 -1
  66. package/dist/components/hooks/useForm/useForm.d.ts +2 -2
  67. package/dist/components/hooks/useForm/useForm.d.ts.map +1 -1
  68. package/dist/components/hooks/useKanban/apiClient.d.ts +16 -16
  69. package/dist/components/hooks/useKanban/apiClient.d.ts.map +1 -1
  70. package/dist/components/hooks/useKanban/context.d.ts +3 -3
  71. package/dist/components/hooks/useKanban/context.d.ts.map +1 -1
  72. package/dist/components/hooks/useKanban/dragDropManager.d.ts +7 -7
  73. package/dist/components/hooks/useKanban/dragDropManager.d.ts.map +1 -1
  74. package/dist/components/hooks/useKanban/index.d.ts +1 -1
  75. package/dist/components/hooks/useKanban/index.d.ts.map +1 -1
  76. package/dist/components/hooks/useKanban/types.d.ts +65 -67
  77. package/dist/components/hooks/useKanban/types.d.ts.map +1 -1
  78. package/dist/components/hooks/useKanban/useKanban.d.ts +2 -2
  79. package/dist/components/hooks/useKanban/useKanban.d.ts.map +1 -1
  80. package/dist/components/hooks/useTable/index.d.ts +1 -1
  81. package/dist/components/hooks/useTable/index.d.ts.map +1 -1
  82. package/dist/components/hooks/useTable/types.d.ts +10 -10
  83. package/dist/components/hooks/useTable/types.d.ts.map +1 -1
  84. package/dist/components/hooks/useTable/useTable.d.ts +2 -2
  85. package/dist/components/hooks/useTable/useTable.d.ts.map +1 -1
  86. package/dist/filter.cjs +1 -0
  87. package/dist/filter.d.ts +3 -0
  88. package/dist/filter.d.ts.map +1 -0
  89. package/dist/filter.mjs +6 -0
  90. package/dist/filter.types.cjs +1 -0
  91. package/dist/filter.types.d.ts +2 -0
  92. package/dist/filter.types.d.ts.map +1 -0
  93. package/dist/filter.types.mjs +1 -0
  94. package/dist/form.cjs +1 -0
  95. package/dist/form.d.ts +3 -0
  96. package/dist/form.d.ts.map +1 -0
  97. package/dist/form.mjs +1346 -0
  98. package/dist/form.types.cjs +1 -0
  99. package/dist/form.types.d.ts +2 -0
  100. package/dist/form.types.d.ts.map +1 -0
  101. package/dist/form.types.mjs +1 -0
  102. package/dist/jsx-runtime-BYECrxsp.cjs +30 -0
  103. package/dist/jsx-runtime-DGlMoOmv.js +630 -0
  104. package/dist/kanban.cjs +13 -0
  105. package/dist/kanban.d.ts +5 -0
  106. package/dist/kanban.d.ts.map +1 -0
  107. package/dist/kanban.mjs +694 -0
  108. package/dist/kanban.types.cjs +1 -0
  109. package/dist/kanban.types.d.ts +2 -0
  110. package/dist/kanban.types.d.ts.map +1 -0
  111. package/dist/kanban.types.mjs +1 -0
  112. package/dist/kanban.ui.cjs +1 -0
  113. package/dist/kanban.ui.d.ts +2 -0
  114. package/dist/kanban.ui.d.ts.map +1 -0
  115. package/dist/kanban.ui.mjs +104 -0
  116. package/dist/metadata-2FLBsFcf.cjs +1 -0
  117. package/dist/metadata-DBcoDth-.js +43 -0
  118. package/dist/table.cjs +1 -0
  119. package/dist/table.d.ts +2 -0
  120. package/dist/table.d.ts.map +1 -0
  121. package/dist/table.mjs +148 -0
  122. package/dist/table.types.cjs +1 -0
  123. package/dist/table.types.d.ts +2 -0
  124. package/dist/table.types.d.ts.map +1 -0
  125. package/dist/table.types.mjs +1 -0
  126. package/dist/types/base-fields.d.ts +45 -45
  127. package/dist/types/base-fields.d.ts.map +1 -1
  128. package/dist/types/common.d.ts +45 -61
  129. package/dist/types/common.d.ts.map +1 -1
  130. package/dist/types.cjs +1 -0
  131. package/dist/types.mjs +1 -0
  132. package/dist/useFilter-CXFqEHyI.js +129 -0
  133. package/dist/useFilter-D-bCDo6Z.cjs +1 -0
  134. package/dist/utils/formatting.d.ts +4 -4
  135. package/dist/utils/formatting.d.ts.map +1 -1
  136. package/dist/utils.cjs +4 -0
  137. package/dist/utils.d.ts +3 -0
  138. package/dist/utils.d.ts.map +1 -0
  139. package/dist/utils.mjs +105 -0
  140. package/package.json +147 -9
  141. package/sdk/api/client.ts +51 -51
  142. package/sdk/api/datetime.ts +5 -5
  143. package/sdk/api/index.ts +28 -33
  144. package/sdk/api/metadata.ts +4 -4
  145. package/sdk/api.ts +24 -0
  146. package/sdk/api.types.ts +58 -0
  147. package/sdk/auth/AuthProvider.tsx +18 -18
  148. package/sdk/auth/authClient.ts +9 -9
  149. package/sdk/auth/authConfig.ts +9 -9
  150. package/sdk/auth/index.ts +5 -5
  151. package/sdk/auth/types.ts +21 -21
  152. package/sdk/auth/useAuth.ts +2 -2
  153. package/sdk/auth.ts +22 -0
  154. package/sdk/auth.types.ts +28 -0
  155. package/sdk/base-types.ts +45 -0
  156. package/sdk/components/hooks/index.ts +22 -22
  157. package/sdk/components/hooks/useFilter/index.ts +8 -14
  158. package/sdk/components/hooks/useFilter/types.ts +29 -58
  159. package/sdk/components/hooks/useFilter/useFilter.ts +41 -44
  160. package/sdk/components/hooks/useForm/apiClient.ts +8 -8
  161. package/sdk/components/hooks/useForm/expressionValidator.utils.ts +18 -18
  162. package/sdk/components/hooks/useForm/index.ts +24 -24
  163. package/sdk/components/hooks/useForm/optimizedExpressionValidator.utils.ts +22 -22
  164. package/sdk/components/hooks/useForm/ruleClassifier.utils.ts +26 -62
  165. package/sdk/components/hooks/useForm/schemaParser.utils.ts +24 -24
  166. package/sdk/components/hooks/useForm/types.ts +66 -82
  167. package/sdk/components/hooks/useForm/useForm.ts +15 -21
  168. package/sdk/components/hooks/useKanban/apiClient.ts +54 -54
  169. package/sdk/components/hooks/useKanban/context.ts +3 -3
  170. package/sdk/components/hooks/useKanban/dragDropManager.ts +19 -19
  171. package/sdk/components/hooks/useKanban/index.ts +26 -26
  172. package/sdk/components/hooks/useKanban/types.ts +69 -71
  173. package/sdk/components/hooks/useKanban/useKanban.ts +43 -51
  174. package/sdk/components/hooks/useKanban/useKanbanSimple.ts +4 -4
  175. package/sdk/components/hooks/useTable/index.ts +5 -1
  176. package/sdk/components/hooks/useTable/types.ts +10 -10
  177. package/sdk/components/hooks/useTable/useTable.ts +10 -10
  178. package/sdk/filter.ts +13 -0
  179. package/sdk/filter.types.ts +17 -0
  180. package/sdk/form.ts +15 -0
  181. package/sdk/form.types.ts +46 -0
  182. package/sdk/kanban.ts +23 -0
  183. package/sdk/kanban.types.ts +50 -0
  184. package/sdk/kanban.ui.ts +16 -0
  185. package/sdk/table.ts +7 -0
  186. package/sdk/table.types.ts +13 -0
  187. package/sdk/types/base-fields.ts +47 -47
  188. package/sdk/types/common.ts +47 -71
  189. package/sdk/types/index.ts +1 -1
  190. package/sdk/utils/formatting.ts +4 -4
  191. package/sdk/utils.ts +10 -0
  192. package/dist/index.cjs +0 -45
  193. package/dist/index.mjs +0 -6387
package/dist/form.mjs ADDED
@@ -0,0 +1,1346 @@
1
+ var Ue = Object.defineProperty;
2
+ var _e = (t, e, r) => e in t ? Ue(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
3
+ var U = (t, e, r) => _e(t, typeof e != "symbol" ? e + "" : e, r);
4
+ import { useState as M, useRef as Y, useEffect as z, useMemo as $, useCallback as S } from "react";
5
+ import { useForm as $e } from "react-hook-form";
6
+ import { useQuery as ue } from "@tanstack/react-query";
7
+ import { a as I } from "./client-CfvLiGfP.js";
8
+ import { g as qe } from "./metadata-DBcoDth-.js";
9
+ function Be() {
10
+ const t = /* @__PURE__ */ new Date();
11
+ return {
12
+ NOW: t,
13
+ TODAY: new Date(t.getFullYear(), t.getMonth(), t.getDate()),
14
+ CURRENT_USER: {
15
+ // These would typically come from auth context
16
+ EmpId: 1,
17
+ Email: "user@example.com",
18
+ FirstName: "John",
19
+ LastName: "Doe",
20
+ Role: "User"
21
+ },
22
+ CURRENT_USER_ID: 1
23
+ };
24
+ }
25
+ const je = {
26
+ // String functions
27
+ CONCAT: (...t) => t.map((e) => String(e || "")).join(""),
28
+ TRIM: (t) => String(t || "").trim(),
29
+ LENGTH: (t) => String(t || "").length,
30
+ UPPER: (t) => String(t || "").toUpperCase(),
31
+ LOWER: (t) => String(t || "").toLowerCase(),
32
+ SUBSTRING: (t, e, r) => {
33
+ const s = String(t || "");
34
+ return r !== void 0 ? s.substring(e, e + r) : s.substring(e);
35
+ },
36
+ CONTAINS: (t, e) => String(t || "").includes(String(e || "")),
37
+ // Date functions
38
+ YEAR: (t) => new Date(t).getFullYear(),
39
+ MONTH: (t) => new Date(t).getMonth() + 1,
40
+ // 1-based
41
+ DAY: (t) => new Date(t).getDate(),
42
+ DATE_DIFF: (t, e) => {
43
+ const r = new Date(t), s = new Date(e), a = Math.abs(r.getTime() - s.getTime());
44
+ return Math.ceil(a / (1e3 * 60 * 60 * 24));
45
+ },
46
+ ADD_DAYS: (t, e) => {
47
+ const r = new Date(t);
48
+ return r.setDate(r.getDate() + e), r;
49
+ },
50
+ ADD_MONTHS: (t, e) => {
51
+ const r = new Date(t);
52
+ return r.setMonth(r.getMonth() + e), r;
53
+ },
54
+ // Math functions
55
+ SUM: (...t) => t.reduce((e, r) => e + (Number(r) || 0), 0),
56
+ AVG: (...t) => {
57
+ const e = t.filter((r) => !isNaN(Number(r)));
58
+ return e.length > 0 ? e.reduce((r, s) => r + Number(s), 0) / e.length : 0;
59
+ },
60
+ MIN: (...t) => Math.min(...t.map((e) => Number(e) || 0)),
61
+ MAX: (...t) => Math.max(...t.map((e) => Number(e) || 0)),
62
+ ROUND: (t) => Math.round(Number(t) || 0),
63
+ FLOOR: (t) => Math.floor(Number(t) || 0),
64
+ CEIL: (t) => Math.ceil(Number(t) || 0),
65
+ ABS: (t) => Math.abs(Number(t) || 0),
66
+ // Conditional functions
67
+ IF: (t, e, r) => t ? e : r,
68
+ // Validation functions
69
+ IS_NULL: (t) => t == null,
70
+ IS_EMPTY: (t) => !t || String(t).trim() === "",
71
+ IS_NUMBER: (t) => !isNaN(Number(t)) && t !== "" && t !== null && t !== void 0,
72
+ IS_DATE: (t) => {
73
+ const e = new Date(t);
74
+ return !isNaN(e.getTime());
75
+ },
76
+ // System functions
77
+ AUTO_NUMBER: () => Math.floor(Math.random() * 1e4) + 1e3,
78
+ // Mock implementation
79
+ UUID: () => "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (t) => {
80
+ const e = Math.random() * 16 | 0;
81
+ return (t == "x" ? e : e & 3 | 8).toString(16);
82
+ }),
83
+ // Array functions
84
+ ARRAY_LENGTH: (t) => Array.isArray(t) ? t.length : 0,
85
+ ARRAY_CONTAINS: (t, e) => Array.isArray(t) ? t.includes(e) : !1,
86
+ ARRAY_JOIN: (t, e = ",") => Array.isArray(t) ? t.join(e) : ""
87
+ };
88
+ function x(t, e) {
89
+ var r;
90
+ switch (t.Type) {
91
+ case "Literal":
92
+ return t.Value;
93
+ case "SystemIdentifier":
94
+ if (t.Property) {
95
+ const f = e.systemValues[t.Name];
96
+ return W(f, t.Property.Name);
97
+ }
98
+ return e.systemValues[t.Name];
99
+ case "Identifier":
100
+ if (t.Property) {
101
+ const f = ce(t, e);
102
+ return W(f, t.Property.Name);
103
+ }
104
+ return ce(t, e);
105
+ case "MemberExpression":
106
+ if (!t.Arguments || t.Arguments.length === 0)
107
+ throw new Error("MemberExpression requires Arguments array");
108
+ const s = x(t.Arguments[0], e), a = (r = t.Arguments[0].Property) == null ? void 0 : r.Name;
109
+ return a ? W(s, a) : s;
110
+ case "BinaryExpression":
111
+ if (!t.Arguments || t.Arguments.length !== 2)
112
+ throw new Error("BinaryExpression requires exactly 2 arguments");
113
+ const n = x(t.Arguments[0], e), o = x(t.Arguments[1], e);
114
+ return Pe(t.Operator, n, o);
115
+ case "LogicalExpression":
116
+ if (!t.Arguments || t.Arguments.length < 2)
117
+ throw new Error("LogicalExpression requires at least 2 arguments");
118
+ return ke(t.Operator, t.Arguments, e);
119
+ case "CallExpression":
120
+ if (!t.Callee)
121
+ throw new Error("CallExpression requires Callee");
122
+ const u = je[t.Callee];
123
+ if (!u)
124
+ throw new Error(`Unknown function: ${t.Callee}`);
125
+ const p = (t.Arguments || []).map(
126
+ (f) => x(f, e)
127
+ );
128
+ return u(...p);
129
+ case "AssignmentExpression":
130
+ if (!t.Arguments || t.Arguments.length !== 1)
131
+ throw new Error("AssignmentExpression requires exactly 1 argument");
132
+ return x(t.Arguments[0], e);
133
+ default:
134
+ throw new Error(`Unknown expression type: ${t.Type}`);
135
+ }
136
+ }
137
+ function ce(t, e) {
138
+ const { Name: r, Source: s } = t;
139
+ if (!r)
140
+ throw new Error("Identifier requires Name");
141
+ switch (s) {
142
+ case "Input":
143
+ default:
144
+ return e.formValues[r];
145
+ }
146
+ }
147
+ function W(t, e) {
148
+ if (!(!t || typeof t != "object"))
149
+ return t[e];
150
+ }
151
+ function Pe(t, e, r) {
152
+ const s = (a) => a == null ? 0 : typeof a == "number" ? a : typeof a == "object" && a !== null && "value" in a ? Number(a.value) || 0 : Number(a) || 0;
153
+ switch (t) {
154
+ case "==":
155
+ return e == r;
156
+ case "!=":
157
+ return e != r;
158
+ case ">":
159
+ return s(e) > s(r);
160
+ case "<":
161
+ return s(e) < s(r);
162
+ case ">=":
163
+ return s(e) >= s(r);
164
+ case "<=":
165
+ return s(e) <= s(r);
166
+ case "+":
167
+ return s(e) + s(r);
168
+ case "-":
169
+ return s(e) - s(r);
170
+ case "*":
171
+ return s(e) * s(r);
172
+ case "/":
173
+ const a = s(r);
174
+ return a !== 0 ? s(e) / a : 0;
175
+ default:
176
+ throw new Error(`Unknown binary operator: ${t}`);
177
+ }
178
+ }
179
+ function ke(t, e, r) {
180
+ switch (t) {
181
+ case "AND":
182
+ return e.every((s) => !!x(s, r));
183
+ case "OR":
184
+ return e.some((s) => !!x(s, r));
185
+ default:
186
+ throw new Error(`Unknown logical operator: ${t}`);
187
+ }
188
+ }
189
+ function pe(t, e, r = {}) {
190
+ const s = {
191
+ formValues: e,
192
+ systemValues: Be(),
193
+ referenceData: r
194
+ };
195
+ try {
196
+ return x(t, s);
197
+ } catch (a) {
198
+ return console.warn("Expression evaluation failed:", a), !1;
199
+ }
200
+ }
201
+ function le(t, e, r = {}) {
202
+ const s = [];
203
+ for (const a of t)
204
+ try {
205
+ pe(
206
+ a.Condition.ExpressionTree,
207
+ e,
208
+ r
209
+ ) || s.push({
210
+ isValid: !1,
211
+ message: a.Message,
212
+ fieldName: a.Id
213
+ });
214
+ } catch (n) {
215
+ console.warn(`Cross-field validation rule ${a.Id} failed:`, n);
216
+ }
217
+ return s;
218
+ }
219
+ function Ye(t, e = {}, r = {}) {
220
+ try {
221
+ return pe(t, e, r);
222
+ } catch (s) {
223
+ return console.warn("Default value calculation failed:", s), null;
224
+ }
225
+ }
226
+ function de(t, e) {
227
+ var u, p, f;
228
+ const r = ((u = t.Expression) == null ? void 0 : u.toLowerCase()) || "", s = ((p = t.Name) == null ? void 0 : p.toLowerCase()) || "", a = ((f = t.Id) == null ? void 0 : f.toLowerCase()) || "", n = e.toLowerCase();
229
+ return s.includes("required") || a.includes("required") ? !0 : [
230
+ `${n} != null`,
231
+ `${n} != ''`,
232
+ `trim(${n}) != ''`,
233
+ `${n}!=null`,
234
+ `${n}!=''`
235
+ ].some((N) => r.includes(N));
236
+ }
237
+ function ze(t) {
238
+ const e = { ...t };
239
+ e.Rules || (e.Rules = {
240
+ Validation: {},
241
+ Computation: {},
242
+ BusinessLogic: {}
243
+ }), e.Rules.Validation || (e.Rules.Validation = {}), e.Rules.Computation || (e.Rules.Computation = {}), e.Rules.BusinessLogic || (e.Rules.BusinessLogic = {});
244
+ const r = { ...e.Fields };
245
+ return Object.entries(r).forEach(([s, a]) => {
246
+ let n = a.Required || !1;
247
+ if (a.Validation && Array.isArray(a.Validation))
248
+ if (a.Validation.length > 0 && typeof a.Validation[0] == "object") {
249
+ const o = [];
250
+ a.Validation.forEach((u) => {
251
+ e.Rules.Validation[u.Id] = u, o.push(u.Id), de(u, s) && (n = !0);
252
+ }), r[s] = {
253
+ ...a,
254
+ Validation: o,
255
+ Required: n
256
+ };
257
+ } else
258
+ a.Validation.forEach((u) => {
259
+ var f;
260
+ const p = (f = e.Rules.Validation) == null ? void 0 : f[u];
261
+ p && de(p, s) && (n = !0);
262
+ }), n && !a.Required && (r[s] = {
263
+ ...a,
264
+ Required: !0
265
+ });
266
+ if (a.Formula) {
267
+ const o = `RULE_COMPUTE_${s.toUpperCase()}`;
268
+ e.Rules.Computation[o] = {
269
+ Id: o,
270
+ Name: a.Formula.Id || `Compute ${s}`,
271
+ Description: a.Formula.Description || `Computes value for ${s}`,
272
+ Expression: a.Formula.Expression,
273
+ ExpressionTree: a.Formula.ExpressionTree,
274
+ ResultType: a.Type
275
+ };
276
+ }
277
+ }), e.Fields = r, e;
278
+ }
279
+ function Ke(t) {
280
+ var r, s, a;
281
+ const e = {
282
+ validation: {},
283
+ computation: {},
284
+ businessLogic: {}
285
+ };
286
+ return (r = t.Rules) != null && r.Validation && (e.validation = { ...t.Rules.Validation }), (s = t.Rules) != null && s.Computation && (e.computation = { ...t.Rules.Computation }), (a = t.Rules) != null && a.BusinessLogic && (e.businessLogic = { ...t.Rules.BusinessLogic }), e;
287
+ }
288
+ function Je(t) {
289
+ var s;
290
+ const e = {};
291
+ if (!((s = t.Rules) != null && s.Computation)) return e;
292
+ const r = Object.entries(t.Fields).filter(([a, n]) => n.Computed).map(([a]) => a);
293
+ return r.sort((a, n) => n.length - a.length), Object.entries(t.Rules.Computation).forEach(([a, n]) => {
294
+ const o = a.toUpperCase().replace(/[_-]/g, "");
295
+ for (const p of r) {
296
+ const f = p.toUpperCase().replace(/[_-]/g, "");
297
+ if (o.includes(f)) {
298
+ e[a] = p;
299
+ return;
300
+ }
301
+ }
302
+ const u = `${n.Name || ""} ${n.Description || ""}`.toLowerCase();
303
+ for (const p of r)
304
+ if (u.includes(p.toLowerCase())) {
305
+ e[a] = p;
306
+ return;
307
+ }
308
+ }), e;
309
+ }
310
+ function We(t, e) {
311
+ const r = {};
312
+ Object.keys(t.Fields).forEach((a) => {
313
+ r[a] = {
314
+ validation: [],
315
+ computation: [],
316
+ businessLogic: []
317
+ };
318
+ }), Object.entries(t.Fields).forEach(([a, n]) => {
319
+ n.Validation && Array.isArray(n.Validation) && n.Validation.forEach((u) => {
320
+ e.validation[u] ? r[a].validation.push(u) : e.computation[u] ? r[a].computation.push(u) : e.businessLogic[u] && r[a].businessLogic.push(u);
321
+ }), n.Formula && Object.entries(e.computation).forEach(([o, u]) => {
322
+ var p;
323
+ u.Expression === ((p = n.Formula) == null ? void 0 : p.Expression) && (r[a].computation.includes(o) || r[a].computation.push(o));
324
+ });
325
+ });
326
+ const s = Je(t);
327
+ return Object.entries(s).forEach(([a, n]) => {
328
+ r[n] && !r[n].computation.includes(a) && r[n].computation.push(a);
329
+ }), r;
330
+ }
331
+ function He(t, e) {
332
+ const r = {}, s = {
333
+ editable: !0,
334
+ readable: !0,
335
+ hidden: !1
336
+ };
337
+ return Object.keys(t.Fields).forEach((a) => {
338
+ var p, f, N, L, b, i;
339
+ if (!e || !((p = t.RolePermission) != null && p[e])) {
340
+ r[a] = s;
341
+ return;
342
+ }
343
+ const n = t.RolePermission[e], o = ((f = n.Editable) == null ? void 0 : f.includes(a)) || ((N = n.Editable) == null ? void 0 : N.includes("*")) || !1, u = ((L = n.ReadOnly) == null ? void 0 : L.includes(a)) || ((b = n.ReadOnly) == null ? void 0 : b.includes("*")) || o;
344
+ r[a] = {
345
+ editable: o && !((i = n.ReadOnly) != null && i.includes(a)),
346
+ readable: u,
347
+ hidden: !u
348
+ };
349
+ }), r;
350
+ }
351
+ function Ge(t, e) {
352
+ switch (t) {
353
+ case "String":
354
+ return e.toLowerCase().includes("email") ? "email" : e.toLowerCase().includes("password") ? "password" : "text";
355
+ case "Number":
356
+ return "number";
357
+ case "Boolean":
358
+ return "checkbox";
359
+ case "Date":
360
+ return "date";
361
+ case "DateTime":
362
+ return "datetime-local";
363
+ case "Reference":
364
+ return "reference";
365
+ case "Array":
366
+ case "Object":
367
+ return "textarea";
368
+ default:
369
+ return "text";
370
+ }
371
+ }
372
+ function he(t) {
373
+ return t.replace(/([A-Z])/g, " $1").replace(/^./, (e) => e.toUpperCase()).replace(/_/g, " ").trim();
374
+ }
375
+ function Xe(t, e, r) {
376
+ const s = {};
377
+ switch (e.Required && (s.required = {
378
+ value: !0,
379
+ message: `${he(t)} is required`
380
+ }), e.Type) {
381
+ case "Number":
382
+ s.valueAsNumber = !0;
383
+ break;
384
+ case "Date":
385
+ case "DateTime":
386
+ s.valueAsDate = !0;
387
+ break;
388
+ }
389
+ return e.Validation && e.Validation.length > 0 && (s.validate = () => !0), s;
390
+ }
391
+ function Qe(t) {
392
+ return t.Values ? t.Values.Mode === "Static" && t.Values.Items ? t.Values.Items.map((e) => ({
393
+ value: e.Value,
394
+ label: e.Label
395
+ })) : t.Values.Mode === "Dynamic" && t.Values.Reference ? [] : [] : [];
396
+ }
397
+ function Ze(t, e = {}) {
398
+ if (!t.DefaultValue)
399
+ switch (t.Type) {
400
+ case "Boolean":
401
+ return !1;
402
+ case "Number":
403
+ return 0;
404
+ case "String":
405
+ return "";
406
+ case "Array":
407
+ return [];
408
+ case "Object":
409
+ return {};
410
+ default:
411
+ return;
412
+ }
413
+ return Ye(
414
+ t.DefaultValue.ExpressionTree,
415
+ e
416
+ );
417
+ }
418
+ function et(t, e, r, s = {}, a, n) {
419
+ const o = {
420
+ editable: !0,
421
+ readable: !0,
422
+ hidden: !1
423
+ }, p = n || {
424
+ validation: [],
425
+ computation: [],
426
+ businessLogic: []
427
+ }, f = e.Computed || !!e.Formula || p.computation.length > 0, N = Xe(t, e);
428
+ return f && (N.disabled = !0), {
429
+ name: t,
430
+ type: Ge(e.Type, t),
431
+ label: e.Name || he(t),
432
+ required: e.Required || !1,
433
+ computed: f,
434
+ defaultValue: Ze(e, s),
435
+ options: Qe(e),
436
+ validation: N,
437
+ description: e.Description,
438
+ _bdoField: e,
439
+ permission: a || o,
440
+ rules: p
441
+ };
442
+ }
443
+ function tt(t, e = {}, r) {
444
+ let s = t;
445
+ s = ze(s);
446
+ const a = {}, n = [], o = [], u = [], p = [], f = Ke(s), N = We(s, f), L = He(s, r);
447
+ for (const [b, i] of Object.entries(s.Fields)) {
448
+ if (b.startsWith("_") && !["_id"].includes(b))
449
+ continue;
450
+ const q = L[b];
451
+ if (q.hidden)
452
+ continue;
453
+ const C = et(
454
+ b,
455
+ i,
456
+ s.Fields,
457
+ e,
458
+ q,
459
+ N[b] || {
460
+ validation: [],
461
+ computation: [],
462
+ businessLogic: []
463
+ }
464
+ );
465
+ a[b] = C, n.push(b), C.computed && o.push(b), C.required && u.push(b);
466
+ }
467
+ return {
468
+ fields: a,
469
+ fieldOrder: n,
470
+ computedFields: o,
471
+ requiredFields: u,
472
+ crossFieldValidation: p,
473
+ rules: f,
474
+ fieldRules: N,
475
+ rolePermissions: s.RolePermission
476
+ };
477
+ }
478
+ function rt(t) {
479
+ var r;
480
+ if (t.type !== "reference" || !((r = t._bdoField.Values) != null && r.Reference))
481
+ return null;
482
+ const e = t._bdoField.Values.Reference;
483
+ return {
484
+ businessObject: e.BusinessObject,
485
+ fields: e.Fields || ["_id"],
486
+ // Default to ID field
487
+ filters: e.Filters,
488
+ sort: e.Sort
489
+ };
490
+ }
491
+ function st(t) {
492
+ const e = {};
493
+ for (const [r, s] of Object.entries(t.fields))
494
+ if (s.type === "reference") {
495
+ const a = rt(s);
496
+ a && (e[r] = a);
497
+ }
498
+ return e;
499
+ }
500
+ async function at(t) {
501
+ try {
502
+ const r = (await qe(t)).BOBlob;
503
+ if (!r || typeof r != "object" || !r.Fields)
504
+ throw new Error(`Invalid BDO schema response for ${t}`);
505
+ return r;
506
+ } catch (e) {
507
+ throw console.error(`Schema fetch error for ${t}:`, e), new Error(
508
+ `Failed to load form schema: ${e instanceof Error ? e.message : "Unknown error"}`
509
+ );
510
+ }
511
+ }
512
+ async function nt(t, e = 3) {
513
+ let r;
514
+ for (let s = 1; s <= e; s++)
515
+ try {
516
+ return await at(t);
517
+ } catch (a) {
518
+ if (r = a, s < e) {
519
+ const n = Math.min(1e3 * Math.pow(2, s - 1), 5e3);
520
+ await new Promise((o) => setTimeout(o, n));
521
+ continue;
522
+ }
523
+ }
524
+ throw r;
525
+ }
526
+ async function it(t, e) {
527
+ try {
528
+ return await I(t).get(e);
529
+ } catch (r) {
530
+ throw console.error(`Record fetch error for ${t}/${e}:`, r), new Error(
531
+ `Failed to load record: ${r instanceof Error ? r.message : "Unknown error"}`
532
+ );
533
+ }
534
+ }
535
+ async function ot(t, e, r, s) {
536
+ try {
537
+ let a;
538
+ if (e === "create")
539
+ return a = await I(t).create(r), {
540
+ success: !0,
541
+ data: a,
542
+ recordId: a._id || s
543
+ };
544
+ if (e === "update") {
545
+ if (!s)
546
+ throw new Error("Record ID is required for update operations");
547
+ return a = await I(t).update(s, r), {
548
+ success: !0,
549
+ data: a,
550
+ recordId: a._id || s
551
+ };
552
+ } else
553
+ throw new Error(`Unsupported operation: ${e}`);
554
+ } catch (a) {
555
+ return console.error("Form submission error:", a), {
556
+ success: !1,
557
+ error: new Error(
558
+ `Failed to ${e} record: ${a instanceof Error ? a.message : "Unknown error"}`
559
+ )
560
+ };
561
+ }
562
+ }
563
+ async function ut(t, e = ["_id"], r, s) {
564
+ try {
565
+ const a = {};
566
+ return r && (a.Filter = r), s && (a.Sort = s), e.length > 0 && (a.Field = e), (await I(t).list(a)).Data || [];
567
+ } catch (a) {
568
+ return console.error(`Reference data fetch error for ${t}:`, a), [];
569
+ }
570
+ }
571
+ async function ct(t) {
572
+ const e = {}, r = Object.entries(t).map(
573
+ async ([a, n]) => {
574
+ try {
575
+ const o = await ut(
576
+ n.businessObject,
577
+ n.fields,
578
+ n.filters,
579
+ n.sort
580
+ );
581
+ return [a, o];
582
+ } catch (o) {
583
+ return console.warn(`Failed to fetch reference data for ${a}:`, o), [a, []];
584
+ }
585
+ }
586
+ );
587
+ return (await Promise.allSettled(r)).forEach((a) => {
588
+ if (a.status === "fulfilled") {
589
+ const [n, o] = a.value;
590
+ e[n] = Array.isArray(o) ? o : [];
591
+ }
592
+ }), e;
593
+ }
594
+ function lt(t, e, r = "create", s) {
595
+ const a = {};
596
+ return Object.keys(t).forEach((n) => {
597
+ const o = n, u = t[o];
598
+ if (!e.includes(n) && u !== void 0) {
599
+ if (r === "create") {
600
+ a[o] = u;
601
+ return;
602
+ }
603
+ if (r === "update") {
604
+ if (!s) {
605
+ a[o] = u;
606
+ return;
607
+ }
608
+ const p = s[o];
609
+ JSON.stringify(u) !== JSON.stringify(p) && (a[o] = u);
610
+ }
611
+ }
612
+ }), a;
613
+ }
614
+ function At(t) {
615
+ var e, r, s, a;
616
+ return typeof t == "string" ? t : t != null && t.message ? t.message : (r = (e = t == null ? void 0 : t.response) == null ? void 0 : e.data) != null && r.message ? t.response.data.message : (a = (s = t == null ? void 0 : t.response) == null ? void 0 : s.data) != null && a.error ? t.response.data.error : "An unexpected error occurred";
617
+ }
618
+ function Ct(t) {
619
+ var e, r;
620
+ return !navigator.onLine || (t == null ? void 0 : t.code) === "NETWORK_ERROR" || ((e = t == null ? void 0 : t.message) == null ? void 0 : e.toLowerCase().includes("network")) || ((r = t == null ? void 0 : t.message) == null ? void 0 : r.toLowerCase().includes("fetch"));
621
+ }
622
+ function Dt(t) {
623
+ var e, r, s;
624
+ return (t == null ? void 0 : t.status) === 400 || ((e = t == null ? void 0 : t.response) == null ? void 0 : e.status) === 400 || ((r = t == null ? void 0 : t.message) == null ? void 0 : r.toLowerCase().includes("validation")) || ((s = t == null ? void 0 : t.message) == null ? void 0 : s.toLowerCase().includes("invalid"));
625
+ }
626
+ const _ = /* @__PURE__ */ new Map();
627
+ function dt(t, e, r = 10) {
628
+ _.set(t, {
629
+ data: e,
630
+ timestamp: Date.now(),
631
+ ttl: r * 60 * 1e3
632
+ });
633
+ }
634
+ function ft(t) {
635
+ const e = _.get(t);
636
+ return e ? Date.now() - e.timestamp > e.ttl ? (_.delete(t), null) : e.data : null;
637
+ }
638
+ function Ot(t) {
639
+ t ? Array.from(_.keys()).filter(
640
+ (r) => r.startsWith(t)
641
+ ).forEach((r) => _.delete(r)) : _.clear();
642
+ }
643
+ async function mt(t) {
644
+ const e = `schema:${t}`, r = ft(e);
645
+ if (r)
646
+ return r;
647
+ const s = await nt(t);
648
+ return dt(e, s, 30), s;
649
+ }
650
+ class fe {
651
+ constructor(e = 1e3) {
652
+ U(this, "cache", /* @__PURE__ */ new Map());
653
+ U(this, "maxSize");
654
+ this.maxSize = e;
655
+ }
656
+ get(e) {
657
+ const r = this.cache.get(e);
658
+ return r !== void 0 && (this.cache.delete(e), this.cache.set(e, r)), r;
659
+ }
660
+ set(e, r) {
661
+ if (this.cache.has(e))
662
+ this.cache.delete(e);
663
+ else if (this.cache.size >= this.maxSize) {
664
+ const s = this.cache.keys().next().value;
665
+ this.cache.delete(s);
666
+ }
667
+ this.cache.set(e, r);
668
+ }
669
+ clear() {
670
+ this.cache.clear();
671
+ }
672
+ }
673
+ function pt(t) {
674
+ const e = /* @__PURE__ */ new Set();
675
+ function r(s) {
676
+ switch (s.Type) {
677
+ case "Identifier":
678
+ s.Name && !s.Name.startsWith("$") && e.add(s.Name);
679
+ break;
680
+ case "MemberExpression":
681
+ s.Arguments && s.Arguments.forEach(r);
682
+ break;
683
+ case "CallExpression":
684
+ case "BinaryExpression":
685
+ case "LogicalExpression":
686
+ s.Arguments && s.Arguments.forEach(r);
687
+ break;
688
+ }
689
+ }
690
+ return r(t), e;
691
+ }
692
+ class ht {
693
+ constructor() {
694
+ U(this, "resultCache", new fe(500));
695
+ U(this, "dependencyCache", new fe(200));
696
+ U(this, "compiledExpressions", /* @__PURE__ */ new Map());
697
+ }
698
+ /**
699
+ * Create cache key from expression and context
700
+ */
701
+ createCacheKey(e, r) {
702
+ const s = JSON.stringify(e), a = JSON.stringify(r);
703
+ return `${s}:${a}`;
704
+ }
705
+ /**
706
+ * Get expression dependencies (cached)
707
+ */
708
+ getDependencies(e) {
709
+ const r = JSON.stringify(e);
710
+ let s = this.dependencyCache.get(r);
711
+ return s || (s = pt(e), this.dependencyCache.set(r, s)), s;
712
+ }
713
+ /**
714
+ * Check if expression result is cached and context hasn't changed
715
+ */
716
+ getCachedResult(e, r, s) {
717
+ if (!s) return;
718
+ const a = this.getDependencies(e);
719
+ for (const o of a)
720
+ if (r[o] !== s[o])
721
+ return;
722
+ const n = this.createCacheKey(e, r);
723
+ return this.resultCache.get(n);
724
+ }
725
+ /**
726
+ * Evaluate expression with caching
727
+ */
728
+ evaluate(e, r, s) {
729
+ const a = this.getCachedResult(e, r, s);
730
+ if (a !== void 0)
731
+ return a;
732
+ const n = this.evaluateNode(e, r), o = this.createCacheKey(e, r);
733
+ return this.resultCache.set(o, n), n;
734
+ }
735
+ /**
736
+ * Core expression evaluation logic
737
+ */
738
+ evaluateNode(e, r) {
739
+ switch (e.Type) {
740
+ case "Literal":
741
+ return e.Value;
742
+ case "Identifier":
743
+ return this.getIdentifierValue(e, r);
744
+ case "BinaryExpression":
745
+ return this.evaluateBinaryExpression(e, r);
746
+ case "LogicalExpression":
747
+ return this.evaluateLogicalExpression(e, r);
748
+ case "CallExpression":
749
+ return this.evaluateCallExpression(e, r);
750
+ case "MemberExpression":
751
+ return this.evaluateMemberExpression(e, r);
752
+ default:
753
+ throw new Error(`Unsupported expression type: ${e.Type}`);
754
+ }
755
+ }
756
+ /**
757
+ * Get identifier value with system context support
758
+ */
759
+ getIdentifierValue(e, r) {
760
+ if (e.Name) {
761
+ if (e.Name === "NOW") return /* @__PURE__ */ new Date();
762
+ if (e.Name === "TODAY") {
763
+ const s = /* @__PURE__ */ new Date();
764
+ return new Date(s.getFullYear(), s.getMonth(), s.getDate());
765
+ }
766
+ return e.Name === "CURRENT_USER_ID" ? 1 : e.Name === "CURRENT_USER" ? {
767
+ EmpId: 1,
768
+ Email: "user@example.com",
769
+ FirstName: "John",
770
+ LastName: "Doe",
771
+ Role: "User"
772
+ } : r[e.Name];
773
+ }
774
+ }
775
+ /**
776
+ * Evaluate binary expressions with type coercion
777
+ */
778
+ evaluateBinaryExpression(e, r) {
779
+ if (!e.Arguments || e.Arguments.length !== 2)
780
+ throw new Error("Binary expression requires exactly 2 arguments");
781
+ const s = this.evaluateNode(e.Arguments[0], r), a = this.evaluateNode(e.Arguments[1], r);
782
+ switch (e.Operator) {
783
+ case "==":
784
+ return s == a;
785
+ case "!=":
786
+ return s != a;
787
+ case "===":
788
+ return s === a;
789
+ case "!==":
790
+ return s !== a;
791
+ case "<":
792
+ return Number(s) < Number(a);
793
+ case "<=":
794
+ return Number(s) <= Number(a);
795
+ case ">":
796
+ return Number(s) > Number(a);
797
+ case ">=":
798
+ return Number(s) >= Number(a);
799
+ case "+":
800
+ return Number(s) + Number(a);
801
+ case "-":
802
+ return Number(s) - Number(a);
803
+ case "*":
804
+ return Number(s) * Number(a);
805
+ case "/":
806
+ return Number(s) / Number(a);
807
+ case "%":
808
+ return Number(s) % Number(a);
809
+ default:
810
+ throw new Error(`Unsupported binary operator: ${e.Operator}`);
811
+ }
812
+ }
813
+ /**
814
+ * Evaluate logical expressions with short-circuiting
815
+ */
816
+ evaluateLogicalExpression(e, r) {
817
+ if (!e.Arguments || e.Arguments.length < 2)
818
+ throw new Error("Logical expression requires at least 2 arguments");
819
+ switch (e.Operator) {
820
+ case "AND":
821
+ for (const s of e.Arguments)
822
+ if (!this.evaluateNode(s, r)) return !1;
823
+ return !0;
824
+ case "OR":
825
+ for (const s of e.Arguments)
826
+ if (this.evaluateNode(s, r)) return !0;
827
+ return !1;
828
+ default:
829
+ throw new Error(`Unsupported logical operator: ${e.Operator}`);
830
+ }
831
+ }
832
+ /**
833
+ * Evaluate function calls with built-in functions
834
+ */
835
+ evaluateCallExpression(e, r) {
836
+ var a;
837
+ if (!e.Callee)
838
+ throw new Error("Call expression requires a function name");
839
+ const s = ((a = e.Arguments) == null ? void 0 : a.map((n) => this.evaluateNode(n, r))) || [];
840
+ switch (e.Callee) {
841
+ case "CONCAT":
842
+ return s.map((f) => String(f || "")).join("");
843
+ case "TRIM":
844
+ return String(s[0] || "").trim();
845
+ case "LENGTH":
846
+ return String(s[0] || "").length;
847
+ case "UPPER":
848
+ return String(s[0] || "").toUpperCase();
849
+ case "LOWER":
850
+ return String(s[0] || "").toLowerCase();
851
+ case "CONTAINS":
852
+ return String(s[0] || "").includes(String(s[1] || ""));
853
+ case "MATCHES":
854
+ return new RegExp(String(s[1])).test(String(s[0] || ""));
855
+ case "SUM":
856
+ return s.reduce((f, N) => f + (Number(N) || 0), 0);
857
+ case "AVG":
858
+ const n = s.filter((f) => !isNaN(Number(f)));
859
+ return n.length > 0 ? n.reduce((f, N) => f + Number(N), 0) / n.length : 0;
860
+ case "MIN":
861
+ return Math.min(...s.map((f) => Number(f) || 0));
862
+ case "MAX":
863
+ return Math.max(...s.map((f) => Number(f) || 0));
864
+ case "ROUND":
865
+ return Math.round(Number(s[0]) || 0);
866
+ case "FLOOR":
867
+ return Math.floor(Number(s[0]) || 0);
868
+ case "CEIL":
869
+ return Math.ceil(Number(s[0]) || 0);
870
+ case "ABS":
871
+ return Math.abs(Number(s[0]) || 0);
872
+ case "YEAR":
873
+ return new Date(s[0]).getFullYear();
874
+ case "MONTH":
875
+ return new Date(s[0]).getMonth() + 1;
876
+ case "DAY":
877
+ return new Date(s[0]).getDate();
878
+ case "DATE_DIFF":
879
+ const o = new Date(s[0]), u = new Date(s[1]), p = Math.abs(o.getTime() - u.getTime());
880
+ return Math.ceil(p / (1e3 * 60 * 60 * 24));
881
+ case "IF":
882
+ return s[0] ? s[1] : s[2];
883
+ case "AUTO_NUMBER":
884
+ return Math.floor(Math.random() * 1e4);
885
+ default:
886
+ throw new Error(`Unknown function: ${e.Callee}`);
887
+ }
888
+ }
889
+ /**
890
+ * Evaluate member expressions
891
+ */
892
+ evaluateMemberExpression(e, r) {
893
+ var n;
894
+ if (!e.Arguments || e.Arguments.length === 0)
895
+ throw new Error("Member expression requires arguments");
896
+ const s = this.evaluateNode(e.Arguments[0], r), a = (n = e.Arguments[0].Property) == null ? void 0 : n.Name;
897
+ return a && s && typeof s == "object" ? s[a] : s;
898
+ }
899
+ /**
900
+ * Clear all caches
901
+ */
902
+ clearCache() {
903
+ this.resultCache.clear(), this.dependencyCache.clear(), this.compiledExpressions.clear();
904
+ }
905
+ }
906
+ const ge = new ht();
907
+ function gt(t, e, r, s, a) {
908
+ if (!r || r.length === 0)
909
+ return { isValid: !0 };
910
+ const n = { ...s, [t]: e };
911
+ for (const o of r)
912
+ try {
913
+ if (!ge.evaluate(
914
+ o.ExpressionTree,
915
+ n,
916
+ a
917
+ ))
918
+ return {
919
+ isValid: !1,
920
+ message: o.Message || `Validation failed for ${o.Name}`,
921
+ fieldName: t
922
+ };
923
+ } catch (u) {
924
+ console.warn(`Validation rule ${o.Id} failed to evaluate:`, u);
925
+ }
926
+ return { isValid: !0 };
927
+ }
928
+ function me(t) {
929
+ return Array.from(ge.getDependencies(t));
930
+ }
931
+ function xt(t) {
932
+ const {
933
+ source: e,
934
+ operation: r,
935
+ recordId: s,
936
+ defaultValues: a = {},
937
+ mode: n = "onBlur",
938
+ // Validation mode - controls when errors are shown (see types.ts for details)
939
+ enabled: o = !0,
940
+ userRole: u,
941
+ onSchemaError: p,
942
+ skipSchemaFetch: f = !1,
943
+ schema: N,
944
+ interactionMode: L = "interactive"
945
+ } = t, b = L === "interactive", [i, q] = M(null), [C, be] = M({}), [H, G] = M(!1), [we] = M({}), [V, X] = M(null), [Q, Z] = M(!1), [ye, ee] = M(null), B = Y(!1), K = Y(null), j = Y(null), v = Y(p);
946
+ z(() => {
947
+ v.current = p;
948
+ }, [p]);
949
+ const {
950
+ data: P,
951
+ isLoading: Ee,
952
+ error: k,
953
+ refetch: te
954
+ } = ue({
955
+ queryKey: ["form-schema", e],
956
+ queryFn: () => f ? Promise.resolve(N || {}) : mt(e),
957
+ enabled: o && (!f || !!N),
958
+ retry: 3,
959
+ staleTime: 30 * 60 * 1e3,
960
+ // 30 minutes - schemas don't change frequently
961
+ gcTime: 60 * 60 * 1e3,
962
+ // 1 hour - keep schemas in cache longer
963
+ throwOnError: !1
964
+ }), {
965
+ data: O,
966
+ isLoading: re,
967
+ error: Ne
968
+ } = ue({
969
+ queryKey: ["form-record", e, s],
970
+ queryFn: () => it(e, s),
971
+ enabled: o && r === "update" && !!s,
972
+ retry: 3,
973
+ staleTime: 5 * 60 * 1e3,
974
+ // 5 minutes - records can change more frequently
975
+ gcTime: 15 * 60 * 1e3,
976
+ // 15 minutes - keep records for a reasonable time
977
+ throwOnError: !1
978
+ }), Fe = $(() => {
979
+ const c = { ...a };
980
+ if (r === "update" && O && Object.assign(c, O), i)
981
+ for (const [d, l] of Object.entries(i.fields))
982
+ l.defaultValue !== void 0 && !(d in c) && (c[d] = l.defaultValue);
983
+ return c;
984
+ }, [a, O, r, i]), m = $e({
985
+ mode: n,
986
+ defaultValues: a,
987
+ values: r === "update" && O ? Fe : void 0
988
+ });
989
+ z(() => {
990
+ var c;
991
+ if (P)
992
+ try {
993
+ const d = tt(
994
+ P,
995
+ {},
996
+ // Pass empty object - validation functions get live values from react-hook-form
997
+ u
998
+ );
999
+ q(d);
1000
+ const l = st(d);
1001
+ Object.keys(l).length > 0 && ct(l).then(be).catch(console.warn);
1002
+ } catch (d) {
1003
+ console.error("Schema processing failed:", d), (c = v.current) == null || c.call(v, d);
1004
+ }
1005
+ }, [P, u]), z(() => {
1006
+ var c;
1007
+ k && ((c = v.current) == null || c.call(v, k));
1008
+ }, [k]), z(() => {
1009
+ if (!b || r !== "create" || !i || !o || V)
1010
+ return;
1011
+ (async () => {
1012
+ Z(!0), ee(null);
1013
+ try {
1014
+ const l = await I(e).draftInteraction({});
1015
+ X(l._id), l && typeof l == "object" && Object.entries(l).forEach(([h, g]) => {
1016
+ if (h === "_id") return;
1017
+ m.getValues(h) !== g && m.setValue(h, g, {
1018
+ shouldDirty: !1,
1019
+ shouldValidate: !1
1020
+ });
1021
+ });
1022
+ } catch (d) {
1023
+ console.error("Failed to create initial draft:", d), ee(d);
1024
+ } finally {
1025
+ Z(!1);
1026
+ }
1027
+ })();
1028
+ }, [b, r, i, o, V, e, m]);
1029
+ const J = $(() => {
1030
+ if (!i) return [];
1031
+ const c = /* @__PURE__ */ new Set(), d = new Set(i.computedFields);
1032
+ return Object.entries(i.fieldRules).forEach(([l, h]) => {
1033
+ h.computation.forEach((g) => {
1034
+ const w = i.rules.computation[g];
1035
+ w != null && w.ExpressionTree && me(w.ExpressionTree).forEach((y) => {
1036
+ i.fields[y] && y !== l && !d.has(y) && c.add(y);
1037
+ });
1038
+ });
1039
+ }), i.computedFields.forEach((l) => {
1040
+ const h = i.fields[l];
1041
+ h._bdoField.Formula && me(
1042
+ h._bdoField.Formula.ExpressionTree
1043
+ ).forEach((w) => {
1044
+ i.fields[w] && w !== l && !d.has(w) && c.add(w);
1045
+ });
1046
+ }), Array.from(c);
1047
+ }, [i]), se = S(
1048
+ async (c) => {
1049
+ if (!(!i || !(b || J.length > 0 && J.includes(c)))) {
1050
+ if (b && r === "create" && !V) {
1051
+ console.warn("Interactive create mode: waiting for draft ID");
1052
+ return;
1053
+ }
1054
+ B.current || (K.current && clearTimeout(K.current), K.current = setTimeout(() => {
1055
+ if (!i || B.current)
1056
+ return;
1057
+ const l = m.getValues();
1058
+ (async () => {
1059
+ B.current = !0;
1060
+ try {
1061
+ const g = I(e), w = {};
1062
+ r === "update" && s && "_id" in l && (w._id = l._id), b && r === "create" && V && (w._id = V);
1063
+ const R = j.current ?? (r === "update" ? O : null) ?? {}, y = new Set(
1064
+ i.computedFields || []
1065
+ );
1066
+ Object.keys(l).forEach((F) => {
1067
+ if (F === "_id" || y.has(F)) return;
1068
+ const A = l[F], oe = R[F];
1069
+ JSON.stringify(A) !== JSON.stringify(oe) && (A !== "" && A !== null && A !== void 0) && (w[F] = A);
1070
+ });
1071
+ const E = w, D = {
1072
+ ...j.current
1073
+ };
1074
+ Object.keys(l).forEach((F) => {
1075
+ y.has(F) || (D[F] = l[F]);
1076
+ }), j.current = D;
1077
+ let T;
1078
+ r === "update" && s ? T = await g.draftPatch(s, E) : b && V ? T = await g.draftInteraction(E) : T = await g.draft(E), T && typeof T == "object" && (Object.entries(T).forEach(
1079
+ ([F, A]) => {
1080
+ l[F] !== A && m.setValue(F, A, {
1081
+ shouldDirty: !1,
1082
+ shouldValidate: !1
1083
+ });
1084
+ }
1085
+ ), Object.entries(T).forEach(
1086
+ ([F, A]) => {
1087
+ y.has(F) && (j.current[F] = A);
1088
+ }
1089
+ ));
1090
+ } catch (g) {
1091
+ console.warn("Failed to compute fields via API:", g);
1092
+ } finally {
1093
+ B.current = !1;
1094
+ }
1095
+ })();
1096
+ }, 300));
1097
+ }
1098
+ },
1099
+ [
1100
+ i,
1101
+ r,
1102
+ s,
1103
+ O,
1104
+ e,
1105
+ m,
1106
+ J,
1107
+ b,
1108
+ V
1109
+ ]
1110
+ ), Re = S(async () => {
1111
+ if (!i)
1112
+ return !1;
1113
+ const c = m.getValues();
1114
+ if (!await m.trigger())
1115
+ return !1;
1116
+ const l = i.crossFieldValidation.map(
1117
+ (g) => ({
1118
+ Id: g.Id,
1119
+ Condition: { ExpressionTree: g.ExpressionTree },
1120
+ Message: g.Message || `Validation failed for ${g.Name}`
1121
+ })
1122
+ ), h = le(
1123
+ l,
1124
+ c,
1125
+ C
1126
+ );
1127
+ return h.length > 0 ? (h.forEach((g, w) => {
1128
+ m.setError(`root.crossField${w}`, {
1129
+ type: "validate",
1130
+ message: g.message
1131
+ });
1132
+ }), !1) : !0;
1133
+ }, [
1134
+ i,
1135
+ m.getValues,
1136
+ m.trigger,
1137
+ m.setError,
1138
+ C
1139
+ ]), Ve = S(
1140
+ (c, d) => m.handleSubmit(
1141
+ // RHF onValid handler - validation passed, now do cross-field + API
1142
+ async (l, h) => {
1143
+ if (!i) {
1144
+ const g = new Error("Schema not loaded");
1145
+ d == null || d(g, h);
1146
+ return;
1147
+ }
1148
+ G(!0);
1149
+ try {
1150
+ const g = i.crossFieldValidation.map(
1151
+ (E) => ({
1152
+ Id: E.Id,
1153
+ Condition: { ExpressionTree: E.ExpressionTree },
1154
+ Message: E.Message || `Validation failed for ${E.Name}`
1155
+ })
1156
+ ), w = le(
1157
+ g,
1158
+ l,
1159
+ C
1160
+ );
1161
+ if (w.length > 0) {
1162
+ w.forEach((E, D) => {
1163
+ m.setError(`root.crossField${D}`, {
1164
+ type: "validate",
1165
+ message: E.message
1166
+ });
1167
+ }), d == null || d(m.formState.errors, h);
1168
+ return;
1169
+ }
1170
+ const R = lt(
1171
+ l,
1172
+ i.computedFields,
1173
+ r,
1174
+ O
1175
+ );
1176
+ let y;
1177
+ if (b) {
1178
+ const E = I(e);
1179
+ if (r === "create") {
1180
+ if (!V)
1181
+ throw new Error(
1182
+ "Interactive create mode requires a draft ID. Draft creation may have failed."
1183
+ );
1184
+ y = { success: !0, data: await E.draft({
1185
+ ...R,
1186
+ _id: V
1187
+ }) };
1188
+ } else
1189
+ y = { success: !0, data: await E.draftUpdate(s, R) };
1190
+ } else if (y = await ot(
1191
+ e,
1192
+ r,
1193
+ R,
1194
+ s
1195
+ ), !y.success)
1196
+ throw y.error || new Error("Submission failed");
1197
+ r === "create" && (m.reset(), b && X(null)), await (c == null ? void 0 : c(y.data || l, h));
1198
+ } catch (g) {
1199
+ d == null || d(g, h);
1200
+ } finally {
1201
+ G(!1);
1202
+ }
1203
+ },
1204
+ // RHF onInvalid handler - validation failed
1205
+ (l, h) => {
1206
+ d == null || d(l, h);
1207
+ }
1208
+ ),
1209
+ [
1210
+ m,
1211
+ i,
1212
+ C,
1213
+ e,
1214
+ r,
1215
+ s,
1216
+ O,
1217
+ b,
1218
+ V
1219
+ ]
1220
+ ), Se = S(
1221
+ (c) => (i == null ? void 0 : i.fields[c]) || null,
1222
+ [i]
1223
+ ), Ae = S(() => {
1224
+ if (!i) return {};
1225
+ const c = {};
1226
+ return Object.entries(i.fields).forEach(([d, l]) => {
1227
+ c[d] = l;
1228
+ }), c;
1229
+ }, [i]), Ce = S(
1230
+ (c) => !!(i != null && i.fields[c]),
1231
+ [i]
1232
+ ), De = S(
1233
+ (c) => (i == null ? void 0 : i.requiredFields.includes(c)) || !1,
1234
+ [i]
1235
+ ), Oe = S(
1236
+ (c) => (i == null ? void 0 : i.computedFields.includes(c)) || !1,
1237
+ [i]
1238
+ ), xe = S(async () => {
1239
+ await te();
1240
+ }, [te]), ve = S(() => {
1241
+ m.clearErrors();
1242
+ }, [m]), ae = Ee || r === "update" && re || b && r === "create" && Q, Te = ae || H, ne = k || Ne || ye, Me = !!ne, Ie = $(
1243
+ () => (i == null ? void 0 : i.computedFields) || [],
1244
+ [i]
1245
+ ), Le = $(
1246
+ () => (i == null ? void 0 : i.requiredFields) || [],
1247
+ [i]
1248
+ ), ie = $(() => {
1249
+ if (!i) return {};
1250
+ const c = {};
1251
+ return Object.entries(i.fields).forEach(([d, l]) => {
1252
+ const h = {};
1253
+ switch (l.required && (h.required = `${l.label} is required`), l.permission.editable || (h.disabled = !0), l.type) {
1254
+ case "number":
1255
+ h.valueAsNumber = !0;
1256
+ break;
1257
+ case "date":
1258
+ case "datetime-local":
1259
+ h.valueAsDate = !0;
1260
+ break;
1261
+ }
1262
+ const g = l.rules.validation;
1263
+ g.length > 0 && (h.validate = {
1264
+ expressionValidation: (w) => {
1265
+ const R = m.getValues();
1266
+ for (const y of g) {
1267
+ const E = i.rules.validation[y];
1268
+ if (E) {
1269
+ const D = gt(
1270
+ d,
1271
+ w,
1272
+ [E],
1273
+ R,
1274
+ we
1275
+ );
1276
+ if (!D.isValid)
1277
+ return D.message || E.Message || "Invalid value";
1278
+ }
1279
+ }
1280
+ return !0;
1281
+ }
1282
+ }), c[d] = h;
1283
+ }), c;
1284
+ }, [i, m, C]);
1285
+ return {
1286
+ // Form methods with strict typing
1287
+ register: S(
1288
+ (c, d) => {
1289
+ const l = ie[c], h = d == null ? void 0 : d.onBlur, g = async (w) => {
1290
+ h && await h(w);
1291
+ let R = !0;
1292
+ n === "onBlur" || n === "onTouched" || n === "all" ? R = await m.trigger(c) : R = !m.getFieldState(c, m.formState).error, R && await se(c);
1293
+ };
1294
+ return m.register(c, {
1295
+ ...l,
1296
+ ...d,
1297
+ onBlur: g
1298
+ });
1299
+ },
1300
+ [m, ie, se, n]
1301
+ ),
1302
+ handleSubmit: Ve,
1303
+ watch: m.watch,
1304
+ // Type assertion for complex generic constraints
1305
+ setValue: m.setValue,
1306
+ reset: m.reset,
1307
+ // Flattened form state (NEW - direct access, no nested formState)
1308
+ errors: m.formState.errors,
1309
+ isValid: m.formState.isValid,
1310
+ isDirty: m.formState.isDirty,
1311
+ isSubmitting: m.formState.isSubmitting || H,
1312
+ isSubmitSuccessful: m.formState.isSubmitSuccessful,
1313
+ // Loading states
1314
+ isLoadingInitialData: ae,
1315
+ isLoadingRecord: re,
1316
+ isLoading: Te,
1317
+ // Interactive mode state
1318
+ draftId: V,
1319
+ isCreatingDraft: Q,
1320
+ // Error handling
1321
+ loadError: ne,
1322
+ hasError: Me,
1323
+ // Schema information
1324
+ schema: P,
1325
+ schemaConfig: i,
1326
+ computedFields: Ie,
1327
+ requiredFields: Le,
1328
+ // Field helpers
1329
+ getField: Se,
1330
+ getFields: Ae,
1331
+ hasField: Ce,
1332
+ isFieldRequired: De,
1333
+ isFieldComputed: Oe,
1334
+ // Operations
1335
+ refreshSchema: xe,
1336
+ validateForm: Re,
1337
+ clearErrors: ve
1338
+ };
1339
+ }
1340
+ export {
1341
+ Ot as clearFormCache,
1342
+ Ct as isNetworkError,
1343
+ Dt as isValidationError,
1344
+ At as parseApiError,
1345
+ xt as useForm
1346
+ };