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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee1fafd
9bd50b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee1fafd
 
 
9bd50b9
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState } from 'react';
import { Button } from "@/components/ui/button"
import {
    DropdownMenu,
    DropdownMenuContent,
    DropdownMenuGroup,
    DropdownMenuItem,
    DropdownMenuShortcut,
    DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"

import { Eye, EyeClosed } from "lucide-react"
import {
    Tooltip,
    TooltipContent,
    TooltipTrigger,
} from "@/components/ui/tooltip"


import { Button } from "@/components/ui/button"


import React from "react"
import {
    Table,
    TableHeader,
    TableBody,
    TableRow,
    TableHead,
    TableCell,
} from "@/components/ui/table"

function parseTable(lines, startIndex) {
    const headerLine = lines[startIndex]
    const separatorLine = lines[startIndex + 1]

    if (!separatorLine || !separatorLine.match(/^\s*\|[-\s|]+\|\s*$/)) {
        return null
    }

    const headers = headerLine
        .split("|")
        .slice(1, -1)
        .map(h => h.trim())

    const rows = []
    let i = startIndex + 2

    while (i < lines.length && lines[i].includes("|")) {
        const cells = lines[i]
            .split("|")
            .slice(1, -1)
            .map(c => c.trim())
        rows.push(cells)
        i++
    }

    return {
        table: { headers, rows },
        nextIndex: i,
    }
}

export function Markdown({ children, dic, show }) {
    const [localMessage, setLocalMessage] = useState(children);
    const [open, setOpen] = useState(false);
    const [position, setPosition] = useState({ x: 0, y: 0 });
    const [selectionData, setSelectionData] = useState(null);
    const lines = children.split("\n")
    const elements = []
    let i = 0

    const handleMouseUp = () => {

        const selection = window.getSelection();

        // 1. Check basic validity
        if (!selection || selection.rangeCount === 0 || selection.isCollapsed) {
            // If just a click (collapsed), we assume menu shouldn't open or should close
            // But we don't want to close immediately if clicking inside the menu, handled by DropdownMenu logic
            return;
        }

        // 2. Expand Selection to Word Boundaries
        const anchorNode = selection.anchorNode;
        const focusNode = selection.focusNode;

        // Only support single text node selection for simplicity to map back to data-index
        if (anchorNode !== focusNode || anchorNode.nodeType !== 3) {
            setOpen(false);
            return;
        }

        const textContent = anchorNode.textContent;
        let start = Math.min(selection.anchorOffset, selection.focusOffset);
        let end = Math.max(selection.anchorOffset, selection.focusOffset);

        // Expand left
        while (start > 0 && /\S/.test(textContent[start - 1])) {
            start--;
        }
        // Expand right
        while (end < textContent.length && /\S/.test(textContent[end])) {
            end++;
        }

        // Update visual selection
        const newRange = document.createRange();
        newRange.setStart(anchorNode, start);
        newRange.setEnd(anchorNode, end);
        selection.removeAllRanges();
        selection.addRange(newRange);


        const newText = textContent.slice(start, end);
        if (!newText.trim()) return;

        // 3. Validation Logic
        // Check 1: Less than 4 words
        const words = newText.split(/\s+/);
        if (words.length >= 4) {
            setOpen(false);
            return;
        }

        // Check 2: Does not contain highlighted word (brackets)
        if (newText.includes('[') || newText.includes(']')) {
            setOpen(false);
            return;
        }

        // Check 3: Check if selection is inside a Badge
        const parentElement = anchorNode.parentElement;
        if (parentElement.closest('.bg-pii')) {
            setOpen(false);
            return;
        }

        // Identify Part Index
        const partIndex = parseInt(parentElement.getAttribute('data-index'), 10);
        // if (isNaN(partIndex)) {
        //     setOpen(false);
        //     return;
        // }

        const rect = newRange.getBoundingClientRect();
        setPosition({
            x: rect.left,
            y: rect.bottom
        });
        setSelectionData({
            partIndex,
            start,
            end,
            text: newText
        });
        setOpen(true);
    };

    const handleAnonymize = () => {
        if (!selectionData) return;
        const { partIndex, start, end, text } = selectionData;

        // Split by brackets to reconstruct parts array as seen in render
        const parts = localMessage.split(/(\[[^\]]+\])/g);

        if (parts[partIndex] === undefined) return;

        const originalPart = parts[partIndex];
        const newPart = originalPart.substring(0, start) + `[ANONYME_${text}]` + originalPart.substring(end);

        parts[partIndex] = newPart;
        const newMessage = parts.join('');

        setLocalMessage(newMessage);
        setOpen(false);
        // Clear selection
        window.getSelection().removeAllRanges();
    };

    while (i < lines.length) {
        const line = lines[i]

        // HEADERS
        const headerMatch = line.match(/^(#{1,6})\s+(.*)$/)
        if (headerMatch) {
            const level = headerMatch[1].length
            const text = headerMatch[2]
            const Tag = `h${level}`

            elements.push(
                <Tag key={i} className="mt-4 mb-2 font-semibold">
                    {renderInline(text, dic, show)}
                </Tag>
            )
            i++
            continue
        }

        // TABLE
        if (line.trim().startsWith("|")) {
            const parsed = parseTable(lines, i)
            if (parsed) {
                const { headers, rows } = parsed.table

                elements.push(
                    <Table key={i} className="my-4">
                        <TableHeader>
                            <TableRow>
                                {headers.map((h, idx) => (
                                    <TableHead key={idx}>
                                        {renderInline(h, dic, show)}
                                    </TableHead>
                                ))}
                            </TableRow>
                        </TableHeader>
                        <TableBody>
                            {rows.map((row, rIdx) => (
                                <TableRow key={rIdx}>
                                    {row.map((cell, cIdx) => (
                                        <TableCell key={cIdx}>
                                            {renderInline(cell, dic, show)}
                                        </TableCell>
                                    ))}
                                </TableRow>
                            ))}
                        </TableBody>
                    </Table>
                )

                i = parsed.nextIndex
                continue
            }
        }

        // PARAGRAPH
        if (line.trim() !== "") {
            elements.push(
                <p key={i} className="mb-2">
                    {renderInline(line, dic, show)}
                </p>
            )
        }

        i++
    }

    return (
        <div onMouseUp={handleMouseUp}>
            <div className="relative bg-accent rounded-3xl px-5 py-2.5 user-message flex-grow-0">
                <p className="leading-[1.75] whitespace-pre-wrap">{elements}</p>

                <DropdownMenu open={open} onOpenChange={setOpen}>
                    <DropdownMenuTrigger asChild>
                        <div
                            style={{
                                position: 'fixed',
                                top: position.y,
                                left: position.x,
                                width: '1px',
                                height: '1px',
                                visibility: 'hidden',
                                pointerEvents: 'none'
                            }}
                        />
                    </DropdownMenuTrigger>
                    <DropdownMenuContent align="start">
                        <DropdownMenuGroup>
                            <DropdownMenuItem onSelect={handleAnonymize}>
                                Anonymiser
                            </DropdownMenuItem>
                        </DropdownMenuGroup>
                    </DropdownMenuContent>
                </DropdownMenu>
            </div>
        </div>
    )
}

function parseInlineText(text) {
    const tokens = []
    let i = 0

    while (i < text.length) {
        // CODE `
        if (text[i] === "`") {
            const end = text.indexOf("`", i + 1)
            if (end !== -1) {
                tokens.push({ type: "code", value: text.slice(i + 1, end) })
                i = end + 1
                continue
            }
        }

        // BOLD **
        if (text.slice(i, i + 2) === "**") {
            const end = text.indexOf("**", i + 2)
            if (end !== -1) {
                tokens.push({ type: "bold", value: text.slice(i + 2, end) })
                i = end + 2
                continue
            }
        }

        // ITALIC *
        if (text[i] === "*") {
            const end = text.indexOf("*", i + 1)
            if (end !== -1) {
                tokens.push({ type: "italic", value: text.slice(i + 1, end) })
                i = end + 1
                continue
            }
        }

        // TEXT
        let j = i
        while (
            j < text.length &&
            text[j] !== "`" &&
            text[j] !== "*"
        ) {
            j++
        }

        tokens.push({ type: "text", value: text.slice(i, j) })
        i = j
    }

    return tokens
}


function renderInline(text, dic, show) {
    const parts = text.split(/(\[[^\]]+\])/g)

    return parts.map((part, index) => {
        // PII
        const match = part.match(/^\[(\w+)_(.*?)\]$/)
        if (match && dic[match[2]]) {
            return show ? (
                <Badge
                    key={index}
                    className="bg-pii text-blue-800 rounded-full px-2 py-1 mx-1"
                >
                    {dic[match[2]].value}
                </Badge>
            ) : (
                <Tooltip key={index}>
                    <TooltipTrigger asChild>
                        <Badge
                            key={index}
                            className="bg-pii text-blue-800 rounded-full px-2 py-1 mx-1 blur-sm"
                        >
                            {dic[match[2]].value}
                        </Badge>
                        {/* <span className="bg-pii px-1 rounded cursor-pointer blur-sm">
                            {/* {part.toUpperCase()} */}
                        {/* {dic[match[2]].value} */}
                        {/* </span> */}
                    </TooltipTrigger>
                    <TooltipContent>
                        <p>{dic[match[2]].value}</p>
                    </TooltipContent>
                </Tooltip >
            )
        }

        // TEXTE NORMAL → inline markdown
        const tokens = parseInlineText(part)

        return (
            <span key={index} data-index={index}>
                {tokens.map((t, i) => {
                    if (t.type === "bold") {
                        return <strong key={i}>{t.value}</strong>
                    }
                    if (t.type === "italic") {
                        return <em key={i}>{t.value}</em>
                    }
                    if (t.type === "code") {
                        return (
                            <code
                                key={i}
                                className="px-1 py-0.5 rounded bg-muted text-sm"
                            >
                                {t.value}
                            </code>
                        )
                    }
                    return <span key={i}>{t.value}</span>
                })}
            </span>
        )
    })
}


const Badge = ({ children, className }) => (
    <span className={` py-0.5 transition-colors text-main-700 ${className}`}>
        {children}
    </span>
);


function MessageWithHighlight({ message, dic, show }) {
    if (!message) return null;

    const [localMessage, setLocalMessage] = useState(message);
    const [open, setOpen] = useState(false);
    const [position, setPosition] = useState({ x: 0, y: 0 });
    const [selectionData, setSelectionData] = useState(null);

    const handleMouseUp = () => {
        const selection = window.getSelection();

        // 1. Check basic validity
        if (!selection || selection.rangeCount === 0 || selection.isCollapsed) {
            // If just a click (collapsed), we assume menu shouldn't open or should close
            // But we don't want to close immediately if clicking inside the menu, handled by DropdownMenu logic
            return;
        }

        // 2. Expand Selection to Word Boundaries
        const anchorNode = selection.anchorNode;
        const focusNode = selection.focusNode;

        // Only support single text node selection for simplicity to map back to data-index
        if (anchorNode !== focusNode || anchorNode.nodeType !== 3) {
            setOpen(false);
            return;
        }

        const textContent = anchorNode.textContent;
        let start = Math.min(selection.anchorOffset, selection.focusOffset);
        let end = Math.max(selection.anchorOffset, selection.focusOffset);

        // Expand left
        while (start > 0 && /\S/.test(textContent[start - 1])) {
            start--;
        }
        // Expand right
        while (end < textContent.length && /\S/.test(textContent[end])) {
            end++;
        }

        // Update visual selection
        const newRange = document.createRange();
        newRange.setStart(anchorNode, start);
        newRange.setEnd(anchorNode, end);
        selection.removeAllRanges();
        selection.addRange(newRange);

        const newText = textContent.slice(start, end);
        if (!newText.trim()) return;

        // 3. Validation Logic
        // Check 1: Less than 4 words
        const words = newText.split(/\s+/);
        if (words.length >= 4) {
            setOpen(false);
            return;
        }

        // Check 2: Does not contain highlighted word (brackets)
        if (newText.includes('[') || newText.includes(']')) {
            setOpen(false);
            return;
        }

        // Check 3: Check if selection is inside a Badge
        const parentElement = anchorNode.parentElement;
        if (parentElement.closest('.bg-pii')) {
            setOpen(false);
            return;
        }

        // Identify Part Index
        const partIndex = parseInt(parentElement.getAttribute('data-index'), 10);
        if (isNaN(partIndex)) {
            setOpen(false);
            return;
        }

        const rect = newRange.getBoundingClientRect();
        setPosition({
            x: rect.left,
            y: rect.bottom
        });
        setSelectionData({
            partIndex,
            start,
            end,
            text: newText
        });
        setOpen(true);
    };

    const handleAnonymize = () => {
        if (!selectionData) return;
        const { partIndex, start, end, text } = selectionData;

        // Split by brackets to reconstruct parts array as seen in render
        const parts = localMessage.split(/(\[[^\]]+\])/g);

        if (parts[partIndex] === undefined) return;

        const originalPart = parts[partIndex];
        const newPart = originalPart.substring(0, start) + `[ANONYME_${text}]` + originalPart.substring(end);

        parts[partIndex] = newPart;
        const newMessage = parts.join('');

        setLocalMessage(newMessage);
        setOpen(false);
        // Clear selection
        window.getSelection().removeAllRanges();
    };

    // Split by brackets, keeping the brackets in the split result for now
    // Regex: split by [content]
    const parts = localMessage.split(/(\[[^\]]+\])/g);

    return (
        <div className='flex flex-row-reverse w-full'>
            <div onMouseUp={handleMouseUp} className="relative bg-accent rounded-3xl px-5 py-2.5 user-message flex-grow-0">


                <DropdownMenu open={open} onOpenChange={setOpen}>
                    <DropdownMenuTrigger asChild>
                        <div
                            style={{
                                position: 'fixed',
                                top: position.y,
                                left: position.x,
                                width: '1px',
                                height: '1px',
                                visibility: 'hidden',
                                pointerEvents: 'none'
                            }}
                        />
                    </DropdownMenuTrigger>
                    <DropdownMenuContent align="start">
                        <DropdownMenuGroup>
                            <DropdownMenuItem onSelect={handleAnonymize}>
                                Anonymiser
                            </DropdownMenuItem>
                        </DropdownMenuGroup>
                    </DropdownMenuContent>
                </DropdownMenu>
            </div>
        </div>
    );
}

export default function UserMessage() {
    const [show, setShow] = useState(true);
    return (
        <div className="flex flex-col gap-2">
            {props.author == 'user' ? (
                <div className='flex flex-row-reverse w-full'>
                    {/* <div className="relative bg-accent rounded-3xl px-5 py-2.5 user-message flex-grow-0"> */}
                    <Markdown dic={props.dic} show={show}>{props.message}</Markdown>
                    {/* </div> */}
                </div>
            ) : (
                <Markdown dic={props.dic} show={show}>{props.message}</Markdown>
            )}
            <div className="flex justify-end">
                <Button variant="outline" size="icon" onClick={() => setShow(!show)}>
                    {show ? <Eye /> : <EyeClosed />}
                </Button>
            </div>
        </div>
    );
}