File size: 35,471 Bytes
9fba84c
1ab1003
 
 
 
 
 
61be1ca
1ab1003
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61be1ca
 
 
 
 
 
 
 
977a02a
 
 
61be1ca
 
 
 
977a02a
 
 
 
61be1ca
977a02a
 
 
61be1ca
 
977a02a
 
61be1ca
 
1ab1003
977a02a
61be1ca
 
977a02a
1ab1003
 
 
 
 
 
196a3fa
1ab1003
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
977a02a
 
61be1ca
977a02a
1ab1003
 
 
61be1ca
1ab1003
 
 
 
977a02a
 
61be1ca
977a02a
1ab1003
 
 
 
977a02a
61be1ca
977a02a
1ab1003
 
 
61be1ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ab1003
61be1ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ab1003
61be1ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ab1003
 
61be1ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ab1003
61be1ca
 
 
 
 
 
1ab1003
 
 
61be1ca
 
1ab1003
 
 
61be1ca
 
 
1ab1003
 
 
 
 
 
 
 
 
 
 
 
 
61be1ca
 
 
 
1ab1003
 
 
 
61be1ca
1ab1003
 
 
 
 
 
 
 
 
 
 
61be1ca
 
1ab1003
61be1ca
 
 
1ab1003
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61be1ca
 
 
 
 
 
1ab1003
 
 
 
61be1ca
 
1ab1003
 
 
 
 
61be1ca
 
1ab1003
 
 
 
 
 
 
 
61be1ca
1ab1003
 
 
 
 
 
61be1ca
 
977a02a
61be1ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
977a02a
 
1ab1003
61be1ca
1ab1003
 
 
9fba84c
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
<!doctype html>
<html lang="vi">
<head>
<meta charset="utf-8">
<title>Hệ thống Dịch Máy Đa Ngữ | Việt – CVNSS4.0 – Anh – Trung – Lào – Khmer</title>

<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Hệ thống dịch máy nghiên cứu – Việt, Anh, Trung, Lào, Khmer">

<style>
:root{
  --primary:#0b3c5d;
  --secondary:#e8edf2;
  --border:#c9d3dc;
  --text:#1a1a1a;
}

body{
  margin:0;
  font-family:"Segoe UI", Arial, sans-serif;
  background:#f5f7fa;
  color:var(--text);
}

header{
  background:var(--primary);
  color:#fff;
  padding:14px 24px;
  font-size:20px;
  font-weight:600;
}

main{
  max-width:1100px;
  margin:24px auto;
  background:#fff;
  border:1px solid var(--border);
  padding:20px;
}

.section-title{
  font-weight:600;
  margin-bottom:8px;
}

.controls{
  display:flex;
  gap:12px;
  margin-bottom:12px;
}

select, button{
  padding:8px 10px;
  font-size:14px;
}

textarea{
  width:100%;
  height:160px;
  border:1px solid var(--border);
  padding:10px;
  font-size:14px;
  resize:vertical;
}

button{
  background:var(--primary);
  color:#fff;
  border:none;
  cursor:pointer;
}

button:disabled{
  background:#999;
}

footer{
  text-align:center;
  font-size:12px;
  color:#666;
  margin:20px 0;
}

/* ===== BỔ SUNG: Nút sao chép (không ảnh hưởng logic CVNSS) ===== */
.textarea-wrapper{
  position:relative;
}
.textarea-wrapper textarea{
  box-sizing:border-box;
  padding-bottom:34px; /* chừa chỗ cho nút */
}
.copy-btn{
  position:absolute;
  right:8px;
  bottom:8px;
  background:var(--primary);
  color:#fff;
  border:none;
  font-size:12px;
  padding:4px 10px;
  border-radius:4px;
  cursor:pointer;
}
.copy-btn:active{
  transform:scale(0.98);
}
footer a{
  color:inherit;
  text-decoration:underline;
}
</style>
</head>

<body>

<header>
  HỆ THỐNG DỊCH MÁY ĐA NGỮ CVNSS4.0 (phiên bản 1.0.0)
</header>

<main>

<div class="section-title">Ngôn ngữ</div>
<div class="controls">
  <select id="sourceLang">
    <option value="vi">Tiếng Việt</option>
    <option value="cvnss">CVNSS4.0</option>
    <option value="en">English</option>
    <option value="zh">中文</option>
    <option value="lo">ພາສາລາວ</option>
    <option value="km">ភាសាខ្មែរ</option>
  </select>

  <span style="align-self:center"></span>

  <select id="targetLang">
    <option value="en">English</option>
    <option value="vi">Tiếng Việt</option>
    <option value="cvnss">CVNSS4.0</option>
    <option value="zh">中文</option>
    <option value="lo">ພາສາລາວ</option>
    <option value="km">ភាសាខ្មែរ</option>
  </select>
</div>

<div class="section-title">Văn bản nguồn</div>
<div class="textarea-wrapper">
  <textarea id="sourceText" placeholder="Nhập nội dung cần dịch..."></textarea>
  <button type="button" class="copy-btn" data-copy="sourceText">Sao chép</button>
</div>

<br><br>

<button id="translateBtn" type="button">DỊCH</button>

<br><br>

<div class="section-title">Kết quả dịch</div>
<div class="textarea-wrapper">
  <textarea id="targetText" readonly></textarea>
  <button type="button" class="copy-btn" data-copy="targetText">Sao chép</button>
</div>

</main>

<footer>
  Phiên bản Demo | Thực hiện Team Long Ngo | 2.2026 |
  <a href="https://chuvnsongsong.com/" target="_blank" rel="noopener noreferrer">Tài trợ bởi Dự án CVNSS4.0</a>
  | Cố vấn bởi Trần Tư Bình và Kiều Trường Lâm
</footer>

