trustformers-wasm 0.2.0

WebAssembly bindings for TrustformeRS transformer library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
//! Automatic documentation generator from TypeScript definitions
//!
//! This module provides comprehensive documentation generation capabilities including:
//! - TypeScript definition parsing and analysis
//! - Multi-format documentation output (HTML, Markdown, JSON)
//! - API reference generation with examples
//! - Interactive playground documentation
//! - Theme support and customization

use js_sys::Array;
use serde::{Deserialize, Serialize};
use std::format;
use std::string::{String, ToString};
use std::vec::Vec;
use wasm_bindgen::prelude::*;

/// Documentation output formats
#[wasm_bindgen]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DocFormat {
    /// HTML documentation with interactive features
    HTML,
    /// Markdown documentation
    Markdown,
    /// JSON API documentation
    JSON,
    /// OpenAPI/Swagger specification
    OpenAPI,
    /// TypeDoc compatible format
    TypeDoc,
}

/// Documentation themes
#[wasm_bindgen]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DocTheme {
    /// Default light theme
    Default,
    /// Dark theme
    Dark,
    /// Material design theme
    Material,
    /// Bootstrap theme
    Bootstrap,
    /// Minimal theme
    Minimal,
}

/// TypeScript node types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TSNodeType {
    Interface,
    Class,
    Function,
    Method,
    Property,
    Parameter,
    Enum,
    Type,
    Module,
    Namespace,
}

/// TypeScript definition node
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TSNode {
    pub name: String,
    pub node_type: TSNodeType,
    pub description: Option<String>,
    pub parameters: Vec<TSParameter>,
    pub return_type: Option<String>,
    pub properties: Vec<TSProperty>,
    pub examples: Vec<String>,
    pub deprecated: bool,
    pub since_version: Option<String>,
    pub file_path: String,
    pub line_number: u32,
}

/// TypeScript parameter
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TSParameter {
    pub name: String,
    pub param_type: String,
    pub optional: bool,
    pub default_value: Option<String>,
    pub description: Option<String>,
}

/// TypeScript property
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TSProperty {
    pub name: String,
    pub property_type: String,
    pub optional: bool,
    pub readonly: bool,
    pub description: Option<String>,
}

/// Documentation configuration
#[wasm_bindgen]
#[derive(Debug, Clone)]
pub struct DocConfig {
    format: DocFormat,
    theme: DocTheme,
    include_examples: bool,
    include_source_links: bool,
    include_private: bool,
    generate_playground: bool,
    output_directory: String,
    base_url: String,
    title: String,
    version: String,
}

impl Default for DocConfig {
    fn default() -> Self {
        Self::new()
    }
}

#[wasm_bindgen]
impl DocConfig {
    /// Create a new documentation configuration
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self {
        Self {
            format: DocFormat::HTML,
            theme: DocTheme::Default,
            include_examples: true,
            include_source_links: true,
            include_private: false,
            generate_playground: true,
            output_directory: "./docs".to_string(),
            base_url: "/".to_string(),
            title: "TrustFormer WASM API".to_string(),
            version: "1.0.0".to_string(),
        }
    }

    /// Set documentation format
    pub fn set_format(&mut self, format: DocFormat) {
        self.format = format;
    }

    /// Set documentation theme
    pub fn set_theme(&mut self, theme: DocTheme) {
        self.theme = theme;
    }

    /// Enable/disable examples
    pub fn set_include_examples(&mut self, include: bool) {
        self.include_examples = include;
    }

    /// Enable/disable source links
    pub fn set_include_source_links(&mut self, include: bool) {
        self.include_source_links = include;
    }

    /// Enable/disable private member documentation
    pub fn set_include_private(&mut self, include: bool) {
        self.include_private = include;
    }

    /// Enable/disable playground generation
    pub fn set_generate_playground(&mut self, generate: bool) {
        self.generate_playground = generate;
    }

    /// Set output directory
    pub fn set_output_directory(&mut self, dir: String) {
        self.output_directory = dir;
    }

    /// Set base URL
    pub fn set_base_url(&mut self, url: String) {
        self.base_url = url;
    }

    /// Set documentation title
    pub fn set_title(&mut self, title: String) {
        self.title = title;
    }

    /// Set version
    pub fn set_version(&mut self, version: String) {
        self.version = version;
    }
}

