/**
 * Minified by jsDelivr using Terser v5.37.0.
 * Original file: /npm/shlinker@0.6.2/dist/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
import{jsx as _jsx,jsxs as _jsxs,Fragment as _Fragment}from"preact/jsx-runtime";import*as preact from"preact";import{useState,useEffect}from"preact/hooks";import*as qrcode from"qrcode";import pako from"pako";import*as jose from"jose";import smartLogo from"./smart-logo.svg";import prettyBytes from"pretty-bytes";const Download04Icon=()=>_jsxs("svg",{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24",width:24,height:24,fill:"none",children:[_jsx("path",{d:"M12 14.5L12 4.5M12 14.5C11.2998 14.5 9.99153 12.5057 9.5 12M12 14.5C12.7002 14.5 14.0085 12.5057 14.5 12",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round"}),_jsx("path",{d:"M20 16.5C20 18.982 19.482 19.5 17 19.5H7C4.518 19.5 4 18.982 4 16.5",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round"})]}),QrCodeIcon=()=>_jsxs("svg",{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24",width:24,height:24,fill:"none",children:[_jsx("path",{d:"M3 6C3 4.58579 3 3.87868 3.43934 3.43934C3.87868 3 4.58579 3 6 3C7.41421 3 8.12132 3 8.56066 3.43934C9 3.87868 9 4.58579 9 6C9 7.41421 9 8.12132 8.56066 8.56066C8.12132 9 7.41421 9 6 9C4.58579 9 3.87868 9 3.43934 8.56066C3 8.12132 3 7.41421 3 6Z",stroke:"currentColor",strokeWidth:"1.5"}),_jsx("path",{d:"M3 18C3 16.5858 3 15.8787 3.43934 15.4393C3.87868 15 4.58579 15 6 15C7.41421 15 8.12132 15 8.56066 15.4393C9 15.8787 9 16.5858 9 18C9 19.4142 9 20.1213 8.56066 20.5607C8.12132 21 7.41421 21 6 21C4.58579 21 3.87868 21 3.43934 20.5607C3 20.1213 3 19.4142 3 18Z",stroke:"currentColor",strokeWidth:"1.5"}),_jsx("path",{d:"M3 12L9 12",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round"}),_jsx("path",{d:"M12 3V8",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round"}),_jsx("path",{d:"M15 6C15 4.58579 15 3.87868 15.4393 3.43934C15.8787 3 16.5858 3 18 3C19.4142 3 20.1213 3 20.5607 3.43934C21 3.87868 21 4.58579 21 6C21 7.41421 21 8.12132 20.5607 8.56066C20.1213 9 19.4142 9 18 9C16.5858 9 15.8787 9 15.4393 8.56066C15 8.12132 15 7.41421 15 6Z",stroke:"currentColor",strokeWidth:"1.5"}),_jsx("path",{d:"M21 12H15C13.5858 12 12.8787 12 12.4393 12.4393C12 12.8787 12 13.5858 12 15M12 17.7692V20.5385M15 15V16.5C15 17.9464 15.7837 18 17 18C17.5523 18 18 18.4477 18 19M16 21H15M18 15C19.4142 15 20.1213 15 20.5607 15.44C21 15.8799 21 16.5881 21 18.0043C21 19.4206 21 20.1287 20.5607 20.5687C20.24 20.8898 19.7767 20.9766 19 21",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round"})]}),Copy01Icon=()=>_jsxs("svg",{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24",width:24,height:24,fill:"none",children:[_jsx("path",{d:"M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round"}),_jsx("path",{d:"M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round"})]}),Tick01Icon=()=>_jsx("svg",{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24",width:24,height:24,fill:"none",children:_jsx("path",{d:"M5 14.5C5 14.5 6.5 14.5 8.5 18C8.5 18 14.0588 8.83333 19 7",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round"})}),Share01Icon=()=>_jsxs("svg",{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24",width:24,height:24,fill:"none",children:[_jsx("path",{d:"M20.3927 8.03168L18.6457 6.51461C17.3871 5.42153 16.8937 4.83352 16.2121 5.04139C15.3622 5.30059 15.642 6.93609 15.642 7.48824C14.3206 7.48824 12.9468 7.38661 11.6443 7.59836C7.34453 8.29742 6 11.3566 6 14.6525C7.21697 13.9065 8.43274 13.0746 9.8954 12.7289C11.7212 12.2973 13.7603 12.5032 15.642 12.5032C15.642 13.0554 15.3622 14.6909 16.2121 14.9501C16.9844 15.1856 17.3871 14.5699 18.6457 13.4769L20.3927 11.9598C21.4642 11.0293 22 10.564 22 9.99574C22 9.4275 21.4642 8.96223 20.3927 8.03168Z",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round"}),_jsx("path",{d:"M10.5676 3C6.70735 3.00694 4.68594 3.10152 3.39411 4.39073C2 5.78202 2 8.02125 2 12.4997C2 16.9782 2 19.2174 3.3941 20.6087C4.78821 22 7.03198 22 11.5195 22C16.0071 22 18.2509 22 19.645 20.6087C20.6156 19.64 20.9104 18.2603 21 16",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round"})]});import"./shlinker.css";function makeShlinkWithPrefix(e,t){const o=null===t?null:t??e.originalPrefix;return o?`${o}#${e.shlink}`:`${e.shlink}`}function SHLinkWidget({shlinkData:e,config:t}){const[o,n]=useState(null),[s,r]=useState(""),[i,l]=useState(t.qrStartsOpen||!1),a=(e.files||[]).length,c=e.totalFileSize??(e.files||[]).reduce(((e,t)=>e+t.size),0),d=(e.files||[]).map((e=>e?.contentJson?.entry?.length||0)).reduce(((e,t)=>e+t),0),h=()=>{navigator.clipboard.writeText(makeShlinkWithPrefix(e,t.viewerPrefix)),n("Copied!"),setTimeout((()=>{n(null)}),600)};useEffect((()=>{qrcode.toDataURL(makeShlinkWithPrefix(e,t.viewerPrefix)).then((e=>r(e)))}),[e]);const p=()=>{l(!1)};return _jsxs("div",{className:"shlink-widget",children:[null!==t.logoOverride&&_jsx("img",{className:"shlink-widget__logo",src:t.logoOverride??smartLogo,alt:"SMART Logo"}),_jsxs("div",{className:"shlink-widget__button-group",children:[!t.showButtons||t?.showButtons?.includes("copy")&&_jsx("button",{className:"shlink-widget__button",onClick:h,title:"Copy",children:_jsx(o?Tick01Icon:Copy01Icon,{})}),(!t.showButtons||t?.showButtons?.includes("download"))&&_jsx("button",{className:"shlink-widget__button",onClick:()=>{(e.files||[]).forEach((e=>{const t=new Blob([JSON.stringify(e.contentJson)],{type:"application/json"}),o=URL.createObjectURL(t),n=document.createElement("a");n.href=o,n.download=e.name,n.click(),URL.revokeObjectURL(o)}))},title:"Download",children:_jsx(Download04Icon,{})}),(!t.showButtons||t?.showButtons?.includes("share"))&&void 0!==navigator.share&&_jsx("button",{className:"shlink-widget__button",onClick:async()=>{if(navigator.share)try{await navigator.share({title:"SMART Health Link",url:makeShlinkWithPrefix(e,t.viewerPrefix)})}catch(e){console.error("Error sharing link:",e)}else h()},title:"Share",children:_jsx(Share01Icon,{})}),(!t.showButtons||t?.showButtons?.includes("qr"))&&_jsxs(_Fragment,{children:["(",!i&&_jsx("button",{className:"shlink-widget__button",onClick:async()=>{l(!0)},title:"QR",children:_jsx(QrCodeIcon,{})}),i&&_jsx("button",{className:"shlink-widget__button",onClick:p,title:"QR",children:_jsx(QrCodeIcon,{})}),")"]})]}),_jsx("div",{ref:e=>{if(!e)return;const t=e.clientWidth;e.style.setProperty("--qrcode-height",`${t}px`)},className:"shlink-widget__qrcode-container "+(i?"shlink-widget__qrcode-container--expand":""),children:_jsx("img",{className:"shlink-widget__qrcode",onClick:p,src:s,alt:"QR Code"})}),e.label&&_jsx("div",{class:"shlink-widget__label",children:e.label})||null,t.showDetails&&_jsxs("table",{className:"shlink-widget__details",children:[_jsxs("tr",{children:[_jsx("td",{children:"Files"}),_jsx("td",{children:a})]}),_jsxs("tr",{children:[_jsx("td",{children:"FHIR"}),_jsx("td",{children:prettyBytes(d).replace("B","R")})]}),_jsxs("tr",{children:[_jsx("td",{children:"Size"}),_jsx("td",{children:prettyBytes(c)})]})]})||null,null!==t.logoBottom&&_jsx("img",{className:"shlink-widget__logo-bottom",src:t.logoBottom,alt:"Logo"})]})}export function parse(e){const t=(e??window.location.href).split("#"),o=t.at(-1),n=t.at(-2);let s=o.split("shlink:/")?.at(1);const r=jose.base64url.decode(s),i=(new TextDecoder).decode(r),{url:l,flag:a,key:c,label:d}=JSON.parse(i);return{shlink:o,url:l,flag:a,key:c,label:d,originalPrefix:n}}export async function retrieve(e,{recipient:t="Generic Recipient",passcode:o}={}){const{url:n,flag:s,key:r}=e;let i=[],l=0;if(s.includes("U")){const e=await fetch(n),t=await e.text();l+=t.length;const o=await decryptFile(t,r);i=o.map(((e,t)=>({...e,name:e.name||`shl-file-${t}.json`})))}else{const e={recipient:t};if(s.includes("P")&&!o){const t=window.prompt("Enter the passcode for the SMART Health Link:");t&&(e.passcode=t)}else s.includes("P")&&o&&(e.passcode=o);const a=await fetch(n,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)}),c=await a.json();await Promise.all(c.files.map((async(e,t)=>{const o=await fetch(e.location),n=await o.text();l+=n.length;(await decryptFile(n,r)).forEach(((e,o)=>{i.push({...e,name:e.name||`shl-file-${t}-bundle-${o}.json`})}))})))}return{...e,files:i,totalFileSize:l}}async function decodeHealthCard(e){const t=jose.base64url.decode(e.split(".")[1]),o=pako.inflateRaw(t,{to:"string"});return{contentJson:JSON.parse(o??"{}").vc?.credentialSubject?.fhirBundle,mimeType:"application/fhir+json",size:o.length}}async function decryptFile(e,t){const o=await jose.compactDecrypt(e,jose.base64url.decode(t),{inflateRaw:async e=>pako.inflateRaw(e)}),n=(new TextDecoder).decode(o.plaintext);let s=o.protectedHeader.cty??"application/fhir+json";try{JSON.parse(n).verifiableCredential&&(s="application/smart-health-card")}catch{}if("application/smart-health-card"===s){const e=JSON.parse(n).verifiableCredential;return await Promise.all(e.map(decodeHealthCard))}if("application/fhir+json"===s)return[{contentJson:JSON.parse(n),mimeType:s,size:n.length}];throw new Error("Unsupported SHL File MIME type: "+s)}export function render(e,t,o={showDetails:!0,qrStartsOpen:!1}){preact.render(_jsx(SHLinkWidget,{shlinkData:e,config:o}),t)}
//# sourceMappingURL=/sm/26b855e9d4b459602717f2c347a76e4d181299941b70ee6c3544ad1deeae7bd4.map