@medplum/core 0.9.7 → 0.9.8

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.
@@ -1,2 +1,2 @@
1
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(((t="undefined"!=typeof globalThis?globalThis:t||self).medplum=t.medplum||{},t.medplum.core={}))}(this,(function(t){"use strict";function e(t,e,r,n){return new(r||(r=Promise))((function(i,s){function o(t){try{c(n.next(t))}catch(t){s(t)}}function a(t){try{c(n.throw(t))}catch(t){s(t)}}function c(t){var e;t.done?i(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(o,a)}c((n=n.apply(t,e||[])).next())}))}function r(t,e,r,n){if("a"===r&&!n)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!n:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?n:"a"===r?n.call(t):n?n.value:e.get(t)}function n(t,e,r,n,i){if("m"===n)throw new TypeError("Private method is not writable");if("a"===n&&!i)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!i:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===n?i.call(t,r):i?i.value=r:e.set(t,r),r}var i,s,o,a;class c{constructor(t=10){i.add(this),s.set(this,void 0),o.set(this,void 0),n(this,s,t,"f"),n(this,o,new Map,"f")}clear(){r(this,o,"f").clear()}get(t){const e=r(this,o,"f").get(t);return e&&(r(this,o,"f").delete(t),r(this,o,"f").set(t,e)),e}set(t,e){r(this,o,"f").has(t)?r(this,o,"f").delete(t):r(this,o,"f").size>=r(this,s,"f")&&r(this,o,"f").delete(r(this,i,"m",a).call(this)),r(this,o,"f").set(t,e)}delete(t){r(this,o,"f").delete(t)}}function l(t,e){const r=[];return t.prefix&&((null==e?void 0:e.all)||(null==e?void 0:e.prefix))&&r.push(...t.prefix),t.given&&r.push(...t.given),t.family&&r.push(t.family),t.suffix&&((null==e?void 0:e.all)||(null==e?void 0:e.suffix))&&r.push(...t.suffix),t.use&&((null==e?void 0:e.all)||(null==e?void 0:e.use))&&r.push("["+t.use+"]"),r.join(" ").trim()}function h(t){const e=u(t),r=d(t);return r===e?{reference:e}:{reference:e,display:r}}function u(t){return t.resourceType+"/"+t.id}function f(t){return"Patient"===t.resourceType||"Practitioner"===t.resourceType||"RelatedPerson"===t.resourceType}function d(t){var e,r;if(f(t)){const e=function(t){const e=t.name;if(e&&e.length>0)return l(e[0]);return}(t);if(e)return e}if("Device"===t.resourceType){const e=function(t){const e=t.deviceName;if(e&&e.length>0)return e[0].name;return}(t);if(e)return e}return"Observation"===t.resourceType&&"code"in t&&(null===(e=t.code)||void 0===e?void 0:e.text)?null===(r=t.code)||void 0===r?void 0:r.text:"User"===t.resourceType&&t.email?t.email:"name"in t&&t.name&&"string"==typeof t.name?t.name:u(t)}function p(t){if(t.url&&t.contentType&&t.contentType.startsWith("image/"))return t.url}function g(t,e){const r=new Date(t);r.setUTCHours(0,0,0,0);const n=e?new Date(e):new Date;n.setUTCHours(0,0,0,0);const i=r.getUTCFullYear(),s=r.getUTCMonth(),o=r.getUTCDate(),a=n.getUTCFullYear(),c=n.getUTCMonth(),l=n.getUTCDate();let h=a-i;(c<s||c===s&&l<o)&&h--;let u=12*a+c-(12*i+s);l<o&&u--;return{years:h,months:u,days:Math.floor((n.getTime()-r.getTime())/864e5)}}function m(t,e){if(t)for(const r of t)r.linkId&&r.answer&&r.answer.length>0&&(e[r.linkId]=r.answer[0]),m(r.item,e)}function v(t,e){return JSON.stringify(t,y,e?2:void 0)}function y(t,e){return!function(t){return!!t.match(/\d+$/)}(t)&&w(e)?void 0:e}function w(t){if(null==t)return!0;const e=typeof t;return"string"===e&&""===t||"object"===e&&0===Object.keys(t).length}function T(t,e,r){return t===e||(!(!w(t)||!w(e))||!w(t)&&!w(e)&&(Array.isArray(t)&&Array.isArray(e)?function(t,e){if(t.length!==e.length)return!1;for(let r=0;r<t.length;r++)if(!T(t[r],e[r]))return!1;return!0}(t,e):!Array.isArray(t)&&!Array.isArray(e)&&(S(t)&&S(e)?function(t,e,r){const n=new Set;Object.keys(t).forEach((t=>n.add(t))),Object.keys(e).forEach((t=>n.add(t))),"meta"===r&&(n.delete("versionId"),n.delete("lastUpdated"),n.delete("author"));for(const r of n){if(!T(t[r],e[r],r))return!1}return!0}(t,e,r):(S(t)||S(e),!1))))}function S(t){return null!==t&&"object"==typeof t}function E(t){return t.every((t=>"string"==typeof t))}s=new WeakMap,o=new WeakMap,i=new WeakSet,a=function(){return r(this,o,"f").keys().next().value};const b=[];for(let t=0;t<256;t++)b.push(t.toString(16).padStart(2,"0"));function A(t){const e=new Uint8Array(t),r=new Array(e.length);for(let t=0;t<e.length;t++)r[t]=b[e[t]];return r.join("")}function P(t){const e=new Uint8Array(t),r=[];for(let t=0;t<e.length;t++)r[t]=String.fromCharCode(e[t]);return window.btoa(r.join(""))}function O(t){return t.charAt(0).toUpperCase()+t.substring(1)}function x(){const t=new Uint32Array(28);return crypto.getRandomValues(t),A(t.buffer)}var k;function R(t){const e=function(t){if("undefined"!=typeof window)return window.atob(t);if("undefined"!=typeof Buffer)return Buffer.from(t,"base64").toString("binary");throw new Error("Unable to decode base64")}(t.replace(/-/g,"+").replace(/_/g,"/")),r=Array.from(e).reduce(((t,e)=>`${t}%${("00"+e.charCodeAt(0).toString(16)).slice(-2)}`),""),n=decodeURIComponent(r);return JSON.parse(n)}k=new WeakMap;const C="ok",U="created",M="gone",L="not-modified",_="not-found",D="access-denied",j={resourceType:"OperationOutcome",id:C,issue:[{severity:"information",code:"information",details:{text:"All OK"}}]},I={resourceType:"OperationOutcome",id:U,issue:[{severity:"information",code:"information",details:{text:"Created"}}]},N={resourceType:"OperationOutcome",id:L,issue:[{severity:"information",code:"information",details:{text:"Not Modified"}}]},W={resourceType:"OperationOutcome",id:_,issue:[{severity:"error",code:"not-found",details:{text:"Not found"}}]},B={resourceType:"OperationOutcome",id:M,issue:[{severity:"error",code:"gone",details:{text:"Gone"}}]},F={resourceType:"OperationOutcome",id:D,issue:[{severity:"error",code:"access-denied",details:{text:"Access Denied"}}]};function $(t){return t.id===C||t.id===U||t.id===L}class H extends Error{constructor(t){var e,r;super(null===(r=null===(e=null==t?void 0:t.issue)||void 0===e?void 0:e[0].details)||void 0===r?void 0:r.text),this.outcome=t}}var Q,q,z,X,G;class Y{constructor(t){this[G]="ReadablePromise",Q.set(this,void 0),q.set(this,"pending"),z.set(this,void 0),X.set(this,void 0),n(this,Q,t.then((t=>(n(this,q,"success","f"),n(this,z,t,"f"),t)),(t=>{throw n(this,q,"error","f"),n(this,X,t,"f"),t})),"f")}isPending(){return"pending"===r(this,q,"f")}read(){switch(r(this,q,"f")){case"pending":throw r(this,Q,"f");case"error":throw r(this,X,"f");default:return r(this,z,"f")}}then(t,e){return r(this,Q,"f").then(t,e)}catch(t){return r(this,Q,"f").catch(t)}finally(t){return r(this,Q,"f").finally(t)}}Q=new WeakMap,q=new WeakMap,z=new WeakMap,X=new WeakMap,G=Symbol.toStringTag;var V;t.Operator=void 0,(V=t.Operator||(t.Operator={})).EQUALS="eq",V.NOT_EQUALS="ne",V.GREATER_THAN="gt",V.LESS_THAN="lt",V.GREATER_THAN_OR_EQUALS="ge",V.LESS_THAN_OR_EQUALS="le",V.STARTS_AFTER="sa",V.ENDS_BEFORE="eb",V.APPROXIMATELY="ap",V.CONTAINS="contains",V.EXACT="exact",V.TEXT="text",V.ABOVE="above",V.BELOW="below",V.IN="in",V.NOT_IN="not-in",V.OF_TYPE="of-type";const K=[t.Operator.CONTAINS,t.Operator.EXACT,t.Operator.TEXT,t.Operator.ABOVE,t.Operator.BELOW,t.Operator.IN,t.Operator.NOT_IN,t.Operator.OF_TYPE],J=[t.Operator.NOT_EQUALS,t.Operator.GREATER_THAN,t.Operator.LESS_THAN,t.Operator.GREATER_THAN_OR_EQUALS,t.Operator.LESS_THAN_OR_EQUALS,t.Operator.STARTS_AFTER,t.Operator.ENDS_BEFORE,t.Operator.APPROXIMATELY];function Z(e){const r=new URL(e,"https://example.com/"),n=r.pathname.replace(/(^\/)|(\/$)/g,"").split("/").pop();let i,s,o,a,c,l;return new URLSearchParams(r.search).forEach(((e,r)=>{"_fields"===r?o=e.split(","):"_offset"===r?a=parseInt(e):"_count"===r?c=parseInt(e):"_total"===r?l=e:"_sort"===r?(s=s||[],s.push(function(t){return t.startsWith("-")?{code:t.substring(1),descending:!0}:{code:t}}(e))):(i=i||[],i.push(function(e,r){let n=e,i=t.Operator.EQUALS;for(const t of K){const e=n.indexOf(":"+t);-1!==e&&(i=t,n=n.substring(0,e))}for(const t of J)r.match(new RegExp("^"+t+"\\d"))&&(i=t,r=r.substring(t.length));return{code:n,operator:i,value:r}}(r,e)))})),{resourceType:n,filters:i,fields:o,offset:a,count:c,total:l,sortRules:s}}function tt(t){const e=[];return t.fields&&e.push("_fields="+t.fields.join(",")),t.filters&&t.filters.forEach((t=>e.push(function(t){const e=K.includes(t.operator)?":"+t.operator:"",r=J.includes(t.operator)?t.operator:"";return`${t.code}${e}=${r}${encodeURIComponent(t.value)}`}(t)))),t.sortRules&&t.sortRules.length>0&&e.push(function(t){if(!t||0===t.length)return"";return"_sort="+t.map((t=>t.descending?"-"+t.code:t.code)).join(",")}(t.sortRules)),void 0!==t.offset&&e.push("_offset="+t.offset),void 0!==t.count&&e.push("_count="+t.count),void 0!==t.total&&e.push("_total="+t.total),0===e.length?"":(e.sort(),"?"+e.join("&"))}var et,rt,nt,it,st,ot,at,ct,lt,ht,ut,ft,dt,pt,gt,mt,vt,yt,wt,Tt,St,Et,bt,At,Pt,Ot,xt,kt,Rt,Ct,Ut,Mt,Lt;class _t{constructor(){et.set(this,void 0),n(this,et,"undefined"!=typeof localStorage?localStorage:new Dt,"f")}clear(){r(this,et,"f").clear()}getString(t){return r(this,et,"f").getItem(t)||void 0}setString(t,e){e?r(this,et,"f").setItem(t,e):r(this,et,"f").removeItem(t)}getObject(t){const e=this.getString(t);return e?JSON.parse(e):void 0}setObject(t,e){this.setString(t,e?v(e):void 0)}}et=new WeakMap;class Dt{constructor(){rt.set(this,void 0),n(this,rt,new Map,"f")}get length(){return r(this,rt,"f").size}clear(){r(this,rt,"f").clear()}getItem(t){var e;return null!==(e=r(this,rt,"f").get(t))&&void 0!==e?e:null}setItem(t,e){e?r(this,rt,"f").set(t,e):r(this,rt,"f").delete(t)}removeItem(t){r(this,rt,"f").delete(t)}key(t){return Array.from(r(this,rt,"f").keys())[t]}}function jt(){return{types:{}}}function It(t,e){return{display:t,description:e,properties:{},searchParams:{_lastUpdated:{base:[t],code:"_lastUpdated",type:"date",expression:t+".meta.lastUpdated"}}}}function Nt(t,e){var r;const n=e.name;if(!n)return;t.types[n]=It(n,e.description);const i=null===(r=e.snapshot)||void 0===r?void 0:r.element;if(i){const e=i.filter((t=>t.path!==n&&t.path));e.forEach((e=>function(t,e){var r,n;const i=e.path,s=null===(n=null===(r=e.type)||void 0===r?void 0:r[0])||void 0===n?void 0:n.code;if("Element"!==s&&"BackboneElement"!==s)return;const o=i.split("."),a=Bt(o);a in t.types||(t.types[a]=It(a,e.definition),t.types[a].parentType=Bt(o.slice(0,o.length-1)))}(t,e))),e.forEach((e=>function(t,e){const r=e.path.split(".");if(1===r.length)return;const n=Bt(r.slice(0,r.length-1)),i=t.types[n],s=r[r.length-1];i.properties[s]=e}(t,e)))}}function Wt(t,e){if(e.base)for(const r of e.base){const n=t.types[r];n&&(n.searchParams||(n.searchParams={}),n.searchParams[e.code]=e)}}function Bt(t){return t.map(O).join("")}rt=new WeakMap,t.PropertyType=void 0,(nt=t.PropertyType||(t.PropertyType={})).Address="Address",nt.Age="Age",nt.Annotation="Annotation",nt.Attachment="Attachment",nt.BackboneElement="BackboneElement",nt.CodeableConcept="CodeableConcept",nt.Coding="Coding",nt.ContactDetail="ContactDetail",nt.ContactPoint="ContactPoint",nt.Contributor="Contributor",nt.Count="Count",nt.DataRequirement="DataRequirement",nt.Distance="Distance",nt.Dosage="Dosage",nt.Duration="Duration",nt.Expression="Expression",nt.Extension="Extension",nt.HumanName="HumanName",nt.Identifier="Identifier",nt.MarketingStatus="MarketingStatus",nt.Meta="Meta",nt.Money="Money",nt.Narrative="Narrative",nt.ParameterDefinition="ParameterDefinition",nt.Period="Period",nt.Population="Population",nt.ProdCharacteristic="ProdCharacteristic",nt.ProductShelfLife="ProductShelfLife",nt.Quantity="Quantity",nt.Range="Range",nt.Ratio="Ratio",nt.Reference="Reference",nt.RelatedArtifact="RelatedArtifact",nt.Resource="Resource",nt.SampledData="SampledData",nt.Signature="Signature",nt.SubstanceAmount="SubstanceAmount",nt.SystemString="http://hl7.org/fhirpath/System.String",nt.Timing="Timing",nt.TriggerDefinition="TriggerDefinition",nt.UsageContext="UsageContext",nt.base64Binary="base64Binary",nt.boolean="boolean",nt.canonical="canonical",nt.code="code",nt.date="date",nt.dateTime="dateTime",nt.decimal="decimal",nt.id="id",nt.instant="instant",nt.integer="integer",nt.markdown="markdown",nt.oid="oid",nt.positiveInt="positiveInt",nt.string="string",nt.time="time",nt.unsignedInt="unsignedInt",nt.uri="uri",nt.url="url",nt.uuid="uuid";const Ft="launch/patient openid fhirUser offline_access user/*.*";function $t(){return window.location.protocol+"//"+window.location.host+"/"}function Ht(t){return null==t?[]:Array.isArray(t)?t:[t]}function Qt(t,e){if(void 0!==t)return Array.isArray(t)?t.map((t=>e(t))).filter((t=>!!t)).flat():e(t)}function qt(t){return Array.isArray(t)&&0===t.length}function zt(t){return!t||qt(t)}function Xt(t){return Array.isArray(t)?0!==t.length&&!!t[0]:!!t}function Gt(t){const e=[];for(const r of t){let t=!1;for(const n of e)if(Yt(r,n)){t=!0;break}t||e.push(r)}return e}function Yt(t,e){return!(!zt(t)||!zt(e))||(qt(t)||qt(e)?[]:"number"==typeof t&&"number"==typeof e?Math.abs(t-e)<1e-8:Jt(t)&&Jt(e)?Zt(t,e):Array.isArray(t)&&Array.isArray(e)?t.length===e.length&&t.every(((t,r)=>Yt(t,e[r]))):"object"==typeof t&&"object"==typeof e?te(t,e):t===e)}function Vt(t,e){return!(!zt(t)||!zt(e))||!qt(t)&&!qt(e)&&("number"==typeof t&&"number"==typeof e?Math.abs(t-e)<.01:Jt(t)&&Jt(e)?Zt(t,e):Array.isArray(t)&&Array.isArray(e)?(t.sort(),e.sort(),t.length===e.length&&t.every(((t,r)=>Yt(t,e[r])))):"object"==typeof t&&"object"==typeof e?te(t,e):"string"==typeof t&&"string"==typeof e?t.toLowerCase()===e.toLowerCase():t===e)}function Kt(t,e){if(null==t)return!1;switch(e){case"Boolean":return"boolean"==typeof t;case"Decimal":case"Integer":return"number"==typeof t;case"Date":return"string"==typeof t&&!!t.match(/^\d{4}(-\d{2}(-\d{2})?)?/);case"DateTime":return"string"==typeof t&&!!t.match(/^\d{4}(-\d{2}(-\d{2})?)?T/);case"Time":return"string"==typeof t&&!!t.match(/^T\d/);case"Period":return!(!(r=t)||"object"!=typeof r||!("start"in r));case"Quantity":return Jt(t);default:return"object"==typeof t&&(null==t?void 0:t.resourceType)===e}var r}function Jt(t){return!(!t||"object"!=typeof t||!("value"in t)||"number"!=typeof t.value)}function Zt(t,e){return Math.abs(t.value-e.value)<.01&&(t.unit===e.unit||t.code===e.code||t.unit===e.code||t.code===e.unit)}function te(t,e){const r=Object.keys(t),n=Object.keys(e);if(r.length!==n.length)return!1;for(const n of r){const r=t[n],i=e[n];if(ee(r)&&ee(i)){if(!te(r,i))return!1}else if(r!==i)return!1}return!0}function ee(t){return null!==t&&"object"==typeof t}st=new WeakMap,ot=new WeakMap,at=new WeakMap,ct=new WeakMap,lt=new WeakMap,ht=new WeakMap,ut=new WeakMap,ft=new WeakMap,dt=new WeakMap,pt=new WeakMap,gt=new WeakMap,mt=new WeakMap,vt=new WeakMap,yt=new WeakMap,wt=new WeakMap,Tt=new WeakMap,it=new WeakSet,St=function(t){const e=this.getLogins().filter((e=>{var r,n;return(null===(r=e.profile)||void 0===r?void 0:r.reference)!==(null===(n=t.profile)||void 0===n?void 0:n.reference)}));e.push(t),r(this,ot,"f").setObject("logins",e)},Et=function(){return e(this,void 0,void 0,(function*(){return n(this,yt,new Promise(((t,e)=>{this.get("auth/me").then((e=>{n(this,yt,void 0,"f"),n(this,wt,e.profile,"f"),n(this,Tt,e.config,"f"),this.dispatchEvent({type:"change"}),t(r(this,wt,"f"))})).catch(e)})),"f"),r(this,yt,"f")}))},bt=function(t,n,i={}){return e(this,void 0,void 0,(function*(){r(this,vt,"f")&&(yield r(this,vt,"f")),n.startsWith("http")||(n=r(this,lt,"f")+n),i.method=t,r(this,it,"m",At).call(this,i);const e=yield r(this,st,"f").call(this,n,i);if(401===e.status)return r(this,it,"m",xt).call(this,t,n,i);if(204===e.status||304===e.status)return;const s=yield e.json();return"OperationOutcome"!==(null==s?void 0:s.resourceType)||$(s)?s:Promise.reject(s)}))},At=function(t){t.headers||(t.headers={});const e=t.headers;e["Content-Type"]||(e["Content-Type"]="application/fhir+json"),r(this,gt,"f")&&(e.Authorization="Bearer "+r(this,gt,"f")),t.cache||(t.cache="no-cache"),t.credentials||(t.credentials="include")},Pt=function(t,e){t.headers||(t.headers={});t.headers["Content-Type"]=e},Ot=function(t,e){"string"==typeof e||"undefined"!=typeof File&&e instanceof File?t.body=e:e&&(t.body=JSON.stringify(e))},xt=function(t,n,i){return e(this,void 0,void 0,(function*(){return r(this,it,"m",Ct).call(this).then((()=>r(this,it,"m",bt).call(this,t,n,i))).catch((t=>(this.clear(),r(this,pt,"f")&&r(this,pt,"f").call(this),Promise.reject(t))))}))},kt=function(){return e(this,void 0,void 0,(function*(){const t=x();r(this,ot,"f").setString("pkceState",t);const n=x();r(this,ot,"f").setString("codeVerifier",n);const i=P(yield function(t){return e(this,void 0,void 0,(function*(){return crypto.subtle.digest("SHA-256",(new TextEncoder).encode(t))}))}(n)).replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"");r(this,ot,"f").setString("codeChallenge",i)}))},Rt=function(){r(this,it,"m",kt).call(this);const t=new URL(r(this,ut,"f"));t.searchParams.set("response_type","code"),t.searchParams.set("state",r(this,ot,"f").getString("pkceState")),t.searchParams.set("client_id",r(this,ht,"f")),t.searchParams.set("redirect_uri",$t()),t.searchParams.set("scope",Ft),t.searchParams.set("code_challenge_method","S256"),t.searchParams.set("code_challenge",r(this,ot,"f").getString("codeChallenge")),window.location.assign(t.toString())},Ct=function(){return e(this,void 0,void 0,(function*(){if(r(this,vt,"f"))return r(this,vt,"f");if(!r(this,mt,"f"))throw this.clear(),new Error("Invalid refresh token");const t=new URLSearchParams;t.set("grant_type","refresh_token"),t.set("client_id",r(this,ht,"f")),t.set("refresh_token",r(this,mt,"f")),n(this,vt,r(this,it,"m",Ut).call(this,t),"f"),yield r(this,vt,"f")}))},Ut=function(t){return e(this,void 0,void 0,(function*(){return r(this,st,"f").call(this,r(this,ft,"f"),{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:t}).then((t=>{if(!t.ok)throw new Error("Failed to fetch tokens");return t.json()})).then((t=>r(this,it,"m",Mt).call(this,t))).then((()=>this.getProfile()))}))},Mt=function(t){return e(this,void 0,void 0,(function*(){const e=t.access_token,n=function(t){const[e,r,n]=t.split(".");return R(r)}(e);if(Date.now()>=1e3*n.exp)throw this.clear(),new Error("Token expired");if(r(this,ht,"f")&&n.client_id!==r(this,ht,"f"))throw this.clear(),new Error("Token was not issued for this audience");yield this.setActiveLogin({accessToken:e,refreshToken:t.refresh_token,project:t.project,profile:t.profile})}))},Lt=function(){try{window.addEventListener("storage",(t=>{null!==t.key&&"activeLogin"!==t.key||window.location.reload()}))}catch(t){}};class re{constructor(t,e){this.original=t,this.child=e}eval(t){try{const e=Qt(t,(t=>this.child.eval(t)));return Array.isArray(e)?e.flat():null==e?[]:[e]}catch(t){throw new Error(`FhirPathError on "${this.original}": ${t}`)}}}class ne{constructor(t){this.value=t}eval(){return this.value}}class ie{constructor(t){this.name=t}eval(t){return"$this"===this.name?t:Qt(t,(t=>{if(t&&"object"==typeof t){if("resourceType"in t&&t.resourceType===this.name)return t;if(this.name in t)return t[this.name];const e=Object.keys(t).find((t=>t.startsWith(this.name)));if(e)return t[e]}}))}}class se{eval(){return[]}}class oe{constructor(t,e){this.child=t,this.impl=e}eval(t){return this.impl(this.child.eval(t))}}class ae{constructor(t,e){this.left=t,this.right=e}eval(t){return this.left.eval(t)}}class ce{constructor(t,e,r){this.left=t,this.right=e,this.impl=r}eval(t){const e=this.left.eval(t),r=this.right.eval(t);return Jt(e)&&Jt(r)?Object.assign(Object.assign({},e),{value:this.impl(e.value,r.value)}):this.impl(e,r)}}class le{constructor(t,e,r){this.left=t,this.right=e,this.impl=r}eval(t){const e=this.left.eval(t),r=this.right.eval(t);return Jt(e)&&Jt(r)?this.impl(e.value,r.value):this.impl(e,r)}}class he{constructor(t,e){this.left=t,this.right=e}eval(t){const e=this.left.eval(t),r=this.right.eval(t),n=[];function i(t){t&&(Array.isArray(t)?n.push(...t):n.push(t))}return i(e),i(r),n.length>0&&n.every((t=>"string"==typeof t))?n.join(""):n}}class ue{constructor(t,e){this.left=t,this.right=e}eval(t){const e=this.left.eval(t),r=this.right.eval(t);return Ht(e).includes(r)}}class fe{constructor(t,e){this.left=t,this.right=e}eval(t){const e=this.left.eval(t);return Ht(this.right.eval(t)).includes(e)}}class de{constructor(t,e){this.left=t,this.right=e}eval(t){return this.right.eval(this.left.eval(t))}}class pe{constructor(t,e){this.left=t,this.right=e}eval(t){const e=this.left.eval(t),r=this.right.eval(t);let n;return n=void 0!==e&&void 0!==r?[e,r].flat():void 0!==e?Ht(e):void 0!==r?Ht(r):[],Gt(n)}}class ge{constructor(t,e){this.left=t,this.right=e}eval(t){const e=this.left.eval(t),r=this.right.eval(t);return Array.isArray(e)&&Array.isArray(r)?Yt(e.flat(),r):Qt(e,(t=>Yt(t,r)))}}class me{constructor(t,e){this.left=t,this.right=e}eval(t){const e=this.left.eval(t),r=this.right.eval(t);let n;return n=Array.isArray(r)?Yt(e,r):Qt(e,(t=>Yt(t,r))),!Xt(n)}}class ve{constructor(t,e){this.left=t,this.right=e}eval(t){const e=this.left.eval(t),r=this.right.eval(t);return Array.isArray(r)?Vt(e,r):Qt(e,(t=>Vt(t,r)))}}class ye{constructor(t,e){this.left=t,this.right=e}eval(t){const e=this.left.eval(t),r=this.right.eval(t);let n;return n=Array.isArray(r)?Vt(e,r):Qt(e,(t=>Vt(t,r))),!Xt(n)}}class we{constructor(t,e){this.left=t,this.right=e}eval(t){const e=this.right.name;return Qt(this.left.eval(t),(t=>Kt(t,e)))}}class Te{constructor(t,e){this.left=t,this.right=e}eval(t){const e=this.left.eval(t),r=this.right.eval(t);return!0===e&&!0===r||!1!==e&&!1!==r&&[]}}class Se{constructor(t,e){this.left=t,this.right=e}eval(t){const e=this.left.eval(t);if(Xt(e))return e;const r=this.right.eval(t);return Xt(r)?r:[]}}class Ee{constructor(t,e){this.left=t,this.right=e}eval(t){const e=this.left.eval(t),r=this.right.eval(t);return!0===e&&!0!==r||!0!==e&&!0===r||!(!0===e&&!0===r||!1===e&&!1===r)&&[]}}class be{constructor(t,e,r){this.name=t,this.args=e,this.impl=r}eval(t){return this.impl(Ht(t),...this.args)}}class Ae{constructor(t,e){this.left=t,this.expr=e}eval(t){const e=this.expr.eval(t);if("number"!=typeof e)throw new Error("Invalid indexer expression: should return integer}");const r=this.left.eval(t);return e in r?r[e]:[]}}function Pe(t){if(t.startsWith("T"))return t+"T00:00:00.000Z".substring(t.length);if(t.length<=10)return t;try{return new Date(t).toISOString()}catch(e){return t}}const Oe=()=>[];const xe=Oe,ke=Oe;const Re=Oe,Ce=Oe;function Ue(t){if(0===t.length)return[];const[e]=He(t,1);if("boolean"==typeof e)return[e];if("number"==typeof e&&(0===e||1===e))return[!!e];if("string"==typeof e){const t=e.toLowerCase();if(["true","t","yes","y","1","1.0"].includes(t))return[!0];if(["false","f","no","n","0","0.0"].includes(t))return[!1]}return[]}function Me(t){if(0===t.length)return[];const[e]=He(t,1);return"number"==typeof e?[e]:"string"==typeof e&&e.match(/^[+-]?\d+$/)?[parseInt(e,10)]:"boolean"==typeof e?[e?1:0]:[]}function Le(t){if(0===t.length)return[];const[e]=He(t,1);return"string"==typeof e&&e.match(/^\d{4}(-\d{2}(-\d{2})?)?/)?[Pe(e)]:[]}function _e(t){if(0===t.length)return[];const[e]=He(t,1);return"string"==typeof e&&e.match(/^\d{4}(-\d{2}(-\d{2})?)?/)?[Pe(e)]:[]}function De(t){if(0===t.length)return[];const[e]=He(t,1);return"number"==typeof e?[e]:"string"==typeof e&&e.match(/^-?\d{1,9}(\.\d{1,9})?$/)?[parseFloat(e)]:"boolean"==typeof e?[e?1:0]:[]}function je(t){if(0===t.length)return[];const[e]=He(t,1);return Jt(e)?[e]:"number"==typeof e?[{value:e,unit:"1"}]:"string"==typeof e&&e.match(/^-?\d{1,9}(\.\d{1,9})?/)?[{value:parseFloat(e),unit:"1"}]:"boolean"==typeof e?[{value:e?1:0,unit:"1"}]:[]}function Ie(t){if(0===t.length)return[];const[e]=He(t,1);return null==e?[]:Jt(e)?[`${e.value} '${e.unit}'`]:[e.toString()]}function Ne(t){if(0===t.length)return[];const[e]=He(t,1);if("string"==typeof e){const t=e.match(/^T?(\d{2}(:\d{2}(:\d{2})?)?)/);if(t)return[Pe("T"+t[1])]}return[]}const We=Oe,Be=Oe;function Fe(t,e,...r){if(0===e.length)return[];const[n]=He(e,1);if("string"!=typeof n)throw new Error("String function cannot be called with non-string");const i=t(n,...r.map((t=>t&&t.eval(n))));return void 0===i?[]:[i]}function $e(t,e,...r){if(0===e.length)return[];const[n]=He(e,1),i=Jt(n),s=i?n.value:n;if("number"!=typeof s)throw new Error("Math function cannot be called with non-number");const o=t(s,...r.map((t=>t.eval(void 0))));return i?[Object.assign(Object.assign({},n),{value:o})]:[o]}function He(t,e){if(t.length!==e)throw new Error(`Expected ${e} arguments`);return t}var Qe,qe,ze,Xe,Ge,Ye,Ve,Ke,Je,Ze,tr,er,rr,nr,ir,sr,or,ar,cr=Object.freeze({__proto__:null,empty:function(t){return[0===t.length]},exists:function(t,e){return e?[t.filter((t=>Xt(e.eval(t)))).length>0]:[t.length>0]},all:function(t,e){return[t.every((t=>Xt(e.eval(t))))]},allTrue:function(t){for(const e of t)if(!e)return[!1];return[!0]},anyTrue:function(t){for(const e of t)if(e)return[!0];return[!1]},allFalse:function(t){for(const e of t)if(e)return[!1];return[!0]},anyFalse:function(t){for(const e of t)if(!e)return[!0];return[!1]},subsetOf:xe,supersetOf:ke,count:function(t){return[t.length]},distinct:function(t){return Array.from(new Set(t))},isDistinct:function(t){return[t.length===new Set(t).size]},where:function(t,e){return t.filter((t=>Xt(e.eval(t))))},select:function(t,e){return Ht(t.map((t=>e.eval(t))).flat())},repeat:Re,ofType:Ce,single:function(t){if(t.length>1)throw new Error("Expected input length one for single()");return 0===t.length?[]:t.slice(0,1)},first:function(t){return 0===t.length?[]:[t[0]]},last:function(t){return 0===t.length?[]:[t[t.length-1]]},tail:function(t){return 0===t.length?[]:t.slice(1,t.length)},skip:function(t,e){const r=e.eval(0);if("number"!=typeof r)throw new Error("Expected a number for skip(num)");return r>=t.length?[]:r<=0?t:t.slice(r,t.length)},take:function(t,e){const r=e.eval(0);if("number"!=typeof r)throw new Error("Expected a number for take(num)");return r>=t.length?t:r<=0?[]:t.slice(0,r)},intersect:function(t,e){if(!e)return t;const r=Ht(e.eval(0));return Gt(t.filter((t=>r.includes(t))))},exclude:function(t,e){if(!e)return t;const r=Ht(e.eval(0));return t.filter((t=>!r.includes(t)))},union:function(t,e){return e?Gt([t,e.eval(0)].flat()):t},combine:function(t,e){return e?[t,e.eval(0)].flat():t},iif:function(t,e,r,n){const i=Ht(e.eval(t));if(i.length>1||1===i.length&&"boolean"!=typeof i[0])throw new Error("Expected criterion to evaluate to a Boolean");return Xt(i)?Ht(r.eval(t)):n?Ht(n.eval(t)):[]},toBoolean:Ue,convertsToBoolean:function(t){return 0===t.length?[]:[1===Ue(t).length]},toInteger:Me,convertsToInteger:function(t){return 0===t.length?[]:[1===Me(t).length]},toDate:Le,convertsToDate:function(t){return 0===t.length?[]:[1===Le(t).length]},toDateTime:_e,convertsToDateTime:function(t){return 0===t.length?[]:[1===_e(t).length]},toDecimal:De,convertsToDecimal:function(t){return 0===t.length?[]:[1===De(t).length]},toQuantity:je,convertsToQuantity:function(t){return 0===t.length?[]:[1===je(t).length]},toString:Ie,convertsToString:function(t){return 0===t.length?[]:[1===Ie(t).length]},toTime:Ne,convertsToTime:function(t){return 0===t.length?[]:[1===Ne(t).length]},indexOf:function(t,e){return Fe(((t,e)=>t.indexOf(e)),t,e)},substring:function(t,e,r){return Fe(((t,e,r)=>{const n=e,i=r?n+r:t.length;return n<0||n>=t.length?void 0:t.substring(n,i)}),t,e,r)},startsWith:function(t,e){return Fe(((t,e)=>t.startsWith(e)),t,e)},endsWith:function(t,e){return Fe(((t,e)=>t.endsWith(e)),t,e)},contains:function(t,e){return Fe(((t,e)=>t.includes(e)),t,e)},upper:function(t){return Fe((t=>t.toUpperCase()),t)},lower:function(t){return Fe((t=>t.toLowerCase()),t)},replace:function(t,e,r){return Fe(((t,e,r)=>t.replaceAll(e,r)),t,e,r)},matches:function(t,e){return Fe(((t,e)=>!!t.match(e)),t,e)},replaceMatches:function(t,e,r){return Fe(((t,e,r)=>t.replaceAll(e,r)),t,e,r)},length:function(t){return Fe((t=>t.length),t)},toChars:function(t){return Fe((t=>t?t.split(""):void 0),t)},abs:function(t){return $e(Math.abs,t)},ceiling:function(t){return $e(Math.ceil,t)},exp:function(t){return $e(Math.exp,t)},floor:function(t){return $e(Math.floor,t)},ln:function(t){return $e(Math.log,t)},log:function(t,e){return $e(((t,e)=>Math.log(t)/Math.log(e)),t,e)},power:function(t,e){return $e(Math.pow,t,e)},round:function(t){return $e(Math.round,t)},sqrt:function(t){return $e(Math.sqrt,t)},truncate:function(t){return $e((t=>0|t),t)},children:We,descendants:Be,trace:function(t,e){return console.log("trace",t,e),t},now:function(){return[(new Date).toISOString()]},timeOfDay:function(){return[(new Date).toISOString().substring(11)]},today:function(){return[(new Date).toISOString().substring(0,10)]},between:function(t,e,r,n){const i=_e(Ht(e.eval(t)));if(0===i.length)throw new Error("Invalid start date");const s=_e(Ht(r.eval(t)));if(0===s.length)throw new Error("Invalid end date");const o=n.eval(t);if("years"!==o&&"months"!==o&&"days"!==o)throw new Error("Invalid units");return[{value:g(i[0],s[0])[o],unit:o}]},is:function(t,e){const r=e.name;return t.map((t=>Kt(t,r)))},not:function(t){return Ue(t).map((t=>!t))},resolve:function(t){return t.map((t=>{let e;if("string"==typeof t)e=t;else if("object"==typeof t){const r=t;if(r.resource)return r.resource;e=r.reference}if(!e)return;const[r,n]=e.split("/");return{resourceType:r,id:n}})).filter((t=>!!t))},as:function(t){return t},type:function(t){return t.map((t=>"boolean"==typeof t?{namespace:"System",name:"Boolean"}:"number"==typeof t?{namespace:"System",name:"Integer"}:t&&"object"==typeof t&&"resourceType"in t?{namespace:"FHIR",name:t.resourceType}:null))},conformsTo:function(t,e){const r=e.eval(void 0);if(!r.startsWith("http://hl7.org/fhir/StructureDefinition/"))throw new Error("Expected a StructureDefinition URL");const n=r.replace("http://hl7.org/fhir/StructureDefinition/","");return t.map((t=>(null==t?void 0:t.resourceType)===n))}});function lr(t){return new fr(t).tokenize()}const hr=["year","years","month","months","week","weeks","day","days","hour","hours","minute","minutes","second","seconds","millisecond","milliseconds"],ur=["!=","!~","<=",">=","{}"];class fr{constructor(t){Qe.add(this),qe.set(this,void 0),ze.set(this,void 0),n(this,qe,t,"f"),n(this,ze,0,"f")}tokenize(){const t=[];for(;r(this,ze,"f")<r(this,qe,"f").length;){const e=r(this,Qe,"m",Ge).call(this);e&&t.push(e)}return t}}function dr(t,e){return{id:t,value:e}}var pr,gr,mr,vr,yr,wr,Tr,Sr,Er;qe=new WeakMap,ze=new WeakMap,Qe=new WeakSet,Xe=function(){const t=r(this,ze,"f"),e=r(this,Qe,"m",Ge).call(this);return n(this,ze,t,"f"),e},Ge=function(){r(this,Qe,"m",Ye).call(this);const t=r(this,Qe,"m",sr).call(this);if(!t)return;const e=r(this,Qe,"m",ar).call(this);return"/"===t&&"*"===e?r(this,Qe,"m",Ve).call(this):"/"===t&&"/"===e?r(this,Qe,"m",Ke).call(this):"'"===t?r(this,Qe,"m",Je).call(this):"`"===t?r(this,Qe,"m",Ze).call(this):"@"===t?r(this,Qe,"m",tr).call(this):t.match(/\d/)?r(this,Qe,"m",er).call(this):t.match(/\w/)||"$"===t&&e.match(/\w/)?r(this,Qe,"m",rr).call(this):r(this,Qe,"m",nr).call(this)},Ye=function(){return dr("Whitespace",r(this,Qe,"m",ir).call(this,(()=>r(this,Qe,"m",sr).call(this).match(/\s/))))},Ve=function(){const t=r(this,ze,"f");return r(this,Qe,"m",ir).call(this,(()=>"*"!==r(this,Qe,"m",sr).call(this)||"/"!==r(this,Qe,"m",ar).call(this))),n(this,ze,r(this,ze,"f")+2,"f"),dr("Comment",r(this,qe,"f").substring(t,r(this,ze,"f")))},Ke=function(){return dr("Comment",r(this,Qe,"m",ir).call(this,(()=>"\n"!==r(this,Qe,"m",sr).call(this))))},Je=function(){var t,e;n(this,ze,(t=r(this,ze,"f"),++t),"f");const i=dr("String",r(this,Qe,"m",ir).call(this,(()=>"\\"===r(this,Qe,"m",or).call(this)||"'"!==r(this,Qe,"m",sr).call(this))));return n(this,ze,(e=r(this,ze,"f"),++e),"f"),i},Ze=function(){var t,e;n(this,ze,(t=r(this,ze,"f"),++t),"f");const i=dr("Symbol",r(this,Qe,"m",ir).call(this,(()=>"`"!==r(this,Qe,"m",sr).call(this))));return n(this,ze,(e=r(this,ze,"f"),++e),"f"),i},tr=function(){var t,e,i,s,o;const a=r(this,ze,"f");return n(this,ze,(t=r(this,ze,"f"),++t),"f"),r(this,Qe,"m",ir).call(this,(()=>r(this,Qe,"m",sr).call(this).match(/[\d-]/))),"T"===r(this,Qe,"m",sr).call(this)&&(n(this,ze,(e=r(this,ze,"f"),++e),"f"),r(this,Qe,"m",ir).call(this,(()=>r(this,Qe,"m",sr).call(this).match(/[\d:]/))),"."===r(this,Qe,"m",sr).call(this)&&r(this,Qe,"m",ar).call(this).match(/\d/)&&(n(this,ze,(i=r(this,ze,"f"),++i),"f"),r(this,Qe,"m",ir).call(this,(()=>r(this,Qe,"m",sr).call(this).match(/[\d]/)))),"Z"===r(this,Qe,"m",sr).call(this)?n(this,ze,(s=r(this,ze,"f"),++s),"f"):"+"!==r(this,Qe,"m",sr).call(this)&&"-"!==r(this,Qe,"m",sr).call(this)||(n(this,ze,(o=r(this,ze,"f"),++o),"f"),r(this,Qe,"m",ir).call(this,(()=>r(this,Qe,"m",sr).call(this).match(/[\d:]/))))),dr("DateTime",r(this,qe,"f").substring(a+1,r(this,ze,"f")))},er=function(){var t;const e=r(this,ze,"f");let i="Number";return r(this,Qe,"m",ir).call(this,(()=>r(this,Qe,"m",sr).call(this).match(/\d/))),"."===r(this,Qe,"m",sr).call(this)&&r(this,Qe,"m",ar).call(this).match(/\d/)&&(n(this,ze,(t=r(this,ze,"f"),++t),"f"),r(this,Qe,"m",ir).call(this,(()=>r(this,Qe,"m",sr).call(this).match(/\d/))))," "===r(this,Qe,"m",sr).call(this)&&function(t){if(t){if("String"===t.id)return!0;if("Symbol"===t.id&&hr.includes(t.value))return!0}return!1}(r(this,Qe,"m",Xe).call(this))&&(i="Quantity",r(this,Qe,"m",Ge).call(this)),dr(i,r(this,qe,"f").substring(e,r(this,ze,"f")))},rr=function(){return dr("Symbol",r(this,Qe,"m",ir).call(this,(()=>r(this,Qe,"m",sr).call(this).match(/[$\w]/))))},nr=function(){var t;const e=r(this,Qe,"m",sr).call(this),i=e+r(this,Qe,"m",ar).call(this);return ur.includes(i)?(n(this,ze,r(this,ze,"f")+2,"f"),dr(i,i)):(n(this,ze,(t=r(this,ze,"f"),++t),"f"),dr(e,e))},ir=function(t){var e;const i=r(this,ze,"f");for(;r(this,ze,"f")<r(this,qe,"f").length&&t();)n(this,ze,(e=r(this,ze,"f"),++e),"f");return r(this,qe,"f").substring(i,r(this,ze,"f"))},sr=function(){return r(this,qe,"f")[r(this,ze,"f")]},or=function(){var t;return null!==(t=r(this,qe,"f")[r(this,ze,"f")-1])&&void 0!==t?t:""},ar=function(){var t;return null!==(t=r(this,qe,"f")[r(this,ze,"f")+1])&&void 0!==t?t:""};pr=new WeakMap,gr=new WeakMap;class br{constructor(t,e,r){mr.add(this),vr.set(this,void 0),yr.set(this,void 0),wr.set(this,void 0),n(this,vr,t,"f"),n(this,yr,e,"f"),n(this,wr,r,"f")}match(t){const e=r(this,mr,"m",Er).call(this);return(null==e?void 0:e.id)===t&&(r(this,mr,"m",Sr).call(this),!0)}consumeAndParse(t=100){const e=r(this,mr,"m",Sr).call(this),n=r(this,yr,"f")[e.id];if(!n)throw Error(`Parse error at ${e.value}. No matching prefix parselet.`);let i=n.parse(this,e);for(;t>r(this,mr,"m",Tr).call(this);){const t=r(this,mr,"m",Sr).call(this);i=r(this,wr,"f")[t.id].parse(this,i,t)}return i}}vr=new WeakMap,yr=new WeakMap,wr=new WeakMap,mr=new WeakSet,Tr=function(){const t=r(this,mr,"m",Er).call(this);if(!t)return 100;const e=r(this,wr,"f")[t.id];return e?e.precedence:100},Sr=function(){if(!r(this,vr,"f").length)throw Error("Cant consume unknown more tokens.");return r(this,vr,"f").shift()},Er=function(){return r(this,vr,"f").length>0?r(this,vr,"f")[0]:void 0};const Ar={parse(t){const e=t.consumeAndParse();if(!t.match(")"))throw new Error("Parse error: expected `)`");return e}},Pr={parse(t,e){const r=t.consumeAndParse();if(!t.match("]"))throw new Error("Parse error: expected `]`");return new Ae(e,r)},precedence:2},Or={parse(t,e){if(!(e instanceof ie))throw new Error("Unexpected parentheses");if(!(e.name in cr))throw new Error("Unrecognized function: "+e.name);const r=[];for(;!t.match(")");)r.push(t.consumeAndParse()),t.match(",");return new be(e.name,r,cr[e.name])},precedence:0};const xr=(new class{constructor(){pr.set(this,{}),gr.set(this,{})}registerInfix(t,e){return r(this,gr,"f")[t]=e,this}registerPrefix(t,e){return r(this,pr,"f")[t]=e,this}prefix(t,e,r){return this.registerPrefix(t,{parse(t,n){const i=t.consumeAndParse(e);return r(n,i)}})}infixLeft(t,e,r){return this.registerInfix(t,{parse(t,n,i){const s=t.consumeAndParse(e);return r(n,i,s)},precedence:e})}construct(t){return new br(lr(t),r(this,pr,"f"),r(this,gr,"f"))}}).registerPrefix("String",{parse:(t,e)=>new ne(e.value)}).registerPrefix("DateTime",{parse:(t,e)=>new ne(Pe(e.value))}).registerPrefix("Quantity",{parse:(t,e)=>new ne(function(t){const e=t.split(" "),r=parseFloat(e[0]);let n=e[1];return n=n&&n.startsWith("'")&&n.endsWith("'")?n.substring(1,n.length-1):"{"+n+"}",{value:r,unit:n}}(e.value))}).registerPrefix("Number",{parse:(t,e)=>new ne(parseFloat(e.value))}).registerPrefix("Symbol",{parse:(t,e)=>"false"===e.value?new ne(!1):"true"===e.value?new ne(!0):new ie(e.value)}).registerPrefix("{}",{parse:()=>new se}).registerPrefix("(",Ar).registerInfix("[",Pr).registerInfix("(",Or).prefix("+",3,((t,e)=>new oe(e,(t=>t)))).prefix("-",3,((t,e)=>new ce(e,e,((t,e)=>-e)))).infixLeft(".",1,((t,e,r)=>new de(t,r))).infixLeft("/",4,((t,e,r)=>new ce(t,r,((t,e)=>t/e)))).infixLeft("*",4,((t,e,r)=>new ce(t,r,((t,e)=>t*e)))).infixLeft("+",5,((t,e,r)=>new ce(t,r,((t,e)=>t+e)))).infixLeft("-",5,((t,e,r)=>new ce(t,r,((t,e)=>t-e)))).infixLeft("|",7,((t,e,r)=>new pe(t,r))).infixLeft("=",9,((t,e,r)=>new ge(t,r))).infixLeft("!=",9,((t,e,r)=>new me(t,r))).infixLeft("~",9,((t,e,r)=>new ve(t,r))).infixLeft("!~",9,((t,e,r)=>new ye(t,r))).infixLeft("<",8,((t,e,r)=>new le(t,r,((t,e)=>t<e)))).infixLeft("<=",8,((t,e,r)=>new le(t,r,((t,e)=>t<=e)))).infixLeft(">",8,((t,e,r)=>new le(t,r,((t,e)=>t>e)))).infixLeft(">=",8,((t,e,r)=>new le(t,r,((t,e)=>t>=e)))).infixLeft("&",5,((t,e,r)=>new he(t,r))).infixLeft("Symbol",6,((t,e,r)=>{switch(e.value){case"and":return new Te(t,r);case"as":return new ae(t,r);case"contains":return new ue(t,r);case"div":return new ce(t,r,((t,e)=>t/e|0));case"in":return new fe(t,r);case"is":return new we(t,r);case"mod":return new ce(t,r,((t,e)=>t%e));case"or":return new Se(t,r);case"xor":return new Ee(t,r);default:throw new Error("Cannot use "+e.value+" as infix operator")}}));function kr(t){try{return new re(t,xr.construct(t).consumeAndParse())}catch(e){throw new Error(`FhirPathError on "${t}": ${e}`)}}class Rr{constructor(t){this.segments=t}get(t){return"number"==typeof t?this.segments[t]:this.segments.find((e=>e.name===t))}getAll(t){return this.segments.filter((e=>e.name===t))}toString(){return this.segments.map((t=>t.toString())).join("\r")}buildAck(){var t,e,r,n,i,s;const o=new Date,a=this.get("MSH"),c=(null===(t=null==a?void 0:a.get(2))||void 0===t?void 0:t.toString())||"",l=(null===(e=null==a?void 0:a.get(3))||void 0===e?void 0:e.toString())||"",h=(null===(r=null==a?void 0:a.get(4))||void 0===r?void 0:r.toString())||"",u=(null===(n=null==a?void 0:a.get(5))||void 0===n?void 0:n.toString())||"",f=(null===(i=null==a?void 0:a.get(9))||void 0===i?void 0:i.toString())||"",d=(null===(s=null==a?void 0:a.get(12))||void 0===s?void 0:s.toString())||"2.5.1";return new Rr([new Cr(["MSH","^~\\&",h,u,c,l,o.toISOString(),"","ACK",o.getTime().toString(),"P",d]),new Cr(["MSA","AA",f,"OK"])])}static parse(t){if(!t.startsWith("MSH|^~\\&")){const t=new Error("Invalid HL7 message");throw t.type="entity.parse.failed",t}return new Rr(t.split(/[\r\n]+/).map((t=>Cr.parse(t))))}}class Cr{constructor(t){E(t)?this.fields=t.map((t=>Ur.parse(t))):this.fields=t,this.name=this.fields[0].components[0]}get(t){return this.fields[t]}toString(){return this.fields.map((t=>t.toString())).join("|")}static parse(t){return new Cr(t.split("|").map((t=>Ur.parse(t))))}}class Ur{constructor(t){this.components=t}get(t){return this.components[t]}toString(){return this.components.join("^")}static parse(t){return new Ur(t.split("^"))}}var Mr;function Lr(t,e){const r=e.split(" | ");for(const e of r){const r=_r(e);if(r.startsWith(t+"."))return r}}function _r(t){let e=t.trim();return e.startsWith("(")&&e.endsWith(")")&&(e=e.substring(1,e.length-1)),e.includes(" as ")&&(e=e.substring(0,e.indexOf(" as "))),e.includes(".where(")&&(e=e.substring(0,e.indexOf(".where("))),e}t.SearchParameterType=void 0,(Mr=t.SearchParameterType||(t.SearchParameterType={})).BOOLEAN="BOOLEAN",Mr.NUMBER="NUMBER",Mr.QUANTITY="QUANTITY",Mr.TEXT="TEXT",Mr.REFERENCE="REFERENCE",Mr.DATE="DATE",Mr.DATETIME="DATETIME",Mr.PERIOD="PERIOD",t.COMPONENT_SEPARATOR="^",t.DEFAULT_SEARCH_COUNT=20,t.FIELD_SEPARATOR="|",t.Hl7Field=Ur,t.Hl7Message=Rr,t.Hl7Segment=Cr,t.LRUCache=c,t.MedplumClient=class extends class{constructor(){k.set(this,void 0),n(this,k,{},"f")}addEventListener(t,e){r(this,k,"f")[t]||(r(this,k,"f")[t]=[]),r(this,k,"f")[t].push(e)}removeEventListeneer(t,e){const n=r(this,k,"f")[t];if(n)for(let t=0;t<n.length;t++)if(n[t]===e)return void n.splice(t,1)}dispatchEvent(t){const e=r(this,k,"f")[t.type];return e&&e.forEach((e=>e.call(this,t))),!t.defaultPrevented}}{constructor(t){var e;if(super(),it.add(this),st.set(this,void 0),ot.set(this,void 0),at.set(this,void 0),ct.set(this,void 0),lt.set(this,void 0),ht.set(this,void 0),ut.set(this,void 0),ft.set(this,void 0),dt.set(this,void 0),pt.set(this,void 0),gt.set(this,void 0),mt.set(this,void 0),vt.set(this,void 0),yt.set(this,void 0),wt.set(this,void 0),Tt.set(this,void 0),null==t?void 0:t.baseUrl){if(!t.baseUrl.startsWith("http"))throw new Error("Base URL must start with http or https");if(!t.baseUrl.endsWith("/"))throw new Error("Base URL must end with a trailing slash")}n(this,st,(null==t?void 0:t.fetch)||window.fetch.bind(window),"f"),n(this,ot,new _t,"f"),n(this,at,{types:{}},"f"),n(this,ct,new c(null!==(e=null==t?void 0:t.resourceCacheSize)&&void 0!==e?e:1e3),"f"),n(this,lt,(null==t?void 0:t.baseUrl)||"https://api.medplum.com/","f"),n(this,ht,(null==t?void 0:t.clientId)||"","f"),n(this,ut,(null==t?void 0:t.authorizeUrl)||r(this,lt,"f")+"oauth2/authorize","f"),n(this,ft,(null==t?void 0:t.tokenUrl)||r(this,lt,"f")+"oauth2/token","f"),n(this,dt,(null==t?void 0:t.logoutUrl)||r(this,lt,"f")+"oauth2/logout","f"),n(this,pt,null==t?void 0:t.onUnauthenticated,"f");const i=this.getActiveLogin();i&&(n(this,gt,i.accessToken,"f"),n(this,mt,i.refreshToken,"f"),r(this,it,"m",Et).call(this).catch(console.log)),r(this,it,"m",Lt).call(this)}getBaseUrl(){return r(this,lt,"f")}clear(){r(this,ot,"f").clear(),r(this,ct,"f").clear(),n(this,gt,void 0,"f"),n(this,mt,void 0,"f"),n(this,wt,void 0,"f"),n(this,Tt,void 0,"f"),this.dispatchEvent({type:"change"})}get(t,e={}){if(t=t.toString(),!(null==e?void 0:e.cache)){const e=r(this,ct,"f").get(t);if(e)return e}const n=new Y(r(this,it,"m",bt).call(this,"GET",t,e));return r(this,ct,"f").set(t,n),n}post(t,e,n,i={}){return t=t.toString(),e&&r(this,it,"m",Ot).call(this,i,e),n&&r(this,it,"m",Pt).call(this,i,n),r(this,ct,"f").delete(t),r(this,it,"m",bt).call(this,"POST",t,i)}put(t,e,n,i={}){return t=t.toString(),e&&r(this,it,"m",Ot).call(this,i,e),n&&r(this,it,"m",Pt).call(this,i,n),r(this,ct,"f").delete(t),r(this,it,"m",bt).call(this,"PUT",t,i)}patch(t,e,n={}){return t=t.toString(),r(this,it,"m",Ot).call(this,n,e),r(this,it,"m",Pt).call(this,n,"application/json-patch+json"),r(this,ct,"f").delete(t),r(this,it,"m",bt).call(this,"PATCH",t,n)}delete(t,e={}){return t=t.toString(),r(this,ct,"f").delete(t),r(this,it,"m",bt).call(this,"DELETE",t,e)}register(t){return e(this,void 0,void 0,(function*(){const e=yield this.post("auth/register",t);yield this.setActiveLogin(e)}))}startLogin(t){return e(this,void 0,void 0,(function*(){return yield r(this,it,"m",kt).call(this),this.post("auth/login",Object.assign(Object.assign({},t),{clientId:r(this,ht,"f"),scope:Ft,codeChallengeMethod:"S256",codeChallenge:r(this,ot,"f").getString("codeChallenge")}))}))}startGoogleLogin(t){return e(this,void 0,void 0,(function*(){return yield r(this,it,"m",kt).call(this),this.post("auth/google",t)}))}signOut(){return this.clear(),Promise.resolve()}signInWithRedirect(){const t=new URLSearchParams(window.location.search).get("code");return t?this.processCode(t):void r(this,it,"m",Rt).call(this)}signOutWithRedirect(){window.location.assign(r(this,dt,"f"))}fhirUrl(...t){return new URL(r(this,lt,"f")+"fhir/R4/"+t.join("/"))}search(t,e={}){return this.get("string"==typeof t?"fhir/R4/"+t:this.fhirUrl(t.resourceType)+tt(t),e)}searchOne(t,e={}){const r="string"==typeof t?Z(t):t;return r.count=1,new Y(this.search(r,e).then((t=>{var e,r;return null===(r=null===(e=t.entry)||void 0===e?void 0:e[0])||void 0===r?void 0:r.resource})))}searchResources(t,e={}){return new Y(this.search(t,e).then((t=>{var e,r;return null!==(r=null===(e=t.entry)||void 0===e?void 0:e.map((t=>t.resource)))&&void 0!==r?r:[]})))}searchValueSet(t,e,r={}){const n=this.fhirUrl("ValueSet","$expand");return n.searchParams.set("url",t),n.searchParams.set("filter",e),this.get(n.toString(),r)}getCached(t,e){const n=r(this,ct,"f").get(this.fhirUrl(t,e).toString());return n&&!n.isPending()?n.read():void 0}getCachedReference(t){const e=t.reference,[r,n]=e.split("/");return this.getCached(r,n)}readResource(t,e){return this.get(this.fhirUrl(t,e))}readCached(t,e){return this.get(this.fhirUrl(t,e))}readReference(t){const e=null==t?void 0:t.reference;if(!e)return new Y(Promise.reject(new Error("Missing reference")));const[r,n]=e.split("/");return this.readResource(r,n)}readCachedReference(t){const e=null==t?void 0:t.reference;if(!e)return new Y(Promise.reject(new Error("Missing reference")));const[r,n]=e.split("/");return this.readCached(r,n)}getSchema(){return r(this,at,"f")}requestSchema(t){return e(this,void 0,void 0,(function*(){if(t in r(this,at,"f").types)return Promise.resolve(r(this,at,"f"));const e=`{\n StructureDefinitionList(name: "${t}") {\n name,\n description,\n snapshot {\n element {\n id,\n path,\n min,\n max,\n type {\n code,\n targetProfile\n },\n binding {\n valueSet\n },\n definition\n }\n }\n }\n SearchParameterList(base: "${t}", _count: 100) {\n base,\n code,\n type,\n expression,\n target\n }\n }`.replace(/\s+/g," "),n=yield this.graphql(e);for(const t of n.data.StructureDefinitionList)Nt(r(this,at,"f"),t);for(const t of n.data.SearchParameterList)Wt(r(this,at,"f"),t);return r(this,at,"f")}))}readHistory(t,e){return this.get(this.fhirUrl(t,e,"_history"))}readVersion(t,e,r){return this.get(this.fhirUrl(t,e,"_history",r))}readPatientEverything(t){return this.get(this.fhirUrl("Patient",t,"$everything"))}createResource(t){if(!t.resourceType)throw new Error("Missing resourceType");return this.post(this.fhirUrl(t.resourceType),t)}createResourceIfNoneExist(t,r){var n;return e(this,void 0,void 0,(function*(){return null!==(n=yield this.searchOne(`${t.resourceType}?${r}`))&&void 0!==n?n:this.createResource(t)}))}createBinary(t,e,r){const n=this.fhirUrl("Binary");return e&&n.searchParams.set("_filename",e),this.post(n,t,r)}createPdf(t,e){const r=this.fhirUrl("Binary","$pdf");return e&&r.searchParams.set("_filename",e),this.post(r,t,"application/json")}createComment(t,e){const r=this.getProfile();let n,i;return"Encounter"===t.resourceType&&(n=h(t),i=t.subject),"ServiceRequest"===t.resourceType&&(n=t.encounter,i=t.subject),"Patient"===t.resourceType&&(i=h(t)),this.createResource({resourceType:"Communication",basedOn:[h(t)],encounter:n,subject:i,sender:r?h(r):void 0,sent:(new Date).toISOString(),payload:[{contentString:e}]})}updateResource(t){if(!t.resourceType)throw new Error("Missing resourceType");if(!t.id)throw new Error("Missing id");return this.put(this.fhirUrl(t.resourceType,t.id),t)}patchResource(t,e,r){return this.patch(this.fhirUrl(t,e),r)}deleteResource(t,e){return this.delete(this.fhirUrl(t,e))}sendEmail(t){return this.post("email/v1/send",t,"application/json")}graphql(t,e){return this.post(this.fhirUrl("$graphql"),{query:t},"application/json",e)}getActiveLogin(){return r(this,ot,"f").getObject("activeLogin")}setActiveLogin(t){return e(this,void 0,void 0,(function*(){n(this,gt,t.accessToken,"f"),n(this,mt,t.refreshToken,"f"),n(this,wt,void 0,"f"),n(this,Tt,void 0,"f"),r(this,ot,"f").setObject("activeLogin",t),r(this,it,"m",St).call(this,t),r(this,ct,"f").clear(),n(this,vt,void 0,"f"),yield r(this,it,"m",Et).call(this)}))}getAccessToken(){return r(this,gt,"f")}setAccessToken(t){n(this,gt,t,"f"),n(this,mt,void 0,"f"),n(this,wt,void 0,"f"),n(this,Tt,void 0,"f")}getLogins(){var t;return null!==(t=r(this,ot,"f").getObject("logins"))&&void 0!==t?t:[]}isLoading(){return!!r(this,yt,"f")}getProfile(){return r(this,wt,"f")}getProfileAsync(){return e(this,void 0,void 0,(function*(){return r(this,yt,"f")&&(yield r(this,yt,"f")),this.getProfile()}))}getUserConfiguration(){return r(this,Tt,"f")}download(t,n={}){return e(this,void 0,void 0,(function*(){r(this,vt,"f")&&(yield r(this,vt,"f")),r(this,it,"m",At).call(this,n);return(yield r(this,st,"f").call(this,t.toString(),n)).blob()}))}processCode(t){if(!r(this,ot,"f").getString("pkceState"))throw this.clear(),new Error("Invalid PCKE state");const e=r(this,ot,"f").getString("codeVerifier");if(!e)throw this.clear(),new Error("Invalid PCKE code verifier");const n=new URLSearchParams;return n.set("grant_type","authorization_code"),n.set("client_id",r(this,ht,"f")),n.set("code_verifier",e),n.set("code",t),n.set("redirect_uri",$t()),r(this,it,"m",Ut).call(this,n)}startClientLogin(t,n){return e(this,void 0,void 0,(function*(){const e=new URLSearchParams;return e.set("grant_type","client_credentials"),e.set("client_id",t),e.set("client_secret",n),r(this,it,"m",Ut).call(this,e)}))}},t.OperationOutcomeError=H,t.ReadablePromise=Y,t.SEGMENT_SEPARATOR="\r",t.accessDenied=F,t.allOk=j,t.arrayBufferToBase64=P,t.arrayBufferToHex=A,t.assertOk=function(t,e){if(!$(t)||void 0===e)throw new H(t)},t.badRequest=function(t,e){return{resourceType:"OperationOutcome",issue:[{severity:"error",code:"invalid",details:{text:t},expression:e?[e]:void 0}]}},t.buildTypeName=Bt,t.calculateAge=g,t.calculateAgeString=function(t,e){const{years:r,months:n,days:i}=g(t,e);return r>=2?r.toString().padStart(3,"0")+"Y":n>=1?n.toString().padStart(3,"0")+"M":i.toString().padStart(3,"0")+"D"},t.capitalize=O,t.createReference=h,t.createSchema=jt,t.createTypeSchema=It,t.created=I,t.deepEquals=T,t.evalFhirPath=function(t,e){return kr(t).eval(e)},t.formatAddress=function(t,e){const r=[];return t.line&&r.push(...t.line),t.city&&r.push(t.city),t.state&&r.push(t.state),t.postalCode&&r.push(t.postalCode),t.use&&((null==e?void 0:e.all)||(null==e?void 0:e.use))&&r.push("["+t.use+"]"),r.join(", ").trim()},t.formatFamilyName=function(t){return t.family||""},t.formatGivenName=function(t){const e=[];return t.given&&e.push(...t.given),e.join(" ").trim()},t.formatHumanName=l,t.formatSearchQuery=tt,t.getDateProperty=function(t){return t?new Date(t):void 0},t.getDisplayString=d,t.getExpressionForResourceType=Lr,t.getExtensionValue=function(t,...e){var r;let n=t;for(let t=0;t<e.length&&n;t++)n=null===(r=null==n?void 0:n.extension)||void 0===r?void 0:r.find((r=>r.url===e[t]));return null==n?void 0:n.valueString},t.getIdentifier=function(t,e){const r=t.identifier;if(!r)return;const n=Array.isArray(r)?r:[r];for(const t of n)if(t.system===e)return t.value},t.getImageSrc=function(t){if(f(t)){const e=t.photo;if(e)for(const t of e){const e=p(t);if(e)return e}}if("Bot"===t.resourceType&&t.photo){const e=p(t.photo);if(e)return e}},t.getPropertyDisplayName=function(t){const e=t.path.replaceAll("[x]","").split(".").pop();return"id"===e?"ID":e.split(/(?=[A-Z])/).map(O).join(" ").replace("_"," ").replace(/\s+/g," ")},t.getQuestionnaireAnswers=function(t){const e={};return m(t.item,e),e},t.getReferenceString=u,t.getSearchParameterDetails=function(e,r,n){var i,s,o,a;if("_lastUpdated"===n.code)return{columnName:"lastUpdated",type:t.SearchParameterType.DATETIME};const c=n.code.split("-").reduce(((t,e,r)=>t+(r?O(e):e)),""),l=null===(i=Lr(r,n.expression))||void 0===i?void 0:i.split(".");if(!l)return{columnName:c,type:t.SearchParameterType.TEXT};let h,u,f=r,d=!1;for(let r=1;r<l.length;r++){const n=l[r];if(h=null===(o=null===(s=e.types[f])||void 0===s?void 0:s.properties)||void 0===o?void 0:o[n],!h)return{columnName:c,type:t.SearchParameterType.TEXT,array:d};if("*"===h.max&&(d=!0),u=null===(a=h.type)||void 0===a?void 0:a[0].code,!u)return{columnName:c,type:t.SearchParameterType.TEXT,array:d};r<l.length-1&&("Element"===u||"BackboneElement"===u?f+=O(n):f=u)}const p=function(e,r){let n=t.SearchParameterType.TEXT;switch(e.type){case"date":n=t.SearchParameterType.DATE;break;case"number":n=t.SearchParameterType.NUMBER;break;case"quantity":n=t.SearchParameterType.QUANTITY;break;case"reference":n=t.SearchParameterType.REFERENCE;break;case"token":"boolean"===r&&(n=t.SearchParameterType.BOOLEAN)}return n}(n,u);return{columnName:c,type:p,elementDefinition:h,array:d}},t.getStatus=function(t){return t.id===C?200:t.id===U?201:t.id===L?304:t.id===D?403:t.id===_?404:t.id===M?410:400},t.gone=B,t.indexSearchParameter=Wt,t.indexStructureDefinition=Nt,t.isGone=function(t){return t.id===M},t.isLowerCase=function(t){return t===t.toLowerCase()},t.isNotFound=function(t){return t.id===_},t.isObject=S,t.isOk=$,t.isProfileResource=f,t.isStringArray=E,t.isUUID=function(t){return!!t.match(/^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$/i)},t.notFound=W,t.notModified=N,t.parseFhirPath=kr,t.parseSearchDefinition=Z,t.resolveId=function(t){var e;return null===(e=null==t?void 0:t.reference)||void 0===e?void 0:e.split("/")[1]},t.stringify=v,t.tokenize=lr,Object.defineProperty(t,"__esModule",{value:!0})}));
1
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(((t="undefined"!=typeof globalThis?globalThis:t||self).medplum=t.medplum||{},t.medplum.core={}))}(this,(function(t){"use strict";function e(t,e,r,n){return new(r||(r=Promise))((function(i,s){function o(t){try{l(n.next(t))}catch(t){s(t)}}function a(t){try{l(n.throw(t))}catch(t){s(t)}}function l(t){var e;t.done?i(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(o,a)}l((n=n.apply(t,e||[])).next())}))}function r(t,e,r,n){if("a"===r&&!n)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!n:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?n:"a"===r?n.call(t):n?n.value:e.get(t)}function n(t,e,r,n,i){if("m"===n)throw new TypeError("Private method is not writable");if("a"===n&&!i)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!i:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===n?i.call(t,r):i?i.value=r:e.set(t,r),r}var i,s,o,a;class l{constructor(t=10){i.add(this),s.set(this,void 0),o.set(this,void 0),n(this,s,t,"f"),n(this,o,new Map,"f")}clear(){r(this,o,"f").clear()}get(t){const e=r(this,o,"f").get(t);return e&&(r(this,o,"f").delete(t),r(this,o,"f").set(t,e)),e}set(t,e){r(this,o,"f").has(t)?r(this,o,"f").delete(t):r(this,o,"f").size>=r(this,s,"f")&&r(this,o,"f").delete(r(this,i,"m",a).call(this)),r(this,o,"f").set(t,e)}delete(t){r(this,o,"f").delete(t)}keys(){return r(this,o,"f").keys()}}function c(t,e){const r=[];return t.prefix&&((null==e?void 0:e.all)||(null==e?void 0:e.prefix))&&r.push(...t.prefix),t.given&&r.push(...t.given),t.family&&r.push(t.family),t.suffix&&((null==e?void 0:e.all)||(null==e?void 0:e.suffix))&&r.push(...t.suffix),t.use&&((null==e?void 0:e.all)||(null==e?void 0:e.use))&&r.push("["+t.use+"]"),r.join(" ").trim()}function u(t){const e=h(t),r=d(t);return r===e?{reference:e}:{reference:e,display:r}}function h(t){return t.resourceType+"/"+t.id}function f(t){return"Patient"===t.resourceType||"Practitioner"===t.resourceType||"RelatedPerson"===t.resourceType}function d(t){var e,r;if(f(t)){const e=function(t){const e=t.name;if(e&&e.length>0)return c(e[0]);return}(t);if(e)return e}if("Device"===t.resourceType){const e=function(t){const e=t.deviceName;if(e&&e.length>0)return e[0].name;return}(t);if(e)return e}return"Observation"===t.resourceType&&"code"in t&&(null===(e=t.code)||void 0===e?void 0:e.text)?null===(r=t.code)||void 0===r?void 0:r.text:"User"===t.resourceType&&t.email?t.email:"name"in t&&t.name&&"string"==typeof t.name?t.name:h(t)}function p(t){if(t.url&&t.contentType&&t.contentType.startsWith("image/"))return t.url}function v(t,e){const r=new Date(t);r.setUTCHours(0,0,0,0);const n=e?new Date(e):new Date;n.setUTCHours(0,0,0,0);const i=r.getUTCFullYear(),s=r.getUTCMonth(),o=r.getUTCDate(),a=n.getUTCFullYear(),l=n.getUTCMonth(),c=n.getUTCDate();let u=a-i;(l<s||l===s&&c<o)&&u--;let h=12*a+l-(12*i+s);c<o&&h--;return{years:u,months:h,days:Math.floor((n.getTime()-r.getTime())/864e5)}}function m(t,e){if(t)for(const r of t)r.linkId&&r.answer&&r.answer.length>0&&(e[r.linkId]=r.answer[0]),m(r.item,e)}function g(t,e){return JSON.stringify(t,y,e?2:void 0)}function y(t,e){return!function(t){return!!t.match(/\d+$/)}(t)&&w(e)?void 0:e}function w(t){if(null==t)return!0;const e=typeof t;return"string"===e&&""===t||"object"===e&&0===Object.keys(t).length}function T(t,e,r){return t===e||(!(!w(t)||!w(e))||!w(t)&&!w(e)&&(Array.isArray(t)&&Array.isArray(e)?function(t,e){if(t.length!==e.length)return!1;for(let r=0;r<t.length;r++)if(!T(t[r],e[r]))return!1;return!0}(t,e):!Array.isArray(t)&&!Array.isArray(e)&&(S(t)&&S(e)?function(t,e,r){const n=new Set;Object.keys(t).forEach((t=>n.add(t))),Object.keys(e).forEach((t=>n.add(t))),"meta"===r&&(n.delete("versionId"),n.delete("lastUpdated"),n.delete("author"));for(const r of n){if(!T(t[r],e[r],r))return!1}return!0}(t,e,r):(S(t)||S(e),!1))))}function S(t){return null!==t&&"object"==typeof t}function b(t){return t.every((t=>"string"==typeof t))}s=new WeakMap,o=new WeakMap,i=new WeakSet,a=function(){return r(this,o,"f").keys().next().value};const E=[];for(let t=0;t<256;t++)E.push(t.toString(16).padStart(2,"0"));function P(t){const e=new Uint8Array(t),r=new Array(e.length);for(let t=0;t<e.length;t++)r[t]=E[e[t]];return r.join("")}function A(t){const e=new Uint8Array(t),r=[];for(let t=0;t<e.length;t++)r[t]=String.fromCharCode(e[t]);return window.btoa(r.join(""))}function O(t){return t.charAt(0).toUpperCase()+t.substring(1)}function k(){const t=new Uint32Array(28);return crypto.getRandomValues(t),P(t.buffer)}var x;function R(t){const e=function(t){if("undefined"!=typeof window)return window.atob(t);if("undefined"!=typeof Buffer)return Buffer.from(t,"base64").toString("binary");throw new Error("Unable to decode base64")}(t.replace(/-/g,"+").replace(/_/g,"/")),r=Array.from(e).reduce(((t,e)=>`${t}%${("00"+e.charCodeAt(0).toString(16)).slice(-2)}`),""),n=decodeURIComponent(r);return JSON.parse(n)}x=new WeakMap;const U="ok",M="created",C="gone",L="not-modified",D="not-found",I="access-denied",_={resourceType:"OperationOutcome",id:U,issue:[{severity:"information",code:"information",details:{text:"All OK"}}]},j={resourceType:"OperationOutcome",id:M,issue:[{severity:"information",code:"information",details:{text:"Created"}}]},N={resourceType:"OperationOutcome",id:L,issue:[{severity:"information",code:"information",details:{text:"Not Modified"}}]},W={resourceType:"OperationOutcome",id:D,issue:[{severity:"error",code:"not-found",details:{text:"Not found"}}]},B={resourceType:"OperationOutcome",id:C,issue:[{severity:"error",code:"gone",details:{text:"Gone"}}]},H={resourceType:"OperationOutcome",id:I,issue:[{severity:"error",code:"access-denied",details:{text:"Access Denied"}}]};function F(t){return t.id===U||t.id===M||t.id===L}class Q extends Error{constructor(t){var e,r;super(null===(r=null===(e=null==t?void 0:t.issue)||void 0===e?void 0:e[0].details)||void 0===r?void 0:r.text),this.outcome=t}}function $(t,r,n){return e(this,void 0,void 0,(function*(){return t.pageSize=t.pageSize||"LETTER",t.pageMargins=t.pageMargins||[60,60,60,60],t.pageOrientation=t.pageOrientation||"portrait",t.defaultStyle=t.defaultStyle||{},t.defaultStyle.font=t.defaultStyle.font||"Helvetica",t.defaultStyle.fontSize=t.defaultStyle.fontSize||11,t.defaultStyle.lineHeight=t.defaultStyle.lineHeight||2,"undefined"==typeof pdfMake?function(t,r,n){return e(this,void 0,void 0,(function*(){return n||(n={Helvetica:{normal:"Helvetica",bold:"Helvetica-Bold",italics:"Helvetica-Oblique",bolditalics:"Helvetica-BoldOblique"},Roboto:{normal:"https://static.medplum.com/fonts/Roboto-Regular.ttf",bold:"https://static.medplum.com/fonts/Roboto-Medium.ttf",italics:"https://static.medplum.com/fonts/Roboto-Italic.ttf",bolditalics:"https://static.medplum.com/fonts/Roboto-MediumItalic.ttf"},Avenir:{normal:"https://static.medplum.com/fonts/avenir.ttf"}}),new Promise(((e,i)=>{const s=new(require("pdfmake"))(n).createPdfKitDocument(t,{tableLayouts:r}),o=[];s.on("data",(t=>o.push(t))),s.on("end",(()=>e(Buffer.concat(o)))),s.on("error",i),s.end()}))}))}(t,r,n):function(t,r,n){return e(this,void 0,void 0,(function*(){return n||(n={Helvetica:{normal:"https://static.medplum.com/fonts/Helvetica.ttf",bold:"https://static.medplum.com/fonts/Helvetica-bold.ttf",italics:"https://static.medplum.com/fonts/Helvetica-italic.ttf",bolditalics:"https://static.medplum.com/fonts/Helvetica-bold-italic.ttf"},Roboto:{normal:"https://static.medplum.com/fonts/Roboto-Regular.ttf",bold:"https://static.medplum.com/fonts/Roboto-Medium.ttf",italics:"https://static.medplum.com/fonts/Roboto-Italic.ttf",bolditalics:"https://static.medplum.com/fonts/Roboto-MediumItalic.ttf"},Avenir:{normal:"https://static.medplum.com/fonts/avenir.ttf"}}),new Promise((e=>{pdfMake.createPdf(t,r,n).getBlob(e)}))}))}(t,r,n)}))}var q,z,X,G,Y;class V{constructor(t){this[Y]="ReadablePromise",q.set(this,void 0),z.set(this,"pending"),X.set(this,void 0),G.set(this,void 0),n(this,q,t.then((t=>(n(this,z,"success","f"),n(this,X,t,"f"),t)),(t=>{throw n(this,z,"error","f"),n(this,G,t,"f"),t})),"f")}isPending(){return"pending"===r(this,z,"f")}read(){switch(r(this,z,"f")){case"pending":throw r(this,q,"f");case"error":throw r(this,G,"f");default:return r(this,X,"f")}}then(t,e){return r(this,q,"f").then(t,e)}catch(t){return r(this,q,"f").catch(t)}finally(t){return r(this,q,"f").finally(t)}}q=new WeakMap,z=new WeakMap,X=new WeakMap,G=new WeakMap,Y=Symbol.toStringTag;var K;t.Operator=void 0,(K=t.Operator||(t.Operator={})).EQUALS="eq",K.NOT_EQUALS="ne",K.GREATER_THAN="gt",K.LESS_THAN="lt",K.GREATER_THAN_OR_EQUALS="ge",K.LESS_THAN_OR_EQUALS="le",K.STARTS_AFTER="sa",K.ENDS_BEFORE="eb",K.APPROXIMATELY="ap",K.CONTAINS="contains",K.EXACT="exact",K.TEXT="text",K.ABOVE="above",K.BELOW="below",K.IN="in",K.NOT_IN="not-in",K.OF_TYPE="of-type";const J=[t.Operator.CONTAINS,t.Operator.EXACT,t.Operator.TEXT,t.Operator.ABOVE,t.Operator.BELOW,t.Operator.IN,t.Operator.NOT_IN,t.Operator.OF_TYPE],Z=[t.Operator.NOT_EQUALS,t.Operator.GREATER_THAN,t.Operator.LESS_THAN,t.Operator.GREATER_THAN_OR_EQUALS,t.Operator.LESS_THAN_OR_EQUALS,t.Operator.STARTS_AFTER,t.Operator.ENDS_BEFORE,t.Operator.APPROXIMATELY];function tt(e){const r=new URL(e,"https://example.com/"),n=r.pathname.replace(/(^\/)|(\/$)/g,"").split("/").pop();let i,s,o,a,l,c;return new URLSearchParams(r.search).forEach(((e,r)=>{"_fields"===r?o=e.split(","):"_offset"===r?a=parseInt(e):"_count"===r?l=parseInt(e):"_total"===r?c=e:"_sort"===r?(s=s||[],s.push(function(t){return t.startsWith("-")?{code:t.substring(1),descending:!0}:{code:t}}(e))):(i=i||[],i.push(function(e,r){let n=e,i=t.Operator.EQUALS;for(const t of J){const e=n.indexOf(":"+t);-1!==e&&(i=t,n=n.substring(0,e))}for(const t of Z)r.match(new RegExp("^"+t+"\\d"))&&(i=t,r=r.substring(t.length));return{code:n,operator:i,value:r}}(r,e)))})),{resourceType:n,filters:i,fields:o,offset:a,count:l,total:c,sortRules:s}}function et(t){const e=[];return t.fields&&e.push("_fields="+t.fields.join(",")),t.filters&&t.filters.forEach((t=>e.push(function(t){const e=J.includes(t.operator)?":"+t.operator:"",r=Z.includes(t.operator)?t.operator:"";return`${t.code}${e}=${r}${encodeURIComponent(t.value)}`}(t)))),t.sortRules&&t.sortRules.length>0&&e.push(function(t){if(!t||0===t.length)return"";return"_sort="+t.map((t=>t.descending?"-"+t.code:t.code)).join(",")}(t.sortRules)),void 0!==t.offset&&e.push("_offset="+t.offset),void 0!==t.count&&e.push("_count="+t.count),void 0!==t.total&&e.push("_total="+t.total),0===e.length?"":(e.sort(),"?"+e.join("&"))}var rt,nt,it,st,ot,at,lt,ct,ut,ht,ft,dt,pt,vt,mt,gt,yt,wt,Tt,St,bt,Et,Pt,At,Ot,kt,xt,Rt,Ut,Mt,Ct,Lt,Dt;class It{constructor(){rt.set(this,void 0),n(this,rt,"undefined"!=typeof localStorage?localStorage:new _t,"f")}clear(){r(this,rt,"f").clear()}getString(t){return r(this,rt,"f").getItem(t)||void 0}setString(t,e){e?r(this,rt,"f").setItem(t,e):r(this,rt,"f").removeItem(t)}getObject(t){const e=this.getString(t);return e?JSON.parse(e):void 0}setObject(t,e){this.setString(t,e?g(e):void 0)}}rt=new WeakMap;class _t{constructor(){nt.set(this,void 0),n(this,nt,new Map,"f")}get length(){return r(this,nt,"f").size}clear(){r(this,nt,"f").clear()}getItem(t){var e;return null!==(e=r(this,nt,"f").get(t))&&void 0!==e?e:null}setItem(t,e){e?r(this,nt,"f").set(t,e):r(this,nt,"f").delete(t)}removeItem(t){r(this,nt,"f").delete(t)}key(t){return Array.from(r(this,nt,"f").keys())[t]}}function jt(){return{types:{}}}function Nt(t,e){return{display:t,description:e,properties:{},searchParams:{_lastUpdated:{base:[t],code:"_lastUpdated",type:"date",expression:t+".meta.lastUpdated"}}}}function Wt(t,e){var r;const n=e.name;if(!n)return;t.types[n]=Nt(n,e.description);const i=null===(r=e.snapshot)||void 0===r?void 0:r.element;if(i){const e=i.filter((t=>t.path!==n&&t.path));e.forEach((e=>function(t,e){var r,n;const i=e.path,s=null===(n=null===(r=e.type)||void 0===r?void 0:r[0])||void 0===n?void 0:n.code;if("Element"!==s&&"BackboneElement"!==s)return;const o=i.split("."),a=Ht(o);a in t.types||(t.types[a]=Nt(a,e.definition),t.types[a].parentType=Ht(o.slice(0,o.length-1)))}(t,e))),e.forEach((e=>function(t,e){const r=e.path.split(".");if(1===r.length)return;const n=Ht(r.slice(0,r.length-1)),i=t.types[n],s=r[r.length-1];i.properties[s]=e}(t,e)))}}function Bt(t,e){if(e.base)for(const r of e.base){const n=t.types[r];n&&(n.searchParams||(n.searchParams={}),n.searchParams[e.code]=e)}}function Ht(t){return t.map(O).join("")}nt=new WeakMap,t.PropertyType=void 0,(it=t.PropertyType||(t.PropertyType={})).Address="Address",it.Age="Age",it.Annotation="Annotation",it.Attachment="Attachment",it.BackboneElement="BackboneElement",it.CodeableConcept="CodeableConcept",it.Coding="Coding",it.ContactDetail="ContactDetail",it.ContactPoint="ContactPoint",it.Contributor="Contributor",it.Count="Count",it.DataRequirement="DataRequirement",it.Distance="Distance",it.Dosage="Dosage",it.Duration="Duration",it.Expression="Expression",it.Extension="Extension",it.HumanName="HumanName",it.Identifier="Identifier",it.MarketingStatus="MarketingStatus",it.Meta="Meta",it.Money="Money",it.Narrative="Narrative",it.ParameterDefinition="ParameterDefinition",it.Period="Period",it.Population="Population",it.ProdCharacteristic="ProdCharacteristic",it.ProductShelfLife="ProductShelfLife",it.Quantity="Quantity",it.Range="Range",it.Ratio="Ratio",it.Reference="Reference",it.RelatedArtifact="RelatedArtifact",it.Resource="Resource",it.SampledData="SampledData",it.Signature="Signature",it.SubstanceAmount="SubstanceAmount",it.SystemString="http://hl7.org/fhirpath/System.String",it.Timing="Timing",it.TriggerDefinition="TriggerDefinition",it.UsageContext="UsageContext",it.base64Binary="base64Binary",it.boolean="boolean",it.canonical="canonical",it.code="code",it.date="date",it.dateTime="dateTime",it.decimal="decimal",it.id="id",it.instant="instant",it.integer="integer",it.markdown="markdown",it.oid="oid",it.positiveInt="positiveInt",it.string="string",it.time="time",it.unsignedInt="unsignedInt",it.uri="uri",it.url="url",it.uuid="uuid";const Ft="launch/patient openid fhirUser offline_access user/*.*";function Qt(){return window.location.protocol+"//"+window.location.host+"/"}function $t(e){return[{type:t.PropertyType.boolean,value:e}]}function qt(e){return Number.isSafeInteger(e)?{type:t.PropertyType.integer,value:e}:"number"==typeof e?{type:t.PropertyType.decimal,value:e}:"boolean"==typeof e?{type:t.PropertyType.boolean,value:e}:"string"==typeof e?{type:t.PropertyType.string,value:e}:te(e)?{type:t.PropertyType.Quantity,value:e}:{type:t.PropertyType.BackboneElement,value:e}}function zt(t){return 0!==t.length&&!!t[0].value}function Xt(t){const e=[];for(const r of t){let t=!1;for(const n of e)if(zt(Vt(r,n))){t=!0;break}t||e.push(r)}return e}function Gt(t){return $t(!zt(t))}function Yt(t,e){return 0===t.length||0===e.length?[]:t.length!==e.length?$t(!1):$t(t.every(((t,r)=>zt(Vt(t,e[r])))))}function Vt(t,e){const r=t.value,n=e.value;return"number"==typeof r&&"number"==typeof n?$t(Math.abs(r-n)<1e-8):te(r)&&te(n)?$t(ee(r,n)):$t("object"==typeof r&&"object"==typeof n?re(t,e):r===n)}function Kt(t,e){return 0===t.length&&0===e.length?$t(!0):t.length!==e.length?$t(!1):(t.sort(Jt),e.sort(Jt),$t(t.every(((t,r)=>zt(function(t,e){const r=t.value,n=e.value;if("number"==typeof r&&"number"==typeof n)return $t(Math.abs(r-n)<.01);if(te(r)&&te(n))return $t(ee(r,n));if("object"==typeof r&&"object"==typeof n)return $t(re(r,n));if("string"==typeof r&&"string"==typeof n)return $t(r.toLowerCase()===n.toLowerCase());return $t(r===n)}(t,e[r]))))))}function Jt(t,e){const r=t.value,n=e.value;return"number"==typeof r&&"number"==typeof n?r-n:"string"==typeof r&&"string"==typeof n?r.localeCompare(n):0}function Zt(t,e){const{value:r}=t;if(null==r)return!1;switch(e){case"Boolean":return"boolean"==typeof r;case"Decimal":case"Integer":return"number"==typeof r;case"Date":return"string"==typeof r&&!!r.match(/^\d{4}(-\d{2}(-\d{2})?)?/);case"DateTime":return"string"==typeof r&&!!r.match(/^\d{4}(-\d{2}(-\d{2})?)?T/);case"Time":return"string"==typeof r&&!!r.match(/^T\d/);case"Period":return!(!(n=r)||"object"!=typeof n||!("start"in n));case"Quantity":return te(r);default:return"object"==typeof r&&(null==r?void 0:r.resourceType)===e}var n}function te(t){return!(!t||"object"!=typeof t||!("value"in t)||"number"!=typeof t.value)}function ee(t,e){return Math.abs(t.value-e.value)<.01&&(t.unit===e.unit||t.code===e.code||t.unit===e.code||t.code===e.unit)}function re(t,e){const r=Object.keys(t),n=Object.keys(e);if(r.length!==n.length)return!1;for(const n of r){const r=t[n],i=e[n];if(ne(r)&&ne(i)){if(!re(r,i))return!1}else if(r!==i)return!1}return!0}function ne(t){return null!==t&&"object"==typeof t}var ie,se;ot=new WeakMap,at=new WeakMap,lt=new WeakMap,ct=new WeakMap,ut=new WeakMap,ht=new WeakMap,ft=new WeakMap,dt=new WeakMap,pt=new WeakMap,vt=new WeakMap,mt=new WeakMap,gt=new WeakMap,yt=new WeakMap,wt=new WeakMap,Tt=new WeakMap,St=new WeakMap,st=new WeakSet,bt=function(t){const e=this.getLogins().filter((e=>{var r,n;return(null===(r=e.profile)||void 0===r?void 0:r.reference)!==(null===(n=t.profile)||void 0===n?void 0:n.reference)}));e.push(t),r(this,at,"f").setObject("logins",e)},Et=function(){return e(this,void 0,void 0,(function*(){return n(this,wt,new Promise(((t,e)=>{this.get("auth/me").then((e=>{n(this,wt,void 0,"f"),n(this,Tt,e.profile,"f"),n(this,St,e.config,"f"),this.dispatchEvent({type:"change"}),t(r(this,Tt,"f"))})).catch(e)})),"f"),r(this,wt,"f")}))},Pt=function(t,n,i={}){return e(this,void 0,void 0,(function*(){r(this,yt,"f")&&(yield r(this,yt,"f")),n.startsWith("http")||(n=r(this,ut,"f")+n),i.method=t,r(this,st,"m",At).call(this,i);const e=yield r(this,ot,"f").call(this,n,i);if(401===e.status)return r(this,st,"m",xt).call(this,t,n,i);if(204===e.status||304===e.status)return;const s=yield e.json();return"OperationOutcome"!==(null==s?void 0:s.resourceType)||F(s)?s:Promise.reject(s)}))},At=function(t){t.headers||(t.headers={});const e=t.headers;e["Content-Type"]||(e["Content-Type"]="application/fhir+json"),r(this,mt,"f")&&(e.Authorization="Bearer "+r(this,mt,"f")),t.cache||(t.cache="no-cache"),t.credentials||(t.credentials="include")},Ot=function(t,e){t.headers||(t.headers={});t.headers["Content-Type"]=e},kt=function(t,e){"string"==typeof e||"undefined"!=typeof Blob&&e instanceof Blob||"undefined"!=typeof Buffer&&e instanceof Buffer||"undefined"!=typeof File&&e instanceof File?t.body=e:e&&(t.body=JSON.stringify(e))},xt=function(t,n,i){return e(this,void 0,void 0,(function*(){return r(this,st,"m",Mt).call(this).then((()=>r(this,st,"m",Pt).call(this,t,n,i))).catch((t=>(this.clear(),r(this,vt,"f")&&r(this,vt,"f").call(this),Promise.reject(t))))}))},Rt=function(){return e(this,void 0,void 0,(function*(){const t=k();r(this,at,"f").setString("pkceState",t);const n=k();r(this,at,"f").setString("codeVerifier",n);const i=A(yield function(t){return e(this,void 0,void 0,(function*(){return crypto.subtle.digest("SHA-256",(new TextEncoder).encode(t))}))}(n)).replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"");r(this,at,"f").setString("codeChallenge",i)}))},Ut=function(){r(this,st,"m",Rt).call(this);const t=new URL(r(this,ft,"f"));t.searchParams.set("response_type","code"),t.searchParams.set("state",r(this,at,"f").getString("pkceState")),t.searchParams.set("client_id",r(this,ht,"f")),t.searchParams.set("redirect_uri",Qt()),t.searchParams.set("scope",Ft),t.searchParams.set("code_challenge_method","S256"),t.searchParams.set("code_challenge",r(this,at,"f").getString("codeChallenge")),window.location.assign(t.toString())},Mt=function(){return e(this,void 0,void 0,(function*(){if(r(this,yt,"f"))return r(this,yt,"f");if(!r(this,gt,"f"))throw this.clear(),new Error("Invalid refresh token");const t=new URLSearchParams;t.set("grant_type","refresh_token"),t.set("client_id",r(this,ht,"f")),t.set("refresh_token",r(this,gt,"f")),n(this,yt,r(this,st,"m",Ct).call(this,t),"f"),yield r(this,yt,"f")}))},Ct=function(t){return e(this,void 0,void 0,(function*(){return r(this,ot,"f").call(this,r(this,dt,"f"),{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:t}).then((t=>{if(!t.ok)throw new Error("Failed to fetch tokens");return t.json()})).then((t=>r(this,st,"m",Lt).call(this,t))).then((()=>this.getProfile()))}))},Lt=function(t){return e(this,void 0,void 0,(function*(){const e=t.access_token,n=function(t){const[e,r,n]=t.split(".");return R(r)}(e);if(Date.now()>=1e3*n.exp)throw this.clear(),new Error("Token expired");if(r(this,ht,"f")&&n.client_id!==r(this,ht,"f"))throw this.clear(),new Error("Token was not issued for this audience");yield this.setActiveLogin({accessToken:e,refreshToken:t.refresh_token,project:t.project,profile:t.profile})}))},Dt=function(){try{window.addEventListener("storage",(t=>{null!==t.key&&"activeLogin"!==t.key||window.location.reload()}))}catch(t){}};class oe{constructor(t,e){this.original=t,this.child=e}eval(t){try{return t.length>0?t.map((t=>this.child.eval([t]))).flat():this.child.eval(t)}catch(t){throw new Error(`FhirPathError on "${this.original}": ${t}`)}}}class ae{constructor(t){this.value=t}eval(){return[this.value]}}class le{constructor(t){this.name=t,ie.add(this)}eval(t){return"$this"===this.name?t:t.map((t=>r(this,ie,"m",se).call(this,t))).flat().filter((t=>void 0!==(null==t?void 0:t.value)))}}ie=new WeakSet,se=function(t){const e=t.value;if(!e||"object"!=typeof e)return;if("resourceType"in e&&e.resourceType===this.name)return t;let r;if(this.name in e)r=e[this.name];else{const t=Object.keys(e).find((t=>t.startsWith(this.name)));t&&(r=e[t])}return void 0!==r?Array.isArray(r)?r.map(qt):[qt(r)]:void 0};class ce{eval(){return[]}}class ue{constructor(t,e){this.child=t,this.impl=e}eval(t){return this.impl(this.child.eval(t))}}class he{constructor(t,e){this.left=t,this.right=e}eval(t){return this.left.eval(t)}}class fe{constructor(t,e,r){this.left=t,this.right=e,this.impl=r}eval(e){const r=this.left.eval(e);if(1!==r.length)return[];const n=this.right.eval(e);if(1!==n.length)return[];const i=r[0].value,s=n[0].value,o=te(i)?i.value:i,a=te(s)?s.value:s,l=this.impl(o,a);return"boolean"==typeof l?$t(l):te(i)?[{type:t.PropertyType.Quantity,value:Object.assign(Object.assign({},i),{value:l})}]:[qt(l)]}}class de{constructor(t,e){this.left=t,this.right=e}eval(e){const r=[...this.left.eval(e),...this.right.eval(e)];return r.length>0&&r.every((t=>"string"==typeof t.value))?[{type:t.PropertyType.string,value:r.map((t=>t.value)).join("")}]:r}}class pe{constructor(t,e){this.left=t,this.right=e}eval(t){const e=this.left.eval(t),r=this.right.eval(t);return $t(e.some((t=>t.value===r[0].value)))}}class ve{constructor(t,e){this.left=t,this.right=e}eval(t){const e=this.left.eval(t);return $t(this.right.eval(t).some((t=>t.value===e[0].value)))}}class me{constructor(t,e){this.left=t,this.right=e}eval(t){return this.right.eval(this.left.eval(t))}}class ge{constructor(t,e){this.left=t,this.right=e}eval(t){return Xt([...this.left.eval(t),...this.right.eval(t)])}}class ye{constructor(t,e){this.left=t,this.right=e}eval(t){return Yt(this.left.eval(t),this.right.eval(t))}}class we{constructor(t,e){this.left=t,this.right=e}eval(t){return Gt(Yt(this.left.eval(t),this.right.eval(t)))}}class Te{constructor(t,e){this.left=t,this.right=e}eval(t){return Kt(this.left.eval(t),this.right.eval(t))}}class Se{constructor(t,e){this.left=t,this.right=e}eval(t){return Gt(Kt(this.left.eval(t),this.right.eval(t)))}}class be{constructor(t,e){this.left=t,this.right=e}eval(t){const e=this.left.eval(t);if(1!==e.length)return[];const r=this.right.name;return $t(Zt(e[0],r))}}class Ee{constructor(t,e){this.left=t,this.right=e}eval(t){var e,r,n,i;const s=this.left.eval(t),o=this.right.eval(t);return!0===(null===(e=s[0])||void 0===e?void 0:e.value)&&!0===(null===(r=o[0])||void 0===r?void 0:r.value)?$t(!0):!1===(null===(n=s[0])||void 0===n?void 0:n.value)||!1===(null===(i=o[0])||void 0===i?void 0:i.value)?$t(!1):[]}}class Pe{constructor(t,e){this.left=t,this.right=e}eval(t){const e=this.left.eval(t);if(zt(e))return e;const r=this.right.eval(t);return zt(r)?r:[]}}class Ae{constructor(t,e){this.left=t,this.right=e}eval(t){const e=this.left.eval(t),r=this.right.eval(t);if(0===e.length&&0===r.length)return[];const n=0===e.length?null:e[0].value,i=0===r.length?null:r[0].value;return!0===n&&!0!==i||!0!==n&&!0===i?$t(!0):!0===n&&!0===i||!1===n&&!1===i?$t(!1):[]}}class Oe{constructor(t,e,r){this.name=t,this.args=e,this.impl=r}eval(t){return this.impl(t,...this.args)}}class ke{constructor(t,e){this.left=t,this.expr=e}eval(t){const e=this.expr.eval(t);if(1!==e.length)return[];const r=e[0].value;if("number"!=typeof r)throw new Error("Invalid indexer expression: should return integer}");const n=this.left.eval(t);return r in n?[n[r]]:[]}}function xe(t){if(t.startsWith("T"))return t+"T00:00:00.000Z".substring(t.length);if(t.length<=10)return t;try{return new Date(t).toISOString()}catch(e){return t}}const Re=()=>[],Ue={empty:t=>$t(0===t.length),exists:(t,e)=>$t(e?t.filter((t=>zt(e.eval([t])))).length>0:t.length>0),all:(t,e)=>$t(t.every((t=>zt(e.eval([t]))))),allTrue:t=>{for(const e of t)if(!e.value)return $t(!1);return $t(!0)},anyTrue:t=>{for(const e of t)if(e.value)return $t(!0);return $t(!1)},allFalse:t=>{for(const e of t)if(e.value)return $t(!1);return $t(!0)},anyFalse:t=>{for(const e of t)if(!e.value)return $t(!0);return $t(!1)},subsetOf:Re,supersetOf:Re,count:e=>[{type:t.PropertyType.integer,value:e.length}],distinct:t=>{const e=[];for(const r of t)e.some((t=>t.value===r.value))||e.push(r);return e},isDistinct:t=>$t(t.length===Ue.distinct(t).length),where:(t,e)=>t.filter((t=>zt(e.eval([t])))),select:(t,e)=>t.map((t=>e.eval([t]))).flat(),repeat:Re,ofType:Re,single:t=>{if(t.length>1)throw new Error("Expected input length one for single()");return 0===t.length?[]:t.slice(0,1)},first:t=>0===t.length?[]:t.slice(0,1),last:t=>0===t.length?[]:t.slice(t.length-1,t.length),tail:t=>0===t.length?[]:t.slice(1,t.length),skip:(t,e)=>{var r;const n=null===(r=e.eval([])[0])||void 0===r?void 0:r.value;if("number"!=typeof n)throw new Error("Expected a number for skip(num)");return n>=t.length?[]:n<=0?t:t.slice(n,t.length)},take:(t,e)=>{var r;const n=null===(r=e.eval([])[0])||void 0===r?void 0:r.value;if("number"!=typeof n)throw new Error("Expected a number for take(num)");return n>=t.length?t:n<=0?[]:t.slice(0,n)},intersect:(t,e)=>{if(!e)return t;const r=e.eval([]),n=[];for(const e of t)!n.some((t=>t.value===e.value))&&r.some((t=>t.value===e.value))&&n.push(e);return n},exclude:(t,e)=>{if(!e)return t;const r=e.eval([]),n=[];for(const e of t)r.some((t=>t.value===e.value))||n.push(e);return n},union:(t,e)=>{if(!e)return t;const r=e.eval([]);return Xt([...t,...r])},combine:(t,e)=>{if(!e)return t;const r=e.eval([]);return[...t,...r]},iif:(t,e,r,n)=>{const i=e.eval(t);if(i.length>1||1===i.length&&"boolean"!=typeof i[0].value)throw new Error("Expected criterion to evaluate to a Boolean");return zt(i)?r.eval(t):n?n.eval(t):[]},toBoolean:e=>{if(0===e.length)return[];const[{value:r}]=Le(e,1);if("boolean"==typeof r)return[{type:t.PropertyType.boolean,value:r}];if("number"==typeof r&&(0===r||1===r))return $t(!!r);if("string"==typeof r){const t=r.toLowerCase();if(["true","t","yes","y","1","1.0"].includes(t))return $t(!0);if(["false","f","no","n","0","0.0"].includes(t))return $t(!1)}return[]},convertsToBoolean:t=>0===t.length?[]:$t(1===Ue.toBoolean(t).length),toInteger:e=>{if(0===e.length)return[];const[{value:r}]=Le(e,1);return"number"==typeof r?[{type:t.PropertyType.integer,value:r}]:"string"==typeof r&&r.match(/^[+-]?\d+$/)?[{type:t.PropertyType.integer,value:parseInt(r,10)}]:"boolean"==typeof r?[{type:t.PropertyType.integer,value:r?1:0}]:[]},convertsToInteger:t=>0===t.length?[]:$t(1===Ue.toInteger(t).length),toDate:e=>{if(0===e.length)return[];const[{value:r}]=Le(e,1);return"string"==typeof r&&r.match(/^\d{4}(-\d{2}(-\d{2})?)?/)?[{type:t.PropertyType.date,value:xe(r)}]:[]},convertsToDate:t=>0===t.length?[]:$t(1===Ue.toDate(t).length),toDateTime:e=>{if(0===e.length)return[];const[{value:r}]=Le(e,1);return"string"==typeof r&&r.match(/^\d{4}(-\d{2}(-\d{2})?)?/)?[{type:t.PropertyType.dateTime,value:xe(r)}]:[]},convertsToDateTime:t=>0===t.length?[]:$t(1===Ue.toDateTime(t).length),toDecimal:e=>{if(0===e.length)return[];const[{value:r}]=Le(e,1);return"number"==typeof r?[{type:t.PropertyType.decimal,value:r}]:"string"==typeof r&&r.match(/^-?\d{1,9}(\.\d{1,9})?$/)?[{type:t.PropertyType.decimal,value:parseFloat(r)}]:"boolean"==typeof r?[{type:t.PropertyType.decimal,value:r?1:0}]:[]},convertsToDecimal:t=>0===t.length?[]:$t(1===Ue.toDecimal(t).length),toQuantity:e=>{if(0===e.length)return[];const[{value:r}]=Le(e,1);return te(r)?[{type:t.PropertyType.Quantity,value:r}]:"number"==typeof r?[{type:t.PropertyType.Quantity,value:{value:r,unit:"1"}}]:"string"==typeof r&&r.match(/^-?\d{1,9}(\.\d{1,9})?/)?[{type:t.PropertyType.Quantity,value:{value:parseFloat(r),unit:"1"}}]:"boolean"==typeof r?[{type:t.PropertyType.Quantity,value:{value:r?1:0,unit:"1"}}]:[]},convertsToQuantity:t=>0===t.length?[]:$t(1===Ue.toQuantity(t).length),toString:e=>{if(0===e.length)return[];const[{value:r}]=Le(e,1);return null==r?[]:te(r)?[{type:t.PropertyType.string,value:`${r.value} '${r.unit}'`}]:[{type:t.PropertyType.string,value:r.toString()}]},convertsToString:t=>0===t.length?[]:$t(1===Ue.toString(t).length),toTime:e=>{if(0===e.length)return[];const[{value:r}]=Le(e,1);if("string"==typeof r){const e=r.match(/^T?(\d{2}(:\d{2}(:\d{2})?)?)/);if(e)return[{type:t.PropertyType.time,value:xe("T"+e[1])}]}return[]},convertsToTime:t=>0===t.length?[]:$t(1===Ue.toTime(t).length),indexOf:(t,e)=>Me(((t,e)=>t.indexOf(e)),t,e),substring:(t,e,r)=>Me(((t,e,r)=>{const n=e,i=r?n+r:t.length;return n<0||n>=t.length?void 0:t.substring(n,i)}),t,e,r),startsWith:(t,e)=>Me(((t,e)=>t.startsWith(e)),t,e),endsWith:(t,e)=>Me(((t,e)=>t.endsWith(e)),t,e),contains:(t,e)=>Me(((t,e)=>t.includes(e)),t,e),upper:t=>Me((t=>t.toUpperCase()),t),lower:t=>Me((t=>t.toLowerCase()),t),replace:(t,e,r)=>Me(((t,e,r)=>t.replaceAll(e,r)),t,e,r),matches:(t,e)=>Me(((t,e)=>!!t.match(e)),t,e),replaceMatches:(t,e,r)=>Me(((t,e,r)=>t.replaceAll(e,r)),t,e,r),length:t=>Me((t=>t.length),t),toChars:t=>Me((t=>t?t.split(""):void 0),t),abs:t=>Ce(Math.abs,t),ceiling:t=>Ce(Math.ceil,t),exp:t=>Ce(Math.exp,t),floor:t=>Ce(Math.floor,t),ln:t=>Ce(Math.log,t),log:(t,e)=>Ce(((t,e)=>Math.log(t)/Math.log(e)),t,e),power:(t,e)=>Ce(Math.pow,t,e),round:t=>Ce(Math.round,t),sqrt:t=>Ce(Math.sqrt,t),truncate:t=>Ce((t=>0|t),t),children:Re,descendants:Re,trace:(t,e)=>(console.log("trace",t,e),t),now:()=>[{type:t.PropertyType.dateTime,value:(new Date).toISOString()}],timeOfDay:()=>[{type:t.PropertyType.time,value:(new Date).toISOString().substring(11)}],today:()=>[{type:t.PropertyType.date,value:(new Date).toISOString().substring(0,10)}],between:(e,r,n,i)=>{const s=Ue.toDateTime(r.eval(e));if(0===s.length)throw new Error("Invalid start date");const o=Ue.toDateTime(n.eval(e));if(0===o.length)throw new Error("Invalid end date");const a=i.eval(e)[0].value;if("years"!==a&&"months"!==a&&"days"!==a)throw new Error("Invalid units");const l=v(s[0].value,o[0].value);return[{type:t.PropertyType.Quantity,value:{value:l[a],unit:a}}]},is:(e,r)=>{let n="";return r instanceof le?n=r.name:r instanceof me&&(n=r.left.name+"."+r.right.name),n?e.map((e=>({type:t.PropertyType.boolean,value:Zt(e,n)}))):[]},not:e=>Ue.toBoolean(e).map((e=>({type:t.PropertyType.boolean,value:!e.value}))),resolve:e=>e.map((e=>{const r=e.value;let n;if("string"==typeof r)n=r;else if("object"==typeof r){const e=r;if(e.resource)return{type:t.PropertyType.BackboneElement,value:e.resource};n=e.reference}if(!n)return{type:t.PropertyType.BackboneElement,value:null};const[i,s]=n.split("/");return{type:t.PropertyType.BackboneElement,value:{resourceType:i,id:s}}})).filter((t=>!!t.value)),as:t=>t,type:e=>e.map((({value:e})=>"boolean"==typeof e?{type:t.PropertyType.BackboneElement,value:{namespace:"System",name:"Boolean"}}:"number"==typeof e?{type:t.PropertyType.BackboneElement,value:{namespace:"System",name:"Integer"}}:e&&"object"==typeof e&&"resourceType"in e?{type:t.PropertyType.BackboneElement,value:{namespace:"FHIR",name:e.resourceType}}:{type:t.PropertyType.BackboneElement,value:null})),conformsTo:(e,r)=>{const n=r.eval([])[0].value;if(!n.startsWith("http://hl7.org/fhir/StructureDefinition/"))throw new Error("Expected a StructureDefinition URL");const i=n.replace("http://hl7.org/fhir/StructureDefinition/","");return e.map((e=>{var r;return{type:t.PropertyType.boolean,value:(null===(r=e.value)||void 0===r?void 0:r.resourceType)===i}}))}};function Me(t,e,...r){if(0===e.length)return[];const[{value:n}]=Le(e,1);if("string"!=typeof n)throw new Error("String function cannot be called with non-string");const i=t(n,...r.map((t=>{var r,n;return t&&(null===(n=null===(r=t.eval(e))||void 0===r?void 0:r[0])||void 0===n?void 0:n.value)})));return void 0===i?[]:Array.isArray(i)?i.map(qt):[qt(i)]}function Ce(e,r,...n){if(0===r.length)return[];const[{value:i}]=Le(r,1),s=te(i),o=s?i.value:i;if("number"!=typeof o)throw new Error("Math function cannot be called with non-number");const a=e(o,...n.map((t=>{var e,r;return null===(r=null===(e=t.eval([]))||void 0===e?void 0:e[0])||void 0===r?void 0:r.value})));return[{type:s?t.PropertyType.Quantity:r[0].type,value:s?Object.assign(Object.assign({},i),{value:a}):a}]}function Le(t,e){if(t.length!==e)throw new Error(`Expected ${e} arguments`);for(const e of t)if(null==e)throw new Error("Expected non-null argument");return t}var De,Ie,_e,je,Ne,We,Be,He,Fe,Qe,$e,qe,ze,Xe,Ge,Ye,Ve,Ke;function Je(t){return new er(t).tokenize()}const Ze=["year","years","month","months","week","weeks","day","days","hour","hours","minute","minutes","second","seconds","millisecond","milliseconds"],tr=["!=","!~","<=",">=","{}"];class er{constructor(t){De.add(this),Ie.set(this,void 0),_e.set(this,void 0),n(this,Ie,t,"f"),n(this,_e,0,"f")}tokenize(){const t=[];for(;r(this,_e,"f")<r(this,Ie,"f").length;){const e=r(this,De,"m",Ne).call(this);e&&t.push(e)}return t}}function rr(t,e){return{id:t,value:e}}var nr,ir,sr,or,ar,lr,cr,ur,hr;Ie=new WeakMap,_e=new WeakMap,De=new WeakSet,je=function(){const t=r(this,_e,"f"),e=r(this,De,"m",Ne).call(this);return n(this,_e,t,"f"),e},Ne=function(){r(this,De,"m",We).call(this);const t=r(this,De,"m",Ye).call(this);if(!t)return;const e=r(this,De,"m",Ke).call(this);return"/"===t&&"*"===e?r(this,De,"m",Be).call(this):"/"===t&&"/"===e?r(this,De,"m",He).call(this):"'"===t?r(this,De,"m",Fe).call(this):"`"===t?r(this,De,"m",Qe).call(this):"@"===t?r(this,De,"m",$e).call(this):t.match(/\d/)?r(this,De,"m",qe).call(this):t.match(/\w/)||"$"===t&&e.match(/\w/)?r(this,De,"m",ze).call(this):r(this,De,"m",Xe).call(this)},We=function(){return rr("Whitespace",r(this,De,"m",Ge).call(this,(()=>r(this,De,"m",Ye).call(this).match(/\s/))))},Be=function(){const t=r(this,_e,"f");return r(this,De,"m",Ge).call(this,(()=>"*"!==r(this,De,"m",Ye).call(this)||"/"!==r(this,De,"m",Ke).call(this))),n(this,_e,r(this,_e,"f")+2,"f"),rr("Comment",r(this,Ie,"f").substring(t,r(this,_e,"f")))},He=function(){return rr("Comment",r(this,De,"m",Ge).call(this,(()=>"\n"!==r(this,De,"m",Ye).call(this))))},Fe=function(){var t,e;n(this,_e,(t=r(this,_e,"f"),++t),"f");const i=rr("String",r(this,De,"m",Ge).call(this,(()=>"\\"===r(this,De,"m",Ve).call(this)||"'"!==r(this,De,"m",Ye).call(this))));return n(this,_e,(e=r(this,_e,"f"),++e),"f"),i},Qe=function(){var t,e;n(this,_e,(t=r(this,_e,"f"),++t),"f");const i=rr("Symbol",r(this,De,"m",Ge).call(this,(()=>"`"!==r(this,De,"m",Ye).call(this))));return n(this,_e,(e=r(this,_e,"f"),++e),"f"),i},$e=function(){var t,e,i,s,o;const a=r(this,_e,"f");return n(this,_e,(t=r(this,_e,"f"),++t),"f"),r(this,De,"m",Ge).call(this,(()=>r(this,De,"m",Ye).call(this).match(/[\d-]/))),"T"===r(this,De,"m",Ye).call(this)&&(n(this,_e,(e=r(this,_e,"f"),++e),"f"),r(this,De,"m",Ge).call(this,(()=>r(this,De,"m",Ye).call(this).match(/[\d:]/))),"."===r(this,De,"m",Ye).call(this)&&r(this,De,"m",Ke).call(this).match(/\d/)&&(n(this,_e,(i=r(this,_e,"f"),++i),"f"),r(this,De,"m",Ge).call(this,(()=>r(this,De,"m",Ye).call(this).match(/[\d]/)))),"Z"===r(this,De,"m",Ye).call(this)?n(this,_e,(s=r(this,_e,"f"),++s),"f"):"+"!==r(this,De,"m",Ye).call(this)&&"-"!==r(this,De,"m",Ye).call(this)||(n(this,_e,(o=r(this,_e,"f"),++o),"f"),r(this,De,"m",Ge).call(this,(()=>r(this,De,"m",Ye).call(this).match(/[\d:]/))))),rr("DateTime",r(this,Ie,"f").substring(a+1,r(this,_e,"f")))},qe=function(){var t;const e=r(this,_e,"f");let i="Number";return r(this,De,"m",Ge).call(this,(()=>r(this,De,"m",Ye).call(this).match(/\d/))),"."===r(this,De,"m",Ye).call(this)&&r(this,De,"m",Ke).call(this).match(/\d/)&&(n(this,_e,(t=r(this,_e,"f"),++t),"f"),r(this,De,"m",Ge).call(this,(()=>r(this,De,"m",Ye).call(this).match(/\d/))))," "===r(this,De,"m",Ye).call(this)&&function(t){if(t){if("String"===t.id)return!0;if("Symbol"===t.id&&Ze.includes(t.value))return!0}return!1}(r(this,De,"m",je).call(this))&&(i="Quantity",r(this,De,"m",Ne).call(this)),rr(i,r(this,Ie,"f").substring(e,r(this,_e,"f")))},ze=function(){return rr("Symbol",r(this,De,"m",Ge).call(this,(()=>r(this,De,"m",Ye).call(this).match(/[$\w]/))))},Xe=function(){var t;const e=r(this,De,"m",Ye).call(this),i=e+r(this,De,"m",Ke).call(this);return tr.includes(i)?(n(this,_e,r(this,_e,"f")+2,"f"),rr(i,i)):(n(this,_e,(t=r(this,_e,"f"),++t),"f"),rr(e,e))},Ge=function(t){var e;const i=r(this,_e,"f");for(;r(this,_e,"f")<r(this,Ie,"f").length&&t();)n(this,_e,(e=r(this,_e,"f"),++e),"f");return r(this,Ie,"f").substring(i,r(this,_e,"f"))},Ye=function(){return r(this,Ie,"f")[r(this,_e,"f")]},Ve=function(){var t;return null!==(t=r(this,Ie,"f")[r(this,_e,"f")-1])&&void 0!==t?t:""},Ke=function(){var t;return null!==(t=r(this,Ie,"f")[r(this,_e,"f")+1])&&void 0!==t?t:""};nr=new WeakMap,ir=new WeakMap;class fr{constructor(t,e,r){sr.add(this),or.set(this,void 0),ar.set(this,void 0),lr.set(this,void 0),n(this,or,t,"f"),n(this,ar,e,"f"),n(this,lr,r,"f")}match(t){const e=r(this,sr,"m",hr).call(this);return(null==e?void 0:e.id)===t&&(r(this,sr,"m",ur).call(this),!0)}consumeAndParse(t=100){const e=r(this,sr,"m",ur).call(this),n=r(this,ar,"f")[e.id];if(!n)throw Error(`Parse error at ${e.value}. No matching prefix parselet.`);let i=n.parse(this,e);for(;t>r(this,sr,"m",cr).call(this);){const t=r(this,sr,"m",ur).call(this);i=r(this,lr,"f")[t.id].parse(this,i,t)}return i}}or=new WeakMap,ar=new WeakMap,lr=new WeakMap,sr=new WeakSet,cr=function(){const t=r(this,sr,"m",hr).call(this);if(!t)return 100;const e=r(this,lr,"f")[t.id];return e?e.precedence:100},ur=function(){if(!r(this,or,"f").length)throw Error("Cant consume unknown more tokens.");return r(this,or,"f").shift()},hr=function(){return r(this,or,"f").length>0?r(this,or,"f")[0]:void 0};const dr={parse(t){const e=t.consumeAndParse();if(!t.match(")"))throw new Error("Parse error: expected `)`");return e}},pr={parse(t,e){const r=t.consumeAndParse();if(!t.match("]"))throw new Error("Parse error: expected `]`");return new ke(e,r)},precedence:2},vr={parse(t,e){if(!(e instanceof le))throw new Error("Unexpected parentheses");if(!(e.name in Ue))throw new Error("Unrecognized function: "+e.name);const r=[];for(;!t.match(")");)r.push(t.consumeAndParse()),t.match(",");return new Oe(e.name,r,Ue[e.name])},precedence:0};function mr(t){const e=t.split(" "),r=parseFloat(e[0]);let n=e[1];return n=n&&n.startsWith("'")&&n.endsWith("'")?n.substring(1,n.length-1):"{"+n+"}",{value:r,unit:n}}const gr=(new class{constructor(){nr.set(this,{}),ir.set(this,{})}registerInfix(t,e){return r(this,ir,"f")[t]=e,this}registerPrefix(t,e){return r(this,nr,"f")[t]=e,this}prefix(t,e,r){return this.registerPrefix(t,{parse(t,n){const i=t.consumeAndParse(e);return r(n,i)}})}infixLeft(t,e,r){return this.registerInfix(t,{parse(t,n,i){const s=t.consumeAndParse(e);return r(n,i,s)},precedence:e})}construct(t){return new fr(Je(t),r(this,nr,"f"),r(this,ir,"f"))}}).registerPrefix("String",{parse:(e,r)=>new ae({type:t.PropertyType.string,value:r.value})}).registerPrefix("DateTime",{parse:(e,r)=>new ae({type:t.PropertyType.dateTime,value:xe(r.value)})}).registerPrefix("Quantity",{parse:(e,r)=>new ae({type:t.PropertyType.Quantity,value:mr(r.value)})}).registerPrefix("Number",{parse:(e,r)=>new ae({type:t.PropertyType.decimal,value:parseFloat(r.value)})}).registerPrefix("Symbol",{parse:(e,r)=>"false"===r.value?new ae({type:t.PropertyType.boolean,value:!1}):"true"===r.value?new ae({type:t.PropertyType.boolean,value:!0}):new le(r.value)}).registerPrefix("{}",{parse:()=>new ce}).registerPrefix("(",dr).registerInfix("[",pr).registerInfix("(",vr).prefix("+",3,((t,e)=>new ue(e,(t=>t)))).prefix("-",3,((t,e)=>new fe(e,e,((t,e)=>-e)))).infixLeft(".",1,((t,e,r)=>new me(t,r))).infixLeft("/",4,((t,e,r)=>new fe(t,r,((t,e)=>t/e)))).infixLeft("*",4,((t,e,r)=>new fe(t,r,((t,e)=>t*e)))).infixLeft("+",5,((t,e,r)=>new fe(t,r,((t,e)=>t+e)))).infixLeft("-",5,((t,e,r)=>new fe(t,r,((t,e)=>t-e)))).infixLeft("|",7,((t,e,r)=>new ge(t,r))).infixLeft("=",9,((t,e,r)=>new ye(t,r))).infixLeft("!=",9,((t,e,r)=>new we(t,r))).infixLeft("~",9,((t,e,r)=>new Te(t,r))).infixLeft("!~",9,((t,e,r)=>new Se(t,r))).infixLeft("<",8,((t,e,r)=>new fe(t,r,((t,e)=>t<e)))).infixLeft("<=",8,((t,e,r)=>new fe(t,r,((t,e)=>t<=e)))).infixLeft(">",8,((t,e,r)=>new fe(t,r,((t,e)=>t>e)))).infixLeft(">=",8,((t,e,r)=>new fe(t,r,((t,e)=>t>=e)))).infixLeft("&",5,((t,e,r)=>new de(t,r))).infixLeft("Symbol",6,((t,e,r)=>{switch(e.value){case"and":return new Ee(t,r);case"as":return new he(t,r);case"contains":return new pe(t,r);case"div":return new fe(t,r,((t,e)=>t/e|0));case"in":return new ve(t,r);case"is":return new be(t,r);case"mod":return new fe(t,r,((t,e)=>t%e));case"or":return new Pe(t,r);case"xor":return new Ae(t,r);default:throw new Error("Cannot use "+e.value+" as infix operator")}}));function yr(t){try{return new oe(t,gr.construct(t).consumeAndParse())}catch(e){throw new Error(`FhirPathError on "${t}": ${e}`)}}class wr{constructor(t){this.segments=t}get(t){return"number"==typeof t?this.segments[t]:this.segments.find((e=>e.name===t))}getAll(t){return this.segments.filter((e=>e.name===t))}toString(){return this.segments.map((t=>t.toString())).join("\r")}buildAck(){var t,e,r,n,i,s;const o=new Date,a=this.get("MSH"),l=(null===(t=null==a?void 0:a.get(2))||void 0===t?void 0:t.toString())||"",c=(null===(e=null==a?void 0:a.get(3))||void 0===e?void 0:e.toString())||"",u=(null===(r=null==a?void 0:a.get(4))||void 0===r?void 0:r.toString())||"",h=(null===(n=null==a?void 0:a.get(5))||void 0===n?void 0:n.toString())||"",f=(null===(i=null==a?void 0:a.get(9))||void 0===i?void 0:i.toString())||"",d=(null===(s=null==a?void 0:a.get(12))||void 0===s?void 0:s.toString())||"2.5.1";return new wr([new Tr(["MSH","^~\\&",u,h,l,c,o.toISOString(),"","ACK",o.getTime().toString(),"P",d]),new Tr(["MSA","AA",f,"OK"])])}static parse(t){if(!t.startsWith("MSH|^~\\&")){const t=new Error("Invalid HL7 message");throw t.type="entity.parse.failed",t}return new wr(t.split(/[\r\n]+/).map((t=>Tr.parse(t))))}}class Tr{constructor(t){b(t)?this.fields=t.map((t=>Sr.parse(t))):this.fields=t,this.name=this.fields[0].components[0]}get(t){return this.fields[t]}toString(){return this.fields.map((t=>t.toString())).join("|")}static parse(t){return new Tr(t.split("|").map((t=>Sr.parse(t))))}}class Sr{constructor(t){this.components=t}get(t){return this.components[t]}toString(){return this.components.join("^")}static parse(t){return new Sr(t.split("^"))}}var br;function Er(t,e){const r=e.split(" | ");for(const e of r){const r=Pr(e);if(r.startsWith(t+"."))return r}}function Pr(t){let e=t.trim();return e.startsWith("(")&&e.endsWith(")")&&(e=e.substring(1,e.length-1)),e.includes(" as ")&&(e=e.substring(0,e.indexOf(" as "))),e.includes(".where(")&&(e=e.substring(0,e.indexOf(".where("))),e}t.SearchParameterType=void 0,(br=t.SearchParameterType||(t.SearchParameterType={})).BOOLEAN="BOOLEAN",br.NUMBER="NUMBER",br.QUANTITY="QUANTITY",br.TEXT="TEXT",br.REFERENCE="REFERENCE",br.DATE="DATE",br.DATETIME="DATETIME",br.PERIOD="PERIOD",t.COMPONENT_SEPARATOR="^",t.DEFAULT_SEARCH_COUNT=20,t.FIELD_SEPARATOR="|",t.Hl7Field=Sr,t.Hl7Message=wr,t.Hl7Segment=Tr,t.LRUCache=l,t.MedplumClient=class extends class{constructor(){x.set(this,void 0),n(this,x,{},"f")}addEventListener(t,e){r(this,x,"f")[t]||(r(this,x,"f")[t]=[]),r(this,x,"f")[t].push(e)}removeEventListeneer(t,e){const n=r(this,x,"f")[t];if(n)for(let t=0;t<n.length;t++)if(n[t]===e)return void n.splice(t,1)}dispatchEvent(t){const e=r(this,x,"f")[t.type];return e&&e.forEach((e=>e.call(this,t))),!t.defaultPrevented}}{constructor(t){var e;if(super(),st.add(this),ot.set(this,void 0),at.set(this,void 0),lt.set(this,void 0),ct.set(this,void 0),ut.set(this,void 0),ht.set(this,void 0),ft.set(this,void 0),dt.set(this,void 0),pt.set(this,void 0),vt.set(this,void 0),mt.set(this,void 0),gt.set(this,void 0),yt.set(this,void 0),wt.set(this,void 0),Tt.set(this,void 0),St.set(this,void 0),null==t?void 0:t.baseUrl){if(!t.baseUrl.startsWith("http"))throw new Error("Base URL must start with http or https");if(!t.baseUrl.endsWith("/"))throw new Error("Base URL must end with a trailing slash")}n(this,ot,(null==t?void 0:t.fetch)||window.fetch.bind(window),"f"),n(this,at,new It,"f"),n(this,lt,{types:{}},"f"),n(this,ct,new l(null!==(e=null==t?void 0:t.resourceCacheSize)&&void 0!==e?e:1e3),"f"),n(this,ut,(null==t?void 0:t.baseUrl)||"https://api.medplum.com/","f"),n(this,ht,(null==t?void 0:t.clientId)||"","f"),n(this,ft,(null==t?void 0:t.authorizeUrl)||r(this,ut,"f")+"oauth2/authorize","f"),n(this,dt,(null==t?void 0:t.tokenUrl)||r(this,ut,"f")+"oauth2/token","f"),n(this,pt,(null==t?void 0:t.logoutUrl)||r(this,ut,"f")+"oauth2/logout","f"),n(this,vt,null==t?void 0:t.onUnauthenticated,"f");const i=this.getActiveLogin();i&&(n(this,mt,i.accessToken,"f"),n(this,gt,i.refreshToken,"f"),r(this,st,"m",Et).call(this).catch(console.log)),r(this,st,"m",Dt).call(this)}getBaseUrl(){return r(this,ut,"f")}clear(){r(this,at,"f").clear(),r(this,ct,"f").clear(),n(this,mt,void 0,"f"),n(this,gt,void 0,"f"),n(this,Tt,void 0,"f"),n(this,St,void 0,"f"),this.dispatchEvent({type:"change"})}invalidateUrl(t){t=t.toString(),r(this,ct,"f").delete(t)}invalidateSearches(t){const e="fhir/R4/"+t;for(const t of r(this,ct,"f").keys())(t.endsWith(e)||t.includes(e+"?"))&&r(this,ct,"f").delete(t)}get(t,e={}){if(t=t.toString(),!(null==e?void 0:e.cache)){const e=r(this,ct,"f").get(t);if(e)return e}const n=new V(r(this,st,"m",Pt).call(this,"GET",t,e));return r(this,ct,"f").set(t,n),n}post(t,e,n,i={}){return t=t.toString(),e&&r(this,st,"m",kt).call(this,i,e),n&&r(this,st,"m",Ot).call(this,i,n),this.invalidateUrl(t),r(this,st,"m",Pt).call(this,"POST",t,i)}put(t,e,n,i={}){return t=t.toString(),e&&r(this,st,"m",kt).call(this,i,e),n&&r(this,st,"m",Ot).call(this,i,n),this.invalidateUrl(t),r(this,st,"m",Pt).call(this,"PUT",t,i)}patch(t,e,n={}){return t=t.toString(),r(this,st,"m",kt).call(this,n,e),r(this,st,"m",Ot).call(this,n,"application/json-patch+json"),this.invalidateUrl(t),r(this,st,"m",Pt).call(this,"PATCH",t,n)}delete(t,e={}){return t=t.toString(),this.invalidateUrl(t),r(this,st,"m",Pt).call(this,"DELETE",t,e)}register(t){return e(this,void 0,void 0,(function*(){const e=yield this.post("auth/register",t);yield this.setActiveLogin(e)}))}startLogin(t){return e(this,void 0,void 0,(function*(){return yield r(this,st,"m",Rt).call(this),this.post("auth/login",Object.assign(Object.assign({},t),{clientId:r(this,ht,"f"),scope:Ft,codeChallengeMethod:"S256",codeChallenge:r(this,at,"f").getString("codeChallenge")}))}))}startGoogleLogin(t){return e(this,void 0,void 0,(function*(){return yield r(this,st,"m",Rt).call(this),this.post("auth/google",t)}))}signOut(){return this.clear(),Promise.resolve()}signInWithRedirect(){const t=new URLSearchParams(window.location.search).get("code");return t?this.processCode(t):void r(this,st,"m",Ut).call(this)}signOutWithRedirect(){window.location.assign(r(this,pt,"f"))}fhirUrl(...t){return new URL(r(this,ut,"f")+"fhir/R4/"+t.join("/"))}fhirSearchUrl(t){if("string"==typeof t)return this.fhirUrl(t);const e=this.fhirUrl(t.resourceType);return e.search=et(t),e}search(t,e={}){return this.get(this.fhirSearchUrl(t),e)}searchOne(t,e={}){const n="string"==typeof t?tt(t):t;n.count=1;const i=this.fhirSearchUrl(t).toString()+"-searchOne";if(!(null==e?void 0:e.cache)){const t=r(this,ct,"f").get(i);if(t)return t}const s=new V(this.search(n,e).then((t=>{var e,r;return null===(r=null===(e=t.entry)||void 0===e?void 0:e[0])||void 0===r?void 0:r.resource})));return r(this,ct,"f").set(i,s),s}searchResources(t,e={}){const n=this.fhirSearchUrl(t).toString()+"-searchResources";if(!(null==e?void 0:e.cache)){const t=r(this,ct,"f").get(n);if(t)return t}const i=new V(this.search(t,e).then((t=>{var e,r;return null!==(r=null===(e=t.entry)||void 0===e?void 0:e.map((t=>t.resource)))&&void 0!==r?r:[]})));return r(this,ct,"f").set(n,i),i}searchValueSet(t,e,r={}){const n=this.fhirUrl("ValueSet","$expand");return n.searchParams.set("url",t),n.searchParams.set("filter",e),this.get(n.toString(),r)}getCached(t,e){const n=r(this,ct,"f").get(this.fhirUrl(t,e).toString());return n&&!n.isPending()?n.read():void 0}getCachedReference(t){const e=t.reference,[r,n]=e.split("/");return this.getCached(r,n)}readResource(t,e){return this.get(this.fhirUrl(t,e))}readReference(t){const e=null==t?void 0:t.reference;if(!e)return new V(Promise.reject(new Error("Missing reference")));const[r,n]=e.split("/");return this.readResource(r,n)}getSchema(){return r(this,lt,"f")}requestSchema(t){return e(this,void 0,void 0,(function*(){if(t in r(this,lt,"f").types)return Promise.resolve(r(this,lt,"f"));const e=`{\n StructureDefinitionList(name: "${t}") {\n name,\n description,\n snapshot {\n element {\n id,\n path,\n min,\n max,\n type {\n code,\n targetProfile\n },\n binding {\n valueSet\n },\n definition\n }\n }\n }\n SearchParameterList(base: "${t}", _count: 100) {\n base,\n code,\n type,\n expression,\n target\n }\n }`.replace(/\s+/g," "),n=yield this.graphql(e);for(const t of n.data.StructureDefinitionList)Wt(r(this,lt,"f"),t);for(const t of n.data.SearchParameterList)Bt(r(this,lt,"f"),t);return r(this,lt,"f")}))}readHistory(t,e){return this.get(this.fhirUrl(t,e,"_history"))}readVersion(t,e,r){return this.get(this.fhirUrl(t,e,"_history",r))}readPatientEverything(t){return this.get(this.fhirUrl("Patient",t,"$everything"))}createResource(t){if(!t.resourceType)throw new Error("Missing resourceType");return this.invalidateSearches(t.resourceType),this.post(this.fhirUrl(t.resourceType),t)}createResourceIfNoneExist(t,r){var n;return e(this,void 0,void 0,(function*(){return null!==(n=yield this.searchOne(`${t.resourceType}?${r}`))&&void 0!==n?n:this.createResource(t)}))}createBinary(t,e,r){const n=this.fhirUrl("Binary");return e&&n.searchParams.set("_filename",e),this.post(n,t,r)}createPdf(t,r,n,i){return e(this,void 0,void 0,(function*(){const e=yield $(t,n,i);return this.createBinary(e,r,"application/pdf")}))}createComment(t,e){const r=this.getProfile();let n,i;return"Encounter"===t.resourceType&&(n=u(t),i=t.subject),"ServiceRequest"===t.resourceType&&(n=t.encounter,i=t.subject),"Patient"===t.resourceType&&(i=u(t)),this.createResource({resourceType:"Communication",basedOn:[u(t)],encounter:n,subject:i,sender:r?u(r):void 0,sent:(new Date).toISOString(),payload:[{contentString:e}]})}updateResource(t){if(!t.resourceType)throw new Error("Missing resourceType");if(!t.id)throw new Error("Missing id");return this.invalidateSearches(t.resourceType),this.put(this.fhirUrl(t.resourceType,t.id),t)}patchResource(t,e,r){return this.invalidateSearches(t),this.patch(this.fhirUrl(t,e),r)}deleteResource(t,e){return this.invalidateSearches(t),this.delete(this.fhirUrl(t,e))}sendEmail(t){return this.post("email/v1/send",t,"application/json")}graphql(t,e){return this.post(this.fhirUrl("$graphql"),{query:t},"application/json",e)}getActiveLogin(){return r(this,at,"f").getObject("activeLogin")}setActiveLogin(t){return e(this,void 0,void 0,(function*(){n(this,mt,t.accessToken,"f"),n(this,gt,t.refreshToken,"f"),n(this,Tt,void 0,"f"),n(this,St,void 0,"f"),r(this,at,"f").setObject("activeLogin",t),r(this,st,"m",bt).call(this,t),r(this,ct,"f").clear(),n(this,yt,void 0,"f"),yield r(this,st,"m",Et).call(this)}))}getAccessToken(){return r(this,mt,"f")}setAccessToken(t){n(this,mt,t,"f"),n(this,gt,void 0,"f"),n(this,Tt,void 0,"f"),n(this,St,void 0,"f")}getLogins(){var t;return null!==(t=r(this,at,"f").getObject("logins"))&&void 0!==t?t:[]}isLoading(){return!!r(this,wt,"f")}getProfile(){return r(this,Tt,"f")}getProfileAsync(){return e(this,void 0,void 0,(function*(){return r(this,wt,"f")&&(yield r(this,wt,"f")),this.getProfile()}))}getUserConfiguration(){return r(this,St,"f")}download(t,n={}){return e(this,void 0,void 0,(function*(){r(this,yt,"f")&&(yield r(this,yt,"f")),r(this,st,"m",At).call(this,n);return(yield r(this,ot,"f").call(this,t.toString(),n)).blob()}))}processCode(t){if(!r(this,at,"f").getString("pkceState"))throw this.clear(),new Error("Invalid PCKE state");const e=r(this,at,"f").getString("codeVerifier");if(!e)throw this.clear(),new Error("Invalid PCKE code verifier");const n=new URLSearchParams;return n.set("grant_type","authorization_code"),n.set("client_id",r(this,ht,"f")),n.set("code_verifier",e),n.set("code",t),n.set("redirect_uri",Qt()),r(this,st,"m",Ct).call(this,n)}startClientLogin(t,n){return e(this,void 0,void 0,(function*(){const e=new URLSearchParams;return e.set("grant_type","client_credentials"),e.set("client_id",t),e.set("client_secret",n),r(this,st,"m",Ct).call(this,e)}))}},t.OperationOutcomeError=Q,t.ReadablePromise=V,t.SEGMENT_SEPARATOR="\r",t.accessDenied=H,t.allOk=_,t.arrayBufferToBase64=A,t.arrayBufferToHex=P,t.assertOk=function(t,e){if(!F(t)||void 0===e)throw new Q(t)},t.badRequest=function(t,e){return{resourceType:"OperationOutcome",issue:[{severity:"error",code:"invalid",details:{text:t},expression:e?[e]:void 0}]}},t.buildTypeName=Ht,t.calculateAge=v,t.calculateAgeString=function(t,e){const{years:r,months:n,days:i}=v(t,e);return r>=2?r.toString().padStart(3,"0")+"Y":n>=1?n.toString().padStart(3,"0")+"M":i.toString().padStart(3,"0")+"D"},t.capitalize=O,t.createReference=u,t.createSchema=jt,t.createTypeSchema=Nt,t.created=j,t.deepEquals=T,t.evalFhirPath=function(e,r){Array.isArray(r)||(r=[r]);const n=Array.isArray(r)?r:[r];for(let e=0;e<n.length;e++){const r=n[e];"object"==typeof r&&"type"in r&&"value"in r||(n[e]={type:t.PropertyType.BackboneElement,value:r})}return yr(e).eval(n).map((t=>t.value))},t.formatAddress=function(t,e){const r=[];return t.line&&r.push(...t.line),t.city&&r.push(t.city),t.state&&r.push(t.state),t.postalCode&&r.push(t.postalCode),t.use&&((null==e?void 0:e.all)||(null==e?void 0:e.use))&&r.push("["+t.use+"]"),r.join(", ").trim()},t.formatFamilyName=function(t){return t.family||""},t.formatGivenName=function(t){const e=[];return t.given&&e.push(...t.given),e.join(" ").trim()},t.formatHumanName=c,t.formatSearchQuery=et,t.getDateProperty=function(t){return t?new Date(t):void 0},t.getDisplayString=d,t.getExpressionForResourceType=Er,t.getExtensionValue=function(t,...e){var r;let n=t;for(let t=0;t<e.length&&n;t++)n=null===(r=null==n?void 0:n.extension)||void 0===r?void 0:r.find((r=>r.url===e[t]));return null==n?void 0:n.valueString},t.getIdentifier=function(t,e){const r=t.identifier;if(!r)return;const n=Array.isArray(r)?r:[r];for(const t of n)if(t.system===e)return t.value},t.getImageSrc=function(t){if(f(t)){const e=t.photo;if(e)for(const t of e){const e=p(t);if(e)return e}}if("Bot"===t.resourceType&&t.photo){const e=p(t.photo);if(e)return e}},t.getPropertyDisplayName=function(t){const e=t.path.replaceAll("[x]","").split(".").pop();return"id"===e?"ID":e.split(/(?=[A-Z])/).map(O).join(" ").replace("_"," ").replace(/\s+/g," ")},t.getQuestionnaireAnswers=function(t){const e={};return m(t.item,e),e},t.getReferenceString=h,t.getSearchParameterDetails=function(e,r,n){var i,s,o,a;if("_lastUpdated"===n.code)return{columnName:"lastUpdated",type:t.SearchParameterType.DATETIME};const l=n.code.split("-").reduce(((t,e,r)=>t+(r?O(e):e)),""),c=null===(i=Er(r,n.expression))||void 0===i?void 0:i.split(".");if(!c)return{columnName:l,type:t.SearchParameterType.TEXT};let u,h,f=r,d=!1;for(let r=1;r<c.length;r++){const n=c[r];if(u=null===(o=null===(s=e.types[f])||void 0===s?void 0:s.properties)||void 0===o?void 0:o[n],!u)return{columnName:l,type:t.SearchParameterType.TEXT,array:d};if("*"===u.max&&(d=!0),h=null===(a=u.type)||void 0===a?void 0:a[0].code,!h)return{columnName:l,type:t.SearchParameterType.TEXT,array:d};r<c.length-1&&("Element"===h||"BackboneElement"===h?f+=O(n):f=h)}const p=function(e,r){let n=t.SearchParameterType.TEXT;switch(e.type){case"date":n=r===t.PropertyType.dateTime||r===t.PropertyType.instant?t.SearchParameterType.DATETIME:t.SearchParameterType.DATE;break;case"number":n=t.SearchParameterType.NUMBER;break;case"quantity":n=t.SearchParameterType.QUANTITY;break;case"reference":n=t.SearchParameterType.REFERENCE;break;case"token":"boolean"===r&&(n=t.SearchParameterType.BOOLEAN)}return n}(n,h);return{columnName:l,type:p,elementDefinition:u,array:d}},t.getStatus=function(t){return t.id===U?200:t.id===M?201:t.id===L?304:t.id===I?403:t.id===D?404:t.id===C?410:400},t.gone=B,t.indexSearchParameter=Bt,t.indexStructureDefinition=Wt,t.isGone=function(t){return t.id===C},t.isLowerCase=function(t){return t===t.toLowerCase()},t.isNotFound=function(t){return t.id===D},t.isObject=S,t.isOk=F,t.isProfileResource=f,t.isStringArray=b,t.isUUID=function(t){return!!t.match(/^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$/i)},t.notFound=W,t.notModified=N,t.parseFhirPath=yr,t.parseSearchDefinition=tt,t.resolveId=function(t){var e;return null===(e=null==t?void 0:t.reference)||void 0===e?void 0:e.split("/")[1]},t.stringify=g,t.tokenize=Je,Object.defineProperty(t,"__esModule",{value:!0})}));
2
2
  //# sourceMappingURL=index.min.js.map