/// Generated documentation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GeneratedDoc {
    pub title: String,
    pub version: String,
    pub generated_at: f64,
    pub format: DocFormat,
    pub content: String,
    pub assets: Vec<DocAsset>,
    pub navigation: DocNavigation,
}

/// Documentation asset (CSS, JS, images)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DocAsset {
    pub filename: String,
    pub content: String,
    pub mime_type: String,
}

/// Documentation navigation structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DocNavigation {
    pub sections: Vec<NavSection>,
}

/// Navigation section
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NavSection {
    pub title: String,
    pub items: Vec<NavItem>,
}

/// Navigation item
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NavItem {
    pub title: String,
    pub link: String,
    pub children: Vec<NavItem>,
}

/// Automatic documentation generator
#[wasm_bindgen]
pub struct AutoDocGenerator {
    config: DocConfig,
    ts_nodes: Vec<TSNode>,
    examples: Vec<CodeExample>,
}

/// Code example
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodeExample {
    pub title: String,
    pub description: String,
    pub code: String,
    pub language: String,
    pub category: String,
}

#[wasm_bindgen]
impl AutoDocGenerator {
    /// Create a new documentation generator
    #[wasm_bindgen(constructor)]
    pub fn new(config: DocConfig) -> Self {
        Self {
            config,
            ts_nodes: Vec::new(),
            examples: Vec::new(),
        }
    }

    /// Parse TypeScript definitions from string
    pub fn parse_typescript(&mut self, typescript_content: &str) -> Result<(), JsValue> {
        // Simplified TypeScript parsing
        // In a real implementation, this would use a proper TypeScript parser

        let lines: Vec<&str> = typescript_content.lines().collect();
        let mut current_line = 0;

        while current_line < lines.len() {
            let line = lines[current_line].trim();

            if line.starts_with("export interface") {
                self.parse_interface(&lines, &mut current_line)?;
            } else if line.starts_with("export class") {
                self.parse_class(&lines, &mut current_line)?;
            } else if line.starts_with("export function")
                || line.starts_with("export declare function")
            {
                self.parse_function(&lines, &mut current_line)?;
            } else if line.starts_with("export enum") {
                self.parse_enum(&lines, &mut current_line)?;
            }

            current_line += 1;
        }

        Ok(())
    }

    /// Generate documentation in the configured format
    pub fn generate_documentation(&self) -> Result<String, JsValue> {
        match self.config.format {
            DocFormat::HTML => self.generate_html(),
            DocFormat::Markdown => self.generate_markdown(),
            DocFormat::JSON => self.generate_json(),
            DocFormat::OpenAPI => self.generate_openapi(),
            DocFormat::TypeDoc => self.generate_typedoc(),
        }
    }

    /// Add a code example
    pub fn add_example(&mut self, example_json: &str) -> Result<(), JsValue> {
        let example: CodeExample = serde_json::from_str(example_json)
            .map_err(|e| JsValue::from_str(&format!("Invalid example: {e}")))?;

        self.examples.push(example);
        Ok(())
    }

    /// Generate navigation structure
    pub fn generate_navigation(&self) -> String {
        let nav = self.build_navigation();
        serde_json::to_string_pretty(&nav).unwrap_or_else(|_| "{}".to_string())
    }

    /// Generate API reference
    pub fn generate_api_reference(&self) -> String {
        let mut content = String::new();

        // Group nodes by type
        let interfaces: Vec<_> = self
            .ts_nodes
            .iter()
            .filter(|n| matches!(n.node_type, TSNodeType::Interface))
            .collect();
        let classes: Vec<_> = self
            .ts_nodes
            .iter()
            .filter(|n| matches!(n.node_type, TSNodeType::Class))
            .collect();
        let functions: Vec<_> = self
            .ts_nodes
            .iter()
            .filter(|n| matches!(n.node_type, TSNodeType::Function))
            .collect();
        let enums: Vec<_> = self
            .ts_nodes
            .iter()
            .filter(|n| matches!(n.node_type, TSNodeType::Enum))
            .collect();

        // Generate sections
        if !interfaces.is_empty() {
            content.push_str("# Interfaces\n\n");
            for interface in interfaces {
                content.push_str(&self.format_interface_doc(interface));
            }
        }

        if !classes.is_empty() {
            content.push_str("# Classes\n\n");
            for class in classes {
                content.push_str(&self.format_class_doc(class));
            }
        }

        if !functions.is_empty() {
            content.push_str("# Functions\n\n");
            for function in functions {
                content.push_str(&self.format_function_doc(function));
            }
        }

        if !enums.is_empty() {
            content.push_str("# Enumerations\n\n");
            for enum_node in enums {
                content.push_str(&self.format_enum_doc(enum_node));
            }
        }

        content
    }