<script>
/* ===== CVNSS4.0 Converter (embedded - giữ nguyên nội dung) ===== */
const CVNSSConverter = (function () {
  // Bảng ánh xạ ký tự đặc biệt
  const specialChars = [
    "`", "“", "”", "<", ">", "@", "-", ";", "=", "…", " ", ",", ".", "?", "!",
    '"', "'", "(", ")", "[", "]", "{", "}", "%", "#", "$", "&", "_", "\\", "/",
    "*", ":", "+", "~", "^", "|", "\r\n", "\r", "\n"
  ];

  // Bảng ánh xạ phụ âm
  const consonants = {
    cqn: ["ngh", "ng", "ch", "gh", "kh", "nh", "ph", "th", "tr", "gi", "qu", "b", "k", "d", "đ", "g", "h", "c", "l", "m", "n", "r", "s", "t", "v", "x"],
    cvn: ["w", "w", "ch", "g", "k", "nh", "f", "th", "tr", "j", "q", "b", "c", "z", "d", "g", "h", "c", "l", "m", "n", "r", "s", "t", "v", "x"]
  };

  // Bảng ánh xạ nguyên âm
  const vowels = {
    cqn: [
      "a", "à", "ả", "ã", "á", "ạ", "oa", "òa", "ỏa", "õa", "óa", "ọa", "oà", "oả", "oã", "oá", "oạ", "oác", "oạc", "oách", "oạch",
      "oai", "oài", "oải", "oãi", "oái", "oại", "oao", "oào", "oảo", "oão", "oáo", "oạo", "oáp", "oạp", "oát", "oạt", "oắt", "oặt", "oắc", "oặc",
      "oăn", "oằn", "oẳn", "oẵn", "oắn", "oặn", "oăm", "oằm", "oẳm", "oẵm", "oắm", "oặm", "oăng", "oằng", "oẳng", "oẵng", "oắng", "oặng", "oay", "oày",
      "oảy", "oãy", "oáy", "oạy", "ác", "ạc", "ách", "ạch", "ai", "ài", "ải", "ãi", "ái", "ại", "am", "àm", "ảm", "ãm", "ám", "ạm", "an", "àn",
      "ản", "ãn", "án", "ạn", "oan", "oàn", "oản", "oãn", "oán", "oạn", "oanh", "oành", "oảnh", "oãnh", "oánh", "oạnh", "ang", "àng", "ảng", "ãng",
      "áng", "ạng", "oang", "oàng", "oảng", "oãng", "oáng", "oạng", "anh", "ành", "ảnh", "ãnh", "ánh", "ạnh", "ao", "ào", "ảo", "ão", "áo", "ạo",
      "áp", "ạp", "át", "ạt", "au", "àu", "ảu", "áu", "ạu", "ay", "ày", "ảy", "ãy", "áy", "ạy", "ắc", "ặc", "ăm", "ằm", "ẳm", "ẵm", "ắm", "ặm",
      "ăn", "ằn", "ẳn", "ẵn", "ắn", "ặn", "ăng", "ằng", "ẳng", "ẵng", "ắng", "ặng", "ắp", "ặp", "ắt", "ặt", "ấc", "ậc", "âm", "ầm", "ẩm", "ẫm",
      "ấm", "ậm", "ân", "ần", "ẩn", "ẫn", "ấn", "ận", "âng", "ầng", "ẩng", "ẫng", "ấng", "ậng", "uân", "uần", "uẩn", "uẫn", "uấn", "uận", "uâng",
      "uầng", "uẩng", "uẫng", "uấng", "uậng", "ấp", "ập", "ất", "ật", "uất", "uật", "âu", "ầu", "ẩu", "ẫu", "ấu", "ậu", "ây", "ầy", "ẩy", "ẫy",
      "ấy", "ậy", "uây", "uầy", "uẩy", "uẫy", "uấy", "uậy", "e", "è", "ẻ", "ẽ", "é", "ẹ", "oe", "òe", "ỏe", "õe", "óe", "ọe", "éc", "ẹc", "em",
      "èm", "ẻm", "ẽm", "ém", "ẹm", "en", "èn", "ẻn", "ẽn", "én", "ẹn", "oen", "oèn", "oẻn", "oẽn", "oén", "oẹn", "eng", "èng", "ẻng", "ẽng",
      "éng", "ẹng", "eo", "èo", "ẻo", "ẽo", "éo", "ẹo", "oeo", "oèo", "oẻo", "oẽo", "oéo", "oẹo", "ép", "ẹp", "ét", "ẹt", "oét", "oẹt", "ê",
      "ề", "ể", "ễ", "ế", "ệ", "uê", "uề", "uể", "uễ", "uế", "uệ", "ếch", "ệch", "uếch", "uệch", "êm", "ềm", "ểm", "ễm", "ếm", "ệm", "ên", "ền",
      "ển", "ễn", "ến", "ện", "ênh", "ềnh", "ểnh", "ễnh", "ếnh", "ệnh", "uênh", "uềnh", "uểnh", "uễnh", "uếnh", "uệnh", "ếp", "ệp", "ết", "ệt",
      "êu", "ều", "ểu", "ễu", "ếu", "ệu", "i", "ì", "ỉ", "ĩ", "í", "ị", "uy", "ùy", "ủy", "ũy", "úy", "ụy", "uỳ", "uỷ", "uỹ", "uý", "uỵ", "ia",
      "ìa", "ỉa", "ĩa", "ía", "ịa", "uya", "íc", "ích", "ịch", "uých", "uỵch", "iếc", "iệc", "iêm", "iềm", "iểm", "iễm", "iếm", "iệm", "iên",
      "iền", "iển", "iễn", "iến", "iện", "uyên", "uyền", "uyển", "uyễn", "uyến", "uyện", "iêng", "iềng", "iểng", "iễng", "iếng", "iệng", "iếp",
      "iệp", "iết", "iệt", "uyết", "uyệt", "iêu", "iều", "iểu", "iễu", "iếu", "iệu", "yêt", "yệt", "yên", "yền", "yển", "yễn", "yến", "yện",
      "yêm", "yềm", "yểm", "yễm", "yếm", "yệm", "yêng", "yềng", "yểng", "yễng", "yếng", "yệnh", "yêu", "yều", "yểu", "yễu", "yếu", "yệu", "im",
      "ìm", "ỉm", "ĩm", "ím", "ịm", "in", "ìn", "ỉn", "ĩn", "ín", "ịn", "inh", "ình", "ỉnh", "ĩnh", "ính", "ịnh", "uynh", "uỳnh", "uỷnh", "uỹnh",
      "uýnh", "uỵnh", "íp", "ịp", "uýp", "uỵp", "ít", "ịt", "uýt", "uỵt", "iu", "ìu", "ỉu", "ĩu", "íu", "ịu", "uyu", "uỳu", "uỷu", "uỹu", "uýu",
      "uỵu", "uỳn", "uỷn", "uỹn", "uýn", "uỵn", "o", "ò", "ỏ", "õ", "ó", "ọ", "óc", "ọc", "oi", "òi", "ỏi", "õi", "ói", "ọi", "om", "òm", "ỏm",
      "õm", "óm", "ọm", "on", "òn", "ỏn", "õn", "ón", "ọn", "ong", "òng", "ỏng", "õng", "óng", "ọng", "oóc", "oong", "oòng", "oỏng", "oõng",
      "oóng", "oòng", "oọng", "óp", "ọp", "ót", "ọt", "ô", "ồ", "ổ", "ỗ", "ố", "ộ", "ốc", "ộc", "ôi", "ồi", "ổi", "ỗi", "ối", "ội", "ôm", "ồm",
      "ổm", "ỗm", "ốm", "ộm", "ôn", "ồn", "ổn", "ỗn", "ốn", "ộn", "ông", "ồng", "ổng", "ỗng", "ống", "ộng", "ốp", "ộp", "ốt", "ột", "ơ", "ờ",
      "ở", "ỡ", "ớ", "ợ", "ơi", "ời", "ởi", "ỡi", "ới", "ợi", "ơm", "ờm", "ởm", "ỡm", "ớm", "ợm", "ơn", "ờn", "ởn", "ỡn", "ớn", "ợn", "ơng",
      "ờng", "ởng", "ỡng", "ớng", "ợng", "ớp", "ợp", "ớt", "ợt", "u", "ù", "ủ", "ũ", "ú", "ụ", "ua", "ùa", "ủa", "ũa", "úa", "ụa", "úc", "ục",
      "ui", "ùi", "ủi", "ũi", "úi", "ụi", "um", "ùm", "ủm", "ũm", "úm", "ụm", "un", "ùn", "ủn", "ũn", "ún", "ụn", "ung", "ùng", "ủng", "ũng",
      "úng", "ụng", "uơ", "uờ", "uở", "uỡ", "uớ", "uợ", "uơn", "uờn", "uởn", "uỡn", "uớn", "uợn", "uớt", "uợt", "uốc", "uộc", "uôi", "uồi", "uổi",
      "uỗi", "uối", "uội", "uôm", "uồm", "uổm", "uỗm", "uốm", "uộm", "uôn", "uồn", "uổn", "uỗn", "uốn", "uộn", "uông", "uồng", "uổng", "uỗng",
      "uống", "uộng", "uốt", "uột", "uốp", "uộp", "úp", "ụp", "út", "ụt", "ư", "ừ", "ử", "ữ", "ứ", "ự", "ưa", "ừa", "ửa", "ữa", "ứa", "ựa",
      "ức", "ực", "ưi", "ừi", "ửi", "ữi", "ứi", "ựi", "ưm", "ừm", "ửm", "ữm", "ứm", "ựm", "ưn", "ừn", "ửn", "ữn", "ứn", "ựn", "ưng", "ừng",
      "ửng", "ững", "ứng", "ựng", "ước", "ược", "ươi", "ười", "ưởi", "ưỡi", "ưới", "ượi", "ươm", "ườm", "ưởm", "ưỡm", "ướm", "ượm", "ươn",
      "ườn", "ưởn", "ưỡn", "ướn", "ượn", "ương", "ường", "ưởng", "ưỡng", "ướng", "ượng", "ướp", "ượp", "ướt", "ượt", "ươu", "ườu", "ưởu",
      "ưỡu", "ướu", "ượu", "ứt", "ựt", "ưu", "ừu", "ửu", "ữu", "ứu", "ựu", "y", "ỳ", "ỷ", "ỹ", "ý", "ỵ", "ỳa", "ỷa", "ỹa", "ýa", "ỵa"
    ],
    cvn: [
      "a", "al", "az", "as", "aj", "ar", "oa", "oal", "oaz", "oas", "oaj", "oar", "oal", "oaz", "oas", "oaj", "oar", "osj", "osr", "oakj", "oakr",
      "ojp", "ojl", "ojz", "ojs", "ojj", "ojr", "owp", "owl", "owz", "ows", "owj", "owr", "ofj", "ofr", "odj", "odr", "adx", "adh", "asx", "ash",
      "alo", "alk", "alv", "alw", "alx", "alh", "avo", "avk", "avv", "avw", "avx", "avh", "azo", "azk", "azv", "azw", "azx", "azh", "ajp", "ajl",
      "ajz", "ajs", "ajj", "ajr", "ac", "acr", "akj", "akr", "ai", "ail", "aiz", "ais", "aij", "air", "am", "aml", "amz", "ams", "amj", "amr",
      "an", "anl", "anz", "ans", "anj", "anr", "olp", "oll", "olz", "ols", "olj", "olr", "oahp", "oahl", "oahz", "oahs", "oahj", "oahr", "agp",
      "agl", "agz", "ags", "agj", "agr", "ozp", "ozl", "ozz", "ozs", "ozj", "ozr", "ahp", "ahl", "ahz", "ahs", "ahj", "ahr", "ao", "aol", "aoz",
      "aos", "aoj", "aor", "ap", "apr", "at", "atr", "au", "aul", "auz", "auj", "aur", "ay", "ayl", "ayz", "ays", "ayj", "ayr", "acx", "ach",
      "amo", "amk", "amv", "amw", "amx", "amh", "ano", "ank", "anv", "anw", "anx", "anh", "ago", "agk", "agv", "agw", "agx", "agh", "apx", "aph",
      "atx", "ath", "acb", "acf", "amy", "amd", "amq", "amg", "amb", "amf", "any", "and", "anq", "ang", "anb", "anf", "agy", "agd", "agq", "agg",
      "agb", "agf", "aly", "ald", "alq", "alg", "alb", "alf", "azy", "azd", "azq", "azg", "azb", "azf", "apb", "apf", "atb", "atf", "adb", "adf",
      "auy", "aud", "auq", "aug", "aub", "auf", "ayy", "ayd", "ayq", "ayg", "ayb", "ayf", "ajy", "ajd", "ajq", "ajg", "ajb", "ajf", "e", "el",
      "ez", "es", "ej", "er", "oe", "oel", "oez", "oes", "oej", "oer", "ec", "ecr", "em", "eml", "emz", "ems", "emj", "emr", "en", "enl", "enz",
      "ens", "enj", "enr", "elp", "ell", "elz", "els", "elj", "elr", "egp", "egl", "egz", "egs", "egj", "egr", "eo", "eol", "eoz", "eos", "eoj",
      "eor", "ewp", "ewl", "ewz", "ews", "ewj", "ewr", "ep", "epr", "et", "etr", "edj", "edr", "ey", "ed", "eq", "eg", "eb", "ef", "uey", "ued",
      "ueq", "ueg", "ueb", "uef", "ekb", "ekf", "uekb", "uekf", "emy", "emd", "emq", "emg", "emb", "emf", "eny", "end", "enq", "eng", "enb", "enf",
      "ehy", "ehd", "ehq", "ehg", "ehb", "ehf", "uehy", "uehd", "uehq", "uehg", "uehb", "uehf", "epb", "epf", "etb", "etf", "euy", "eud", "euq",
      "eug", "eub", "euf", "i", "il", "iz", "is", "ij", "ir", "y", "yl", "yz", "ys", "yj", "yr", "yl", "yz", "ys", "yj", "yr", "ia", "ial", "iaz",
      "ias", "iaj", "iar", "ya", "ic", "ikj", "ikr", "ykj", "ykr", "isb", "isf", "ivy", "ivd", "ivq", "ivg", "ivb", "ivf", "ily", "ild", "ilq",
      "ilg", "ilb", "ilf", "yly", "yld", "ylq", "ylg", "ylb", "ylf", "izy", "izd", "izq", "izg", "izb", "izf", "ifb", "iff", "idb", "idf", "ydb",
      "ydf", "iwy", "iwd", "iwq", "iwg", "iwb", "iwf", "idb", "idf", "ily", "ild", "ilq", "ilg", "ilb", "ilf", "ivy", "ivd", "ivq", "ivg", "ivb",
      "ivf", "izy", "izd", "izq", "izg", "izb", "izf", "iwy", "iwd", "iwq", "iwg", "iwb", "iwf", "im", "iml", "imz", "ims", "imj", "imr", "in",
      "inl", "inz", "ins", "inj", "inr", "ihp", "ihl", "ihz", "ihs", "ihj", "ihr", "yhp", "yhl", "yhz", "yhs", "yhj", "yhr", "ip", "ipr", "yp",
      "ypr", "it", "itr", "yt", "ytr", "iu", "iul", "iuz", "ius", "iuj", "iur", "yu", "yul", "yuz", "yus", "yuj", "yur", "ynl", "ynz", "yns",
      "ynj", "ynr", "o", "ol", "oz", "os", "oj", "or", "oc", "ocr", "oi", "oil", "oiz", "ois", "oij", "oir", "om", "oml", "omz", "oms", "omj",
      "omr", "on", "onl", "onz", "ons", "onj", "onr", "ogp", "ogl", "ogz", "ogs", "ogj", "ogr", "ooc", "oog", "oogl", "oogz", "oogs", "oogj",
      "oogl", "oogr", "op", "opr", "ot", "otr", "oy", "od", "oq", "og", "ob", "of", "ocb", "ocf", "oiy", "oid", "oiq", "oig", "oib", "oif",
      "omy", "omd", "omq", "omg", "omb", "omf", "ony", "ond", "onq", "ong", "onb", "onf", "ogy", "ogd", "ogq", "ogg", "ogb", "ogf", "opb",
      "opf", "otb", "otf", "oo", "ok", "ov", "ow", "ox", "oh", "oio", "oik", "oiv", "oiw", "oix", "oih", "omo", "omk", "omv", "omw", "omx",
      "omh", "ono", "onk", "onv", "onw", "onx", "onh", "ogo", "ogk", "ogv", "ogw", "ogx", "ogh", "opx", "oph", "otx", "oth", "u", "ul", "uz",
      "us", "uj", "ur", "ua", "ual", "uaz", "uas", "uaj", "uar", "uc", "ucr", "ui", "uil", "uiz", "uis", "uij", "uir", "um", "uml", "umz", "ums",
      "umj", "umr", "un", "unl", "unz", "uns", "unj", "unr", "ugp", "ugl", "ugz", "ugs", "ugj", "ugr", "uoo", "uok", "uov", "uow", "uox", "uoh",
      "olo", "olk", "olv", "olw", "olx", "olh", "odx", "odh", "usb", "usf", "ujy", "ujd", "ujq", "ujg", "ujb", "ujf", "uvy", "uvd", "uvq", "uvg",
      "uvb", "uvf", "uly", "uld", "ulq", "ulg", "ulb", "ulf", "uzy", "uzd", "uzq", "uzg", "uzb", "uzf", "udb", "udf", "ufb", "uff", "up", "upr",
      "ut", "utr", "uo", "uk", "uv", "uw", "ux", "uh", "uao", "uak", "uav", "uaw", "uax", "uah", "ucx", "uch", "uio", "uik", "uiv", "uiw", "uix",
      "uih", "umo", "umk", "umv", "umw", "umx", "umh", "uno", "unk", "unv", "unw", "unx", "unh", "ugo", "ugk", "ugv", "ugw", "ugx", "ugh", "usx",
      "ush", "ujo", "ujk", "ujv", "ujw", "ujx", "ujh", "uvo", "uvk", "uvv", "uvw", "uvx", "uvh", "ulo", "ulk", "ulv", "ulw", "ulx", "ulh", "uzo",
      "uzk", "uzv", "uzw", "uzx", "uzh", "ufx", "ufh", "udx", "udh", "uwo", "uwk", "uwv", "uww", "uwx", "uwh", "utx", "uth", "uuo", "uuk", "uuv",
      "uuw", "uux", "uuh", "i", "il", "iz", "is", "ij", "ir", "ial", "iaz", "ias", "iaj", "iar"
    ],
    cvss: [
      "a", "al", "az", "as", "aj", "ar", "oa", "oal", "oaz", "oas", "oaj", "oar", "oal", "oaz", "oas", "oaj", "oar", "osj", "osr", "oakj", "oakr",
      "ojp", "ojl", "ojz", "ojs", "ojj", "ojr", "owp", "owl", "owz", "ows", "owj", "owr", "ofj", "ofr", "odj", "odr", "adx", "adh", "asx", "ash",
      "alo", "alk", "alv", "alw", "alx", "alh", "avo", "avk", "avv", "avw", "avx", "avh", "azo", "azk", "azv", "azw", "azx", "azh", "ajp", "ajl",
      "ajz", "ajs", "ajj", "ajr", "ac", "acr", "akj", "akr", "ai", "ail", "aiz", "ais", "aij", "air", "am", "aml", "amz", "ams", "amj", "amr",
      "an", "anl", "anz", "ans", "anj", "anr", "olp", "oll", "olz", "ols", "olj", "olr", "oahp", "oahl", "oahz", "oahs", "oahj", "oahr", "agp",
      "agl", "agz", "ags", "agj", "agr", "ozp", "ozl", "ozz", "ozs", "ozj", "ozr", "ahp", "ahl", "ahz", "ahs", "ahj", "ahr", "ao", "aol", "aoz",
      "aos", "aoj", "aor", "ap", "apr", "at", "atr", "au", "aul", "auz", "auj", "aur", "ay", "ayl", "ayz", "ays", "ayj", "ayr", "acx", "ach",
      "amo", "amk", "amv", "amw", "amx", "amh", "ano", "ank", "anv", "anw", "anx", "anh", "ago", "agk", "agv", "agw", "agx", "agh", "apx", "aph",
      "atx", "ath", "acb", "acf", "amy", "amd", "amq", "amg", "amb", "amf", "any", "and", "anq", "ang", "anb", "anf", "agy", "agd", "agq", "agg",
      "agb", "agf", "aly", "ald", "alq", "alg", "alb", "alf", "azy", "azd", "azq", "azg", "azb", "azf", "apb", "apf", "atb", "atf", "adb", "adf",
      "auy", "aud", "auq", "aug", "aub", "auf", "ayy", "ayd", "ayq", "ayg", "ayb", "ayf", "ajy", "ajd", "ajq", "ajg", "ajb", "ajf", "e", "el",
      "ez", "es", "ej", "er", "oe", "oel", "oez", "oes", "oej", "oer", "ec", "ecr", "em", "eml", "emz", "ems", "emj", "emr", "en", "enl", "enz",
      "ens", "enj", "enr", "elp", "ell", "elz", "els", "elj", "elr", "egp", "egl", "egz", "egs", "egj", "egr", "eo", "eol", "eoz", "eos", "eoj",
      "eor", "ewp", "ewl", "ewz", "ews", "ewj", "ewr", "ep", "epr", "et", "etr", "edj", "edr", "ey", "ed", "eq", "eg", "eb", "ef", "uey", "ued",
      "ueq", "ueg", "ueb", "uef", "ekb", "ekf", "uekb", "uekf", "emy", "emd", "emq", "emg", "emb", "emf", "eny", "end", "enq", "eng", "enb", "enf",
      "ehy", "ehd", "ehq", "ehg", "ehb", "ehf", "uehy", "uehd", "uehq", "uehg", "uehb", "uehf", "epb", "epf", "etb", "etf", "euy", "eud", "euq",
      "eug", "eub", "euf", "i", "il", "iz", "is", "ij", "ir", "y", "yl", "yz", "ys", "yj", "yr", "yl", "yz", "ys", "yj", "yr", "ia", "ial", "iaz",
      "ias", "iaj", "iar", "ya", "ic", "ikj", "ikr", "ykj", "ykr", "isb", "isf", "ivy", "ivd", "ivq", "ivg", "ivb", "ivf", "ily", "ild", "ilq",
      "ilg", "ilb", "ilf", "yly", "yld", "ylq", "ylg", "ylb", "ylf", "izy", "izd", "izq", "izg", "izb", "izf", "ifb", "iff", "idb", "idf", "ydb",
      "ydf", "iwy", "iwd", "iwq", "iwg", "iwb", "iwf", "idb", "idf", "ily", "ild", "ilq", "ilg", "ilb", "ilf", "ivy", "ivd", "ivq", "ivg", "ivb",
      "ivf", "izy", "izd", "izq", "izg", "izb", "izf", "iwy", "iwd", "iwq", "iwg", "iwb", "iwf", "im", "iml", "imz", "ims", "imj", "imr", "in",
      "inl", "inz", "ins", "inj", "inr", "ihp", "ihl", "ihz", "ihs", "ihj", "ihr", "yhp", "yhl", "yhz", "yhs", "yhj", "yhr", "ip", "ipr", "yp",
      "ypr", "it", "itr", "yt", "ytr", "iu", "iul", "iuz", "ius", "iuj", "iur", "yu", "yul", "yuz", "yus", "yuj", "yur", "ynl", "ynz", "yns",
      "ynj", "ynr", "o", "ol", "oz", "os", "oj", "or", "oc", "ocr", "oi", "oil", "oiz", "ois", "oij", "oir", "om", "oml", "omz", "oms", "omj",
      "omr", "on", "onl", "onz", "ons", "onj", "onr", "ogp", "ogl", "ogz", "ogs", "ogj", "ogr", "ooc", "oog", "oogl", "oogz", "oogs", "oogj",
      "oogl", "oogr", "op", "opr", "ot", "otr", "oy", "od", "oq", "og", "ob", "of", "ocb", "ocf", "oiy", "oid", "oiq", "oig", "oib", "oif",
      "omy", "omd", "omq", "omg", "omb", "omf", "ony", "ond", "onq", "ong", "onb", "onf", "ogy", "ogd", "ogq", "ogg", "ogb", "ogf", "opb",
      "opf", "otb", "otf", "oo", "ok", "ov", "ow", "ox", "oh", "oio", "oik", "oiv", "oiw", "oix", "oih", "omo", "omk", "omv", "omw", "omx",
      "omh", "ono", "onk", "onv", "onw", "onx", "onh", "ogo", "ogk", "ogv", "ogw", "ogx", "ogh", "opx", "oph", "otx", "oth", "u", "ul", "uz",
      "us", "uj", "ur", "ua", "ual", "uaz", "uas", "uaj", "uar", "uc", "ucr", "ui", "uil", "uiz", "uis", "uij", "uir", "um", "uml", "umz", "ums",
      "umj", "umr", "un", "unl", "unz", "uns", "unj", "unr", "ugp", "ugl", "ugz", "ugs", "ugj", "ugr", "uoo", "uok", "uov", "uow", "uox", "uoh",
      "olo", "olk", "olv", "olw", "olx", "olh", "odx", "odh", "usb", "usf", "ujy", "ujd", "ujq", "ujg", "ujb", "ujf", "uvy", "uvd", "uvq", "uvg",
      "uvb", "uvf", "uly", "uld", "ulq", "ulg", "ulb", "ulf", "uzy", "uzd", "uzq", "uzg", "uzb", "uzf", "udb", "udf", "ufb", "uff", "up", "upr",
      "ut", "utr", "uo", "uk", "uv", "uw", "ux", "uh", "uao", "uak", "uav", "uaw", "uax", "uah", "ucx", "uch", "uio", "uik", "uiv", "uiw", "uix",
      "uih", "umo", "umk", "umv", "umw", "umx", "umh", "uno", "unk", "unv", "unw", "unx", "unh", "ugo", "ugk", "ugv", "ugw", "ugx", "ugh", "usx",
      "ush", "ujo", "ujk", "ujv", "ujw", "ujx", "ujh", "uvo", "uvk", "uvv", "uvw", "uvx", "uvh", "ulo", "ulk", "ulv", "ulw", "ulx", "ulh", "uzo",
      "uzk", "uzv", "uzw", "uzx", "uzh", "ufx", "ufh", "udx", "udh", "uwo", "uwk", "uwv", "uww", "uwx", "uwh", "utx", "uth", "uuo", "uuk", "uuv",
      "uuw", "uux", "uuh", "i", "il", "iz", "is", "ij", "ir", "ial", "iaz", "ias", "iaj", "iar"
    ]
  };

  // Bảng ánh xạ nguyên âm cơ bản
  const baseVowels = [
    "aàảãáạ", "ăằẳẵắặ", "âầẩẫấậ", "eèẻẽéẹ", "êềểễếệ", "iìỉĩíị",
    "oòỏõóọ", "ôồổỗốộ", "ơờởỡớợ", "uùủũúụ", "ưừửữứự", "yỳỷỹýỵ"
  ];

  // Bảng ánh xạ thay thế đặc biệt
  const specialReplacements = {
    y: "yỳỷỹýỵ",
    i: "iìỉĩíị"
  };

  // Quy tắc điều chỉnh phụ âm
  const consonantAdjustments = {
    phu_am: ["ngh", "gh", "k"],
    phu_am_chuyen_doi: ["ng", "g", "c"],
    nguyen_am: "ieê"
  };

  // Hàm kiểm tra chữ in hoa
  function isUpperCase(str) {
    return /^[A-ZÀÁẢÃẠĂẰẮẲẴẶÂẦẤẨẪẬĐÈÉẺẼẸÊỀẾỂỄỆÌÍỈĨỊÒÓỎÕỌÔỒỐỔỖỘƠỜỚỞỠỢÙÚỦŨỤƯỪỨỬỮỰỲÝỶỸỴ]+$/.test(str);
  }

  // Hàm lấy nguyên âm cơ bản
  function getBaseVowel(char) {
    for (const vowelGroup of baseVowels) {
      if (vowelGroup.includes(char)) return vowelGroup[0];
    }
    return char;
  }

  // Hàm tách chuỗi thành token
  function splitString(str) {
    str = str.normalize("NFC");
    const tokens = str.split(/([\s|,|;|`|@|<|>|“|”|.|=|…|?|!|\\|'|"|(|)|[|\]|{|}|%|#|$|&|\-|_|/|*|:|+|~|^|||\r\n|\n|\r])/gm);
    return tokens.filter(token => token !== "");
  }

  // Hàm điều chỉnh phụ âm và nguyên âm
  function adjustConsonantVowel(cqnPad, cqnVan) {
    const firstChar = cqnVan[0] || "";
    if (cqnPad === "qu" && getBaseVowel(firstChar) === "u") {
      cqnPad = "q";
    }
    if (!cqnPad && getBaseVowel(firstChar) === "i") {
      cqnVan = cqnVan.replace(firstChar, specialReplacements.y[specialReplacements.i.indexOf(firstChar)]);
    }
    if (cqnPad === "gi" && getBaseVowel(firstChar) === "i") {
      cqnPad = "g";
    }
    if (consonantAdjustments.phu_am.includes(cqnPad) && !consonantAdjustments.nguyen_am.includes(getBaseVowel(firstChar))) {
      cqnPad = consonantAdjustments.phu_am_chuyen_doi[consonantAdjustments.phu_am.indexOf(cqnPad)];
    }
    return { cqnPad, cqnVan };
  }

  // Hàm chuyển từ CQN sang CVN và CVSS
  function cqnToCvnAndCvss(word) {
    const lowerWord = word.toLowerCase();
    let consonant = "", vowelPart = lowerWord, cvnConsonant = "", cqnResult = "", cvnResult = "", cvssResult = "";
    
    // Tìm phụ âm
    for (const c of consonants.cqn) {
      if (lowerWord.startsWith(c)) {
        consonant = c;
        vowelPart = lowerWord.replace(c, "");
        break;
      }
    }

    // Điều chỉnh đặc biệt
    if (consonant === "gi" && vowelPart !== "a" && vowels.cqn.includes("i" + vowelPart)) {
      vowelPart = "i" + vowelPart;
    }
    if (consonant === "qu" && getBaseVowel(vowelPart[0]) === "y") {
      vowelPart = "u" + vowelPart;
    }
    if (consonant === "g" && getBaseVowel(vowelPart[0]) === "i") {
      consonant = "gi";
    }

    // Ánh xạ phụ âm
    cvnConsonant = consonants.cqn.includes(consonant) ? consonants.cvn[consonants.cqn.indexOf(consonant)] : consonant;

    // Ánh xạ nguyên âm
    const vowelIndex = vowels.cqn.indexOf(vowelPart);
    if (vowelIndex !== -1) {
      cqnResult = vowelPart;
      cvnResult = vowels.cvn[vowelIndex];
      cvssResult = vowels.cvss[vowelIndex];
    } else {
      cqnResult = vowelPart;
      cvnResult = vowelPart;
      cvssResult = vowelPart;
    }

    // Kết hợp kết quả
    let cvnOutput = cvnConsonant + cvnResult;
    let cvssOutput = cvnConsonant + cvssResult;

    // Xử lý chữ hoa
    if (word[0] !== word[0].toLowerCase()) {
      if (cqnResult.length) cqnResult = cqnResult[0].toUpperCase() + cqnResult.slice(1);
      if (cvnOutput.length) cvnOutput = cvnOutput[0].toUpperCase() + cvnOutput.slice(1);
      if (cvssOutput.length) cvssOutput = cvssOutput[0].toUpperCase() + cvssOutput.slice(1);
    }
    if (isUpperCase(word)) {
      cqnResult = cqnResult.toUpperCase();
      cvnOutput = cvnOutput.toUpperCase();
      cvssOutput = cvssOutput.toUpperCase();
    }

    return { cqn: cqnResult, cvn: cvnOutput, cvss: cvssOutput };
  }

  // Hàm chuyển từ CVN sang CQN và CVSS
  function cvnToCqnAndCvss(word) {
    const lowerWord = word.toLowerCase();
    let consonant = "", vowelPart = lowerWord, cqnConsonant = "", cqnResult = "", cvnResult = "", cvssResult = "";

    // Tìm phụ âm
    for (const c of consonants.cvn) {
      if (lowerWord.startsWith(c)) {
        consonant = c;
        cqnConsonant = consonants.cqn[consonants.cvn.indexOf(c)];
        vowelPart = lowerWord.replace(c, "");
        break;
      }
    }

    // Ánh xạ nguyên âm
    const vowelIndex = vowels.cvn.indexOf(vowelPart);
    if (vowelIndex !== -1) {
      cqnResult = vowels.cqn[vowelIndex];
      cvnResult = vowelPart;
      cvssResult = vowels.cvss[vowelIndex];
    } else {
      cqnResult = vowelPart;
      cvnResult = vowelPart;
      cvssResult = vowelPart;
    }

    // Điều chỉnh đặc biệt
    if (consonant === "j" && vowelPart === "ịa") {
      cqnConsonant = "gi";
      cqnResult = "ỵa";
    }

    // Điều chỉnh phụ âm và nguyên âm
    const adjusted = adjustConsonantVowel(cqnConsonant, cqnResult);
    cqnConsonant = adjusted.cqnPad;
    cqnResult = adjusted.cqnVan;

    // Kết hợp kết quả
    let cqnOutput = cqnConsonant + cqnResult;
    let cvssOutput = consonant + cvssResult;

    // Xử lý chữ hoa
    if (word[0] !== word[0].toLowerCase()) {
      if (cqnOutput.length) cqnOutput = cqnOutput[0].toUpperCase() + cqnOutput.slice(1);
      if (cvnResult.length) cvnResult = cvnResult[0].toUpperCase() + cvnResult.slice(1);
      if (cvssOutput.length) cvssOutput = cvssOutput[0].toUpperCase() + cvssOutput.slice(1);
    }
    if (isUpperCase(word)) {
      cqnOutput = cqnOutput.toUpperCase();
      cvnResult = cvnResult.toUpperCase();
      cvssOutput = cvssOutput.toUpperCase();
    }

    return { cqn: cqnOutput, cvn: cvnResult, cvss: cvssOutput };
  }

  // Hàm chuyển từ CVSS sang CQN và CVN
  function cvssToCqnAndCvn(word) {
    const lowerWord = word.toLowerCase();
    let consonant = "", vowelPart = lowerWord, cqnConsonant = "", cqnResult = "", cvnResult = "", cvssResult = "";

    // Tìm phụ âm
    for (const c of consonants.cvn) {
      if (lowerWord.startsWith(c)) {
        consonant = c;
        cqnConsonant = consonants.cqn[consonants.cvn.indexOf(c)];
        vowelPart = lowerWord.replace(c, "");
        break;
      }
    }

    // Ánh xạ nguyên âm
    const vowelIndex = vowels.cvss.indexOf(vowelPart);
    if (vowelIndex !== -1) {
      cqnResult = vowels.cqn[vowelIndex];
      cvnResult = vowels.cvn[vowelIndex];
      cvssResult = vowelPart;
    } else {
      cqnResult = vowelPart;
      cvnResult = vowelPart;
      cvssResult = vowelPart;
    }

    // Điều chỉnh đặc biệt
    if (consonant === "j" && vowelPart === "iar") {
      cqnConsonant = "gi";
      cqnResult = "ỵa";
    }
    if (lowerWord === "it") {
      cqnResult = "ít";
    }
    if (lowerWord === "ikj") {
      cqnResult = "ích";
    }

    // Điều chỉnh phụ âm và nguyên âm
    const adjusted = adjustConsonantVowel(cqnConsonant, cqnResult);
    cqnConsonant = adjusted.cqnPad;
    cqnResult = adjusted.cqnVan;

    // Kết hợp kết quả
    let cqnOutput = cqnConsonant + cqnResult;
    let cvnOutput = consonant + cvnResult;

    // Xử lý chữ hoa
    if (word[0] !== word[0].toLowerCase()) {
      if (cqnOutput.length) cqnOutput = cqnOutput[0].toUpperCase() + cqnOutput.slice(1);
      if (cvnOutput.length) cvnOutput = cvnOutput[0].toUpperCase() + cvnOutput.slice(1);
      if (cvssResult.length) cvssResult = cvssResult[0].toUpperCase() + cvssResult.slice(1);
    }
    if (isUpperCase(word)) {
      cqnOutput = cqnOutput.toUpperCase();
      cvnOutput = cvnOutput.toUpperCase();
      cvssResult = cvssResult.toUpperCase();
    }

    return { cqn: cqnOutput, cvn: cvnOutput, cvss: cvssResult };
  }

  // Hàm chuyển đổi toàn bộ văn bản
  function convertText(input, mode) {
    const tokens = splitString(input);
    const result = { cqn: [], cvn: [], cvss: [] };

    tokens.forEach(token => {
      if (specialChars.includes(token)) {
        result.cqn.push(token);
        result.cvn.push(token);
        result.cvss.push(token);
      } else {
        let converted;
        if (mode === "cqn") {
          converted = cqnToCvnAndCvss(token);
        } else if (mode === "cvn") {
          converted = cvnToCqnAndCvss(token);
        } else if (mode === "cvss") {
          converted = cvssToCqnAndCvn(token);
        }
        result.cqn.push(converted.cqn);
        result.cvn.push(converted.cvn);
        result.cvss.push(converted.cvss);
      }
    });

    return {
      cqn: result.cqn.join(""),
      cvn: result.cvn.join(""),
      cvss: result.cvss.join("")
    };
  }

  // Xuất module
  return {
    convert: convertText,
    specialChars
  };
})();

// Xuất module cho môi trường Node.js hoặc trình duyệt
if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
  module.exports = CVNSSConverter;
} else {
  window.CVNSSConverter = CVNSSConverter;
}
</script>

<script>
/* ===== MT API Client ===== */
const API = "https://dichmay.itrithuc.vn:1721";

/**
 * CVNSS_MODE:
 * - "cvn": chữ Việt Nhanh (khuyến nghị cho nhập/xuất CVNSS4.0)
 * - "cvss": biến thể CVSS (nếu bạn muốn)
 */
const CVNSS_MODE = "cvn";

function cvnssToVietnamese(text){
  // Input đang ở CVNSS -> trả về CQN (Tiếng Việt chuẩn)
  return window.CVNSSConverter.convert(text, CVNSS_MODE).cqn;
}

function vietnameseToCvnss(text){
  // Input đang ở CQN (Tiếng Việt chuẩn) -> trả về CVNSS
  const converted = window.CVNSSConverter.convert(text, "cqn");
  return (CVNSS_MODE === "cvss") ? converted.cvss : converted.cvn;
}

async function apiTranslateOnce(srcText, srcLang, tgtLang){
  const res = await fetch(API + "/translate", {
    method:"POST",
    headers:{ "Content-Type":"application/json" },
    body: JSON.stringify({
      sourceText: srcText,
      sourceLang: srcLang,
      targetLang: tgtLang
    })
  }).then(r=>r.json());

  if(!res || !res.data || !res.data.taskId){
    throw new Error("API /translate trả về không hợp lệ.");
  }

  const taskId = res.data.taskId;
  const hid = res.data.translationHitoryId;

  let resultUrl = null;
  for(let i=0;i<8;i++){ // poll chắc hơn
    await new Promise(r=>setTimeout(r,800));
    const st = await fetch(
      `${API}/translation-history/get-single?translationHistoryId=${hid}&taskId=${taskId}`
    ).then(r=>r.json());

    if(st?.data?.status === "translated"){
      resultUrl = st.data.resultUrl;
      break;
    }
  }

  if(!resultUrl) throw new Error("Không lấy được kết quả (timeout).");

  const out = await fetch(API + "/" + resultUrl).then(r=>r.json());
  const t = (out?.target_text ?? "").trim();
  if(!t) throw new Error("Kết quả rỗng.");
  return t;
}

async function translate(){
  const raw = sourceText.value.trim();
  if(!raw) return;

  const srcSel = sourceLang.value;
  const tgtSel = targetLang.value;

  translateBtn.disabled = true;
  targetText.value = "Đang xử lý...";

  try {
    let output = "";

    if(srcSel === tgtSel){
      if(srcSel === "cvnss") {
        output = vietnameseToCvnss(cvnssToVietnamese(raw));
      } else {
        output = raw;
      }
      targetText.value = output;
      return;
    }

    if(srcSel === "cvnss" && tgtSel !== "cvnss"){
      const vi = cvnssToVietnamese(raw);         // CVNSS -> VI (pivot)
      output = await apiTranslateOnce(vi, "vi", tgtSel); // VI -> đích
      targetText.value = output;
      return;
    }

    if(srcSel !== "cvnss" && tgtSel === "cvnss"){
      const vi = await apiTranslateOnce(raw, srcSel, "vi"); // nguồn -> VI (pivot)
      output = vietnameseToCvnss(vi);                       // VI -> CVNSS
      targetText.value = output;
      return;
    }

    output = await apiTranslateOnce(raw, srcSel, tgtSel);
    targetText.value = output;

  } catch (e) {
    console.error(e);
    targetText.value = "Lỗi: " + (e?.message || e);
  } finally {
    translateBtn.disabled = false;
  }
}

/* ===== BỔ SUNG: Sao chép (không đổi code CVNSS / dịch) ===== */
async function copyTextById(id){
  const el = document.getElementById(id);
  if(!el) return;
  const text = (el.value ?? "").toString();

  // Ưu tiên Clipboard API (https), fallback execCommand (file:// hoặc trình duyệt cũ)
  try {
    if (navigator.clipboard && window.isSecureContext) {
      await navigator.clipboard.writeText(text);
      return true;
    }
  } catch (_) {}

  try {
    const ta = document.createElement("textarea");
    ta.value = text;
    ta.setAttribute("readonly", "");
    ta.style.position = "fixed";
    ta.style.left = "-9999px";
    ta.style.top = "0";
    document.body.appendChild(ta);
    ta.select();
    ta.setSelectionRange(0, ta.value.length);
    const ok = document.execCommand("copy");
    document.body.removeChild(ta);
    return ok;
  } catch (e) {
    console.error(e);
    return false;
  }
}

function bindCopyButtons(){
  const btns = document.querySelectorAll("button.copy-btn[data-copy]");
  btns.forEach(btn => {
    btn.addEventListener("click", async () => {
      const id = btn.getAttribute("data-copy");
      const old = btn.textContent;
      const ok = await copyTextById(id);
      btn.textContent = ok ? "Đã chép ✓" : "Không chép được";
      setTimeout(() => (btn.textContent = old), 1100);
    });
  });
}

translateBtn.onclick = translate;
bindCopyButtons();
</script>

</body>
</html>