-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf-export.js
More file actions
1292 lines (1291 loc) · 70.5 KB
/
pdf-export.js
File metadata and controls
1292 lines (1291 loc) · 70.5 KB
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
/* pdf-export.js
* Extracted from app.js on 2026-05-01.
* Defines the global generatePDF(mode) used by sendPlanEmail() in app.js.
* Loaded statically alongside app.js — splitting just keeps each file under
* 3,500 lines so they're easier to navigate and review.
*/
function generatePDF(mode){
if(!window.jspdf)return null;
const isSummaryOnly=(mode==='summary');
const{jsPDF}=window.jspdf;
/* Round-14: iPhone-optimized page for the Summary Card.
- Custom 115 mm-wide portrait page — narrow enough that an iPhone shows
the body text large at fit-to-width.
- Page height is COMPUTED from the user's actual plan size so we never
overflow the page or leave excessive blank space. Top zone + section
headers + N rows + interaction-notes panel + footer.
- Margin tightened from 18 to 9 mm to maximise usable text width.
- The Full Guide (mode!=='summary') keeps A4 — its multi-page layout
and cover need the wider canvas. */
let _summaryPageHeight = 260;
if(isSummaryOnly){
/* Pre-count supplements + estimate interaction count to size the page so
nothing overflows the footer. _allRecs() reads the same globals the
renderer uses below — defensive fallback if dependencies aren't loaded. */
let _nSupps = 10;
let _nInts = 6; // generous default
try {
if(typeof _allRecs === 'function' && typeof selectedSupps !== 'undefined') {
const _sel = _allRecs().filter(function(r){ return selectedSupps.has(r.n); });
_nSupps = _sel.length;
/* Estimate interactions: positive pairs across the stack + cautions.
Conservative — over-allocates rather than under-allocates. Each
interaction occupies one cell in a 2-column grid → ceil(N/2) rows. */
let _intEst = 0;
const _names = _sel.map(function(r){ return r.n; });
if(typeof getPairPartners === 'function'){
const _seen = new Set();
_names.forEach(function(n){
try {
getPairPartners(n).forEach(function(p){
if(!_names.includes(p)) return;
const k = [n,p].sort().join('||');
if(_seen.has(k)) return; _seen.add(k); _intEst++;
});
} catch(_){}
});
}
if(typeof getSuppCautionsIn === 'function'){
const _seenC = new Set();
_names.forEach(function(n){
try {
getSuppCautionsIn(n, _names).forEach(function(c){
const k = [n, c.with].sort().join('||');
if(_seenC.has(k)) return; _seenC.add(k); _intEst++;
});
} catch(_){}
});
}
_nInts = Math.max(_intEst, 0);
}
} catch(_){}
/* Round-17 — height formula reconciled with the actual drawn dimensions:
Top zone (wordmark + rule): 22 mm
Three section headers (Morning/Daytime/Night): 3 × 10.5 = 32 mm
Supplement rows: _nSupps × 11.5 mm
Interaction-notes header + rule: 16 mm
Interaction-notes grid (2-column, 9 mm/row): ceil(N/2) × 9 mm
Bottom buffer (footer rule + safe margin): 24 mm
Plus a 12 mm safety cushion so future small layout tweaks don't
immediately overflow again. */
const _intRows = Math.ceil(_nInts / 2);
_summaryPageHeight = 22 + 32 + _nSupps * 11.5 + (_nInts > 0 ? 16 + _intRows * 9 : 0) + 24 + 12;
if(_summaryPageHeight < 240) _summaryPageHeight = 240;
if(_summaryPageHeight > 700) _summaryPageHeight = 700;
}
const doc = isSummaryOnly
? new jsPDF({unit:'mm', format:[115, _summaryPageHeight]})
: new jsPDF({unit:'mm', format:'a4'});
const pw=doc.internal.pageSize.getWidth();
const ph=doc.internal.pageSize.getHeight();
const M = isSummaryOnly ? 9 : 18;
const TW=pw-M*2;
// Brand palette — Supplement Score (navy + green on white).
// Kept the original constant names so the rest of the renderer is untouched;
// semantics: DARK=navy ink, GOLD=brand green accent, PUR=dark green kicker,
// CREAM/WARM=white surfaces, RULE/TBRD=light gray hairlines, GRY=muted text.
const DARK=[31,42,61];const GOLD=[139,195,74];const PUR=[107,143,42];
const CREAM=[255,255,255];const WARM=[255,255,255];const RULE=[229,231,235];
const ALT=[249,250,247];const TBRD=[229,231,235];const GRY=[107,114,128];
let pageNum=1;
// Data
const recs=_allRecs();
const selRecs=recs.filter(r=>selectedSupps.has(r.n));
const age=document.getElementById('asl').value;
const sexLabel=sex==='fp'?'Pregnant woman':sex==='m'?'Male':'Female';
const monthYear=new Date().toLocaleDateString('en-US',{month:'long',year:'numeric'});
const condLabel=selectedConds.size?[...selectedConds].map(k=>CONDITIONS[k]?.label||k).join(', '):'General Wellbeing';
const medsLabel=selectedMeds.size?[...selectedMeds].map(k=>MEDS[k]?.label||k).join(', '):'None';
const allItems=selRecs.map(r=>{
const sup=_suppByName.get(r.n);const sc=sup?calcScore(sup):0;
const timing=getTimingLabel(r);const ints=INTERACT_MAP[r.n]||[];const hasMedInt=ints.length>0;
const tips=sup?.tips||'';
const foodHint=tips.toLowerCase().includes('fat')?'With fat':tips.toLowerCase().includes('empty stomach')?'Empty stomach':(tips.toLowerCase().includes('with food')||tips.toLowerCase().includes('with meal'))?'With food':'Any';
const effi=sup?.e||1;const safe=sup?.s||1;const rd=sup?.r||1;const so=sup?.o||1;
const onsetLabel=so>=5?'Immediate':so>=4?'Hours\u2013days':so>=3?'1\u20134 wks':so>=2?'4\u20138 wks':'8+ wks';
const tierLabel=sup?.t==='t1'?'Tier 1 \u2014 Highest Evidence':sup?.t==='t2'?'Tier 2 \u2014 Good Evidence':'Tier 3 \u2014 Emerging Evidence';
const cycleInfo=(function(s){if(!s)return'Continuous';const n=s.n.toLowerCase(),tag=(s.tag||'').toLowerCase();if(n.includes('ashwagandha'))return'8\u201312 wks on, 2\u20134 off';if(n.includes('rhodiola'))return'6\u20138 wks on, 2\u20134 off';if(n.includes('melatonin'))return'2\u20134 wks, reassess';if(tag.includes('adaptogen')||n.includes('ginseng'))return'6\u20138 wks on, 2\u20134 off';return'Continuous';})(sup);
const priLabel=r.p==='essential'?'Essential':r.p==='recommended'?'Recommended':'Consider';
const desc=sup?.desc||r.why||'';const dose=r.dose||'';
const intText=ints.map(i=>(i.type==='avoid'?'Avoid':'Caution')+' with '+(i.med||'')).join('; ');
return{r,sup,sc,timing,hasMedInt,ints,intText,foodHint,effi,safe,rd,so,onsetLabel,tierLabel,cycleInfo,priLabel,desc,dose,tips};
});
// Helpers
function sCol(sc){return sc>=80?GOLD:sc>=60?PUR:sc>=40?[140,150,150]:[180,185,185];}
function drawCircle(cx,cy,r,sc){
const c=sCol(sc);doc.setFillColor(c[0],c[1],c[2]);doc.circle(cx,cy,r,'F');
doc.setFont('times','normal');doc.setFontSize(r*2.9);doc.setTextColor(250,248,244);
doc.text(String(sc),cx,cy+r*0.4,{align:'center'});
doc.setFont('helvetica','normal');doc.setFontSize(5.5);doc.setTextColor(GOLD[0],GOLD[1],GOLD[2]);
doc.text('SCORE',cx,cy+r*0.88,{align:'center'});
}
function drawPips(x,y,n,total,pW=8,gap=1.5){
const pH=2.5;
for(let i=0;i<total;i++){
i<n?doc.setFillColor(GOLD[0],GOLD[1],GOLD[2]):doc.setFillColor(TBRD[0],TBRD[1],TBRD[2]);
doc.roundedRect(x+i*(pW+gap),y,pW,pH,1,1,'F');
}
}
// Brand logo lockup — leaf badge (navy outline, green ascending bar chart, green growth
// curve emerging from a gap in the lower-left of the leaf) + "Supplement Score" wordmark.
// Traced from the source artwork: asymmetric elongated leaf with a pointed top-right tip,
// soft right and left bulges, and a deliberate break in the outline where the stem exits.
function drawBrandLogo(x,y,opts){
opts=opts||{};
const s=opts.scale||1;
const layout=opts.layout||'stacked';
const iconOnly=!!opts.iconOnly;
const darkBg=!!opts.darkBg;
const showTagline=opts.tagline!==false;
const ink=darkBg?[255,255,255]:[31,42,61];
const grn=[139,195,74];
const W=14*s, H=18*s;
// Helper: stroke a multi-segment cubic bezier from absolute coordinates.
// segs is an array of [c1x,c1y, c2x,c2y, endX,endY] — all absolute mm.
function bezPath(startX,startY,segs){
const rels=[];let px=startX, py=startY;
for(let i=0;i<segs.length;i++){
const sg=segs[i];
rels.push([sg[0]-px,sg[1]-py, sg[2]-px,sg[3]-py, sg[4]-px,sg[5]-py]);
px=sg[4];py=sg[5];
}
doc.lines(rels,startX,startY,[1,1],'S',false);
}
doc.setDrawColor(ink[0],ink[1],ink[2]);
doc.setLineWidth(Math.max(0.55,0.95*s));
doc.setLineJoin('round');doc.setLineCap('round');
// Path A — right side: top-tip → right shoulder → right peak → bottom-right close
bezPath(x+W*0.58, y+H*0.03, [
[x+W*0.94,y+H*0.07, x+W*1.02,y+H*0.34, x+W*0.94,y+H*0.50],
[x+W*0.88,y+H*0.74, x+W*0.74,y+H*0.95, x+W*0.55,y+H*0.97]
]);
// Path B — left side: a hair below the bottom-right close (this gap is where the stem exits) up to top-tip
bezPath(x+W*0.22, y+H*0.86, [
[x+W*0.06,y+H*0.78, x+W*-0.02,y+H*0.58, x+W*0.05,y+H*0.42],
[x+W*0.10,y+H*0.20, x+W*0.30,y+H*0.05, x+W*0.58,y+H*0.03]
]);
// === BARS — three vertical green rectangles, ascending right ===
doc.setFillColor(grn[0],grn[1],grn[2]);
const barBase=y+H*0.78;
const barW=1.65*s, barGap=1.0*s;
const heights=[4.5*s, 6.7*s, 8.7*s];
const totalW=barW*3+barGap*2;
const clusterCx=x+W*0.56;
const firstX=clusterCx-totalW/2;
for(let i=0;i<3;i++){
const bx=firstX+i*(barW+barGap);
doc.rect(bx, barBase-heights[i], barW, heights[i], 'F');
}
// === GROWTH CURVE — emerges from below-left of the leaf, sweeps up to base of the first bar ===
doc.setDrawColor(grn[0],grn[1],grn[2]);
doc.setLineWidth(Math.max(0.7,1.15*s));
bezPath(x+W*0.02, y+H*1.03, [
[x+W*0.10,y+H*0.99, x+W*0.20,y+H*0.92, firstX+barW*0.5, barBase]
]);
// === WORDMARK ===
if(!iconOnly){
if(layout==='horizontal'){
const wmX=x+W+3*s;
const wmY=y+H*0.58+1.5*s;
doc.setFont('helvetica','bold');
doc.setFontSize(Math.max(8,12*s));
doc.setTextColor(ink[0],ink[1],ink[2]);
doc.text('Supplement ',wmX,wmY);
const wSupp=doc.getTextWidth('Supplement ');
doc.setTextColor(grn[0],grn[1],grn[2]);
doc.text('Score',wmX+wSupp,wmY);
} else {
const wmY=y+H+6*s;
const wmCx=x+W/2;
doc.setFont('helvetica','bold');
doc.setFontSize(11*s);
const wSupp=doc.getTextWidth('Supplement ');
const wScore=doc.getTextWidth('Score');
const wTot=wSupp+wScore;
doc.setTextColor(ink[0],ink[1],ink[2]);
doc.text('Supplement ',wmCx-wTot/2,wmY);
doc.setTextColor(grn[0],grn[1],grn[2]);
doc.text('Score',wmCx-wTot/2+wSupp,wmY);
if(showTagline){
doc.setFont('helvetica','normal');
doc.setFontSize(Math.max(5,5.5*s));
doc.setTextColor(ink[0],ink[1],ink[2]);
doc.text('KNOW WHAT WORKS.',wmCx,wmY+5*s,{align:'center'});
}
}
}
doc.setLineCap('butt');doc.setLineJoin('miter');
}
function footer(){
doc.setDrawColor(RULE[0],RULE[1],RULE[2]);doc.setLineWidth(0.25);doc.line(M,ph-12,pw-M,ph-12);
doc.setFont('helvetica','normal');doc.setFontSize(6.5);doc.setTextColor(GRY[0],GRY[1],GRY[2]);
doc.text('SUPPLEMENTSCORE.ORG',M,ph-7);
doc.setFont('times','italic');doc.setFontSize(7.5);doc.text('Page '+pageNum+' / '+totalPages,pw/2,ph-7,{align:'center'});
doc.setFont('helvetica','normal');doc.setFontSize(6.5);doc.text(monthYear.toUpperCase(),pw-M,ph-7,{align:'right'});
}
const totalPages=isSummaryOnly?1:(allItems.length+2+((typeof bloodWork!=='undefined'&&Object.keys(bloodWork).length>0)?1:0));
const essN=allItems.filter(x=>x.r.p==='essential').length;
const recN=allItems.filter(x=>x.r.p==='recommended').length;
const conN=allItems.filter(x=>x.r.p==='consider').length;
const intItems=allItems.filter(x=>x.hasMedInt);
if(!isSummaryOnly){
// ═══════════════════════════════════════
// PAGE 1 — COVER
// ═══════════════════════════════════════
// Masthead
doc.setFillColor(DARK[0],DARK[1],DARK[2]);doc.rect(0,0,pw,15,'F');
drawBrandLogo(M,3,{layout:'horizontal',scale:0.45,darkBg:true});
doc.setFont('helvetica','normal');doc.setFontSize(6.5);doc.setTextColor(GOLD[0],GOLD[1],GOLD[2]);
doc.text('PERSONALISED \u00B7 EVIDENCE-BASED \u00B7 '+monthYear.toUpperCase(),pw-M,9,{align:'right'});
// Hero — extends to y=131 so meta values (at y=117) sit on dark background before gold band
doc.setFillColor(DARK[0],DARK[1],DARK[2]);doc.rect(0,15,pw,116,'F');
doc.setFont('helvetica','normal');doc.setFontSize(7.5);doc.setTextColor(GOLD[0],GOLD[1],GOLD[2]);
doc.text('PERSONALISED SUPPLEMENT REPORT',M,34);
doc.setFont('times','normal');doc.setFontSize(46);doc.setTextColor(CREAM[0],CREAM[1],CREAM[2]);
doc.text('Your',M,56);doc.text('Optimal',M,72);doc.text('Protocol',M,88);
doc.setFont('times','italic');doc.setFontSize(14);doc.setTextColor(GOLD[0],GOLD[1],GOLD[2]);
const goalStr=condLabel.length>40?condLabel.substring(0,39)+'\u2026':condLabel;
doc.text('A curated plan for '+goalStr.toLowerCase(),M,97);
doc.setDrawColor(GOLD[0],GOLD[1],GOLD[2]);doc.setLineWidth(1);doc.line(M,103,M+20,103);doc.setLineWidth(0.2);
// Meta items
const mItms=[['PREPARED FOR',sexLabel+', '+age+' yrs'],['PRIMARY GOAL',condLabel.length>22?condLabel.substring(0,21)+'\u2026':condLabel],['MEDICATION',medsLabel.length>22?medsLabel.substring(0,21)+'\u2026':medsLabel],['SUPPLEMENTS',allItems.length+' in plan']];
mItms.forEach(([lbl,val],i)=>{
const mx=M+i*(TW/4);
doc.setFont('helvetica','normal');doc.setFontSize(6);doc.setTextColor(GOLD[0],GOLD[1],GOLD[2]);doc.text(lbl,mx,110);
doc.setFont('times','italic');doc.setFontSize(10);doc.setTextColor(CREAM[0],CREAM[1],CREAM[2]);doc.text(val,mx,117);
});
// Gold band — starts after hero area (y=131), meta values safely on dark bg above
doc.setFillColor(GOLD[0],GOLD[1],GOLD[2]);doc.rect(0,131,pw,14,'F');
doc.setFont('helvetica','normal');doc.setFontSize(7.5);doc.setTextColor(DARK[0],DARK[1],DARK[2]);
doc.text(allItems.length+' SUPPLEMENTS \u00B7 EVIDENCE-RANKED \u00B7 INTERACTION-CHECKED',M,140);
doc.setFont('times','italic');doc.setFontSize(8);doc.text('SupplementScore.org',pw-M,140,{align:'right'});
// Intro
let cy=153;
doc.setFont('helvetica','normal');doc.setFontSize(7);doc.setTextColor(PUR[0],PUR[1],PUR[2]);
doc.text('EDITORIAL NOTE',M,cy);cy+=8;
doc.setFont('times','normal');doc.setFontSize(10);doc.setTextColor(DARK[0],DARK[1],DARK[2]);
const introTxt='This report presents your personalised supplement protocol, ranked by scientific evidence and tailored to your '+goalStr.toLowerCase()+' goals. Each recommendation has been cross-referenced against your medications to flag any clinically relevant interactions.';
doc.splitTextToSize(introTxt,TW).forEach(l=>{doc.text(l,M,cy);cy+=5.2;});cy+=4;
// Next-up cue (single line, no faux contents box)
cy+=2;
doc.setFont('helvetica','normal');doc.setFontSize(7);doc.setTextColor(PUR[0],PUR[1],PUR[2]);
doc.text('NEXT \u00B7 PROTOCOL OVERVIEW, THEN '+allItems.length+' SUPPLEMENT DETAIL PAGES',M,cy+4);
// Disclaimer band at the foot of the cover (replaces the standalone disclaimer page when there's no blood work)
const dy=ph-30;
doc.setDrawColor(RULE[0],RULE[1],RULE[2]);doc.setLineWidth(0.25);doc.line(M,dy,pw-M,dy);
doc.setFont('helvetica','normal');doc.setFontSize(6);doc.setTextColor(PUR[0],PUR[1],PUR[2]);
doc.text('IMPORTANT',M,dy+6);
doc.setFont('times','italic');doc.setFontSize(8);doc.setTextColor(GRY[0],GRY[1],GRY[2]);
doc.splitTextToSize('Informational only. Not medical advice. Consult a qualified healthcare provider before starting any supplement regimen, especially if you have medical conditions or take prescription medications.',TW).forEach((l,i)=>{doc.text(l,M,dy+11+i*4.2);});
footer();
} // end !isSummaryOnly (cover)
// ═══════════════════════════════════════
// PAGE — SUMMARY TABLE (page 2 in full guide, page 1 in summary card)
// ═══════════════════════════════════════
if(!isSummaryOnly){doc.addPage();pageNum++;}
// Masthead — skipped in summary card for ink-friendly printing
if(!isSummaryOnly){
doc.setFillColor(DARK[0],DARK[1],DARK[2]);doc.rect(0,0,pw,20,'F');
doc.setFont('helvetica','bold');doc.setFontSize(7);doc.setTextColor(GOLD[0],GOLD[1],GOLD[2]);
drawBrandLogo(M,4.5,{layout:'horizontal',scale:0.5,darkBg:true});
doc.setFont('helvetica','normal');doc.setFontSize(7);doc.setTextColor(GOLD[0],GOLD[1],GOLD[2]);
doc.text('SECTION 01 \u00B7 SUMMARY OVERVIEW',pw-M,12,{align:'right'});
} else {
/* Round-15: replaced the leaf logo with a clean "SupplementScore.org"
wordmark. "SupplementScore" in dark navy bold, ".org" in brand green for
a subtle accent. Keeps the top of the page calm and readable. */
const _topY = 11;
doc.setFont('helvetica','bold');doc.setFontSize(13);doc.setTextColor(DARK[0],DARK[1],DARK[2]);
doc.text('SupplementScore', M, _topY);
const _wMark = doc.getTextWidth('SupplementScore');
doc.setFont('helvetica','normal');doc.setFontSize(13);doc.setTextColor(GOLD[0],GOLD[1],GOLD[2]);
doc.text('.org', M + _wMark, _topY);
/* Hairline rule under the wordmark for a proper editorial top. */
doc.setDrawColor(RULE[0],RULE[1],RULE[2]);doc.setLineWidth(0.25);
doc.line(M, _topY + 2.4, pw - M, _topY + 2.4);
}
let y = isSummaryOnly ? 20 : 28;
// ── Display helpers (defined early so top-right interaction card can use them) ──
// WinAnsi-safe text normalizer — jsPDF's built-in Helvetica/Times use WinAnsi
// encoding. Most common unicode glyphs (ellipsis, en/em dash, smart quotes,
// middle dot, ×) are in WinAnsi, but mathematical comparison operators and a
// few other glyphs are NOT — jsPDF falls back in a way that disrupts kerning
// (the "3 0 0 m g e x t r a c t" letter-spacing artefact). Only replace the
// codepoints that are actually outside WinAnsi.
const _winAnsiSafe=(s)=>{
if(!s)return s;
return String(s)
.replace(/\u2265/g,'>=') // ≥ (not in WinAnsi)
.replace(/\u2264/g,'<=') // ≤ (not in WinAnsi)
.replace(/\u2212/g,'-') // unicode minus → hyphen
.replace(/\u200B/g,''); // zero-width space
};
// Compact display form of a supplement name for the "paired with …" label
// and the interaction-notes card. Strips parentheticals and abbreviates a
// few long canonical names so the reference fits without clipping.
const _shortSuppName=(n)=>{
if(!n)return '';
const _aliases={
'Vitamin K2 (MK-7)':'K2',
'Vitamin D3':'D3',
'Vitamin C (moderate dose)':'Vitamin C',
'Vitamin E (mixed tocopherols)':'Vitamin E',
'NAC (N-Acetyl Cysteine)':'NAC',
'Omega-3 (EPA/DHA)':'Omega-3',
'Curcumin (bioavailable form)':'Curcumin',
'CoQ10 (Ubiquinol)':'CoQ10',
'Ferrous bisglycinate (gentle iron)':'Iron',
'Psyllium husk (Plantago ovata)':'Psyllium',
'Tart cherry (Montmorency)':'Tart cherry',
'EAAs (Essential amino acids)':'EAAs',
'Boswellia serrata':'Boswellia',
'Magnesium glycinate':'Magnesium',
'Collagen peptides':'Collagen'
};
if(Object.prototype.hasOwnProperty.call(_aliases,n))return _aliases[n];
let s=String(n).replace(/\([^)]*\)/g,'').trim().replace(/\s+/g,' ');
if(s.length>14)s=s.substring(0,13).trim()+'\u2026';
return s;
};
// ── INTERACTION NOTES data + render ──────────────────────────────────────
// Surfaces the "why" behind every red ⚠ on the rows below — which supp × which med
// and the clinical reason. The full guide draws this as a top-right floating card.
// The summary card stashes the data here and renders a full-width panel below the
// supplement list (so it never overlaps the title).
let _summaryNotesData=null;
{
const _selMedLabels=new Set([...selectedMeds].map(k=>MEDS[k]?.label).filter(Boolean));
const _hasSelMeds=selectedMeds.size>0;
// ─ (1) MED × SUPP rows — ONLY when a med is actually ticked on the intake form ─
const _medRows=[];const _medSeen=new Set();
if(_hasSelMeds){
allItems.forEach(item=>{
(item.ints||[]).forEach(int=>{
if(!_selMedLabels.has(int.med))return;
const k=item.r.n+'|'+int.med;
if(_medSeen.has(k))return;_medSeen.add(k);
_medRows.push({a:item.r.n,b:int.med,type:int.type,kind:'med'});
});
});
}
// ─ (2) SUPP × SUPP rows — only when BOTH supps are in the recommendation list ─
const _suppRows=[];const _suppSeen=new Set();
const _selNames=allItems.map(x=>x.r.n);
if(typeof getSuppCautionsIn==='function'){
allItems.forEach(item=>{
const hits=getSuppCautionsIn(item.r.n,_selNames);
hits.forEach(h=>{
// Dedup by unordered pair
const pairKey=[item.r.n,h.with].sort().join('||');
if(_suppSeen.has(pairKey))return;_suppSeen.add(pairKey);
_suppRows.push({a:item.r.n,b:h.with,type:h.severity||'caution',reason:h.reason||'',kind:'supp'});
});
});
}
// Per-(supp,med) short "why" strings. Falls back to a generic reason if no entry.
const _medReasonFor=(supp,med,type)=>{
const key=supp+'||'+med;
const map={
'CoQ10 (Ubiquinol)||Statins (cholesterol)':'Statins deplete CoQ10 — pair them',
'Berberine||Statins (cholesterol)':'Additive statin-like effect; watch LFTs',
'Vitamin B12||Metformin':'Metformin reduces B12 absorption',
'Berberine||Metformin':'Additive glucose lowering — hypoglycemia',
'Vitamin B12||PPIs (omeprazole, lansoprazole)':'Long-term PPIs reduce B12 absorption',
'Magnesium||PPIs (omeprazole, lansoprazole)':'Long-term PPIs deplete Mg',
'Vitamin K2 (MK-7)||Warfarin / Blood thinners':'Directly antagonises warfarin — avoid',
'Omega-3 (EPA/DHA)||Warfarin / Blood thinners':'Bleeding risk at high doses',
'Curcumin (bioavailable form)||Warfarin / Blood thinners':'May raise bleeding risk',
'Ginger (Zingiber officinale)||Warfarin / Blood thinners':'Mild antiplatelet — additive',
'NAC (N-Acetyl Cysteine)||Warfarin / Blood thinners':'May mildly raise INR',
'Iron||Levothyroxine / Thyroid meds':'Blocks thyroid absorption — space 4h',
'Calcium||Levothyroxine / Thyroid meds':'Blocks thyroid absorption — space 4h',
'Magnesium||Levothyroxine / Thyroid meds':'Blocks thyroid absorption — space 4h',
'Iodine||Levothyroxine / Thyroid meds':'Adds iodine load — monitor TSH',
'Selenium||Levothyroxine / Thyroid meds':'Alters thyroid hormone metabolism',
'Iron||Antibiotics':'Chelation — space >=2h apart',
'Calcium||Antibiotics':'Chelation — space >=2h apart',
'Magnesium||Antibiotics':'Chelation — space >=2h apart',
'Zinc||Antibiotics':'Chelation — space >=2h apart',
'Ashwagandha (KSM-66)||Benzodiazepines (Xanax, Valium)':'Additive sedation',
'L-Theanine||Benzodiazepines (Xanax, Valium)':'Additive sedation',
'Magnesium||Benzodiazepines (Xanax, Valium)':'Additive sedation',
'Melatonin||Benzodiazepines (Xanax, Valium)':'Additive sedation',
'Melatonin||Prescription sleep aids (Ambien, trazodone)':'Additive sedation',
'Ashwagandha (KSM-66)||Prescription sleep aids (Ambien, trazodone)':'Additive sedation',
'L-Theanine||Prescription sleep aids (Ambien, trazodone)':'Additive sedation',
'Glycine||Prescription sleep aids (Ambien, trazodone)':'Additive sedation',
'Magnesium||Prescription sleep aids (Ambien, trazodone)':'Additive sedation',
'5-HTP||SSRIs / SNRIs (antidepressants)':'Serotonin syndrome risk — avoid',
"St. John's Wort||SSRIs / SNRIs (antidepressants)":'Serotonin syndrome — avoid',
'Saffron (Crocus sativus)||SSRIs / SNRIs (antidepressants)':'Mild serotonergic effect',
'Berberine||Diabetes meds (insulin / sulfonylurea)':'Additive glucose lowering',
'Alpha-Lipoic Acid (ALA)||Diabetes meds (insulin / sulfonylurea)':'Additive glucose lowering',
'Chromium picolinate||Diabetes meds (insulin / sulfonylurea)':'Additive glucose lowering',
'Berberine||Blood pressure medication':'Additive BP lowering',
'CoQ10 (Ubiquinol)||Blood pressure medication':'Mild additive BP lowering',
'Taurine||Blood pressure medication':'Mild additive BP lowering',
'Omega-3 (EPA/DHA)||NSAIDs (ibuprofen, naproxen)':'Additive bleeding risk',
'NAC (N-Acetyl Cysteine)||Chemotherapy':'May blunt chemo — consult oncologist',
'Curcumin (bioavailable form)||Chemotherapy':'Consult oncologist — antioxidant',
'Omega-3 (EPA/DHA)||Chemotherapy':'Consult oncologist — antioxidant',
'Calcium||Corticosteroids (prednisone)':'Steroid-induced bone loss — replete',
'Vitamin D3||Corticosteroids (prednisone)':'Steroid-induced bone loss — replete',
'Zinc||Corticosteroids (prednisone)':'Steroid-induced Zn depletion'
};
if(map[key])return map[key];
return type==='avoid'?'Avoid combining — consult clinician':'Use with care — monitor';
};
// Sort: avoid before caution within each section
const _sortBySev=(a,b)=>(a.type==='avoid'?0:1)-(b.type==='avoid'?0:1);
_medRows.sort(_sortBySev);_suppRows.sort(_sortBySev);
// Summary card: stash data and render the panel later (full-width, below the list).
if(isSummaryOnly && (_medRows.length||_suppRows.length)){
_summaryNotesData={medRows:_medRows.slice(),suppRows:_suppRows.slice(),medReasonFor:_medReasonFor};
}
// Full guide: keep the existing top-right floating card behaviour.
if(!isSummaryOnly && (_medRows.length||_suppRows.length)){
const boxW=78;
const boxX=pw-M-boxW;
const boxY=isSummaryOnly?14:22;
const hdrH=5.8;
const subHdrH=4.2;
const rowH=4.4;
const moreH=3.6;
const padBottom=2.5;
const sectionGap=1.2;
// Budget: up to ~5 total rows across both sections. Favor meds, then supp-supp.
const totalBudget=5;
const medShow=Math.min(_medRows.length,Math.max(totalBudget-Math.min(_suppRows.length,2),_suppRows.length?3:totalBudget));
const suppShow=Math.min(_suppRows.length,totalBudget-medShow);
const medOver=_medRows.length-medShow;
const suppOver=_suppRows.length-suppShow;
// Compute height
let boxH=hdrH+padBottom;
if(medShow>0)boxH+=subHdrH+medShow*rowH+(medOver>0?moreH:0);
if(suppShow>0)boxH+=(medShow>0?sectionGap:0)+subHdrH+suppShow*rowH+(suppOver>0?moreH:0);
// Background — warm cream with amber hairline border
doc.setFillColor(253,244,226);
doc.setDrawColor(180,140,50);doc.setLineWidth(0.35);
doc.roundedRect(boxX,boxY,boxW,boxH,1.8,1.8,'FD');
// Main header
doc.setFont('helvetica','bold');doc.setFontSize(6.5);doc.setTextColor(140,70,20);
doc.text('INTERACTION NOTES',boxX+3,boxY+4);
doc.setDrawColor(210,185,140);doc.setLineWidth(0.2);
doc.line(boxX+3,boxY+hdrH-0.4,boxX+boxW-3,boxY+hdrH-0.4);
// Row renderer
const renderRow=(ry,rec,leftLabel,rightLabel)=>{
const typeLbl=rec.type==='avoid'?'AVOID':'CAUTION';
const typeCol=rec.type==='avoid'?[185,28,28]:[160,95,20];
doc.setFont('helvetica','bold');doc.setFontSize(5.5);doc.setTextColor(typeCol[0],typeCol[1],typeCol[2]);
doc.text(typeLbl,boxX+3,ry);
const typeW=doc.getTextWidth(typeLbl);
// Pair line
doc.setFont('helvetica','bold');doc.setFontSize(6.2);doc.setTextColor(60,45,30);
const pair=leftLabel+' + '+rightLabel;
const pairMaxW=boxW-6-typeW-2.5;
let pairTxt=pair;
while(doc.getTextWidth(pairTxt)>pairMaxW && pairTxt.length>3){
pairTxt=pairTxt.substring(0,pairTxt.length-1);
}
if(pairTxt!==pair)pairTxt=pairTxt.substring(0,pairTxt.length-1)+'\u2026';
doc.text(pairTxt,boxX+3+typeW+2.5,ry);
// Reason line
const reason=_winAnsiSafe(rec.kind==='med'?_medReasonFor(rec.a,rec.b,rec.type):(rec.reason||'Space apart — see full guide'));
doc.setFont('helvetica','normal');doc.setFontSize(5.8);doc.setTextColor(110,80,50);
const reasonMaxW=boxW-6;
let reasonTxt=reason;
while(doc.getTextWidth(reasonTxt)>reasonMaxW && reasonTxt.length>3){
reasonTxt=reasonTxt.substring(0,reasonTxt.length-1);
}
if(reasonTxt!==reason)reasonTxt=reasonTxt.substring(0,reasonTxt.length-1)+'\u2026';
doc.text(reasonTxt,boxX+3,ry+2.1);
};
let ry=boxY+hdrH+1.6;
// ── Section: With your meds ──
if(medShow>0){
doc.setFont('helvetica','bold');doc.setFontSize(5.5);doc.setTextColor(130,95,45);
doc.text('WITH YOUR MEDS',boxX+3,ry+1);
ry+=subHdrH;
_medRows.slice(0,medShow).forEach(n=>{
const suppShort=_winAnsiSafe(_shortSuppName(n.a));
const medShort=_winAnsiSafe(n.b).replace(/\s*\([^)]*\)\s*/g,' ').trim();
renderRow(ry,n,suppShort,medShort);
ry+=rowH;
});
if(medOver>0){
doc.setFont('helvetica','italic');doc.setFontSize(5.5);doc.setTextColor(140,100,60);
doc.text('+ '+medOver+' more — see full guide',boxX+3,ry+1.4);
ry+=moreH;
}
}
// ── Section: Within your stack ──
if(suppShow>0){
if(medShow>0)ry+=sectionGap;
doc.setFont('helvetica','bold');doc.setFontSize(5.5);doc.setTextColor(130,95,45);
doc.text('WITHIN YOUR STACK',boxX+3,ry+1);
ry+=subHdrH;
_suppRows.slice(0,suppShow).forEach(n=>{
const aShort=_winAnsiSafe(_shortSuppName(n.a));
const bShort=_winAnsiSafe(_shortSuppName(n.b));
renderRow(ry,n,aShort,bShort);
ry+=rowH;
});
if(suppOver>0){
doc.setFont('helvetica','italic');doc.setFontSize(5.5);doc.setTextColor(140,100,60);
doc.text('+ '+suppOver+' more — see full guide',boxX+3,ry+1.4);
}
}
}
}
// Title block — full guide only; summary card goes straight to supplement rows
if(!isSummaryOnly){
doc.setFont('helvetica','normal');doc.setFontSize(7);doc.setTextColor(PUR[0],PUR[1],PUR[2]);
doc.text('SUMMARY',M,y);y+=8;
doc.setFont('times','normal');doc.setFontSize(22);doc.setTextColor(DARK[0],DARK[1],DARK[2]);
doc.text(allItems.length+' Supplements,',M,y);y+=9;
doc.setFont('times','italic');doc.setFontSize(22);doc.setTextColor(GOLD[0],GOLD[1],GOLD[2]);
doc.text('One Coherent Protocol',M,y);y+=5;
doc.setDrawColor(RULE[0],RULE[1],RULE[2]);doc.setLineWidth(0.25);doc.line(M,y,pw-M,y);y+=6;
} else {
y+=3; // small top margin before first section header
}
// Food-icon + warning drawing helpers
const FOOD_COL={with:[139,195,74],away:[31,42,61]}; // brand: green for "with food", navy for "away"
// Explicit, evidence-based overrides where the tips-keyword sniff is unreliable.
// Keys are supplement names (must match r.n); value is the canonical foodCat.
const _foodCatOverride={
'Berberine':'with', // always with meals — blunts post-prandial glucose
'Berberine HCl (sustained release)':'with',
'Dihydroberberine (DHB)':'with',
'Psyllium husk (Plantago ovata)':'any', // psyllium itself: any timing. Separation from other supps handled in Full Guide
'Psyllium husk (soluble fibre)':'any',
'Iron':'away', // absorbs far better on an empty stomach
'Ferrous bisglycinate (gentle iron)':'away',
'NAC (N-Acetyl Cysteine)':'any', // flexible — no strict food rule
'Creatine monohydrate':'any',
'Collagen peptides':'any',
'Tart cherry (Montmorency)':'any',
'EAAs (Essential amino acids)':'any',
'Glycine':'any',
'L-Theanine':'any',
'Vitamin D3':'with', // fat-soluble — with a fatty meal
'Vitamin K2 (MK-7)':'with',
'Vitamin E (mixed tocopherols)':'with',
'Omega-3 (EPA/DHA)':'with',
'Curcumin (bioavailable form)':'with',
'Boswellia serrata':'with',
'CoQ10 (Ubiquinol)':'with',
'Zinc':'with' // with food to reduce nausea
};
const _foodCat=(fh,name)=>{
if(name && Object.prototype.hasOwnProperty.call(_foodCatOverride,name)){
return _foodCatOverride[name];
}
if(!fh)return'any';
const f=fh.toLowerCase();
// "empty stomach" and "without food" collapsed into a single "away from food" category
if(f.includes('empty')||f.includes('without food')||f.includes('fasted'))return'away';
if(f.includes('food')||f.includes('fat')||f.includes('meal'))return'with';
return'any';
};
const drawFoodIcon=(cx,cy,cat)=>{
const r=2.2;
if(cat==='any'){
doc.setDrawColor(170,160,145);doc.setLineWidth(0.45);
doc.circle(cx,cy,r,'S');
} else {
const c=FOOD_COL[cat];
doc.setFillColor(c[0],c[1],c[2]);doc.circle(cx,cy,r,'F');
doc.setFont('helvetica','bold');doc.setFontSize(6);doc.setTextColor(255,255,255);
// Use ASCII letters — jsPDF's built-in fonts don't support Unicode glyphs
const ch=cat==='with'?'F':'A';
doc.text(ch,cx,cy+1.05,{align:'center'});
}
};
const drawWarnIcon=(cx,cy)=>{
doc.setFillColor(254,226,226);
doc.roundedRect(cx-2.2,cy-2.2,4.4,4.4,0.8,0.8,'F');
doc.setFont('helvetica','bold');doc.setFontSize(6.5);doc.setTextColor(185,28,28);
doc.text('!',cx,cy+1.1,{align:'center'});
};
// Small chain-link glyph to indicate "this row is paired with another supplement"
const drawPairIcon=(cx,cy)=>{
doc.setDrawColor(GOLD[0],GOLD[1],GOLD[2]);doc.setLineWidth(0.55);
// two interlocked ellipses — reads as a chain link at small size
doc.ellipse(cx-1.1,cy,1.4,0.85,'S');
doc.ellipse(cx+1.1,cy,1.4,0.85,'S');
};
// Legend bar \u2014 full guide only; summary card uses inline badges instead
if(!isSummaryOnly){
const legH=8.5;
doc.setFillColor(WARM[0],WARM[1],WARM[2]);doc.rect(M,y,TW,legH,'F');
doc.setFillColor(GOLD[0],GOLD[1],GOLD[2]);doc.rect(M,y,1.2,legH,'F');
let lx=M+5;
doc.setFont('helvetica','bold');doc.setFontSize(6.5);doc.setTextColor(PUR[0],PUR[1],PUR[2]);
doc.text('FOOD',lx,y+5.5);lx+=doc.getTextWidth('FOOD')+3.5;
const legItems=[['with','With food'],['away','Away from food'],['any','Doesn\u2019t matter']];
legItems.forEach(([cat,lbl])=>{
drawFoodIcon(lx+2,y+4.2,cat);
doc.setFont('helvetica','normal');doc.setFontSize(7);doc.setTextColor(DARK[0],DARK[1],DARK[2]);
doc.text(lbl,lx+5,y+5.5);
lx+=5+doc.getTextWidth(lbl)+4.5;
});
// Separator between FOOD and INTERACTION sections
doc.setDrawColor(RULE[0],RULE[1],RULE[2]);doc.setLineWidth(0.3);
doc.line(lx-1.5,y+1.8,lx-1.5,y+legH-1.8);lx+=1.5;
drawPairIcon(lx+2.2,y+4.2);
doc.setFont('helvetica','normal');doc.setFontSize(7);doc.setTextColor(DARK[0],DARK[1],DARK[2]);
doc.text('Pair w/ \u2026',lx+5.5,y+5.5);
lx+=5.5+doc.getTextWidth('Pair w/ \u2026')+4.5;
doc.setDrawColor(RULE[0],RULE[1],RULE[2]);doc.setLineWidth(0.3);
doc.line(lx-1.5,y+1.8,lx-1.5,y+legH-1.8);lx+=1.5;
drawWarnIcon(lx+2,y+4.2);
doc.setFont('helvetica','normal');doc.setFontSize(7);doc.setTextColor(DARK[0],DARK[1],DARK[2]);
doc.text('May interact',lx+5,y+5.5);
y+=legH+5;
}
// ── Build row order: within each timing group, away-from-food first (score desc), then others (score desc), then apply pair adjacency ──
const _groupBy=(arr,keyFn)=>{const o={};arr.forEach(x=>{const k=keyFn(x);if(!o[k])o[k]=[];o[k].push(x);});return o;};
const _timeGrp=(t)=>t==='Night'?'Night':(t==='Morning'?'Morning':'Daytime');
const itemsWithCat=allItems.map(it=>({...it,foodCat:_foodCat(it.foodHint,it.r.n),timeGroup:_timeGrp(it.timing.time)}));
const selNames=itemsWithCat.map(x=>x.r.n);
// Pair-follow overrides — when a flexible-timing supp and its clinically-anchored pair
// partner are both selected, move the flexible one into the partner's timing group so
// they're taken together. This is the whole point of flagging pairs.
// Format: [flexibleSupp, anchorSupp, targetGroup, foodHintOverride?]
const _pairFollowRules=[
// D3's "morning" placement is soft (cortisol/energy folklore); D3 is fat-soluble and stored.
// If Mg glycinate is selected, co-locate D3 with Mg at night ("with dinner, fat").
['Vitamin D3','Magnesium glycinate','Night','With dinner (fatty meal)'],
['Vitamin D3','Magnesium L-threonate','Night','With dinner (fatty meal)'],
['Vitamin D3','Magnesium','Night','With dinner (fatty meal)'],
// NAC: do NOT auto-relocate to Night. The Glycine pairing is a glutathione-synthesis
// pairing (both contribute to the GSH pool over the day), not a co-timing requirement.
// Keeping NAC in its natural slot also balances stack density when night is heavy.
// Collagen is flexible; Vit C anchors morning (iron absorption). Move Collagen to morning.
['Collagen peptides','Vitamin C (moderate dose)','Morning',null],
// Vit E piggybacks on Omega-3's morning fatty-meal window (same absorption requirement).
['Vitamin E (mixed tocopherols)','Omega-3 (EPA/DHA)','Morning',null],
// Curcumin + Boswellia is a classic anti-inflammatory pair (synergistic joint/inflammation
// support). Both need a meal containing fat for absorption — co-locate at Daytime (lunch)
// so the user takes them together with the same fatty meal.
['Boswellia serrata','Curcumin (bioavailable form)','Daytime','With a meal containing fat'],
['Curcumin (bioavailable form)','Boswellia serrata','Daytime','With a meal containing fat']
];
{
const _selNameSet=new Set(selNames);
_pairFollowRules.forEach(([flex,anchor,tgt,foodHintOverride])=>{
if(!_selNameSet.has(flex)||!_selNameSet.has(anchor))return;
itemsWithCat.forEach(it=>{
if(it.r.n!==flex)return;
it.timeGroup=tgt;
if(it.timing)it.timing={...it.timing,time:tgt};
if(foodHintOverride)it.foodHint=foodHintOverride;
});
});
}
const byGroup=_groupBy(itemsWithCat,x=>x.timeGroup);
const groupOrder=['Morning','Daytime','Night'];
const orderedByGroup={};
groupOrder.forEach(gName=>{
const items=byGroup[gName]||[];
// sort: away-from-food first (strictest timing), then rest; each by score desc
const away=items.filter(x=>x.foodCat==='away').sort((a,b)=>b.sc-a.sc);
const rest=items.filter(x=>x.foodCat!=='away').sort((a,b)=>b.sc-a.sc);
let ordered=[...away,...rest];
// Apply pair adjacency — pull each item's partner right after it if both in this group
const placed=new Set();const result=[];
ordered.forEach(it=>{
if(placed.has(it.r.n))return;
result.push(it);placed.add(it.r.n);
const partners=getPairPartners(it.r.n);
partners.forEach(pn=>{
if(placed.has(pn))return;
const partner=ordered.find(x=>x.r.n===pn&&!placed.has(x.r.n));
if(partner){result.push({...partner,_pairAnchor:it.r.n});placed.add(pn);}
});
});
orderedByGroup[gName]=result;
});
// Cross-group pair annotation — if a supplement's partner is in the protocol but not
// adjacent (e.g. Vitamin D3 in Morning pairs with Vitamin K2 (MK-7) in Daytime), surface
// the partner with a "Pairs with …" label on the row, even across timing groups.
{
const _selSet=new Set(selNames);
// Build a per-item lookup of which partner it is already adjacent-paired with (either
// the row's own _pairAnchor, or the next row if this one IS the anchor).
const _adjPartnerByName=new Map();
groupOrder.forEach(gName=>{
const arr=orderedByGroup[gName]||[];
arr.forEach((it,i)=>{
if(it._pairAnchor){
_adjPartnerByName.set(it.r.n,it._pairAnchor);
// Mark the anchor too so it doesn't also get a cross-group label for the same partner.
if(!_adjPartnerByName.has(it._pairAnchor))_adjPartnerByName.set(it._pairAnchor,it.r.n);
}
});
});
groupOrder.forEach(gName=>{
const arr=orderedByGroup[gName]||[];
arr.forEach(it=>{
if(it._pairAnchor)return; // already labelled by adjacent pair
const adjPartner=_adjPartnerByName.get(it.r.n);
const partners=getPairPartners(it.r.n).filter(pn=>_selSet.has(pn)&&pn!==adjPartner);
if(partners.length){
// Prefer a partner in a different timing group (more useful cue for the reader).
const otherGroup=partners.find(pn=>{
for(const g of groupOrder){
if((orderedByGroup[g]||[]).some(x=>x.r.n===pn))return g!==gName;
}
return false;
});
it._remotePair=otherGroup||partners[0];
}
});
});
}
// Row geometry
/* Round-13: in summary mode the row is taller so we can stack name+badge
on the top line and the (often long) dose on a second line. The previous
single-line layout broke at iPhone width — name + badge + right-aligned
dose all overlapped each other. */
const rH = isSummaryOnly ? 11.5 : 7.5;
const iconX=M+4; // food icon center x
const nameX=M+9; // supplement name start x
const doseOffsetPad=2; // px padding between name end and dose start
const intRightX=pw-M-2; // right edge for interaction text
// Helper: does name pair bar need to extend top/bottom? computed as adjacent pairing
const drawGroupBand=(label,count)=>{
if(isSummaryOnly){
// Section header: small gray uppercase label + hairline rule
y+=5;
doc.setFont('helvetica','normal');doc.setFontSize(6.5);doc.setTextColor(GRY[0],GRY[1],GRY[2]);
doc.text(label.toUpperCase(),M,y);
y+=2;
doc.setDrawColor(RULE[0],RULE[1],RULE[2]);doc.setLineWidth(0.25);
doc.line(M,y,pw-M,y);
y+=3.5;
} else {
const bH=6;
doc.setFillColor(GOLD[0],GOLD[1],GOLD[2]);doc.rect(M,y,TW,bH,'F');
doc.setFont('helvetica','bold');doc.setFontSize(7);doc.setTextColor(DARK[0],DARK[1],DARK[2]);
doc.text(label.toUpperCase(),M+3,y+4.2);
y+=bH+1;
}
};
// Pair-group bracket — draws a "[" with rounded-feel corners and a midline tick
// along the left margin spanning the rows of an adjacent-pair block. Summary card only.
const drawPairBracket=(y1,y2)=>{
const bx=M-3.0; // bracket vertical x — sits in the left margin
const arm=1.6; // length of the top/bottom horizontal arms
const inset=1.8; // top/bottom inset so tips align with icon centers
const top=y1+inset;
const bot=y2-inset;
if(bot<=top)return;
doc.setDrawColor(GOLD[0],GOLD[1],GOLD[2]);doc.setLineWidth(0.5);
doc.line(bx+arm,top,bx,top);
doc.line(bx,top,bx,bot);
doc.line(bx,bot,bx+arm,bot);
// small inward tick at the midline (curly-brace cue)
const my=(top+bot)/2;
doc.line(bx-0.7,my,bx,my);
};
// Draw rows per group
groupOrder.forEach(gName=>{
const items=orderedByGroup[gName];
if(!items||!items.length)return;
drawGroupBand(gName,items.length);
// Pre-compute which rows are part of a pair block (item i pairs with item i-1 or i+1)
const isPairBlock=items.map((it,i)=>{
const prev=items[i-1];const next=items[i+1];
const partnersOf=getPairPartners(it.r.n);
const prevPaired=prev&&partnersOf.includes(prev.r.n);
const nextPaired=next&&partnersOf.includes(next.r.n);
return prevPaired||nextPaired;
});
let rowIdx=0;
let pairStartY=null; // tracks the top of the current adjacent-pair run (summary card only)
items.forEach((item,i)=>{
const inPair=isPairBlock[i];
// ── Summary card row (Round-13 stacked layout) ──────────────────────────
// Top line: Supplement name (bold) + small food badge to the right
// Second : Dose, muted gray, full-width, single line with width-aware truncation.
// Single-line dose is intentional — keeps every row exactly the same height
// so the layout never collides with the next supplement on a narrow phone page.
if(isSummaryOnly){
const topY = y + 4;
// Truncate-to-width helper used for both name and dose.
const _truncW = function(txt, maxW){
if(doc.getTextWidth(txt) <= maxW) return txt;
let t = txt;
while(t.length > 1 && doc.getTextWidth(t + '…') > maxW) t = t.substring(0, t.length - 1);
return t + '…';
};
// ── Name (bold, fits with room reserved for the badge) ──
doc.setFont('helvetica','bold');doc.setFontSize(9);doc.setTextColor(DARK[0],DARK[1],DARK[2]);
const _nmRaw = _winAnsiSafe(item.r.n);
// Pre-measure how much room the badge will need so the name truncates
// BEFORE bumping into it — the previous version used a fixed reserve
// which over-clipped names with no badge and under-clipped long names
// with a badge.
let _badgeReserve = 0;
if(item.foodCat !== 'any'){
const _bTxt = item.foodCat === 'with' ? 'w/ food' : 'empty stomach';
doc.setFont('helvetica','normal');doc.setFontSize(6);
_badgeReserve = doc.getTextWidth(_bTxt) + 3.4 + 3; // pill + 3mm gap
doc.setFont('helvetica','bold');doc.setFontSize(9);
}
const _nameMaxW = TW - _badgeReserve;
const _nm = _truncW(_nmRaw, _nameMaxW);
doc.text(_nm, M, topY);
const _nameW = doc.getTextWidth(_nm);
// ── Food badge sitting just to the right of the name ──
if(item.foodCat !== 'any'){
const _bdgTxt = item.foodCat === 'with' ? 'w/ food' : 'empty stomach';
doc.setFont('helvetica','normal');doc.setFontSize(6);
const _bw = doc.getTextWidth(_bdgTxt) + 3.4;
const _bx = M + _nameW + 3;
const _by = topY - 3;
if(item.foodCat === 'with'){
doc.setFillColor(220,252,231);doc.roundedRect(_bx,_by,_bw,3.8,1,1,'F');
doc.setTextColor(22,101,52);
} else {
doc.setFillColor(243,244,246);doc.roundedRect(_bx,_by,_bw,3.8,1,1,'F');
doc.setTextColor(107,114,128);
}
doc.text(_bdgTxt, _bx + 1.7, _by + 2.7);
}
// ── Dose on the second line, single line, width-aware truncation ──
const _sd = _winAnsiSafe(item.dose.split(';')[0].trim());
doc.setFont('helvetica','normal');doc.setFontSize(7.8);doc.setTextColor(GRY[0],GRY[1],GRY[2]);
const _doseLine = _truncW(_sd, TW);
doc.text(_doseLine, M, topY + 4);
// ── Row separator at exact bottom of the row ──
doc.setDrawColor(TBRD[0],TBRD[1],TBRD[2]);doc.setLineWidth(0.15);
doc.line(M, y + rH, pw - M, y + rH);
y += rH; rowIdx++;
return;
}
// ── Full guide row ───────────────────────────────────────────────────────
// Row background:
// • full guide → cream highlight + gold left bar across pair rows, zebra elsewhere
// • summary card → handled above
if(!isSummaryOnly){
if(inPair){
doc.setFillColor(255,251,232);doc.rect(M,y,TW,rH,'F');
// Gold left bar (3px wide) — only across paired rows
doc.setFillColor(GOLD[0],GOLD[1],GOLD[2]);doc.rect(M,y,1.2,rH,'F');
} else if(rowIdx%2===1){
doc.setFillColor(ALT[0],ALT[1],ALT[2]);doc.rect(M,y,TW,rH,'F');
}
} else if(inPair && pairStartY===null){
pairStartY=y;
}
// Food icon
drawFoodIcon(iconX,y+rH/2,item.foodCat);
// Name (WinAnsi-normalised)
doc.setFont('times','normal');doc.setFontSize(10);doc.setTextColor(DARK[0],DARK[1],DARK[2]);
const nmRaw=_winAnsiSafe(item.r.n);
const nm=nmRaw.length>42?nmRaw.substring(0,41)+'\u2026':nmRaw;
doc.text(nm,nameX,y+rH/2+1.3);
const nameW=doc.getTextWidth(nm);
// Width-based truncation helper (used for both partner label and dose text)
const truncToWidth=(txt,maxW)=>{
if(doc.getTextWidth(txt)<=maxW)return txt;
let t=txt;
while(t.length>1 && doc.getTextWidth(t+'\u2026')>maxW)t=t.substring(0,t.length-1);
return t+'\u2026';
};
// Chain-link glyph + compact "w/ <partner>" label when paired with another listed supplement
const pairPartnerName=item._pairAnchor||item._remotePair;
let afterNameX=nameX+nameW;
if(pairPartnerName){
drawPairIcon(afterNameX+3.5,y+rH/2);
afterNameX+=6.2;
// Partner name in muted gold so the pairing is legible at a glance
const partnerShort=_winAnsiSafe(_shortSuppName(pairPartnerName));
doc.setFont('helvetica','normal');doc.setFontSize(7);doc.setTextColor(150,115,40);
// Budget: reserve space for dose (~24mm) and the right-side interaction icon
const partnerMaxW=Math.max(8, intRightX - afterNameX - 24 - 7);
const partnerFit=truncToWidth(partnerShort, partnerMaxW);
doc.text(partnerFit, afterNameX+1.5, y+rH/2+1.1);
afterNameX += 1.5 + doc.getTextWidth(partnerFit) + 2;
}
// Dose (beside name, grey) — width-based truncation so the full dose shows when space allows
const sd=_winAnsiSafe(item.dose.split(';')[0].trim());
doc.setFont('helvetica','normal');doc.setFontSize(7.5);doc.setTextColor(GRY[0],GRY[1],GRY[2]);
const doseX=afterNameX+doseOffsetPad+1;
// Reserve ~7mm at the right for the interaction warning icon
const hasMedIntSel=(function(){
const ints=item.ints||[];
if(!ints.length)return false;
const selectedMedLabels=new Set([...selectedMeds].map(k=>MEDS[k]?.label).filter(Boolean));
return ints.some(it=>selectedMedLabels.has(it.med));
})();
const needsIntIcon=(item.ints||[]).length>0;
const rightReserve=needsIntIcon?(hasMedIntSel&&!isSummaryOnly?14:7):0;
const doseMaxW=Math.max(10,intRightX-doseX-rightReserve);
const sdFit=truncToWidth(sd,doseMaxW);
doc.text(sdFit,doseX,y+rH/2+1.1);
const doseW=doc.getTextWidth(sdFit);
let extraRight=doseX+doseW+3;
// Supp-supp caution note (red) — FULL GUIDE ONLY. The Summary Card keeps rows quiet.
if(!isSummaryOnly){
const cau=getSuppCaution(item.r.n,selNames);
if(cau){
const cauLbl=_winAnsiSafe(cau.reason+' with '+cau.with);
// Clip to the space actually available so nothing overflows the page margin
const cauMaxW=Math.max(10,intRightX-extraRight-rightReserve-2);
const cauShort=truncToWidth(cauLbl,cauMaxW-4);
doc.setFont('helvetica','normal');doc.setFontSize(6.8);
const cw=doc.getTextWidth(cauShort)+4;
doc.setFillColor(253,228,228);doc.roundedRect(extraRight,y+rH/2-2.5,cw,4.6,1,1,'F');
doc.setTextColor(138,42,42);doc.text(cauShort,extraRight+2,y+rH/2+0.9);
}
}
// Medication interaction (right-aligned "!" icon, optional med label in full guide only)
if(needsIntIcon){
if(hasMedIntSel && !isSummaryOnly){
const ints=item.ints||[];
const selectedMedLabels=new Set([...selectedMeds].map(k=>MEDS[k]?.label).filter(Boolean));
const hitSel=ints.filter(it=>selectedMedLabels.has(it.med));
const medLblRaw=_winAnsiSafe(hitSel[0].med);
const medLbl=medLblRaw.length>18?medLblRaw.substring(0,17)+'\u2026':medLblRaw;
doc.setFont('helvetica','normal');doc.setFontSize(8);doc.setTextColor(185,28,28);
const tw=doc.getTextWidth(medLbl);
doc.text(medLbl,intRightX,y+rH/2+1.1,{align:'right'});
drawWarnIcon(intRightX-tw-3,y+rH/2);
} else {
drawWarnIcon(intRightX-1.5,y+rH/2);
}
}
// Bottom border
doc.setDrawColor(TBRD[0],TBRD[1],TBRD[2]);doc.setLineWidth(0.15);doc.line(M,y+rH,pw-M,y+rH);
y+=rH;rowIdx++;
// Summary card: when a paired run ends, draw the bracket spanning the run.
if(isSummaryOnly){
const nextInPair=(i+1<items.length)&&isPairBlock[i+1];
if(inPair && !nextInPair && pairStartY!==null){
drawPairBracket(pairStartY,y);
pairStartY=null;
}
}
});
});
y+=8;
// ── Interactions panel (summary card only): Pair → Caution → Avoid ─────────
if(isSummaryOnly){
// ── 1. Collect negative interactions (supp–supp + supp–med) ────────────────
// Normalise supplement names for dedup: strip parenthetical forms & common suffixes
const _normName=n=>{
let s=n.replace(/\s*\([^)]*\)/g,'').trim();
// collapse magnesium variants
s=s.replace(/^Magnesium\s+\S+$/i,'Magnesium');
// collapse vitamin C variants
s=s.replace(/^Vitamin C\b.*/i,'Vitamin C');
return s.toLowerCase();
};
// Build negative set from _summaryNotesData (supp–med rows)
const _negMap=new Map(); // key: norm(a)+'||'+norm(b) sorted → {a,b,severity,reason,_kind}
const _mergeNeg=(a,b,severity,reason,kind)=>{
const k=[_normName(a),_normName(b)].sort().join('||');
if(!_negMap.has(k)||severity==='avoid')_negMap.set(k,{a,b,severity,reason,_kind:kind});
};
if(_summaryNotesData){
_summaryNotesData.medRows.forEach(r=>_mergeNeg(r.a,r.b,r.type||'caution',r.reason||'','med'));
_summaryNotesData.suppRows.forEach(r=>_mergeNeg(r.a,r.b,r.type||'caution',r.reason||'','supp'));
}
// Also call getSuppCautionsIn directly for supp–supp negatives (independent of _summaryNotesData)
if(typeof getSuppCautionsIn==='function'){