@eka-care/medassist-widget-embed 0.2.19 → 0.2.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +56 -1
- package/dist/index.js +85 -4
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -6,7 +6,8 @@ declare const DEFAULT_WIDGET_ASSET_BASE: string;
|
|
|
6
6
|
/** Theme from agent-config API (has mode; textColor derived: dark → black, light → white) */
|
|
7
7
|
type AgentConfig = {
|
|
8
8
|
name?: string;
|
|
9
|
-
allowed?: ("text" | "file" | "audio")[];
|
|
9
|
+
allowed?: ("text" | "file" | "audio" | "voice")[];
|
|
10
|
+
nudge?: boolean;
|
|
10
11
|
theme?: {
|
|
11
12
|
background?: string;
|
|
12
13
|
background_img?: string;
|
|
@@ -14,6 +15,7 @@ type AgentConfig = {
|
|
|
14
15
|
mode?: "light" | "dark";
|
|
15
16
|
title_img?: string;
|
|
16
17
|
icon_img?: string;
|
|
18
|
+
nudge_color?: string;
|
|
17
19
|
};
|
|
18
20
|
};
|
|
19
21
|
type MedAssistInitConfig = {
|
|
@@ -37,6 +39,53 @@ declare function mapAgentConfigThemeToWidgetTheme(apiTheme: AgentConfig["theme"]
|
|
|
37
39
|
declare function preloadImage(url: string): void;
|
|
38
40
|
/** Fetch agent-config from API and return theme from response data */
|
|
39
41
|
declare function fetchAgentConfig(baseUrl: string, agentId: string): Promise<AgentConfig | undefined>;
|
|
42
|
+
type NudgeApiResponse = {
|
|
43
|
+
url_pattern?: {
|
|
44
|
+
domain?: string;
|
|
45
|
+
path?: string;
|
|
46
|
+
};
|
|
47
|
+
nudges?: string[];
|
|
48
|
+
delay?: number;
|
|
49
|
+
expiry?: number;
|
|
50
|
+
};
|
|
51
|
+
type NudgePathData = {
|
|
52
|
+
nudges: string[];
|
|
53
|
+
expiry: number;
|
|
54
|
+
delay: number;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* localStorage shape:
|
|
58
|
+
* {
|
|
59
|
+
* [agentId]: {
|
|
60
|
+
* [domain (url.host)]: {
|
|
61
|
+
* [path]: NudgePathData
|
|
62
|
+
* }
|
|
63
|
+
* }
|
|
64
|
+
* }
|
|
65
|
+
*/
|
|
66
|
+
type NudgeLocalStore = {
|
|
67
|
+
[agentId: string]: {
|
|
68
|
+
[domain: string]: {
|
|
69
|
+
[path: string]: NudgePathData;
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
declare const NUDGE_STORAGE_KEY = "eka-medassist-nudges";
|
|
74
|
+
declare function getNudgeStore(): NudgeLocalStore;
|
|
75
|
+
declare function setNudgeStore(store: NudgeLocalStore): void;
|
|
76
|
+
declare function normalizePath(pathname: string): string;
|
|
77
|
+
type NudgeCacheHit = {
|
|
78
|
+
domain: string;
|
|
79
|
+
path: string;
|
|
80
|
+
data: NudgePathData;
|
|
81
|
+
};
|
|
82
|
+
declare function findCachedNudge(agentId: string, currentUrl: string): NudgeCacheHit | null;
|
|
83
|
+
declare function clearNudgeForPath(agentId: string, domain: string, path: string): void;
|
|
84
|
+
declare function storeNudgeResponse(agentId: string, response: NudgeApiResponse): void;
|
|
85
|
+
declare const NUDGE_COOKIE_PREFIX = "eka-nudge";
|
|
86
|
+
declare function setNudgeCookie(agentId: string, value: "open" | "closed"): void;
|
|
87
|
+
declare function getNudgeCookie(agentId: string): "open" | "closed" | null;
|
|
88
|
+
declare function fetchNudgeData(agentId: string, baseUrl?: string): Promise<NudgeApiResponse | undefined>;
|
|
40
89
|
type ReactRoot = {
|
|
41
90
|
unmount: () => void;
|
|
42
91
|
};
|
|
@@ -63,6 +112,8 @@ declare class MedAssistWidgetLoader extends HTMLElement {
|
|
|
63
112
|
private widgetLoaded;
|
|
64
113
|
private displayMode;
|
|
65
114
|
private reactRoot;
|
|
115
|
+
private agentConfigPromise;
|
|
116
|
+
private getAgentConfig;
|
|
66
117
|
constructor();
|
|
67
118
|
static get observedAttributes(): string[];
|
|
68
119
|
connectedCallback(): void;
|
|
@@ -73,6 +124,10 @@ declare class MedAssistWidgetLoader extends HTMLElement {
|
|
|
73
124
|
private preloadBackgroundImage;
|
|
74
125
|
private setupAuthExpirationListener;
|
|
75
126
|
openFromBridge(): void;
|
|
127
|
+
/** Resolve nudge data (from cache or API) and schedule the popup. */
|
|
128
|
+
private initNudge;
|
|
129
|
+
private showNudgePopup;
|
|
130
|
+
private dismissNudgePopup;
|
|
76
131
|
renderButton(): void;
|
|
77
132
|
initializeFullMode(): void;
|
|
78
133
|
loadAndRender(): Promise<void>;
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,85 @@
|
|
|
1
|
-
"use strict";const getCurrentScript=()=>{if(typeof document=="undefined")return console.error("document is not defined"),null;const{currentScript:e}=document;if(e instanceof HTMLScriptElement)return e;const t=document.getElementsByTagName("script");return t.length?t[t.length-1]:null},getEnvironment=e=>{if(e==="production"||e==="development"||e==="staging")return e},scriptEl=getCurrentScript(),DEFAULT_WIDGET_ASSET_BASE=(()=>{var e;return!((e=scriptEl==null?void 0:scriptEl.src)===null||e===void 0)&&e.includes("@dev")?"https://unpkg.com/@eka-care/medassist-widget@dev/dist/":"https://unpkg.com/@eka-care/medassist-widget@latest/dist/"})();function mapAgentConfigThemeToWidgetTheme(e){if(!e)return;const t=e.mode==="dark"?"white":e.mode==="light"?"black":void 0;return{...e.background&&{background:e.background},...e.background_img&&{backgroundImage:e.background_img},...e.accent&&{primary:e.accent},...t&&{textColor:t},...e.title_img&&{titleImg:e.title_img}}}function preloadImage(e){if(!(!e||typeof document=="undefined"))try{const t=document.createElement("link");t.rel="preload",t.as="image",t.href=e,document.head.appendChild(t)}catch{const t=new Image;t.src=e}}async function fetchAgentConfig(e,t){const
|
|
1
|
+
"use strict";const getCurrentScript=()=>{if(typeof document=="undefined")return console.error("document is not defined"),null;const{currentScript:e}=document;if(e instanceof HTMLScriptElement)return e;const t=document.getElementsByTagName("script");return t.length?t[t.length-1]:null},getEnvironment=e=>{if(e==="production"||e==="development"||e==="staging")return e},scriptEl=getCurrentScript(),DEFAULT_WIDGET_ASSET_BASE=(()=>{var e;return!((e=scriptEl==null?void 0:scriptEl.src)===null||e===void 0)&&e.includes("@dev")?"https://unpkg.com/@eka-care/medassist-widget@dev/dist/":"https://unpkg.com/@eka-care/medassist-widget@latest/dist/"})();function mapAgentConfigThemeToWidgetTheme(e){if(!e)return;const t=e.mode==="dark"?"white":e.mode==="light"?"black":void 0;return{...e.background&&{background:e.background},...e.background_img&&{backgroundImage:e.background_img},...e.accent&&{primary:e.accent},...t&&{textColor:t},...e.title_img&&{titleImg:e.title_img}}}function preloadImage(e){if(!(!e||typeof document=="undefined"))try{const t=document.createElement("link");t.rel="preload",t.as="image",t.href=e,document.head.appendChild(t)}catch{const t=new Image;t.src=e}}async function fetchAgentConfig(e,t){const o=`${e.replace(/\/$/,"")}/med-assist/agent-config/${t}`,i=await fetch(o,{headers:{"ngrok-skip-browser-warning":"69420"}});if(!i.ok)return;const n=await i.json();if(n!=null&&n.success&&(n!=null&&n.data))return n.data}const NUDGE_STORAGE_KEY="eka-medassist-nudges";function getNudgeStore(){try{const e=localStorage.getItem(NUDGE_STORAGE_KEY);return e?JSON.parse(e):{}}catch{return{}}}function setNudgeStore(e){try{localStorage.setItem(NUDGE_STORAGE_KEY,JSON.stringify(e))}catch{}}function normalizePath(e){let t=e.replace(/\/+$/,"");return t.startsWith("/")||(t="/"+t),t||"/"}function findCachedNudge(e,t){const i=getNudgeStore()[e];if(!i)return null;let n;try{n=new URL(t)}catch{return null}const d=n.host,s=i[d];if(!s)return null;const r=n.pathname.split("/").filter(Boolean),u=[];for(let a=r.length;a>=0;a--)u.push(a===0?"/":"/"+r.slice(0,a).join("/"));for(const a of u){if(s[a]!==void 0)return{domain:d,path:a,data:s[a]};const l=a==="/"?"/*":a+"/*";if(s[l]!==void 0)return{domain:d,path:l,data:s[l]}}return null}function clearNudgeForPath(e,t,o){const i=getNudgeStore(),n=i[e];!n||!n[t]||(delete n[t][o],setNudgeStore(i))}function storeNudgeResponse(e,t){var o,i,n,d;const s=(o=t==null?void 0:t.url_pattern)===null||o===void 0?void 0:o.domain,r=(i=t==null?void 0:t.url_pattern)===null||i===void 0?void 0:i.path;if(!(!((n=t.nudges)===null||n===void 0)&&n.length)||!t.expiry||!s||!r)return;const u=getNudgeStore();u[e]||(u[e]={}),u[e][s]||(u[e][s]={}),u[e][s][r]={nudges:t.nudges,expiry:t.expiry,delay:(d=t.delay)!==null&&d!==void 0?d:5},setNudgeStore(u)}const NUDGE_COOKIE_PREFIX="eka-nudge";function setNudgeCookie(e,t){const o=new Date(Date.now()+864e5).toUTCString();document.cookie=`${NUDGE_COOKIE_PREFIX}-${e}=${t}; expires=${o}; path=/; SameSite=Lax`}function getNudgeCookie(e){const t=document.cookie.split(";").map(i=>i.trim()).find(i=>i.startsWith(`${NUDGE_COOKIE_PREFIX}-${e}=`));if(!t)return null;const o=t.split("=")[1];return o==="open"||o==="closed"?o:null}async function fetchNudgeData(e,t){const o=`${t}/med-assist/user-nudge`,i=[];typeof document!="undefined"&&document.querySelectorAll("meta").forEach(n=>{var d,s,r;const u=(d=n.getAttribute("name"))!==null&&d!==void 0?d:void 0,a=(s=n.getAttribute("property"))!==null&&s!==void 0?s:void 0,l=(r=n.getAttribute("content"))!==null&&r!==void 0?r:void 0;(u||a)&&i.push({name:u,property:a,content:l})});try{const n=await fetch(o,{method:"POST",headers:{"Content-Type":"application/json","x-agent-id":e,referer:typeof window!="undefined"?window.location.href:""},body:JSON.stringify({meta_tags:i,url:typeof window!="undefined"?window.location.href:""})});return n.ok?await n.json():void 0}catch{return}}const globalMedAssistConfig={},getWidgetElement=()=>typeof document=="undefined"?null:document.querySelector("eka-medassist-widget");if(typeof window!="undefined"){const e=window;e.__ekaMedAssistConfig__=globalMedAssistConfig,e.EkaMedAssist={init(t){var o,i,n;Object.assign(globalMedAssistConfig,t);const d=getWidgetElement();if(d){if(t.agentId&&d.setAttribute("agent-id",t.agentId),t.title&&d.setAttribute("title",t.title),t.iconUrl&&d.setAttribute("icon-url",t.iconUrl),t.baseUrl&&d.setAttribute("base-url",t.baseUrl),t.context)try{d.setAttribute("context",JSON.stringify(t.context))}catch{console.warn("Failed to stringify context passed to init")}!((o=t.theme)===null||o===void 0)&&o.backgroundImage&&preloadImage(t.theme.backgroundImage),(n=(i=d).openFromBridge)===null||n===void 0||n.call(i)}}}}const scriptBaseUrl=(()=>{if(!scriptEl)return"";try{return new URL(".",scriptEl.src||window.location.href).href}catch{return""}})(),widgetAssetBaseUrl=(()=>{if(!scriptEl)return DEFAULT_WIDGET_ASSET_BASE;const e=scriptEl.dataset.widgetAssets;if(e==="local")return`${scriptBaseUrl}src/`;if(e&&e.length>0)try{return new URL(e,scriptBaseUrl).href}catch{return e.endsWith("/")?e:`${e}/`}return DEFAULT_WIDGET_ASSET_BASE})(),WIDGET_JS_URL=`${widgetAssetBaseUrl}medassist-widget.js`,WIDGET_CSS_URL=`${widgetAssetBaseUrl}medassist-widget.css`;let widgetScriptPromise=null,widgetCssTextPromise=null;function getWidgetCssTextPromise(){return widgetCssTextPromise||(widgetCssTextPromise=fetch(WIDGET_CSS_URL).then(e=>{if(!e.ok)throw new Error(`Unable to fetch widget styles from ${WIDGET_CSS_URL}`);return e.text()})),widgetCssTextPromise}class MedAssistWidgetLoader extends HTMLElement{getAgentConfig(){if(!this.agentConfigPromise){const t=(typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0)||{},o=t.baseUrl||this.getAttribute("base-url")||"https://matrix.eka.care/reloaded",i=t.agentId||this.getAttribute("agent-id")||"";if(!i)return Promise.resolve(void 0);this.agentConfigPromise=fetchAgentConfig(o,i).catch(()=>{})}return this.agentConfigPromise}constructor(){super(),this.reactRoot=null,this.agentConfigPromise=null,this.attachShadow({mode:"open"}),this.defaultIconUrl="https://cdn.eka.care/bot-icon.svg",this.widgetLoaded=!1,this.displayMode=this.getAttribute("display-mode")==="full"?"full":"widget",this.setupAuthExpirationListener()}static get observedAttributes(){return["icon-url","display-mode"]}connectedCallback(){this.displayMode==="full"?(this.initializeFullMode(),this.loadWidgetCss().then(()=>this.loadWidgetScript()).then(()=>this.loadAndRender()).catch(t=>{console.error("Failed to load MedAssist widget in full mode",t)})):(this.renderButton(),this.preloadWidgetAssets(),this.initNudge())}attributeChangedCallback(t){if(t==="icon-url"&&this.displayMode==="widget"&&this.renderButton(),t==="display-mode"){const o=this.getAttribute("display-mode")==="full"?"full":"widget";o!==this.displayMode&&(this.displayMode=o,o==="full"?this.initializeFullMode():this.renderButton())}}preloadWidgetAssets(){getWidgetCssTextPromise(),this.loadWidgetScript(),this.preloadBackgroundImage()}preloadBackgroundImage(){var t;const o=typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0,i=this.getAttribute("theme")?(()=>{try{return JSON.parse(this.getAttribute("theme")||"{}")}catch{return}})():void 0,n=(t=o==null?void 0:o.theme)===null||t===void 0?void 0:t.backgroundImage,d=i==null?void 0:i.backgroundImage;n&&preloadImage(n),d&&d!==n&&preloadImage(d),this.getAgentConfig().then(s=>{var r;const u=(r=s==null?void 0:s.theme)===null||r===void 0?void 0:r.background_img;u&&preloadImage(u)}).catch(()=>{})}setupAuthExpirationListener(){if(typeof window=="undefined")return;const t=o=>{var i;((i=o.data)===null||i===void 0?void 0:i.type)==="AUTH_EXPIRED"&&window.parent!==window&&window.parent.postMessage({type:"AUTH_EXPIRED",message:"Authentication expired"},"*")};window.addEventListener("message",t)}openFromBridge(){this.displayMode=this.getAttribute("display-mode")==="full"?"full":"widget",this.displayMode==="full"?this.initializeFullMode():this.renderButton(),this.loadWidgetCss().then(()=>this.loadWidgetScript()).then(()=>this.loadAndRender()).catch(t=>{console.error("Failed to open MedAssist widget from bridge",t)})}async initNudge(){var t,o,i,n;const d=typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0,s=(d==null?void 0:d.agentId)||this.getAttribute("agent-id")||"";if(!s)return;const r=await this.getAgentConfig();if(!(r!=null&&r.nudge))return;const u=(o=(t=r==null?void 0:r.theme)===null||t===void 0?void 0:t.nudge_color)!==null&&o!==void 0?o:"#ffffff",a=typeof window!="undefined"?window.location.href:"";let l=findCachedNudge(s,a);if(l){const g=Math.floor(Date.now()/1e3);l.data.expiry<=g&&(clearNudgeForPath(s,l.domain,l.path),l=null)}if(!l){const g=typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0,m=(g==null?void 0:g.baseUrl)||this.getAttribute("base-url")||"https://matrix.eka.care/reloaded",p=await fetchNudgeData(s,m).catch(()=>{});if(!(!((i=p==null?void 0:p.nudges)===null||i===void 0)&&i.length)||(storeNudgeResponse(s,p),l=findCachedNudge(s,a),!l))return}const h=l.data.nudges[Math.floor(Math.random()*l.data.nudges.length)],y=(n=l.data.delay)!==null&&n!==void 0?n:5;setTimeout(()=>{this.showNudgePopup(h,u)},y*1e3)}showNudgePopup(t,o){var i,n;const d=this.shadowRoot;if(!d||!(!((i=d.getElementById("medassist-widget-root"))===null||i===void 0)&&i.classList.contains("hidden")))return;const r=((typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0)||{}).agentId||this.getAttribute("agent-id")||"";if(r&&getNudgeCookie(r)!==null||d.getElementById("medassist-nudge-popup"))return;const a=document.createElement("div");a.id="medassist-nudge-popup",a.innerHTML=`
|
|
2
|
+
<style>
|
|
3
|
+
@keyframes nudge-slide-up {
|
|
4
|
+
from { opacity: 0; transform: translateY(12px) scale(0.97); }
|
|
5
|
+
to { opacity: 1; transform: translateY(0) scale(1); }
|
|
6
|
+
}
|
|
7
|
+
#medassist-nudge-popup {
|
|
8
|
+
position: absolute;
|
|
9
|
+
bottom: 76px;
|
|
10
|
+
right: 0;
|
|
11
|
+
width: 272px;
|
|
12
|
+
background: ${o};
|
|
13
|
+
border-radius: 16px;
|
|
14
|
+
box-shadow: 0 8px 32px rgba(0,0,0,0.14), 0 2px 8px rgba(0,0,0,0.08);
|
|
15
|
+
padding: 16px 16px 14px 16px;
|
|
16
|
+
display: flex;
|
|
17
|
+
flex-direction: column;
|
|
18
|
+
gap: 12px;
|
|
19
|
+
z-index: 200001;
|
|
20
|
+
animation: nudge-slide-up 0.28s cubic-bezier(0.34,1.56,0.64,1);
|
|
21
|
+
font-family: ui-sans-serif, system-ui, -apple-system, sans-serif;
|
|
22
|
+
box-sizing: border-box;
|
|
23
|
+
}
|
|
24
|
+
/* Speech bubble tail pointing down-right toward the FAB */
|
|
25
|
+
#medassist-nudge-popup::after {
|
|
26
|
+
content: '';
|
|
27
|
+
position: absolute;
|
|
28
|
+
bottom: -8px;
|
|
29
|
+
right: 20px;
|
|
30
|
+
width: 16px;
|
|
31
|
+
height: 10px;
|
|
32
|
+
background: ${o};
|
|
33
|
+
clip-path: polygon(0 0, 100% 0, 50% 100%);
|
|
34
|
+
}
|
|
35
|
+
#medassist-nudge-popup {
|
|
36
|
+
cursor: pointer;
|
|
37
|
+
}
|
|
38
|
+
#medassist-nudge-popup:hover {
|
|
39
|
+
box-shadow: 0 10px 36px rgba(0,0,0,0.18), 0 2px 8px rgba(0,0,0,0.1);
|
|
40
|
+
}
|
|
41
|
+
#medassist-nudge-popup .nudge-header {
|
|
42
|
+
display: flex;
|
|
43
|
+
align-items: flex-start;
|
|
44
|
+
justify-content: space-between;
|
|
45
|
+
gap: 8px;
|
|
46
|
+
}
|
|
47
|
+
#medassist-nudge-popup .nudge-text {
|
|
48
|
+
font-size: 13.5px;
|
|
49
|
+
line-height: 1.5;
|
|
50
|
+
color: #1a1a1a;
|
|
51
|
+
margin: 0;
|
|
52
|
+
flex: 1;
|
|
53
|
+
font-weight: 400;
|
|
54
|
+
letter-spacing: -0.01em;
|
|
55
|
+
}
|
|
56
|
+
#medassist-nudge-popup .nudge-close {
|
|
57
|
+
flex-shrink: 0;
|
|
58
|
+
width: 22px;
|
|
59
|
+
height: 22px;
|
|
60
|
+
display: flex;
|
|
61
|
+
align-items: center;
|
|
62
|
+
justify-content: center;
|
|
63
|
+
background: rgba(0,0,0,0.06);
|
|
64
|
+
border: none;
|
|
65
|
+
border-radius: 50%;
|
|
66
|
+
cursor: pointer;
|
|
67
|
+
font-size: 13px;
|
|
68
|
+
line-height: 1;
|
|
69
|
+
color: #555;
|
|
70
|
+
padding: 0;
|
|
71
|
+
margin-top: -1px;
|
|
72
|
+
transition: background 0.15s;
|
|
73
|
+
}
|
|
74
|
+
#medassist-nudge-popup .nudge-close:hover {
|
|
75
|
+
background: rgba(0,0,0,0.18);
|
|
76
|
+
}
|
|
77
|
+
</style>
|
|
78
|
+
<div class="nudge-header">
|
|
79
|
+
<p class="nudge-text">${t.replace(/</g,"<").replace(/>/g,">")}</p>
|
|
80
|
+
<button class="nudge-close" aria-label="Dismiss">✕</button>
|
|
81
|
+
</div>
|
|
82
|
+
`,(n=a.querySelector(".nudge-close"))===null||n===void 0||n.addEventListener("click",l=>{l.stopPropagation(),r&&setNudgeCookie(r,"closed"),this.dismissNudgePopup()}),a.addEventListener("click",()=>{r&&setNudgeCookie(r,"closed"),this.dismissNudgePopup(),this.loadAndRender()}),d.appendChild(a)}dismissNudgePopup(){var t;const o=(t=this.shadowRoot)===null||t===void 0?void 0:t.getElementById("medassist-nudge-popup");o==null||o.remove()}renderButton(){var t;const o=this.getAttribute("icon-url")||this.defaultIconUrl,i=this.shadowRoot;if(!i){console.error("Shadow root was not created");return}if(!i.querySelector("button"))i.innerHTML=`
|
|
2
83
|
<style>
|
|
3
84
|
#medassist-open-btn {
|
|
4
85
|
width: 60px;
|
|
@@ -35,10 +116,10 @@
|
|
|
35
116
|
</style>
|
|
36
117
|
|
|
37
118
|
<button id="medassist-open-btn">
|
|
38
|
-
<img src="${
|
|
119
|
+
<img src="${o}" alt="MedAssist Icon">
|
|
39
120
|
</button>
|
|
40
121
|
<div id="medassist-widget-root" class="hidden"></div>
|
|
41
|
-
`,(t=
|
|
122
|
+
`,(t=i.getElementById("medassist-open-btn"))===null||t===void 0||t.addEventListener("click",()=>{const d=((typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0)||{}).agentId||this.getAttribute("agent-id")||"";d&&setNudgeCookie(d,"closed"),this.dismissNudgePopup(),this.loadAndRender()});else{const n=i.querySelector("#medassist-open-btn img");n&&(n.src=o)}}initializeFullMode(){const t=this.shadowRoot;if(!t){console.error("Shadow root is not available");return}t.getElementById("medassist-widget-root")||(t.innerHTML=`
|
|
42
123
|
<style>
|
|
43
124
|
:host {
|
|
44
125
|
display: block;
|
|
@@ -55,4 +136,4 @@
|
|
|
55
136
|
}
|
|
56
137
|
</style>
|
|
57
138
|
<div id="medassist-widget-root"></div>
|
|
58
|
-
`)}async loadAndRender(){var t,i
|
|
139
|
+
`)}async loadAndRender(){var t,o,i;const n=this.shadowRoot;if(!n){console.error("Shadow root is not available");return}this.style.display="";const d=n.getElementById("medassist-open-btn"),s=n.getElementById("medassist-widget-root"),r=this.getAttribute("agent-id");let u=this.getAttribute("icon-url")||this.defaultIconUrl;const a=(t=getEnvironment(this.getAttribute("environment")))!==null&&t!==void 0?t:"production";let l=this.getAttribute("title")||"Medi Clinic";const h=this.getAttribute("base-url")||"https://matrix.eka.care/reloaded",g=this.getAttribute("display-mode")==="full"?"full":"widget",m=this.getAttribute("context")?JSON.parse(this.getAttribute("context")||"{}"):void 0,p=this.getAttribute("theme")?(()=>{try{return JSON.parse(this.getAttribute("theme")||"{}")}catch{return}})():void 0,f=(typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0)||{},k={...m||{},...f.context||{}},C=f.auth||void 0,w=f.agentId||r,E=f.baseUrl||h,b=typeof window!="undefined"?(o=window.EkaMedAssist)===null||o===void 0?void 0:o.onClose:void 0;if(!w){console.error("Agent ID is required");return}if(!s){console.error("Widget root element is missing");return}d&&d.classList.add("hidden"),s.classList.remove("hidden");let x,A;try{const c=await this.getAgentConfig();l=(c==null?void 0:c.name)||l,u=((i=c==null?void 0:c.theme)===null||i===void 0?void 0:i.icon_img)||u,x=mapAgentConfigThemeToWidgetTheme((c==null?void 0:c.theme)||void 0),A=c==null?void 0:c.allowed}catch{}const _={title:f.title||l,iconUrl:f.iconUrl||u,allowed:A,environment:a,onClose:()=>{var c;(c=this.reactRoot)===null||c===void 0||c.unmount(),this.reactRoot=null,b&&typeof b=="function"&&b(),g==="full"?this.style.display="none":(d&&d.classList.remove("hidden"),s.classList.add("hidden"))},baseUrl:E,context:k,displayMode:g,auth:C,theme:{...x||{},...p||{},...f.theme||{}}},v=window;if(this.widgetLoaded){this.reactRoot=v.renderMedAssist(s,w,_);return}try{if(await Promise.all([this.loadWidgetCss(),this.loadWidgetScript()]),typeof v.renderMedAssist!="function")throw new Error("renderMedAssist is not available on window");this.reactRoot=v.renderMedAssist(s,w,_),this.widgetLoaded=!0}catch(c){console.error("Failed to load MedAssist widget",c),d&&d.classList.remove("hidden"),s.classList.add("hidden")}}async loadWidgetCss(){const t=this.shadowRoot;if(!t)throw new Error("Shadow root is not available");if(t.querySelector("[data-medassist-style='true']"))return;const o=await getWidgetCssTextPromise(),i=document.createElement("style");i.setAttribute("data-medassist-style","true"),i.textContent=o,t.appendChild(i)}loadWidgetScript(){return widgetScriptPromise||(widgetScriptPromise=new Promise((t,o)=>{const i=document.querySelector(`script[src="${WIDGET_JS_URL}"]`);if(i){if(i.dataset.loaded==="true"){t();return}i.addEventListener("load",()=>t()),i.addEventListener("error",d=>o(d.error||new Error("Unknown script loading error")));return}const n=document.createElement("script");n.src=WIDGET_JS_URL,n.async=!0,n.dataset.widget="medassist",n.onload=()=>{n.dataset.loaded="true",t()},n.onerror=d=>{if(d instanceof ErrorEvent&&d.error){o(d.error);return}o(new Error(`Failed to load ${WIDGET_JS_URL}`))},document.head.appendChild(n)})),widgetScriptPromise}}customElements.define("eka-medassist-widget",MedAssistWidgetLoader);
|