llmz 0.2.0 → 0.2.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.
@@ -0,0 +1,788 @@
1
+ import {
2
+ CodeFormattingError
3
+ } from "./chunk-WSVDMGMR.js";
4
+ import {
5
+ escapeString,
6
+ getMultilineComment,
7
+ toPropertyKey
8
+ } from "./chunk-YX27XM67.js";
9
+
10
+ // src/typings.ts
11
+ import { z } from "@bpinternal/zui";
12
+
13
+ // src/typings-formatter.ts
14
+ var WIDTH = 80;
15
+ var INDENT = " ";
16
+ var isGroup = (n) => n.kind === "group";
17
+ var isPunct = (n, text) => !isGroup(n) && n.kind === "punct" && n.text === text;
18
+ var WORD_CHAR = /[A-Za-z0-9_$.]/;
19
+ function tokenize(src) {
20
+ const toks = [];
21
+ let i = 0;
22
+ let nl = 0;
23
+ const len = src.length;
24
+ while (i < len) {
25
+ const c = src[i];
26
+ if (c === "\n") {
27
+ nl++;
28
+ i++;
29
+ continue;
30
+ }
31
+ if (c === " " || c === " " || c === "\r") {
32
+ i++;
33
+ continue;
34
+ }
35
+ if (c === "/" && src[i + 1] === "*") {
36
+ const end = src.indexOf("*/", i + 2);
37
+ if (end === -1) {
38
+ throw new Error("unterminated block comment");
39
+ }
40
+ toks.push({ kind: "block-comment", text: src.slice(i, end + 2), nl });
41
+ nl = 0;
42
+ i = end + 2;
43
+ continue;
44
+ }
45
+ if (c === "/" && src[i + 1] === "/") {
46
+ let end = src.indexOf("\n", i);
47
+ if (end === -1) {
48
+ end = len;
49
+ }
50
+ toks.push({ kind: "line-comment", text: src.slice(i, end).trimEnd(), nl });
51
+ nl = 0;
52
+ i = end;
53
+ continue;
54
+ }
55
+ if (c === "'" || c === '"' || c === "`") {
56
+ let j = i + 1;
57
+ while (j < len && src[j] !== c) {
58
+ j += src[j] === "\\" ? 2 : 1;
59
+ }
60
+ if (j >= len) {
61
+ throw new Error("unterminated string");
62
+ }
63
+ toks.push({ kind: "string", text: src.slice(i, j + 1), nl });
64
+ nl = 0;
65
+ i = j + 1;
66
+ continue;
67
+ }
68
+ if (c === "=" && src[i + 1] === ">") {
69
+ toks.push({ kind: "punct", text: "=>", nl });
70
+ nl = 0;
71
+ i += 2;
72
+ continue;
73
+ }
74
+ if (WORD_CHAR.test(c) || c === "-" && /\d/.test(src[i + 1] ?? "")) {
75
+ let j = i + 1;
76
+ while (j < len && WORD_CHAR.test(src[j])) {
77
+ j++;
78
+ }
79
+ toks.push({ kind: "word", text: src.slice(i, j), nl });
80
+ nl = 0;
81
+ i = j;
82
+ continue;
83
+ }
84
+ toks.push({ kind: "punct", text: c, nl });
85
+ nl = 0;
86
+ i++;
87
+ }
88
+ return toks;
89
+ }
90
+ var CLOSER = { "{": "}", "(": ")", "[": "]", "<": ">" };
91
+ function parse(toks) {
92
+ const root = [];
93
+ const stack = [];
94
+ const siblings = () => stack.length ? stack[stack.length - 1].children : root;
95
+ for (const t of toks) {
96
+ if (t.kind === "punct" && (t.text === "{" || t.text === "(" || t.text === "[")) {
97
+ const g = { kind: "group", open: t.text, close: CLOSER[t.text], children: [], nl: t.nl };
98
+ siblings().push(g);
99
+ stack.push(g);
100
+ continue;
101
+ }
102
+ if (t.kind === "punct" && t.text === "<") {
103
+ const prev = siblings()[siblings().length - 1];
104
+ if (prev && !isGroup(prev) && prev.kind === "word") {
105
+ const g = { kind: "group", open: "<", close: ">", children: [], nl: t.nl };
106
+ siblings().push(g);
107
+ stack.push(g);
108
+ continue;
109
+ }
110
+ siblings().push(t);
111
+ continue;
112
+ }
113
+ if (t.kind === "punct" && (t.text === "}" || t.text === ")" || t.text === "]" || t.text === ">")) {
114
+ const g = stack[stack.length - 1];
115
+ if (t.text === ">" && (!g || g.open !== "<")) {
116
+ siblings().push(t);
117
+ continue;
118
+ }
119
+ if (!g || g.close !== t.text) {
120
+ throw new Error("unbalanced brackets");
121
+ }
122
+ stack.pop();
123
+ continue;
124
+ }
125
+ siblings().push(t);
126
+ }
127
+ if (stack.length) {
128
+ throw new Error("unbalanced brackets");
129
+ }
130
+ return root;
131
+ }
132
+ function normalize(nodes) {
133
+ for (let i = 0; i < nodes.length; i++) {
134
+ const n = nodes[i];
135
+ if (!isGroup(n)) {
136
+ const key = n.kind === "string" && /^["']([A-Za-z_$][A-Za-z0-9_$]*)["']$/.exec(n.text);
137
+ const next2 = nodes[i + 1];
138
+ if (key && next2 && isPunct(next2, ":")) {
139
+ nodes[i] = { kind: "word", text: key[1], nl: n.nl };
140
+ }
141
+ continue;
142
+ }
143
+ normalize(n.children);
144
+ while (n.children.length && (isPunct(n.children[n.children.length - 1], ",") || isPunct(n.children[n.children.length - 1], ";"))) {
145
+ n.children.pop();
146
+ }
147
+ const prev = nodes[i - 1];
148
+ const next = nodes[i + 1];
149
+ const wrapsWholeValue = next === void 0 || isPunct(next, ";") || isPunct(next, ",");
150
+ const isFunctionType = n.children.some((c) => isPunct(c, "=>"));
151
+ if (n.open === "(" && n.children.length > 0 && prev && isPunct(prev, ":") && wrapsWholeValue && !isFunctionType) {
152
+ n.children[0].nl = n.nl;
153
+ nodes.splice(i, 1, ...n.children);
154
+ i--;
155
+ }
156
+ }
157
+ }
158
+ function isMemberEnd(n) {
159
+ if (isGroup(n)) {
160
+ return true;
161
+ }
162
+ return n.kind === "word" || n.kind === "string" || n.kind === "block-comment" || n.kind === "line-comment" || n.text === "?";
163
+ }
164
+ function isMemberStart(n) {
165
+ if (isGroup(n)) {
166
+ return false;
167
+ }
168
+ return n.kind === "word" || n.kind === "string" || n.kind === "block-comment" || n.kind === "line-comment";
169
+ }
170
+ function splitMembers(nodes, splitOnComma) {
171
+ const members = [];
172
+ let cur = [];
173
+ let hasSeparators = false;
174
+ let usedComma = false;
175
+ const push = () => {
176
+ if (cur.length) {
177
+ members.push(cur);
178
+ }
179
+ cur = [];
180
+ };
181
+ for (const n of nodes) {
182
+ if (isPunct(n, ";") || splitOnComma && isPunct(n, ",")) {
183
+ hasSeparators = true;
184
+ usedComma ||= n.text === ",";
185
+ push();
186
+ continue;
187
+ }
188
+ const isTrailingLineComment = !isGroup(n) && n.kind === "line-comment" && n.nl === 0;
189
+ if (isTrailingLineComment && !cur.length && members.length) {
190
+ members[members.length - 1].push(n);
191
+ continue;
192
+ }
193
+ if (cur.length) {
194
+ const prev = cur[cur.length - 1];
195
+ const prevIsLineComment = !isGroup(prev) && prev.kind === "line-comment";
196
+ if (prevIsLineComment || n.nl > 0 && !isTrailingLineComment && isMemberEnd(prev) && isMemberStart(n)) {
197
+ push();
198
+ }
199
+ }
200
+ cur.push(n);
201
+ }
202
+ push();
203
+ for (let i = members.length - 2; i >= 0; i--) {
204
+ const onlyComments = members[i].every((n) => !isGroup(n) && n.kind === "block-comment");
205
+ if (onlyComments) {
206
+ members[i + 1].unshift(...members[i]);
207
+ members.splice(i, 1);
208
+ }
209
+ }
210
+ return { members, hasSeparators, usedComma };
211
+ }
212
+ function hasMultilineComment(nodes) {
213
+ return nodes.some((n) => !isGroup(n) && n.kind === "block-comment" && n.text.includes("\n"));
214
+ }
215
+ function hasLineComment(nodes) {
216
+ return nodes.some((n) => !isGroup(n) && n.kind === "line-comment");
217
+ }
218
+ function mustBreak(g) {
219
+ if (hasLineComment(g.children) || hasMultilineComment(g.children)) {
220
+ return true;
221
+ }
222
+ const { members, hasSeparators } = splitMembers(g.children, true);
223
+ if (members.length > 1 && !hasSeparators) {
224
+ return true;
225
+ }
226
+ if ((g.open === "{" || g.open === "(") && members.some((m) => !isGroup(m[0]) && m[0].kind === "block-comment")) {
227
+ return true;
228
+ }
229
+ return g.children.some((c) => isGroup(c) && mustBreak(c));
230
+ }
231
+ function needSpace(a, b) {
232
+ if (!a) {
233
+ return false;
234
+ }
235
+ if (a.kind === "open" && b.kind === "close") {
236
+ return false;
237
+ }
238
+ if (b.kind === "punct" && (b.text === "," || b.text === ";" || b.text === ":" || b.text === "?")) {
239
+ return false;
240
+ }
241
+ if (b.kind === "close") {
242
+ return b.text === "}";
243
+ }
244
+ if (a.kind === "open") {
245
+ return a.text === "{";
246
+ }
247
+ if (b.kind === "open") {
248
+ if (b.text === "(" || b.text === "[") {
249
+ return !(a.kind === "word" || a.kind === "string" || a.kind === "close");
250
+ }
251
+ if (b.text === "<") {
252
+ return false;
253
+ }
254
+ return true;
255
+ }
256
+ return true;
257
+ }
258
+ function flatAtoms(nodes) {
259
+ const out = [];
260
+ for (const n of nodes) {
261
+ if (isGroup(n)) {
262
+ out.push({ kind: "open", text: n.open });
263
+ out.push(...flatAtoms(n.children));
264
+ out.push({ kind: "close", text: n.close });
265
+ } else {
266
+ out.push({ kind: n.kind, text: n.text });
267
+ }
268
+ }
269
+ return out;
270
+ }
271
+ function joinAtoms(atoms) {
272
+ let s = "";
273
+ let prev = null;
274
+ for (const a of atoms) {
275
+ if (needSpace(prev, a)) {
276
+ s += " ";
277
+ }
278
+ s += a.text;
279
+ prev = a;
280
+ }
281
+ return s;
282
+ }
283
+ var canFlatten = (nodes) => !hasLineComment(nodes) && !hasMultilineComment(nodes) && !nodes.some((n) => isGroup(n) && mustBreak(n));
284
+ var Printer = class {
285
+ _lines = [];
286
+ _cur = "";
287
+ _prev = null;
288
+ atom(a) {
289
+ if (this._prev && this._cur !== "" && needSpace(this._prev, a)) {
290
+ this._cur += " ";
291
+ }
292
+ this._cur += a.text;
293
+ this._prev = a;
294
+ }
295
+ flat(nodes) {
296
+ for (const a of flatAtoms(nodes)) {
297
+ this.atom(a);
298
+ }
299
+ }
300
+ newline(level) {
301
+ this._lines.push(this._cur);
302
+ this._cur = INDENT.repeat(level);
303
+ this._prev = null;
304
+ }
305
+ blankLine(level) {
306
+ this._lines.push(this._cur);
307
+ this._lines.push("");
308
+ this._cur = INDENT.repeat(level);
309
+ this._prev = null;
310
+ }
311
+ get column() {
312
+ return this._cur.length;
313
+ }
314
+ get lineIsEmpty() {
315
+ return this._cur.trim() === "";
316
+ }
317
+ comment(text, level) {
318
+ const lines = text.split("\n").map((l) => l.trim());
319
+ for (let i = 0; i < lines.length; i++) {
320
+ if (i > 0) {
321
+ this.newline(level);
322
+ this._cur += lines[i].startsWith("*") ? " " : "";
323
+ this._cur += lines[i];
324
+ } else {
325
+ this.atom({ kind: "block-comment", text: lines[i] });
326
+ }
327
+ this._prev = { kind: "block-comment", text: lines[i] };
328
+ }
329
+ }
330
+ toString() {
331
+ this._lines.push(this._cur);
332
+ const out = this._lines.map((l) => l.trim() === "" ? "" : l).join("\n").replace(/\n{3,}/g, "\n\n");
333
+ this._lines.pop();
334
+ return out.trim();
335
+ }
336
+ };
337
+ function flatWidth(nodes) {
338
+ if (!canFlatten(nodes)) {
339
+ return null;
340
+ }
341
+ return joinAtoms(flatAtoms(nodes)).length;
342
+ }
343
+ function fitsFlat(p, nodes) {
344
+ const width = flatWidth(nodes);
345
+ return width !== null && p.column + width + 1 <= WIDTH;
346
+ }
347
+ function walk(p, nodes, level, tailWidth = 0) {
348
+ for (let i = 0; i < nodes.length; i++) {
349
+ const n = nodes[i];
350
+ if (isGroup(n)) {
351
+ const own = mustBreak(n) ? null : flatWidth([n]);
352
+ let restNodes = nodes.slice(i + 1);
353
+ const lineCommentIdx = restNodes.findIndex((x) => !isGroup(x) && x.kind === "line-comment");
354
+ if (lineCommentIdx !== -1) {
355
+ restNodes = restNodes.slice(0, lineCommentIdx);
356
+ }
357
+ const rest = flatWidth(restNodes);
358
+ const after = rest === null ? 0 : rest + tailWidth;
359
+ if (own !== null && p.column + 1 + own + after <= WIDTH) {
360
+ p.flat([n]);
361
+ } else {
362
+ breakGroup(p, n, level, after);
363
+ }
364
+ continue;
365
+ }
366
+ if (n.kind === "block-comment" && n.text.includes("\n")) {
367
+ if (!p.lineIsEmpty) {
368
+ p.newline(level);
369
+ }
370
+ p.comment(n.text, level);
371
+ p.newline(level);
372
+ continue;
373
+ }
374
+ if (n.kind === "line-comment") {
375
+ p.atom({ kind: "line-comment", text: n.text });
376
+ continue;
377
+ }
378
+ p.atom({ kind: n.kind, text: n.text });
379
+ }
380
+ }
381
+ function breakGroup(p, g, level, tailWidth = 0) {
382
+ if (g.open === "<") {
383
+ p.atom({ kind: "open", text: "<" });
384
+ walk(p, g.children, level, tailWidth + 1);
385
+ p.atom({ kind: "close", text: ">" });
386
+ return;
387
+ }
388
+ const { members, usedComma } = splitMembers(g.children, true);
389
+ const hasLeadingComment = members.some((m) => !isGroup(m[0]) && m[0].kind === "block-comment");
390
+ const single = members.length === 1 ? members[0] : null;
391
+ const endsInObject = !!single && isGroup(single[single.length - 1]) && single[single.length - 1].open === "{";
392
+ const hasUnion = !!single && single.some((n) => isPunct(n, "|"));
393
+ if (g.open === "(" && endsInObject && !hasUnion && !hasLeadingComment) {
394
+ p.atom({ kind: "open", text: "(" });
395
+ walk(p, g.children, level, tailWidth + 1);
396
+ p.atom({ kind: "close", text: ")" });
397
+ return;
398
+ }
399
+ const separator = g.open === "(" || g.open === "[" ? "," : usedComma ? "," : null;
400
+ const trailingSeparator = g.open === "(" || g.open === "[";
401
+ p.atom({ kind: "open", text: g.open });
402
+ for (let i = 0; i < members.length; i++) {
403
+ p.newline(level + 1);
404
+ renderMember(p, members[i], level + 1);
405
+ if (separator && (trailingSeparator || i < members.length - 1)) {
406
+ p.atom({ kind: "punct", text: separator });
407
+ }
408
+ }
409
+ p.newline(level);
410
+ p.atom({ kind: "close", text: g.close });
411
+ }
412
+ function renderMember(p, nodes, level) {
413
+ let rest = nodes;
414
+ while (rest.length && !isGroup(rest[0]) && rest[0].kind === "block-comment") {
415
+ const c = rest[0];
416
+ p.comment(c.text, level);
417
+ rest = rest.slice(1);
418
+ if (rest.length) {
419
+ p.newline(level);
420
+ }
421
+ }
422
+ if (!rest.length) {
423
+ return;
424
+ }
425
+ if (fitsFlat(p, rest)) {
426
+ p.flat(rest);
427
+ return;
428
+ }
429
+ const pipeIdx = rest.findIndex((n) => isPunct(n, "|"));
430
+ const parenIdx = rest.findIndex((n) => isGroup(n) && n.open === "(");
431
+ if (pipeIdx !== -1 && (parenIdx === -1 || pipeIdx < parenIdx)) {
432
+ let colonIdx = -1;
433
+ for (let i = 0; i < pipeIdx; i++) {
434
+ if (isPunct(rest[i], ":") || isPunct(rest[i], "=")) {
435
+ colonIdx = i;
436
+ }
437
+ }
438
+ const prefix = rest.slice(0, colonIdx + 1);
439
+ const tail = rest.slice(colonIdx + 1);
440
+ if (!prefix.length || fitsFlat(p, prefix)) {
441
+ p.flat(prefix);
442
+ const options = [];
443
+ let cur = [];
444
+ for (const n of tail) {
445
+ if (isPunct(n, "|")) {
446
+ options.push(cur);
447
+ cur = [];
448
+ } else {
449
+ cur.push(n);
450
+ }
451
+ }
452
+ options.push(cur);
453
+ const optionLevel = prefix.length ? level + 1 : level;
454
+ let first = true;
455
+ for (const option of options) {
456
+ if (!option.length) {
457
+ continue;
458
+ }
459
+ if (!first || prefix.length) {
460
+ p.newline(optionLevel);
461
+ }
462
+ first = false;
463
+ p.atom({ kind: "punct", text: "|" });
464
+ walk(p, option, optionLevel + 1);
465
+ }
466
+ return;
467
+ }
468
+ }
469
+ const eqIdx = rest.findIndex((n) => isPunct(n, "="));
470
+ if (eqIdx !== -1) {
471
+ const prefix = rest.slice(0, eqIdx + 1);
472
+ if (fitsFlat(p, prefix)) {
473
+ p.flat(prefix);
474
+ walk(p, rest.slice(eqIdx + 1), level);
475
+ return;
476
+ }
477
+ }
478
+ walk(p, rest, level);
479
+ }
480
+ function renderStatements(p, nodes, level) {
481
+ const { members: statements } = splitMembers(nodes, false);
482
+ for (let i = 0; i < statements.length; i++) {
483
+ const stmt = statements[i];
484
+ if (i > 0) {
485
+ if ((stmt[0].nl ?? 0) >= 2) {
486
+ p.blankLine(level);
487
+ } else {
488
+ p.newline(level);
489
+ }
490
+ }
491
+ renderStatement(p, stmt, level);
492
+ }
493
+ }
494
+ function renderStatement(p, stmt, level) {
495
+ while (stmt.length && !isGroup(stmt[0]) && stmt[0].kind === "block-comment") {
496
+ p.comment(stmt[0].text, level);
497
+ stmt = stmt.slice(1);
498
+ if (stmt.length) {
499
+ p.newline(level);
500
+ }
501
+ }
502
+ if (!stmt.length) {
503
+ return;
504
+ }
505
+ const nsIdx = stmt.findIndex(
506
+ (n) => !isGroup(n) && n.kind === "word" && (n.text === "namespace" || n.text === "module")
507
+ );
508
+ const bodyIdx = stmt.findIndex((n) => isGroup(n) && n.open === "{");
509
+ if (nsIdx !== -1 && bodyIdx > nsIdx) {
510
+ const body = stmt[bodyIdx];
511
+ p.flat(stmt.slice(0, bodyIdx));
512
+ p.atom({ kind: "open", text: "{" });
513
+ if (body.children.length) {
514
+ p.newline(level + 1);
515
+ renderStatements(p, body.children, level + 1);
516
+ p.newline(level);
517
+ }
518
+ p.atom({ kind: "close", text: "}" });
519
+ walk(p, stmt.slice(bodyIdx + 1), level);
520
+ return;
521
+ }
522
+ renderMember(p, stmt, level);
523
+ }
524
+ function formatTypingsUnsafe(typings) {
525
+ const tokens = tokenize(typings);
526
+ if (!tokens.length) {
527
+ return "";
528
+ }
529
+ const nodes = parse(tokens);
530
+ normalize(nodes);
531
+ const printer = new Printer();
532
+ renderStatements(printer, nodes, 0);
533
+ return printer.toString();
534
+ }
535
+
536
+ // src/formatting.ts
537
+ async function formatTypings(typings, options) {
538
+ try {
539
+ return formatTypingsUnsafe(typings);
540
+ } catch (err) {
541
+ if ((options == null ? void 0 : options.throwOnError) ?? true) {
542
+ throw new CodeFormattingError(err instanceof Error ? err.message : (err == null ? void 0 : err.toString()) ?? "Unknown Error", typings);
543
+ }
544
+ return typings;
545
+ }
546
+ }
547
+
548
+ // src/typings.ts
549
+ var Primitives = [
550
+ "string",
551
+ "number",
552
+ "boolean",
553
+ "unknown",
554
+ "void",
555
+ "any",
556
+ "null",
557
+ "undefined",
558
+ "never",
559
+ "bigint",
560
+ "symbol",
561
+ "object"
562
+ ];
563
+ var LARGE_DECLARATION_LINES = 5;
564
+ var isPrimitive = (type) => Primitives.includes(type);
565
+ var isArrayOfPrimitives = (type) => Primitives.map((p) => `${p}[]`).includes(type);
566
+ var stripSpaces = (typings) => typings.replace(/ +/g, " ").trim();
567
+ var KeyValue = class {
568
+ constructor(key, value) {
569
+ this.key = key;
570
+ this.value = value;
571
+ }
572
+ };
573
+ var FnParameters = class {
574
+ constructor(schema) {
575
+ this.schema = schema;
576
+ }
577
+ };
578
+ var FnReturn = class {
579
+ constructor(schema) {
580
+ this.schema = schema;
581
+ }
582
+ };
583
+ var Declaration = class {
584
+ constructor(schema, identifier) {
585
+ this.schema = schema;
586
+ this.identifier = identifier;
587
+ }
588
+ };
589
+ async function getTypings(schema, options) {
590
+ options ??= {};
591
+ options.declaration ??= false;
592
+ let wrappedSchema = schema;
593
+ if ((options == null ? void 0 : options.declaration) && z.is.zuiType(schema)) {
594
+ const title = "title" in schema.ui ? schema.ui.title : null;
595
+ if (!title) {
596
+ throw new Error('Only schemas with "title" Zui property can be declared.');
597
+ }
598
+ wrappedSchema = new Declaration(schema, title);
599
+ }
600
+ let dts = await sUnwrapZodRecursive(wrappedSchema, { ...options });
601
+ dts = await formatTypings(dts, { throwOnError: false });
602
+ return dts;
603
+ }
604
+ async function sUnwrapZodRecursive(schema, options) {
605
+ return sUnwrapZod(schema, options);
606
+ }
607
+ async function sUnwrapZod(schema, options) {
608
+ var _a, _b;
609
+ const newOptions = {
610
+ ...options,
611
+ declaration: false,
612
+ parent: schema
613
+ };
614
+ if (schema instanceof Declaration) {
615
+ const description = getMultilineComment(schema.schema.description);
616
+ const withoutDesc = schema.schema.describe("");
617
+ const typings = await sUnwrapZodRecursive(withoutDesc, { ...newOptions, declaration: true });
618
+ const isLargeDeclaration = typings.split("\n").length >= LARGE_DECLARATION_LINES;
619
+ const closingTag = isLargeDeclaration ? `// end of ${schema.identifier}` : "";
620
+ if (z.is.zuiFunction(schema.schema)) {
621
+ return stripSpaces(`${description}
622
+ declare function ${schema.identifier}${typings};${closingTag}`);
623
+ }
624
+ return stripSpaces(`${description}
625
+ declare const ${schema.identifier}: ${typings};${closingTag}`);
626
+ }
627
+ if (schema instanceof KeyValue) {
628
+ if (z.is.zuiOptional(schema.value) || z.is.zuiDefault(schema.value)) {
629
+ let innerType = schema.value._def.innerType;
630
+ if (z.is.zuiType(innerType) && !innerType.description && schema.value.description) {
631
+ innerType = innerType == null ? void 0 : innerType.describe(schema.value.description);
632
+ }
633
+ const optionalToken = schema.key.endsWith("?") ? "" : "?";
634
+ return sUnwrapZodRecursive(new KeyValue(schema.key + optionalToken, innerType), newOptions);
635
+ }
636
+ const description = getMultilineComment(schema.value._def.description || schema.value.description);
637
+ const delimiter = (description == null ? void 0 : description.trim().length) > 0 ? "\n" : "";
638
+ const withoutDesc = schema.value.describe("");
639
+ return `${delimiter}${description}${delimiter}${schema.key}: ${await sUnwrapZodRecursive(withoutDesc, newOptions)}${delimiter}`;
640
+ }
641
+ if (schema instanceof FnParameters) {
642
+ if (z.is.zuiTuple(schema.schema)) {
643
+ let args = "";
644
+ for (let i = 0; i < schema.schema.items.length; i++) {
645
+ const argName = ((_b = (_a = schema.schema.items[i]) == null ? void 0 : _a.ui) == null ? void 0 : _b.title) ?? `arg${i}`;
646
+ const item = schema.schema.items[i];
647
+ args += `${await sUnwrapZodRecursive(new KeyValue(toPropertyKey(argName), item), newOptions)}, `;
648
+ }
649
+ return args;
650
+ }
651
+ const isLiteral = z.is.zuiLiteral(schema.schema.naked());
652
+ const typings = (await sUnwrapZodRecursive(schema.schema, newOptions)).trim();
653
+ const startsWithPairs = typings.startsWith("{") && typings.endsWith("}") || typings.startsWith("[") && typings.endsWith("]") || typings.startsWith("(") && typings.endsWith(")") || typings.startsWith("Array<") && typings.endsWith(">") || typings.startsWith("Record<") && typings.endsWith(">") || isArrayOfPrimitives(typings);
654
+ if (startsWithPairs || isLiteral) {
655
+ return `args: ${typings}`;
656
+ } else {
657
+ return typings;
658
+ }
659
+ }
660
+ if (schema instanceof FnReturn) {
661
+ if (z.is.zuiOptional(schema.schema)) {
662
+ return `${await sUnwrapZodRecursive(schema.schema.unwrap(), newOptions)} | undefined`;
663
+ }
664
+ return sUnwrapZodRecursive(schema.schema, newOptions);
665
+ }
666
+ if (schema === null) {
667
+ return "unknown";
668
+ }
669
+ schema;
670
+ if (z.is.zuiDefault(schema)) {
671
+ return sUnwrapZodRecursive(schema._def.innerType, options);
672
+ }
673
+ if (z.is.zuiVoid(schema)) {
674
+ return "void";
675
+ }
676
+ if (z.is.zuiUnknown(schema)) {
677
+ return "unknown";
678
+ }
679
+ if (z.is.zuiAny(schema)) {
680
+ return "any";
681
+ }
682
+ if (z.is.zuiPromise(schema)) {
683
+ return `Promise<${await sUnwrapZodRecursive(schema.unwrap(), newOptions)}>`;
684
+ }
685
+ if (z.is.zuiFunction(schema)) {
686
+ const description = getMultilineComment(schema._def.description);
687
+ const input = await sUnwrapZodRecursive(new FnParameters(schema._def.args), newOptions);
688
+ const output = await sUnwrapZodRecursive(new FnReturn(schema._def.returns), newOptions);
689
+ if (options == null ? void 0 : options.declaration) {
690
+ return `${description}
691
+ (${input}): ${output}`;
692
+ }
693
+ return `${description}
694
+ (${input}) => ${output}`;
695
+ }
696
+ if (z.is.zuiArray(schema)) {
697
+ const item = await sUnwrapZodRecursive(schema._def.type, newOptions);
698
+ if (isPrimitive(item)) {
699
+ return `${item}[]`;
700
+ }
701
+ return `Array<${item}>`;
702
+ }
703
+ if (z.is.zuiEnum(schema)) {
704
+ const values = schema._def.values.map(escapeString);
705
+ return values.join(" | ");
706
+ }
707
+ if (z.is.zuiTuple(schema)) {
708
+ if (schema.items.length === 0) {
709
+ return "[]";
710
+ }
711
+ const items = await Promise.all(schema.items.map((i) => sUnwrapZodRecursive(i, newOptions)));
712
+ return `[${items.join(", ")}]`;
713
+ }
714
+ if (z.is.zuiNullable(schema)) {
715
+ return `${await sUnwrapZodRecursive(schema.unwrap(), options)} | null`;
716
+ }
717
+ if (z.is.zuiOptional(schema)) {
718
+ if ((options == null ? void 0 : options.declaration) || z.is.zuiType(options == null ? void 0 : options.parent) && options.parent.typeName === "ZodRecord") {
719
+ return `${await sUnwrapZodRecursive(schema._def.innerType, newOptions)} | undefined`;
720
+ }
721
+ const optionalToken = options.parent instanceof KeyValue ? "| undefined" : "";
722
+ const val = `${await sUnwrapZodRecursive(schema._def.innerType, newOptions)}${optionalToken}`;
723
+ return val;
724
+ }
725
+ if (z.is.zuiObject(schema)) {
726
+ const props = await Promise.all(
727
+ Object.entries(schema.shape).map(async ([key, value]) => {
728
+ if (z.is.zuiType(value)) {
729
+ return sUnwrapZodRecursive(new KeyValue(toPropertyKey(key), value), newOptions);
730
+ }
731
+ return `${key}: unknown`;
732
+ })
733
+ );
734
+ return `{ ${props.join("; ")} }`;
735
+ }
736
+ if (z.is.zuiString(schema)) {
737
+ const description = getMultilineComment(schema._def.description);
738
+ return `${description} string`.trim();
739
+ }
740
+ if (z.is.zuiUnion(schema)) {
741
+ const description = getMultilineComment(schema._def.description);
742
+ const options2 = await Promise.all(
743
+ schema.options.map(async (option) => {
744
+ return sUnwrapZodRecursive(option, newOptions);
745
+ })
746
+ );
747
+ return `${description}
748
+ ${options2.join(" | ")}`;
749
+ }
750
+ if (z.is.zuiLiteral(schema)) {
751
+ const description = getMultilineComment(schema._def.description);
752
+ return `${description}
753
+ ${typeof schema.value === "string" ? escapeString(schema.value) : String(schema.value)}`.trim();
754
+ }
755
+ if (z.is.zuiNumber(schema)) {
756
+ const description = getMultilineComment(schema._def.description);
757
+ return `${description} number`.trim();
758
+ }
759
+ if (z.is.zuiBoolean(schema)) {
760
+ const description = getMultilineComment(schema._def.description);
761
+ return `${description} boolean`.trim();
762
+ }
763
+ if (z.is.zuiCatch(schema)) {
764
+ return sUnwrapZodRecursive(schema.removeCatch(), newOptions);
765
+ }
766
+ if (z.is.zuiLazy(schema)) {
767
+ return sUnwrapZodRecursive(schema._def.getter(), newOptions);
768
+ }
769
+ if (z.is.zuiRecord(schema)) {
770
+ const description = getMultilineComment(schema._def.description);
771
+ const keyType = await sUnwrapZodRecursive(schema._def.keyType, newOptions);
772
+ const valueType = await sUnwrapZodRecursive(schema._def.valueType, newOptions);
773
+ return `${description} { [key: (${keyType})]: (${valueType}) }`;
774
+ }
775
+ try {
776
+ let typings = schema == null ? void 0 : schema.toTypescriptType({ treatDefaultAsOptional: true });
777
+ typings ??= "unknown";
778
+ return stripSpaces(typings);
779
+ } catch (error) {
780
+ console.error("Error in sUnwrapZod", { error, schema, parent: options == null ? void 0 : options.parent });
781
+ return "unknown";
782
+ }
783
+ }
784
+
785
+ export {
786
+ formatTypings,
787
+ getTypings
788
+ };