    /// Generate playground documentation
    pub fn generate_playground(&self) -> String {
        if !self.config.generate_playground {
            return "Playground generation disabled".to_string();
        }

        let mut playground = String::new();

        playground.push_str(r#"
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TrustFormer WASM Playground</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/codemirror.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/theme/monokai.min.css">
    <style>
        body { margin: 0; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; }
        .playground { display: flex; height: 100vh; }
        .editor-panel { flex: 1; display: flex; flex-direction: column; }
        .output-panel { flex: 1; display: flex; flex-direction: column; border-left: 1px solid #ccc; }
        .toolbar { padding: 10px; background: #f5f5f5; border-bottom: 1px solid #ccc; }
        .editor { flex: 1; }
        .output { flex: 1; padding: 10px; overflow: auto; background: #1e1e1e; color: #fff; font-family: monospace; }
        button { padding: 8px 16px; margin-right: 8px; }
        select { padding: 8px; margin-right: 8px; }
    </style>
</head>
<body>
    <div class="playground">
        <div class="editor-panel">
            <div class="toolbar">
                <select id="example-select">
                    <option value="">Select an example...</option>
"#);

        // Add examples to playground
        for (i, example) in self.examples.iter().enumerate() {
            playground.push_str(&format!(
                r#"                    <option value="{}">{}</option>
"#,
                i, example.title
            ));
        }

        playground.push_str(r#"
                </select>
                <button onclick="runCode()">Run</button>
                <button onclick="clearOutput()">Clear</button>
            </div>
            <div class="editor">
                <textarea id="code-editor"></textarea>
            </div>
        </div>
        <div class="output-panel">
            <div class="toolbar">
                <strong>Output</strong>
            </div>
            <div class="output" id="output"></div>
        </div>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/codemirror.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/mode/javascript/javascript.min.js"></script>
    <script>
        const examples = [
"#);

        // Add example data
        for example in &self.examples {
            playground.push_str(&format!(
                r#"            {{
                title: "{}",
                description: "{}",
                code: `{}`
            }},
"#,
                example.title,
                example.description,
                example.code.replace('`', "\\`")
            ));
        }

        playground.push_str(r#"
        ];

        const editor = CodeMirror.fromTextArea(document.getElementById('code-editor'), {
            mode: 'javascript',
            theme: 'monokai',
            lineNumbers: true,
            autoCloseBrackets: true,
            matchBrackets: true
        });

        document.getElementById('example-select').addEventListener('change', function(e) {
            const index = parseInt(e.target.value);
            if (!isNaN(index) && examples[index]) {
                editor.setValue(examples[index].code);
            }
        });

        function runCode() {
            const code = editor.getValue();
            const output = document.getElementById('output');

            try {
                // This would execute the code with the WASM module
                output.innerHTML += '> ' + code + '\n';
                output.innerHTML += 'Note: This is a demo playground. Real execution would require the WASM module.\n\n';
                output.scrollTop = output.scrollHeight;
            } catch (error) {
                output.innerHTML += 'Error: ' + error.message + '\n\n';
                output.scrollTop = output.scrollHeight;
            }
        }

        function clearOutput() {
            document.getElementById('output').innerHTML = '';
        }

        // Load first example by default
        if (examples.length > 0) {
            editor.setValue(examples[0].code);
        }
    </script>
</body>
</html>
"#);

        playground
    }

    /// Get statistics about the parsed TypeScript
    pub fn get_statistics(&self) -> String {
        let interface_count = self
            .ts_nodes
            .iter()
            .filter(|n| matches!(n.node_type, TSNodeType::Interface))
            .count();
        let class_count = self
            .ts_nodes
            .iter()
            .filter(|n| matches!(n.node_type, TSNodeType::Class))
            .count();
        let function_count = self
            .ts_nodes
            .iter()
            .filter(|n| matches!(n.node_type, TSNodeType::Function))
            .count();
        let enum_count =
            self.ts_nodes.iter().filter(|n| matches!(n.node_type, TSNodeType::Enum)).count();

        format!(
            r#"{{
  "total_nodes": {},
  "interfaces": {},
  "classes": {},
  "functions": {},
  "enums": {},
  "examples": {}
}}"#,
            self.ts_nodes.len(),
            interface_count,
            class_count,
            function_count,
            enum_count,
            self.examples.len()
        )
    }

    // Private parsing methods

    fn parse_interface(&mut self, lines: &[&str], current_line: &mut usize) -> Result<(), JsValue> {
        let line = lines[*current_line];

        // Extract interface name
        let name = self.extract_name_from_declaration(line, "interface")?;

        let mut properties = Vec::new();
        *current_line += 1;

        // Parse properties until closing brace
        while *current_line < lines.len() {
            let prop_line = lines[*current_line].trim();
            if prop_line == "}" {
                break;
            }

            if !prop_line.is_empty() && !prop_line.starts_with("//") {
                if let Ok(property) = self.parse_property(prop_line) {
                    properties.push(property);
                }
            }

            *current_line += 1;
        }

        let node = TSNode {
            name,
            node_type: TSNodeType::Interface,
            description: self
                .extract_description_comment(lines, *current_line - properties.len() - 1),
            parameters: Vec::new(),
            return_type: None,
            properties,
            examples: Vec::new(),
            deprecated: false,
            since_version: None,
            file_path: "parsed.ts".to_string(),
            line_number: *current_line as u32,
        };

        self.ts_nodes.push(node);
        Ok(())
    }

    fn parse_class(&mut self, lines: &[&str], current_line: &mut usize) -> Result<(), JsValue> {
        let line = lines[*current_line];
        let name = self.extract_name_from_declaration(line, "class")?;

        // For simplicity, treat class similar to interface
        // In a real implementation, this would parse methods, constructors, etc.
        let node = TSNode {
            name,
            node_type: TSNodeType::Class,
            description: self.extract_description_comment(lines, *current_line - 1),
            parameters: Vec::new(),
            return_type: None,
            properties: Vec::new(),
            examples: Vec::new(),
            deprecated: false,
            since_version: None,
            file_path: "parsed.ts".to_string(),
            line_number: *current_line as u32,
        };

        self.ts_nodes.push(node);
        Ok(())
    }

    fn parse_function(&mut self, lines: &[&str], current_line: &mut usize) -> Result<(), JsValue> {
        let line = lines[*current_line];
        let name = self.extract_name_from_declaration(line, "function")?;

        // Extract parameters and return type
        let (parameters, return_type) = self.parse_function_signature(line)?;

        let node = TSNode {
            name,
            node_type: TSNodeType::Function,
            description: self.extract_description_comment(lines, *current_line - 1),
            parameters,
            return_type,
            properties: Vec::new(),
            examples: Vec::new(),
            deprecated: false,
            since_version: None,
            file_path: "parsed.ts".to_string(),
            line_number: *current_line as u32,
        };

        self.ts_nodes.push(node);
        Ok(())
    }

    fn parse_enum(&mut self, lines: &[&str], current_line: &mut usize) -> Result<(), JsValue> {
        let line = lines[*current_line];
        let name = self.extract_name_from_declaration(line, "enum")?;

        let node = TSNode {
            name,
            node_type: TSNodeType::Enum,
            description: self.extract_description_comment(lines, *current_line - 1),
            parameters: Vec::new(),
            return_type: None,
            properties: Vec::new(),
            examples: Vec::new(),
            deprecated: false,
            since_version: None,
            file_path: "parsed.ts".to_string(),
            line_number: *current_line as u32,
        };

        self.ts_nodes.push(node);
        Ok(())
    }

    fn extract_name_from_declaration(&self, line: &str, keyword: &str) -> Result<String, JsValue> {
        let parts: Vec<&str> = line.split_whitespace().collect();

        for i in 0..parts.len() {
            if parts[i] == keyword && i + 1 < parts.len() {
                let name = parts[i + 1];
                // Remove any generic parameters or extends clauses
                let clean_name =
                    name.split('<').next().unwrap_or(name).split('(').next().unwrap_or(name);
                return Ok(clean_name.to_string());
            }
        }

        Err(JsValue::from_str(&format!(
            "Could not extract name from {} declaration",
            keyword
        )))
    }

    fn parse_property(&self, line: &str) -> Result<TSProperty, JsValue> {
        // Simplified property parsing
        let parts: Vec<&str> = line.split(':').collect();
        if parts.len() != 2 {
            return Err(JsValue::from_str("Invalid property syntax"));
        }

        let name_part = parts[0].trim();
        let type_part = parts[1].trim().trim_end_matches(';');

        let optional = name_part.ends_with('?');
        let readonly = name_part.starts_with("readonly ");

        let name = name_part
            .trim_start_matches("readonly ")
            .trim_end_matches('?')
            .trim()
            .to_string();

        Ok(TSProperty {
            name,
            property_type: type_part.to_string(),
            optional,
            readonly,
            description: None,
        })
    }

    fn parse_function_signature(
        &self,
        _line: &str,
    ) -> Result<(Vec<TSParameter>, Option<String>), JsValue> {
        // Simplified function signature parsing
        let parameters = Vec::new(); // Would parse actual parameters
        let return_type = Some("any".to_string()); // Would parse actual return type

        Ok((parameters, return_type))
    }

    fn extract_description_comment(&self, lines: &[&str], line_index: usize) -> Option<String> {
        if line_index == 0 {
            return None;
        }

        let comment_line = lines[line_index - 1].trim();
        if comment_line.starts_with("//") {
            Some(comment_line.trim_start_matches("//").trim().to_string())
        } else {
            None
        }
    }

    // Documentation generation methods

    fn generate_html(&self) -> Result<String, JsValue> {
        let mut html = String::new();

        html.push_str(&format!(
            r#"
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{}</title>
    {}
</head>
<body>
    <div class="container">
        <header>
            <h1>{}</h1>
            <p>Version: {}</p>
        </header>
        <nav>
            {}
        </nav>
        <main>
            {}
        </main>
    </div>
    {}
</body>
</html>
"#,
            self.config.title,
            self.get_html_styles(),
            self.config.title,
            self.config.version,
            self.generate_html_navigation(),
            self.generate_api_reference(),
            self.get_html_scripts()
        ));

        Ok(html)
    }

    fn generate_markdown(&self) -> Result<String, JsValue> {
        let mut md = String::new();

        md.push_str(&format!("# {title}\n\n", title = self.config.title));
        md.push_str(&format!(
            "Version: {version}\n\n",
            version = self.config.version
        ));
        md.push_str(&self.generate_api_reference());

        Ok(md)
    }

    fn generate_json(&self) -> Result<String, JsValue> {
        let doc = GeneratedDoc {
            title: self.config.title.clone(),
            version: self.config.version.clone(),
            generated_at: js_sys::Date::now(),
            format: self.config.format,
            content: self.generate_api_reference(),
            assets: Vec::new(),
            navigation: self.build_navigation(),
        };

        serde_json::to_string_pretty(&doc).map_err(|e| JsValue::from_str(&e.to_string()))
    }

    fn generate_openapi(&self) -> Result<String, JsValue> {
        // Generate OpenAPI specification
        let mut spec = String::new();
        spec.push_str("openapi: 3.0.0\n");
        spec.push_str(&format!(
            "info:\n  title: {}\n  version: {}\n",
            self.config.title, self.config.version
        ));
        spec.push_str("paths:\n");

        // Add API endpoints based on functions
        for node in &self.ts_nodes {
            if matches!(node.node_type, TSNodeType::Function) {
                spec.push_str(&format!(
                    "  /{}:\n    post:\n      summary: {}\n",
                    node.name,
                    node.description.as_deref().unwrap_or(&node.name)
                ));
            }
        }

        Ok(spec)
    }

    fn generate_typedoc(&self) -> Result<String, JsValue> {
        // Generate TypeDoc compatible JSON
        self.generate_json()
    }

    fn build_navigation(&self) -> DocNavigation {
        let mut sections = Vec::new();

        // Group by node type
        let interface_items: Vec<_> = self
            .ts_nodes
            .iter()
            .filter(|n| matches!(n.node_type, TSNodeType::Interface))
            .map(|n| NavItem {
                title: n.name.clone(),
                link: format!("#{name}", name = n.name.to_lowercase()),
                children: Vec::new(),
            })
            .collect();

        if !interface_items.is_empty() {
            sections.push(NavSection {
                title: "Interfaces".to_string(),
                items: interface_items,
            });
        }

        let class_items: Vec<_> = self
            .ts_nodes
            .iter()
            .filter(|n| matches!(n.node_type, TSNodeType::Class))
            .map(|n| NavItem {
                title: n.name.clone(),
                link: format!("#{name}", name = n.name.to_lowercase()),
                children: Vec::new(),
            })
            .collect();

        if !class_items.is_empty() {
            sections.push(NavSection {
                title: "Classes".to_string(),
                items: class_items,
            });
        }

        DocNavigation { sections }
    }

    fn format_interface_doc(&self, interface: &TSNode) -> String {
        let mut doc = String::new();
        doc.push_str(&format!("## {name}\n\n", name = interface.name));

        if let Some(ref description) = interface.description {
            doc.push_str(&format!("{description}\n\n"));
        }

        if !interface.properties.is_empty() {
            doc.push_str("### Properties\n\n");
            for prop in &interface.properties {
                doc.push_str(&format!(
                    "- **{}**{}: `{}` {}\n",
                    prop.name,
                    if prop.optional { "?" } else { "" },
                    prop.property_type,
                    if prop.readonly { "(readonly)" } else { "" }
                ));

                if let Some(ref desc) = prop.description {
                    doc.push_str(&format!("  {desc}\n"));
                }
            }
            doc.push('\n');
        }

        doc
    }

    fn format_class_doc(&self, class: &TSNode) -> String {
        let mut doc = String::new();
        doc.push_str(&format!("## {name}\n\n", name = class.name));

        if let Some(ref description) = class.description {
            doc.push_str(&format!("{description}\n\n"));
        }

        doc
    }

    fn format_function_doc(&self, function: &TSNode) -> String {
        let mut doc = String::new();
        doc.push_str(&format!("## {name}\n\n", name = function.name));

        if let Some(ref description) = function.description {
            doc.push_str(&format!("{description}\n\n"));
        }

        if !function.parameters.is_empty() {
            doc.push_str("### Parameters\n\n");
            for param in &function.parameters {
                doc.push_str(&format!(
                    "- **{}**{}: `{}`\n",
                    param.name,
                    if param.optional { "?" } else { "" },
                    param.param_type
                ));

                if let Some(ref desc) = param.description {
                    doc.push_str(&format!("  {desc}\n"));
                }
            }
            doc.push('\n');
        }

        if let Some(ref return_type) = function.return_type {
            doc.push_str(&format!("### Returns\n\n`{return_type}`\n\n"));
        }

        doc
    }

    fn format_enum_doc(&self, enum_node: &TSNode) -> String {
        let mut doc = String::new();
        doc.push_str(&format!("## {name}\n\n", name = enum_node.name));

        if let Some(ref description) = enum_node.description {
            doc.push_str(&format!("{description}\n\n"));
        }

        doc
    }

    fn generate_html_navigation(&self) -> String {
        let nav = self.build_navigation();
        let mut html = String::new();

        html.push_str("<ul>");
        for section in &nav.sections {
            html.push_str(&format!(
                "<li><strong>{title}</strong><ul>",
                title = section.title
            ));
            for item in &section.items {
                html.push_str(&format!(
                    "<li><a href=\"{}\">{}</a></li>",
                    item.link, item.title
                ));
            }
            html.push_str("</ul></li>");
        }
        html.push_str("</ul>");

        html
    }

    fn get_html_styles(&self) -> String {
        match self.config.theme {
            DocTheme::Default => self.get_default_styles(),
            DocTheme::Dark => self.get_dark_styles(),
            DocTheme::Material => self.get_material_styles(),
            DocTheme::Bootstrap => self.get_bootstrap_styles(),
            DocTheme::Minimal => self.get_minimal_styles(),
        }
    }

    fn get_default_styles(&self) -> String {
        r#"
<style>
    body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; margin: 0; background: #fff; }
    .container { max-width: 1200px; margin: 0 auto; padding: 20px; }
    header { text-align: center; margin-bottom: 2rem; }
    nav { background: #f8f9fa; padding: 1rem; border-radius: 8px; margin-bottom: 2rem; }
    nav ul { list-style: none; padding: 0; }
    nav li { margin: 0.5rem 0; }
    nav a { text-decoration: none; color: #0366d6; }
    h1, h2, h3 { color: #24292e; }
    code { background: #f6f8fa; padding: 2px 4px; border-radius: 3px; font-family: monospace; }
    pre { background: #f6f8fa; padding: 1rem; border-radius: 6px; overflow-x: auto; }
</style>
"#.to_string()
    }

    fn get_dark_styles(&self) -> String {
        r#"
<style>
    body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; margin: 0; background: #0d1117; color: #c9d1d9; }
    .container { max-width: 1200px; margin: 0 auto; padding: 20px; }
    header { text-align: center; margin-bottom: 2rem; }
    nav { background: #161b22; padding: 1rem; border-radius: 8px; margin-bottom: 2rem; border: 1px solid #30363d; }
    nav ul { list-style: none; padding: 0; }
    nav li { margin: 0.5rem 0; }
    nav a { text-decoration: none; color: #58a6ff; }
    h1, h2, h3 { color: #f0f6fc; }
    code { background: #161b22; padding: 2px 4px; border-radius: 3px; font-family: monospace; }
    pre { background: #161b22; padding: 1rem; border-radius: 6px; overflow-x: auto; border: 1px solid #30363d; }
</style>
"#.to_string()
    }

    fn get_material_styles(&self) -> String {
        r#"
<style>
    body { font-family: 'Roboto', sans-serif; margin: 0; background: #fafafa; }
    .container { max-width: 1200px; margin: 0 auto; padding: 20px; }
    header { text-align: center; margin-bottom: 2rem; background: #2196f3; color: white; padding: 2rem; border-radius: 4px; }
    nav { background: white; padding: 1rem; border-radius: 4px; margin-bottom: 2rem; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
    nav ul { list-style: none; padding: 0; }
    nav li { margin: 0.5rem 0; }
    nav a { text-decoration: none; color: #2196f3; }
    h1, h2, h3 { color: #212121; }
    code { background: #e8eaf6; padding: 2px 4px; border-radius: 3px; font-family: monospace; }
    pre { background: #e8eaf6; padding: 1rem; border-radius: 4px; overflow-x: auto; }
</style>
"#.to_string()
    }

    fn get_bootstrap_styles(&self) -> String {
        r#"
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
    .container { margin-top: 2rem; }
    nav { background: #f8f9fa; padding: 1rem; border-radius: 0.375rem; margin-bottom: 2rem; }
    code { color: #e83e8c; }
</style>
"#.to_string()
    }

    fn get_minimal_styles(&self) -> String {
        r#"
<style>
    body { font-family: Georgia, serif; margin: 0; background: white; line-height: 1.6; }
    .container { max-width: 800px; margin: 0 auto; padding: 20px; }
    header { margin-bottom: 2rem; }
    nav { margin-bottom: 2rem; }
    nav ul { list-style: none; padding: 0; }
    nav a { text-decoration: none; color: #000; border-bottom: 1px dotted; }
    h1, h2, h3 { font-weight: normal; }
    code { font-family: 'Courier New', monospace; }
</style>
"#
        .to_string()
    }

    fn get_html_scripts(&self) -> String {
        r##"
<script>
    // Add interactive features
    document.addEventListener('DOMContentLoaded', function() {
        // Smooth scrolling for navigation links
        const links = document.querySelectorAll('nav a[href^="#"]');
        links.forEach(link => {
            link.addEventListener('click', function(e) {
                e.preventDefault();
                const target = document.querySelector(this.getAttribute('href'));
                if (target) {
                    target.scrollIntoView({ behavior: 'smooth' });
                }
            });
        });
    });
</script>
"##
        .to_string()
    }
}

/// Create a documentation generator with default settings
#[wasm_bindgen]
pub fn create_default_doc_generator() -> AutoDocGenerator {
    AutoDocGenerator::new(DocConfig::new())
}

/// Create a documentation generator for HTML output
#[wasm_bindgen]
pub fn create_html_doc_generator() -> AutoDocGenerator {
    let mut config = DocConfig::new();
    config.set_format(DocFormat::HTML);
    AutoDocGenerator::new(config)
}

/// Create a documentation generator for Markdown output
#[wasm_bindgen]
pub fn create_markdown_doc_generator() -> AutoDocGenerator {
    let mut config = DocConfig::new();
    config.set_format(DocFormat::Markdown);
    AutoDocGenerator::new(config)
}

/// Version and build information for the trustformers-wasm crate
#[wasm_bindgen]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VersionInfo {
    /// Crate version
    version: String,
    /// Git commit hash (if available)
    git_hash: String,
    /// Build target
    target: String,
    /// Build profile (debug/release)
    profile: String,
    /// Build timestamp
    build_time: String,
    /// Enabled features
    features: Vec<String>,
}

#[wasm_bindgen]
impl VersionInfo {
    #[wasm_bindgen(getter)]
    pub fn version(&self) -> String {
        self.version.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn git_hash(&self) -> String {
        self.git_hash.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn target(&self) -> String {
        self.target.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn profile(&self) -> String {
        self.profile.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn build_time(&self) -> String {
        self.build_time.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn features(&self) -> Array {
        self.features.iter().map(|f| JsValue::from(f.as_str())).collect()
    }
}

/// Get version and build information for the trustformers-wasm crate
#[wasm_bindgen]
pub fn get_version_info() -> VersionInfo {
    VersionInfo {
        version: env!("CARGO_PKG_VERSION").to_string(),
        git_hash: option_env!("GIT_HASH").unwrap_or("unknown").to_string(),
        target: std::env::consts::ARCH.to_string() + "-" + std::env::consts::OS,
        profile: if cfg!(debug_assertions) { "debug".to_string() } else { "release".to_string() },
        build_time: option_env!("BUILD_TIME").unwrap_or("unknown").to_string(),
        features: get_enabled_features(),
    }
}

/// Get list of enabled Cargo features
#[allow(clippy::vec_init_then_push)] // Needed for cfg-gated feature collection
fn get_enabled_features() -> Vec<String> {
    let mut features = Vec::new();

    #[cfg(feature = "console_panic")]
    features.push("console_panic".to_string());

    #[cfg(feature = "webgpu")]
    features.push("webgpu".to_string());

    #[cfg(feature = "web-workers")]
    features.push("web-workers".to_string());

    #[cfg(feature = "shared-memory")]
    features.push("shared-memory".to_string());

    #[cfg(feature = "kernel-fusion")]
    features.push("kernel-fusion".to_string());

    #[cfg(feature = "async-executor")]
    features.push("async-executor".to_string());

    #[cfg(feature = "indexeddb")]
    features.push("indexeddb".to_string());

    #[cfg(feature = "memory64")]
    features.push("memory64".to_string());

    #[cfg(feature = "streaming-loader")]
    features.push("streaming-loader".to_string());

    #[cfg(feature = "model-splitting")]
    features.push("model-splitting".to_string());

    #[cfg(feature = "react-components")]
    features.push("react-components".to_string());

    #[cfg(feature = "vue-components")]
    features.push("vue-components".to_string());

    #[cfg(feature = "angular-components")]
    features.push("angular-components".to_string());

    #[cfg(feature = "web-components")]
    features.push("web-components".to_string());

    #[cfg(feature = "playground")]
    features.push("playground".to_string());

    #[cfg(feature = "streaming-generation")]
    features.push("streaming-generation".to_string());

    #[cfg(feature = "mobile-optimization")]
    features.push("mobile-optimization".to_string());

    #[cfg(feature = "dlmalloc-alloc")]
    features.push("dlmalloc-alloc".to_string());

    features
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_doc_config() {
        let mut config = DocConfig::new();
        assert_eq!(config.format, DocFormat::HTML);
        assert_eq!(config.theme, DocTheme::Default);

        config.set_format(DocFormat::Markdown);
        assert_eq!(config.format, DocFormat::Markdown);
    }

    #[test]
    fn test_typescript_parsing() {
        let mut generator = AutoDocGenerator::new(DocConfig::new());

        let typescript = r#"
export interface TestInterface {
  name: string;
  optional?: number;
}
        "#;

        let result = generator.parse_typescript(typescript);
        assert!(result.is_ok());
        assert_eq!(generator.ts_nodes.len(), 1);
        assert_eq!(generator.ts_nodes[0].name, "TestInterface");
    }

    #[test]
    fn test_documentation_generation() {
        let generator = AutoDocGenerator::new(DocConfig::new());
        let doc = generator.generate_documentation();
        assert!(doc.is_ok());
    }
}