@postalsys/ee-client 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js ADDED
@@ -0,0 +1,865 @@
1
+ export class EmailEngineClient {
2
+ constructor(options = {}) {
3
+ this.apiUrl = options.apiUrl || 'http://127.0.0.1:3000';
4
+ this.account = options.account;
5
+ this.accessToken = options.accessToken;
6
+ this.container = options.container;
7
+
8
+ this.currentFolder = null;
9
+ this.currentMessage = null;
10
+ this.folders = [];
11
+ this.messages = [];
12
+ this.nextPageCursor = null;
13
+ this.prevPageCursor = null;
14
+
15
+ if (this.container) {
16
+ this.init();
17
+ }
18
+ }
19
+
20
+ async apiRequest(method, endpoint, data = null) {
21
+ const url = `${this.apiUrl}${endpoint}`;
22
+ const options = {
23
+ method: method,
24
+ headers: {
25
+ 'Content-Type': 'application/json'
26
+ }
27
+ };
28
+
29
+ if (this.accessToken) {
30
+ options.headers['Authorization'] = `Bearer ${this.accessToken}`;
31
+ }
32
+
33
+ if (data) {
34
+ options.body = JSON.stringify(data);
35
+ }
36
+
37
+ let fetchFn = globalThis.fetch;
38
+ if (!fetchFn && typeof require !== 'undefined') {
39
+ try {
40
+ const nodeFetch = await import('node-fetch');
41
+ fetchFn = nodeFetch.default;
42
+ } catch (err) {
43
+ throw new Error(
44
+ 'fetch is not available. In Node.js environments, please install node-fetch: npm install node-fetch'
45
+ );
46
+ }
47
+ }
48
+
49
+ if (!fetchFn) {
50
+ throw new Error('fetch is not available');
51
+ }
52
+
53
+ const response = await fetchFn(url, options);
54
+ if (!response.ok) {
55
+ throw new Error(`API request failed: ${response.statusText}`);
56
+ }
57
+
58
+ return await response.json();
59
+ }
60
+
61
+ async loadFolders() {
62
+ try {
63
+ const data = await this.apiRequest('GET', `/v1/account/${this.account}/mailboxes`);
64
+ this.folders = data.mailboxes || [];
65
+ if (this.container) {
66
+ this.renderFolderList();
67
+ }
68
+ return this.folders;
69
+ } catch (error) {
70
+ console.error('Failed to load folders:', error);
71
+ throw error;
72
+ }
73
+ }
74
+
75
+ async loadMessages(path, cursor = null) {
76
+ if (this.container) {
77
+ const messageList = this.container.querySelector('.ee-message-list');
78
+ if (messageList) {
79
+ messageList.innerHTML = '<div class="ee-loading">Loading messages...</div>';
80
+ }
81
+ }
82
+
83
+ try {
84
+ const params = new URLSearchParams({ path: path, pageSize: 50 });
85
+ if (cursor) {
86
+ params.set('cursor', cursor);
87
+ }
88
+
89
+ const data = await this.apiRequest('GET', `/v1/account/${this.account}/messages?${params}`);
90
+ this.messages = data.messages || [];
91
+ this.currentFolder = path;
92
+ this.nextPageCursor = data.nextPageCursor || null;
93
+ this.prevPageCursor = data.prevPageCursor || null;
94
+
95
+ if (this.container) {
96
+ this.renderMessageList();
97
+ }
98
+
99
+ return {
100
+ messages: this.messages,
101
+ nextPageCursor: this.nextPageCursor,
102
+ prevPageCursor: this.prevPageCursor
103
+ };
104
+ } catch (error) {
105
+ console.error('Failed to load messages:', error);
106
+ if (this.container) {
107
+ const messageList = this.container.querySelector('.ee-message-list');
108
+ if (messageList) {
109
+ messageList.innerHTML = '<div class="ee-error">Failed to load messages</div>';
110
+ }
111
+ }
112
+ throw error;
113
+ }
114
+ }
115
+
116
+ async loadMessage(messageId) {
117
+ if (this.container) {
118
+ const viewer = this.container.querySelector('.ee-message-viewer');
119
+ if (viewer) {
120
+ viewer.innerHTML = '<div class="ee-loading">Loading message...</div>';
121
+ }
122
+ }
123
+
124
+ try {
125
+ const params = new URLSearchParams({
126
+ webSafeHtml: true,
127
+ markAsSeen: true
128
+ });
129
+ const data = await this.apiRequest('GET', `/v1/account/${this.account}/message/${messageId}?${params}`);
130
+ this.currentMessage = data;
131
+
132
+ this.currentMessage.unseen = false;
133
+
134
+ const msg = this.messages.find(m => m.id === messageId);
135
+ if (msg) {
136
+ msg.unseen = false;
137
+ if (this.container) {
138
+ this.renderMessageList();
139
+ }
140
+ }
141
+
142
+ if (this.container) {
143
+ this.renderMessage();
144
+ }
145
+
146
+ return this.currentMessage;
147
+ } catch (error) {
148
+ console.error('Failed to load message:', error);
149
+ if (this.container) {
150
+ const viewer = this.container.querySelector('.ee-message-viewer');
151
+ if (viewer) {
152
+ viewer.innerHTML = '<div class="ee-error">Failed to load message</div>';
153
+ }
154
+ }
155
+ throw error;
156
+ }
157
+ }
158
+
159
+ async markAsRead(messageId, seen = true) {
160
+ try {
161
+ const flagUpdate = seen ? { flags: { add: ['\\Seen'] } } : { flags: { delete: ['\\Seen'] } };
162
+
163
+ await this.apiRequest('PUT', `/v1/account/${this.account}/message/${messageId}`, flagUpdate);
164
+
165
+ const msg = this.messages.find(m => m.id === messageId);
166
+ if (msg) {
167
+ msg.unseen = !seen;
168
+ if (this.container) {
169
+ this.renderMessageList();
170
+ }
171
+ }
172
+
173
+ if (this.currentMessage && this.currentMessage.id === messageId) {
174
+ this.currentMessage.unseen = !seen;
175
+ if (this.container) {
176
+ this.renderMessage();
177
+ }
178
+ }
179
+
180
+ return true;
181
+ } catch (error) {
182
+ console.error('Failed to update message flags:', error);
183
+ throw error;
184
+ }
185
+ }
186
+
187
+ async deleteMessage(messageId) {
188
+ try {
189
+ await this.apiRequest('DELETE', `/v1/account/${this.account}/message/${messageId}`);
190
+
191
+ this.messages = this.messages.filter(m => m.id !== messageId);
192
+ if (this.container) {
193
+ this.renderMessageList();
194
+ }
195
+
196
+ if (this.currentMessage && this.currentMessage.id === messageId) {
197
+ this.currentMessage = null;
198
+ if (this.container) {
199
+ this.renderMessage();
200
+ }
201
+ }
202
+
203
+ return true;
204
+ } catch (error) {
205
+ console.error('Failed to delete message:', error);
206
+ throw error;
207
+ }
208
+ }
209
+
210
+ async moveMessage(messageId, targetPath) {
211
+ try {
212
+ await this.apiRequest('PUT', `/v1/account/${this.account}/message/${messageId}/move`, {
213
+ path: targetPath
214
+ });
215
+
216
+ this.messages = this.messages.filter(m => m.id !== messageId);
217
+ if (this.container) {
218
+ this.renderMessageList();
219
+ }
220
+
221
+ if (this.currentMessage && this.currentMessage.id === messageId) {
222
+ this.currentMessage = null;
223
+ if (this.container) {
224
+ this.renderMessage();
225
+ }
226
+ }
227
+
228
+ return true;
229
+ } catch (error) {
230
+ console.error('Failed to move message:', error);
231
+ throw error;
232
+ }
233
+ }
234
+
235
+ formatDate(dateStr) {
236
+ const date = new Date(dateStr);
237
+ const now = new Date();
238
+ const diff = now - date;
239
+
240
+ if (diff < 86400000) {
241
+ return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
242
+ } else if (diff < 604800000) {
243
+ return date.toLocaleDateString([], { weekday: 'short', hour: '2-digit', minute: '2-digit' });
244
+ } else {
245
+ return date.toLocaleDateString([], { month: 'short', day: 'numeric' });
246
+ }
247
+ }
248
+
249
+ createStyles() {
250
+ if (typeof document === 'undefined') {
251
+ return;
252
+ }
253
+
254
+ const style = document.createElement('style');
255
+ style.textContent = `
256
+ .ee-client {
257
+ display: flex;
258
+ height: 100%;
259
+ min-height: 400px;
260
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
261
+ font-size: 14px;
262
+ line-height: 1.5;
263
+ color: #333;
264
+ background: #fff;
265
+ border: 1px solid #ddd;
266
+ }
267
+
268
+ .ee-client * {
269
+ box-sizing: border-box;
270
+ }
271
+
272
+ .ee-sidebar {
273
+ width: 200px;
274
+ background: #f5f5f5;
275
+ border-right: 1px solid #ddd;
276
+ overflow-y: auto;
277
+ }
278
+
279
+ .ee-folder-list {
280
+ list-style: none;
281
+ margin: 0;
282
+ padding: 0;
283
+ }
284
+
285
+ .ee-folder-item {
286
+ cursor: pointer;
287
+ border-bottom: 1px solid #e0e0e0;
288
+ }
289
+
290
+ .ee-folder-item:hover {
291
+ background: #e8e8e8;
292
+ }
293
+
294
+ .ee-folder-item.active {
295
+ background: #007bff;
296
+ color: white;
297
+ }
298
+
299
+ .ee-folder-content {
300
+ padding: 8px 16px 8px 0px;
301
+ display: flex;
302
+ align-items: center;
303
+ position: relative;
304
+ }
305
+
306
+ .ee-folder-indent {
307
+ color: #999;
308
+ font-size: 12px;
309
+ margin-right: 4px;
310
+ font-family: monospace;
311
+ }
312
+
313
+ .ee-folder-name {
314
+ font-weight: 500;
315
+ flex: 1;
316
+ }
317
+
318
+ .ee-folder-name.has-children {
319
+ font-weight: 600;
320
+ }
321
+
322
+ .ee-folder-item.active .ee-folder-indent {
323
+ color: rgba(255, 255, 255, 0.7);
324
+ }
325
+
326
+ .ee-folder-count {
327
+ font-size: 12px;
328
+ opacity: 0.7;
329
+ margin-left: 8px;
330
+ flex-shrink: 0;
331
+ }
332
+
333
+ .ee-main {
334
+ flex: 1;
335
+ display: flex;
336
+ flex-direction: column;
337
+ overflow: hidden;
338
+ }
339
+
340
+ .ee-message-list {
341
+ width: 350px;
342
+ border-right: 1px solid #ddd;
343
+ overflow-y: auto;
344
+ }
345
+
346
+ .ee-message-item {
347
+ padding: 12px 16px;
348
+ border-bottom: 1px solid #e0e0e0;
349
+ cursor: pointer;
350
+ }
351
+
352
+ .ee-message-item:hover {
353
+ background: #f8f8f8;
354
+ }
355
+
356
+ .ee-message-item.active {
357
+ background: #e3f2fd;
358
+ }
359
+
360
+ .ee-message-item.unread {
361
+ font-weight: 600;
362
+ }
363
+
364
+ .ee-message-header {
365
+ display: flex;
366
+ justify-content: space-between;
367
+ margin-bottom: 4px;
368
+ }
369
+
370
+ .ee-message-from {
371
+ flex: 1;
372
+ overflow: hidden;
373
+ text-overflow: ellipsis;
374
+ white-space: nowrap;
375
+ }
376
+
377
+ .ee-message-date {
378
+ font-size: 12px;
379
+ color: #666;
380
+ flex-shrink: 0;
381
+ margin-left: 8px;
382
+ }
383
+
384
+ .ee-message-subject {
385
+ overflow: hidden;
386
+ text-overflow: ellipsis;
387
+ white-space: nowrap;
388
+ margin-bottom: 2px;
389
+ }
390
+
391
+ .ee-message-preview {
392
+ font-size: 12px;
393
+ color: #666;
394
+ overflow: hidden;
395
+ text-overflow: ellipsis;
396
+ white-space: nowrap;
397
+ font-weight: normal;
398
+ }
399
+
400
+ .ee-message-viewer {
401
+ flex: 1;
402
+ display: flex;
403
+ flex-direction: column;
404
+ overflow: hidden;
405
+ }
406
+
407
+ .ee-message-actions {
408
+ padding: 12px 16px;
409
+ background: #f5f5f5;
410
+ border-bottom: 1px solid #ddd;
411
+ display: flex;
412
+ gap: 8px;
413
+ }
414
+
415
+ .ee-button {
416
+ padding: 6px 12px;
417
+ border: 1px solid #ddd;
418
+ background: white;
419
+ border-radius: 4px;
420
+ cursor: pointer;
421
+ font-size: 13px;
422
+ }
423
+
424
+ .ee-button:hover:not(:disabled) {
425
+ background: #f0f0f0;
426
+ }
427
+
428
+ .ee-button:disabled {
429
+ background: #e9ecef;
430
+ color: #6c757d;
431
+ cursor: not-allowed;
432
+ opacity: 0.6;
433
+ }
434
+
435
+ .ee-message-content {
436
+ flex: 1;
437
+ padding: 16px;
438
+ overflow-y: auto;
439
+ }
440
+
441
+ .ee-message-meta {
442
+ margin-bottom: 16px;
443
+ padding-bottom: 16px;
444
+ border-bottom: 1px solid #e0e0e0;
445
+ }
446
+
447
+ .ee-message-meta-row {
448
+ margin-bottom: 4px;
449
+ }
450
+
451
+ .ee-message-meta-label {
452
+ display: inline-block;
453
+ width: 60px;
454
+ color: #666;
455
+ font-weight: 500;
456
+ }
457
+
458
+ .ee-message-body {
459
+ line-height: 1.6;
460
+ }
461
+
462
+ .ee-message-body img {
463
+ max-width: 100%;
464
+ height: auto;
465
+ }
466
+
467
+ .ee-empty-state {
468
+ display: flex;
469
+ align-items: center;
470
+ justify-content: center;
471
+ height: 100%;
472
+ color: #999;
473
+ }
474
+
475
+ .ee-loading {
476
+ display: flex;
477
+ align-items: center;
478
+ justify-content: center;
479
+ padding: 20px;
480
+ color: #666;
481
+ }
482
+
483
+ .ee-error {
484
+ padding: 16px;
485
+ background: #fee;
486
+ color: #c00;
487
+ border: 1px solid #fcc;
488
+ border-radius: 4px;
489
+ margin: 16px;
490
+ }
491
+
492
+ .ee-flag {
493
+ display: inline-block;
494
+ width: 8px;
495
+ height: 8px;
496
+ border-radius: 50%;
497
+ background: #4CAF50;
498
+ margin-right: 4px;
499
+ }
500
+
501
+ .ee-flag.unread {
502
+ background: #2196F3;
503
+ }
504
+
505
+ .ee-pagination {
506
+ padding: 8px 16px;
507
+ background: #f8f9fa;
508
+ border-top: 1px solid #e0e0e0;
509
+ border-bottom: 1px solid #e0e0e0;
510
+ display: flex;
511
+ justify-content: space-between;
512
+ align-items: center;
513
+ }
514
+
515
+ .ee-pagination-btn {
516
+ font-size: 12px;
517
+ padding: 4px 8px;
518
+ }
519
+
520
+ .ee-message-items {
521
+ flex: 1;
522
+ overflow-y: auto;
523
+ }
524
+ `;
525
+ document.head.appendChild(style);
526
+ }
527
+
528
+ calculateFolderDepth(folder) {
529
+ if (!folder.parentPath || !folder.delimiter) {
530
+ return 0;
531
+ }
532
+
533
+ const pathParts = folder.path.split(folder.delimiter);
534
+ return Math.max(0, pathParts.length - 1);
535
+ }
536
+
537
+ buildFolderTree() {
538
+ const specialFolders = [];
539
+ const regularFolders = [];
540
+
541
+ this.folders.forEach(folder => {
542
+ if (folder.specialUse) {
543
+ specialFolders.push(folder);
544
+ } else {
545
+ regularFolders.push(folder);
546
+ }
547
+ });
548
+
549
+ const specialOrder = ['\\Inbox', '\\Drafts', '\\Sent', '\\Trash', '\\Junk', '\\Archive'];
550
+ specialFolders.sort((a, b) => {
551
+ if (a.specialUse === '\\Inbox' || a.name.toLowerCase() === 'inbox') {
552
+ return -1;
553
+ }
554
+ if (b.specialUse === '\\Inbox' || b.name.toLowerCase() === 'inbox') {
555
+ return 1;
556
+ }
557
+
558
+ const aIndex = specialOrder.indexOf(a.specialUse);
559
+ const bIndex = specialOrder.indexOf(b.specialUse);
560
+ if (aIndex !== -1 && bIndex !== -1) {
561
+ return aIndex - bIndex;
562
+ }
563
+ if (aIndex !== -1) {
564
+ return -1;
565
+ }
566
+ if (bIndex !== -1) {
567
+ return 1;
568
+ }
569
+ return a.name.localeCompare(b.name);
570
+ });
571
+
572
+ const buildHierarchy = (folders, parentPath = null, depth = 0) => {
573
+ const result = [];
574
+
575
+ const children = folders.filter(f => {
576
+ if (parentPath === null) {
577
+ return f.parentPath === 'INBOX' || !f.parentPath || f.parentPath === '';
578
+ } else {
579
+ return f.parentPath === parentPath;
580
+ }
581
+ });
582
+
583
+ children.sort((a, b) => a.name.localeCompare(b.name));
584
+
585
+ children.forEach(folder => {
586
+ result.push(folder);
587
+ result.push(...buildHierarchy(folders, folder.path, depth + 1));
588
+ });
589
+
590
+ return result;
591
+ };
592
+
593
+ const hierarchicalRegular = buildHierarchy(regularFolders);
594
+
595
+ return [...specialFolders, ...hierarchicalRegular];
596
+ }
597
+
598
+ renderFolderList() {
599
+ if (typeof document === 'undefined' || !this.container) {
600
+ return;
601
+ }
602
+
603
+ const sidebar = this.container.querySelector('.ee-sidebar');
604
+ if (!sidebar) {
605
+ return;
606
+ }
607
+
608
+ const sortedFolders = this.buildFolderTree();
609
+
610
+ const html = `
611
+ <ul class="ee-folder-list">
612
+ ${sortedFolders
613
+ .map(folder => {
614
+ const depth = this.calculateFolderDepth(folder);
615
+ const hasChildren = this.folders.some(f => f.parentPath === folder.path);
616
+ return `
617
+ <li class="ee-folder-item ${folder.path === this.currentFolder ? 'active' : ''}"
618
+ data-path="${folder.path}"
619
+ data-depth="${depth}">
620
+ <div class="ee-folder-content" style="padding-left: ${8 + depth * 12}px;">
621
+ ${depth > 0 ? '<span class="ee-folder-indent">└ </span>' : ''}
622
+ <span class="ee-folder-name ${hasChildren ? 'has-children' : ''}">${folder.name}</span>
623
+ ${folder.status && folder.status.messages > 0 ? `<span class="ee-folder-count">${folder.status.messages}</span>` : ''}
624
+ </div>
625
+ </li>
626
+ `;
627
+ })
628
+ .join('')}
629
+ </ul>
630
+ `;
631
+ sidebar.innerHTML = html;
632
+
633
+ sidebar.querySelectorAll('.ee-folder-item').forEach(item => {
634
+ item.addEventListener('click', () => {
635
+ const path = item.getAttribute('data-path');
636
+ this.loadMessages(path);
637
+ });
638
+ });
639
+ }
640
+
641
+ renderMessageList() {
642
+ if (typeof document === 'undefined' || !this.container) {
643
+ return;
644
+ }
645
+
646
+ const messageList = this.container.querySelector('.ee-message-list');
647
+ if (!messageList) {
648
+ return;
649
+ }
650
+
651
+ if (!this.messages.length) {
652
+ messageList.innerHTML = '<div class="ee-empty-state">No messages</div>';
653
+ return;
654
+ }
655
+
656
+ const paginationHtml =
657
+ this.nextPageCursor || this.prevPageCursor
658
+ ? `
659
+ <div class="ee-pagination">
660
+ ${this.prevPageCursor ? `<button class="ee-button ee-pagination-btn" data-action="prev-page">← Previous</button>` : ''}
661
+ ${this.nextPageCursor ? `<button class="ee-button ee-pagination-btn" data-action="next-page">Next →</button>` : ''}
662
+ </div>
663
+ `
664
+ : '';
665
+
666
+ const html = `
667
+ ${paginationHtml}
668
+ <div class="ee-message-items">
669
+ ${this.messages
670
+ .map(
671
+ msg => `
672
+ <div class="ee-message-item ${msg.unseen ? 'unread' : ''} ${msg.id === (this.currentMessage && this.currentMessage.id) ? 'active' : ''}" data-id="${msg.id}">
673
+ <div class="ee-message-header">
674
+ <span class="ee-message-from">${msg.from ? msg.from.name || msg.from.address : 'Unknown'}</span>
675
+ <span class="ee-message-date">${this.formatDate(msg.date)}</span>
676
+ </div>
677
+ <div class="ee-message-subject">${msg.subject || '(no subject)'}</div>
678
+ <div class="ee-message-preview">${msg.intro || ''}</div>
679
+ </div>
680
+ `
681
+ )
682
+ .join('')}
683
+ </div>
684
+ ${paginationHtml}
685
+ `;
686
+ messageList.innerHTML = html;
687
+
688
+ messageList.querySelectorAll('.ee-message-item').forEach(item => {
689
+ item.addEventListener('click', () => {
690
+ const messageId = item.getAttribute('data-id');
691
+ this.loadMessage(messageId);
692
+ });
693
+ });
694
+
695
+ messageList.querySelectorAll('[data-action="prev-page"]').forEach(btn => {
696
+ btn.addEventListener('click', () => {
697
+ this.loadMessages(this.currentFolder, this.prevPageCursor);
698
+ });
699
+ });
700
+
701
+ messageList.querySelectorAll('[data-action="next-page"]').forEach(btn => {
702
+ btn.addEventListener('click', () => {
703
+ this.loadMessages(this.currentFolder, this.nextPageCursor);
704
+ });
705
+ });
706
+ }
707
+
708
+ renderMessage() {
709
+ if (typeof document === 'undefined' || !this.container) {
710
+ return;
711
+ }
712
+
713
+ const viewer = this.container.querySelector('.ee-message-viewer');
714
+ if (!viewer) {
715
+ return;
716
+ }
717
+
718
+ if (!this.currentMessage) {
719
+ viewer.innerHTML = '<div class="ee-empty-state">Select a message to view</div>';
720
+ return;
721
+ }
722
+
723
+ const msg = this.currentMessage;
724
+ const isUnseen = msg.unseen;
725
+ const html = `
726
+ <div class="ee-message-actions">
727
+ <button class="ee-button" data-action="toggle-read">Mark as ${isUnseen ? 'seen' : 'unseen'}</button>
728
+ <button class="ee-button" data-action="delete">Delete</button>
729
+ <select class="ee-button" data-action="move">
730
+ <option value="">Move to...</option>
731
+ ${this.buildFolderTree()
732
+ .map(folder => {
733
+ const depth = this.calculateFolderDepth(folder);
734
+ const indent = ' '.repeat(depth);
735
+ const prefix = depth > 0 ? '└ ' : '';
736
+ return `<option value="${folder.path}" ${folder.path === this.currentFolder ? 'disabled' : ''}>${indent}${prefix}${folder.name}</option>`;
737
+ })
738
+ .join('')}
739
+ </select>
740
+ </div>
741
+ <div class="ee-message-content">
742
+ <div class="ee-message-meta">
743
+ <div class="ee-message-meta-row">
744
+ <span class="ee-message-meta-label">From:</span>
745
+ ${msg.from ? `${msg.from.name || ''} &lt;${msg.from.address}&gt;` : 'Unknown'}
746
+ </div>
747
+ <div class="ee-message-meta-row">
748
+ <span class="ee-message-meta-label">To:</span>
749
+ ${msg.to ? msg.to.map(t => `${t.name || ''} &lt;${t.address}&gt;`).join(', ') : ''}
750
+ </div>
751
+ ${
752
+ msg.cc && msg.cc.length
753
+ ? `
754
+ <div class="ee-message-meta-row">
755
+ <span class="ee-message-meta-label">Cc:</span>
756
+ ${msg.cc.map(c => `${c.name || ''} &lt;${c.address}&gt;`).join(', ')}
757
+ </div>
758
+ `
759
+ : ''
760
+ }
761
+ <div class="ee-message-meta-row">
762
+ <span class="ee-message-meta-label">Date:</span>
763
+ ${new Date(msg.date).toLocaleString()}
764
+ </div>
765
+ <div class="ee-message-meta-row">
766
+ <span class="ee-message-meta-label">Subject:</span>
767
+ ${msg.subject || '(no subject)'}
768
+ </div>
769
+ </div>
770
+ <div class="ee-message-body">
771
+ ${msg.text && msg.text.html ? msg.text.html : msg.text && msg.text.plain ? `<pre>${msg.text.plain}</pre>` : ''}
772
+ </div>
773
+ </div>
774
+ `;
775
+ viewer.innerHTML = html;
776
+
777
+ viewer.querySelector('[data-action="toggle-read"]').addEventListener('click', () => {
778
+ const currentlyUnseen = msg.unseen;
779
+ this.markAsRead(msg.id, currentlyUnseen);
780
+ });
781
+
782
+ viewer.querySelector('[data-action="delete"]').addEventListener('click', () => {
783
+ if (confirm('Delete this message?')) {
784
+ this.deleteMessage(msg.id);
785
+ }
786
+ });
787
+
788
+ viewer.querySelector('[data-action="move"]').addEventListener('change', e => {
789
+ const targetPath = e.target.value;
790
+ if (targetPath) {
791
+ this.moveMessage(msg.id, targetPath);
792
+ }
793
+ });
794
+ }
795
+
796
+ createLayout() {
797
+ if (typeof document === 'undefined' || !this.container) {
798
+ return;
799
+ }
800
+
801
+ this.container.innerHTML = `
802
+ <div class="ee-client">
803
+ <div class="ee-sidebar">
804
+ <div class="ee-loading">Loading folders...</div>
805
+ </div>
806
+ <div class="ee-message-list">
807
+ <div class="ee-empty-state">Select a folder</div>
808
+ </div>
809
+ <div class="ee-message-viewer">
810
+ <div class="ee-empty-state">Select a message to view</div>
811
+ </div>
812
+ </div>
813
+ `;
814
+ }
815
+
816
+ init() {
817
+ if (typeof document === 'undefined') {
818
+ console.warn('EmailEngineClient UI features are only available in browser environments');
819
+ return;
820
+ }
821
+
822
+ this.createStyles();
823
+ this.createLayout();
824
+ this.loadFolders();
825
+
826
+ setTimeout(() => {
827
+ const inbox =
828
+ this.folders.find(f => f.specialUse === '\\Inbox' || f.name.toLowerCase() === 'inbox') ||
829
+ this.folders[0];
830
+ if (inbox) {
831
+ this.loadMessages(inbox.path);
832
+ }
833
+ }, 500);
834
+ }
835
+ }
836
+
837
+ export function createEmailEngineClient(options) {
838
+ if (typeof options === 'string') {
839
+ options = {
840
+ container: typeof document !== 'undefined' ? document.getElementById(options) : null
841
+ };
842
+ } else if (typeof HTMLElement !== 'undefined' && options instanceof HTMLElement) {
843
+ options = {
844
+ container: options
845
+ };
846
+ } else if (typeof options === 'object' && options.containerId) {
847
+ options.container = typeof document !== 'undefined' ? document.getElementById(options.containerId) : null;
848
+ }
849
+
850
+ if (options.container && !options.container) {
851
+ throw new Error('Container element not found');
852
+ }
853
+
854
+ if (!options.apiUrl) {
855
+ console.warn('No API URL specified, using default http://127.0.0.1:3000');
856
+ }
857
+
858
+ if (!options.account) {
859
+ throw new Error('Account identifier is required');
860
+ }
861
+
862
+ return new EmailEngineClient(options);
863
+ }
864
+
865
+ export default EmailEngineClient;