@ziila/sdk 0.1.247 → 0.1.269
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.cjs.js +1 -1
- package/dist/index.esm.js +4 -4
- package/dist/src/MessageBridge.d.ts +2 -1
- package/dist/src/types.d.ts +21 -2
- package/dist/widget.iife.js +1 -1
- package/package.json +1 -1
package/dist/index.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=class{constructor(e,t){this.iframe=null,this.port=null,this.handleHandshake=e=>{let t=this.iframe?.contentWindow;if(!t||e.source!==t||e.origin!==new URL(this.url).origin)return;let n=e.data;if(typeof n!=`object`||!n||n.type!==`HANDSHAKE`)return;let r=new MessageChannel;t.postMessage({type:`HANDSHAKE_ACK`},new URL(this.url).origin,[r.port2]),this.port=r.port1,this.port.start(),window.removeEventListener(`message`,this.handleHandshake),this.onPortReady(r.port1)},this.onPortReady=e,this.url=t??`https://widget.ziila.com`}mount(e){let t=new URL(this.url);t.searchParams.set(`embedded`,`1`),this.iframe=document.createElement(`iframe`),this.iframe.src=t.toString(),this.iframe.title=`Ziila Shopping Assistant`,this.iframe.setAttribute(`allow`,`microphone; autoplay`),this.iframe.style.cssText=`display:block;width:100%;height:100%;border:none;`,e.appendChild(this.iframe),window.addEventListener(`message`,this.handleHandshake)}send(e){this.port?.postMessage(e)}destroy(){window.removeEventListener(`message`,this.handleHandshake),this.port?.close(),this.iframe?.remove(),this.iframe=null,this.port=null}},t=class{constructor(){this.handlers=new Map}on(e,t){return this.handlers.has(e)||this.handlers.set(e,new Set),this.handlers.get(e).add(t),()=>this.off(e,t)}off(e,t){this.handlers.get(e)?.delete(t)}emit(e,t){this.handlers.get(e)?.forEach(e=>e(t))}destroy(){this.handlers.clear()}},n=e=>`position:fixed;inset:0;z-index:2147483647;display:flex;align-items:center;justify-content:center;background:rgba(0,0,0,0.4);padding:24px;font-family:${e.fontFamily??`system-ui,-apple-system,sans-serif`};`,r=e=>`width:100%;max-width:24rem;border-radius:${e.borderRadius??`12px`};background:${e.backgroundColor??`#fff`};padding:16px;box-shadow:2px 4px 2px rgba(0,0,0,0.1);display:flex;flex-direction:column;gap:16px;outline:none;`,i=`display:flex;justify-content:flex-end;`,a=e=>`border:none;background:${e.mutedColor??`#f0f0f0`};border-radius:${e.buttonRadius??`12px`};width:32px;height:32px;display:flex;align-items:center;justify-content:center;cursor:pointer;font-size:14px;color:${e.mutedFg??`#555`};line-height:1;padding:0;`,o=`display:flex;flex-direction:column;gap:16px;align-items:center;`,s=e=>`margin:0;font-size:24px;font-weight:700;color:${e.primaryColor??`#195f6b`};text-align:center;`,c=e=>`margin:0;font-size:16px;color:${e.foregroundColor??`#2d2d2d`};font-weight:500;line-height:27px;text-align:center;max-width:345px;`,l=`display:flex;gap:16px;padding:0 16px;width:100%;box-sizing:border-box;`,u=e=>`flex:1;border:1px solid ${e.primaryColor??`#195f6b`};border-radius:${e.buttonRadius??`8px`};background:${e.backgroundColor??`#fff`};color:${e.primaryColor??`#195f6b`};padding:16px;font-size:16px;font-weight:${e.buttonFontWeight??`500`};cursor:pointer;`,d=(e,t)=>`flex:1;border:none;border-radius:${e.buttonRadius??`8px`};background:${t===`primary`?e.primaryColor??`#195f6b`:`#cc0000`};color:${t===`primary`?e.primaryFg??`#fff`:`#fff`};padding:16px;font-size:16px;font-weight:${e.buttonFontWeight??`500`};cursor:pointer;`,f=class{constructor(){this.overlay=null,this.pendingResolve=null,this.previousFocus=null}show(e){return this.pendingResolve?.(!1),this.pendingResolve=null,this.close(),new Promise(t=>{this.pendingResolve=t;let f=e=>{this.pendingResolve=null,this.close(),t(e)},p=e.tokens??{},m=document.createElement(`div`);m.style.cssText=n(p);let h=document.createElement(`div`);h.setAttribute(`role`,`dialog`),h.setAttribute(`aria-modal`,`true`),h.setAttribute(`aria-labelledby`,`ziila-confirm-title`),h.tabIndex=-1,h.style.cssText=r(p),h.dir=e.dir;let g=document.createElement(`div`);g.style.cssText=i;let _=document.createElement(`button`);_.type=`button`,_.setAttribute(`aria-label`,e.closeLabel),_.textContent=`✕`,_.style.cssText=a(p),_.addEventListener(`click`,()=>f(!1)),g.appendChild(_);let v=document.createElement(`div`);v.style.cssText=o;let y=document.createElement(`h2`);y.id=`ziila-confirm-title`,y.textContent=e.title,y.style.cssText=s(p);let b=document.createElement(`p`);b.textContent=e.message,b.style.cssText=c(p);let x=document.createElement(`button`);x.type=`button`,x.textContent=e.cancelLabel,x.style.cssText=u(p),x.addEventListener(`click`,()=>f(!1));let S=document.createElement(`button`);S.type=`button`,S.textContent=e.confirmLabel,S.style.cssText=d(p,e.tone??`destructive`),S.addEventListener(`click`,()=>f(!0));let C=document.createElement(`div`);C.style.cssText=l,C.append(x,S),v.append(y,b,C),h.append(g,v),m.appendChild(h);let w=[_,x,S];m.addEventListener(`keydown`,e=>{if(e.key===`Escape`){f(!1);return}if(e.key===`Tab`){e.preventDefault();let t=(w.indexOf(document.activeElement)+(e.shiftKey?-1:1)+w.length)%w.length;w[t].focus()}}),this.previousFocus=document.activeElement,this.overlay=m,document.body.appendChild(m),h.focus()})}close(){this.overlay?.remove(),this.overlay=null,this.previousFocus?.focus(),this.previousFocus=null}},p={primaryColor:[`--primary`,`--primary-color`,`--color-primary`,`--brand`,`--brand-color`,`--accent-color`,`--bs-primary`,`--mui-palette-primary-main`,`--ant-color-primary`],borderRadius:[`--radius`,`--border-radius`,`--rounded`,`--bs-border-radius`]},m=new Set([``,`transparent`,`inherit`,`initial`,`unset`,`none`,`rgba(0, 0, 0, 0)`,`rgb(0, 0, 0)`,`depends on user agent`]);function h(e){return!!e&&!m.has(e.trim().toLowerCase())}function g(e=document){let t=e.defaultView??globalThis,n=t.getComputedStyle(e.documentElement),r=e.body?t.getComputedStyle(e.body):null,i={};r&&(h(r.fontFamily)&&(i.fontFamily=r.fontFamily),h(r.backgroundColor)&&(i.backgroundColor=r.backgroundColor),h(r.color)&&(i.foregroundColor=r.color));for(let[e,t]of Object.entries(p))for(let r of t){let t=n.getPropertyValue(r).trim();if(h(t)){i[e]=t;break}}return i}function _(e,t={}){let n={...g(t.doc??document),...e?.tokens},r={};return Object.keys(n).length>0&&(r.tokens=n),e?.elements&&Object.keys(e.elements).length>0&&(r.elements=e.elements),r}var v={},y=[`en`,`ar`],b=`PARAGLIDE_LOCALE`,x=[`baseLocale`],S=[],C,w;function T(e){if(S.length===0)return;let t=typeof e==`string`?e:e.href;if(C===t)return w;let n=new URL(t,`http://dummy.com`),r;for(let e of S)if(new v(e.match,n.href).exec(n.href)){r=e;break}return C=t,w=r,r}function E(e){let t=T(e);return t&&t.exclude!==!0&&Array.isArray(t.strategy)?t.strategy:x}globalThis.__paraglide=globalThis.__paraglide??{},globalThis.__paraglide.ssr=globalThis.__paraglide.ssr??{};var D=!1,O=()=>{let e=x;typeof window<`u`&&window.location?.href&&(e=E(window.location.href));let t=k(e,typeof window<`u`?window.location?.href:void 0);if(t)return D||(D=!0,j(t,{reload:!1})),t;throw Error(`No locale found. Read the docs https://paraglidejs.com/errors#no-locale-found`)};function k(e,t){let n;for(let t of e){if(t===`baseLocale`)n=`en`;else if(I(t)&&F.has(t)){let e=F.get(t);if(e){let t=e.getLocale();if(t instanceof Promise)continue;if(t!==void 0)return N(t)}}let e=M(n);if(e)return e}}var A=e=>{e?window.location.href=e:window.location.reload()},j=(e,t)=>{let n={reload:!0,...t},r;try{r=O()}catch{}let i=[],a=x;typeof window<`u`&&window.location?.href&&(a=E(window.location.href));for(let t of a)if(t===`baseLocale`)continue;else if(I(t)&&F.has(t)){let n=F.get(t);if(n){let r=n.setLocale(e);r instanceof Promise&&(r=r.catch(e=>{throw Error(`Custom strategy "${t}" setLocale failed.`,{cause:e})}),i.push(r))}}let o=()=>{n.reload&&window.location&&e!==r&&A(void 0)};if(i.length)return Promise.all(i).then(()=>{o()});o()};function M(e){if(typeof e!=`string`)return;let t=e.toLowerCase();for(let e of y)if(e.toLowerCase()===t)return e}function N(e){let t=M(e);if(t)return t;throw Error(`Invalid locale: ${e}. Expected one of: ${y.join(`, `)}`)}var P=b.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`);RegExp(`(?:^|;\\s*)${P}=([^;]*)`);var F=new Map;function I(e){return typeof e==`string`&&/^custom-[A-Za-z0-9_-]+$/.test(e)}var L=()=>`End Chat`,R=()=>`إنهاء المحادثة`,z=((e={},t={})=>(t.locale??O())===`en`?L(e):R(e)),B=()=>`Are you sure you want to end this chat?`,V=()=>`هل أنت متأكد أنك تريد إنهاء هذه المحادثة؟`,H=((e={},t={})=>(t.locale??O())===`en`?B(e):V(e)),U=()=>`End Chat`,W=()=>`إنهاء المحادثة`,G=((e={},t={})=>(t.locale??O())===`en`?U(e):W(e)),K=()=>`Go to Checkout`,ee=()=>`الذهاب للدفع`,q=((e={},t={})=>(t.locale??O())===`en`?K(e):ee(e)),J=()=>`You will leave this chat and continue to the store's checkout page.`,Y=()=>`سوف تغادر هذه المحادثة وتنتقل إلى صفحة الدفع في المتجر.`,X=((e={},t={})=>(t.locale??O())===`en`?J(e):Y(e)),Z=()=>`Continue`,Q=()=>`متابعة`,$=((e={},t={})=>(t.locale??O())===`en`?Z(e):Q(e)),te=()=>`Cancel`,ne=()=>`إلغاء`,re=((e={},t={})=>(t.locale??O())===`en`?te(e):ne(e)),ie=()=>`Close`,ae=()=>`إغلاق`,oe=((e={},t={})=>(t.locale??O())===`en`?ie(e):ae(e)),se=new Set(y);function ce(e){return e&&se.has(e)?e:`en`}function le(e){return e===`ar`?`rtl`:`ltr`}function ue(e,t){let n={cancelLabel:re({},{locale:t}),closeLabel:oe({},{locale:t}),dir:le(t)};switch(e){case`checkout`:return{...n,title:q({},{locale:t}),message:X({},{locale:t}),confirmLabel:$({},{locale:t}),tone:`primary`};case`endChat`:return{...n,title:z({},{locale:t}),message:H({},{locale:t}),confirmLabel:G({},{locale:t}),tone:`destructive`}}}function de(e){let t;try{t=new URL(e,window.location.href)}catch{return null}return t.protocol!==`https:`&&t.protocol!==`http:`?null:t.href}var fe=class{get isDirty(){return this._isDirty}constructor(e){this.bridge=null,this.bus=new t,this.confirmDialog=new f,this.resolvedTheme={},this._isDirty=!1,this.handleBeforeUnload=e=>{this._isDirty&&(e.preventDefault(),e.returnValue=``)},this.handleMessage=e=>{let t=e.data;t.type===`READY`?this.bus.emit(`ready`):t.type===`EVENT`?this.bus.emit(t.event,t.payload):t.type===`DIRTY_STATE`?this._isDirty=t.isDirty:t.type===`CONFIRM_REQUEST`&&this.handleConfirmRequest(t.kind,t.locale,t.url)},this.config=e}mount(){if(this.bridge)return;let t=typeof this.config.container==`string`?document.querySelector(this.config.container):this.config.container;if(!t)throw Error(`Ziila: container not found: ${this.config.container}`);this.bridge=new e(e=>{e.onmessage=this.handleMessage;let{container:t,...n}=this.config,r=_(this.config.theme);this.resolvedTheme=r,this.bridge?.send({type:`INIT`,payload:{...n,theme:r}})},this.config.widgetUrl),this.bridge.mount(t),window.addEventListener(`beforeunload`,this.handleBeforeUnload)}handleConfirmRequest(e,t,n){this.confirmDialog.show({...ue(e,ce(t)),tokens:this.resolvedTheme.tokens}).then(t=>{this.bridge?.send({type:`CONFIRM_RESULT`,kind:e,confirmed:t}),t&&e===`checkout`&&n&&this.navigateToCheckout(n)})}navigateToCheckout(e){let t=de(e);t&&window.location.assign(t)}on(e,t){return this.bus.on(e,t)}off(e,t){this.bus.off(e,t)}emit(e,t){this.bridge?.send({type:`COMMAND`,command:e,payload:t})}update(e){this.config={...this.config,...e},`theme`in e&&(this.resolvedTheme=_(this.config.theme)),this.bridge?.send({type:`UPDATE`,payload:e})}destroy(){window.removeEventListener(`beforeunload`,this.handleBeforeUnload),this.confirmDialog.close(),this.bridge?.destroy(),this.bus.destroy(),this.bridge=null}};function pe(e){return typeof e==`object`&&!!e&&typeof e.code==`string`&&typeof e.message==`string`}exports.ZiilaWidget=fe,exports.isZiilaError=pe;
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=class{constructor(e,t,n=!1){this.iframe=null,this.port=null,this.handleHandshake=e=>{let t=this.iframe?.contentWindow;if(!t||e.source!==t||e.origin!==new URL(this.url).origin)return;let n=e.data;if(typeof n!=`object`||!n||n.type!==`HANDSHAKE`)return;let r=new MessageChannel;t.postMessage({type:`HANDSHAKE_ACK`},new URL(this.url).origin,[r.port2]),this.port=r.port1,this.port.start(),window.removeEventListener(`message`,this.handleHandshake),this.onPortReady(r.port1)},this.onPortReady=e,this.url=t??`https://widget.ziila.com`,this.standalone=n}mount(e){let t=new URL(this.url);t.searchParams.set(`embedded`,this.standalone?`0`:`1`),this.iframe=document.createElement(`iframe`),this.iframe.src=t.toString(),this.iframe.title=`Ziila Shopping Assistant`,this.iframe.setAttribute(`allow`,`microphone; autoplay`),this.iframe.style.cssText=`display:block;width:100%;height:100%;border:none;`,e.appendChild(this.iframe),window.addEventListener(`message`,this.handleHandshake)}send(e){this.port?.postMessage(e)}destroy(){window.removeEventListener(`message`,this.handleHandshake),this.port?.close(),this.iframe?.remove(),this.iframe=null,this.port=null}},t=class{constructor(){this.handlers=new Map}on(e,t){return this.handlers.has(e)||this.handlers.set(e,new Set),this.handlers.get(e).add(t),()=>this.off(e,t)}off(e,t){this.handlers.get(e)?.delete(t)}emit(e,t){this.handlers.get(e)?.forEach(e=>e(t))}destroy(){this.handlers.clear()}},n=e=>`position:fixed;inset:0;z-index:2147483647;display:flex;align-items:center;justify-content:center;background:rgba(0,0,0,0.4);padding:24px;font-family:${e.fontFamily??`system-ui,-apple-system,sans-serif`};`,r=e=>`width:100%;max-width:24rem;border-radius:${e.borderRadius??`12px`};background:${e.backgroundColor??`#fff`};padding:16px;box-shadow:2px 4px 2px rgba(0,0,0,0.1);display:flex;flex-direction:column;gap:16px;outline:none;`,i=`display:flex;justify-content:flex-end;`,a=e=>`border:none;background:${e.mutedColor??`#f0f0f0`};border-radius:${e.buttonRadius??`12px`};width:32px;height:32px;display:flex;align-items:center;justify-content:center;cursor:pointer;font-size:14px;color:${e.mutedFg??`#555`};line-height:1;padding:0;`,o=`display:flex;flex-direction:column;gap:16px;align-items:center;`,s=e=>`margin:0;font-size:24px;font-weight:700;color:${e.primaryColor??`#195f6b`};text-align:center;`,c=e=>`margin:0;font-size:16px;color:${e.foregroundColor??`#2d2d2d`};font-weight:500;line-height:27px;text-align:center;max-width:345px;`,l=`display:flex;gap:16px;padding:0 16px;width:100%;box-sizing:border-box;`,u=e=>`flex:1;border:1px solid ${e.primaryColor??`#195f6b`};border-radius:${e.buttonRadius??`8px`};background:${e.backgroundColor??`#fff`};color:${e.primaryColor??`#195f6b`};padding:16px;font-size:16px;font-weight:${e.buttonFontWeight??`500`};cursor:pointer;`,d=(e,t)=>`flex:1;border:none;border-radius:${e.buttonRadius??`8px`};background:${t===`primary`?e.primaryColor??`#195f6b`:`#cc0000`};color:${t===`primary`?e.primaryFg??`#fff`:`#fff`};padding:16px;font-size:16px;font-weight:${e.buttonFontWeight??`500`};cursor:pointer;`,f=class{constructor(){this.overlay=null,this.pendingResolve=null,this.previousFocus=null}show(e){return this.pendingResolve?.(!1),this.pendingResolve=null,this.close(),new Promise(t=>{this.pendingResolve=t;let f=e=>{this.pendingResolve=null,this.close(),t(e)},p=e.tokens??{},m=document.createElement(`div`);m.style.cssText=n(p);let h=document.createElement(`div`);h.setAttribute(`role`,`dialog`),h.setAttribute(`aria-modal`,`true`),h.setAttribute(`aria-labelledby`,`ziila-confirm-title`),h.tabIndex=-1,h.style.cssText=r(p),h.dir=e.dir;let g=document.createElement(`div`);g.style.cssText=i;let _=document.createElement(`button`);_.type=`button`,_.setAttribute(`aria-label`,e.closeLabel),_.textContent=`✕`,_.style.cssText=a(p),_.addEventListener(`click`,()=>f(!1)),g.appendChild(_);let v=document.createElement(`div`);v.style.cssText=o;let y=document.createElement(`h2`);y.id=`ziila-confirm-title`,y.textContent=e.title,y.style.cssText=s(p);let b=document.createElement(`p`);b.textContent=e.message,b.style.cssText=c(p);let x=document.createElement(`button`);x.type=`button`,x.textContent=e.cancelLabel,x.style.cssText=u(p),x.addEventListener(`click`,()=>f(!1));let S=document.createElement(`button`);S.type=`button`,S.textContent=e.confirmLabel,S.style.cssText=d(p,e.tone??`destructive`),S.addEventListener(`click`,()=>f(!0));let C=document.createElement(`div`);C.style.cssText=l,C.append(x,S),v.append(y,b,C),h.append(g,v),m.appendChild(h);let w=[_,x,S];m.addEventListener(`keydown`,e=>{if(e.key===`Escape`){f(!1);return}if(e.key===`Tab`){e.preventDefault();let t=(w.indexOf(document.activeElement)+(e.shiftKey?-1:1)+w.length)%w.length;w[t].focus()}}),this.previousFocus=document.activeElement,this.overlay=m,document.body.appendChild(m),h.focus()})}close(){this.overlay?.remove(),this.overlay=null,this.previousFocus?.focus(),this.previousFocus=null}},p={primaryColor:[`--primary`,`--primary-color`,`--color-primary`,`--brand`,`--brand-color`,`--accent-color`,`--bs-primary`,`--mui-palette-primary-main`,`--ant-color-primary`],borderRadius:[`--radius`,`--border-radius`,`--rounded`,`--bs-border-radius`]},m=new Set([``,`transparent`,`inherit`,`initial`,`unset`,`none`,`rgba(0, 0, 0, 0)`,`rgb(0, 0, 0)`,`depends on user agent`]);function h(e){return!!e&&!m.has(e.trim().toLowerCase())}function g(e=document){let t=e.defaultView??globalThis,n=t.getComputedStyle(e.documentElement),r=e.body?t.getComputedStyle(e.body):null,i={};r&&(h(r.fontFamily)&&(i.fontFamily=r.fontFamily),h(r.backgroundColor)&&(i.backgroundColor=r.backgroundColor),h(r.color)&&(i.foregroundColor=r.color));for(let[e,t]of Object.entries(p))for(let r of t){let t=n.getPropertyValue(r).trim();if(h(t)){i[e]=t;break}}return i}function _(e,t={}){let n={...g(t.doc??document),...e?.tokens},r={};return Object.keys(n).length>0&&(r.tokens=n),e?.elements&&Object.keys(e.elements).length>0&&(r.elements=e.elements),r}var v={},y=[`en`,`ar`],b=`PARAGLIDE_LOCALE`,x=[`baseLocale`],S=[],C,w;function T(e){if(S.length===0)return;let t=typeof e==`string`?e:e.href;if(C===t)return w;let n=new URL(t,`http://dummy.com`),r;for(let e of S)if(new v(e.match,n.href).exec(n.href)){r=e;break}return C=t,w=r,r}function E(e){let t=T(e);return t&&t.exclude!==!0&&Array.isArray(t.strategy)?t.strategy:x}globalThis.__paraglide=globalThis.__paraglide??{},globalThis.__paraglide.ssr=globalThis.__paraglide.ssr??{};var D=!1,O=()=>{let e=x;typeof window<`u`&&window.location?.href&&(e=E(window.location.href));let t=k(e,typeof window<`u`?window.location?.href:void 0);if(t)return D||(D=!0,j(t,{reload:!1})),t;throw Error(`No locale found. Read the docs https://paraglidejs.com/errors#no-locale-found`)};function k(e,t){let n;for(let t of e){if(t===`baseLocale`)n=`en`;else if(I(t)&&F.has(t)){let e=F.get(t);if(e){let t=e.getLocale();if(t instanceof Promise)continue;if(t!==void 0)return N(t)}}let e=M(n);if(e)return e}}var A=e=>{e?window.location.href=e:window.location.reload()},j=(e,t)=>{let n={reload:!0,...t},r;try{r=O()}catch{}let i=[],a=x;typeof window<`u`&&window.location?.href&&(a=E(window.location.href));for(let t of a)if(t===`baseLocale`)continue;else if(I(t)&&F.has(t)){let n=F.get(t);if(n){let r=n.setLocale(e);r instanceof Promise&&(r=r.catch(e=>{throw Error(`Custom strategy "${t}" setLocale failed.`,{cause:e})}),i.push(r))}}let o=()=>{n.reload&&window.location&&e!==r&&A(void 0)};if(i.length)return Promise.all(i).then(()=>{o()});o()};function M(e){if(typeof e!=`string`)return;let t=e.toLowerCase();for(let e of y)if(e.toLowerCase()===t)return e}function N(e){let t=M(e);if(t)return t;throw Error(`Invalid locale: ${e}. Expected one of: ${y.join(`, `)}`)}var P=b.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`);RegExp(`(?:^|;\\s*)${P}=([^;]*)`);var F=new Map;function I(e){return typeof e==`string`&&/^custom-[A-Za-z0-9_-]+$/.test(e)}var L=()=>`End Chat`,R=()=>`إنهاء المحادثة`,z=((e={},t={})=>(t.locale??O())===`en`?L(e):R(e)),B=()=>`Are you sure you want to end this chat?`,V=()=>`هل أنت متأكد أنك تريد إنهاء هذه المحادثة؟`,H=((e={},t={})=>(t.locale??O())===`en`?B(e):V(e)),U=()=>`End Chat`,W=()=>`إنهاء المحادثة`,G=((e={},t={})=>(t.locale??O())===`en`?U(e):W(e)),K=()=>`Go to Checkout`,ee=()=>`الذهاب للدفع`,q=((e={},t={})=>(t.locale??O())===`en`?K(e):ee(e)),J=()=>`You will leave this chat and continue to the store's checkout page.`,Y=()=>`سوف تغادر هذه المحادثة وتنتقل إلى صفحة الدفع في المتجر.`,X=((e={},t={})=>(t.locale??O())===`en`?J(e):Y(e)),Z=()=>`Continue`,Q=()=>`متابعة`,$=((e={},t={})=>(t.locale??O())===`en`?Z(e):Q(e)),te=()=>`Cancel`,ne=()=>`إلغاء`,re=((e={},t={})=>(t.locale??O())===`en`?te(e):ne(e)),ie=()=>`Close`,ae=()=>`إغلاق`,oe=((e={},t={})=>(t.locale??O())===`en`?ie(e):ae(e)),se=new Set(y);function ce(e){return e&&se.has(e)?e:`en`}function le(e){return e===`ar`?`rtl`:`ltr`}function ue(e,t){let n={cancelLabel:re({},{locale:t}),closeLabel:oe({},{locale:t}),dir:le(t)};switch(e){case`checkout`:return{...n,title:q({},{locale:t}),message:X({},{locale:t}),confirmLabel:$({},{locale:t}),tone:`primary`};case`endChat`:return{...n,title:z({},{locale:t}),message:H({},{locale:t}),confirmLabel:G({},{locale:t}),tone:`destructive`}}}function de(e){let t;try{t=new URL(e,window.location.href)}catch{return null}return t.protocol!==`https:`&&t.protocol!==`http:`?null:t.href}var fe=class{get isDirty(){return this._isDirty}constructor(e){this.bridge=null,this.bus=new t,this.confirmDialog=new f,this.resolvedTheme={},this._isDirty=!1,this.handleBeforeUnload=e=>{this._isDirty&&(e.preventDefault(),e.returnValue=``)},this.handleMessage=e=>{let t=e.data;t.type===`READY`?this.bus.emit(`ready`):t.type===`EVENT`?this.bus.emit(t.event,t.payload):t.type===`DIRTY_STATE`?this._isDirty=t.isDirty:t.type===`CONFIRM_REQUEST`&&this.handleConfirmRequest(t.kind,t.locale,t.url)},this.config=e}mount(){if(this.bridge)return;let t=typeof this.config.container==`string`?document.querySelector(this.config.container):this.config.container;if(!t)throw Error(`Ziila: container not found: ${this.config.container}`);this.bridge=new e(e=>{e.onmessage=this.handleMessage;let{container:t,...n}=this.config,r=_(this.config.theme);this.resolvedTheme=r,this.bridge?.send({type:`INIT`,payload:{...n,theme:r}})},this.config.widgetUrl,this.config.standalone),this.bridge.mount(t),window.addEventListener(`beforeunload`,this.handleBeforeUnload)}handleConfirmRequest(e,t,n){this.confirmDialog.show({...ue(e,ce(t)),tokens:this.resolvedTheme.tokens}).then(t=>{this.bridge?.send({type:`CONFIRM_RESULT`,kind:e,confirmed:t}),t&&e===`checkout`&&n&&this.navigateToCheckout(n)})}navigateToCheckout(e){let t=de(e);t&&window.location.assign(t)}on(e,t){return this.bus.on(e,t)}off(e,t){this.bus.off(e,t)}emit(e,t){this.bridge?.send({type:`COMMAND`,command:e,payload:t})}update(e){this.config={...this.config,...e},`theme`in e&&(this.resolvedTheme=_(this.config.theme)),this.bridge?.send({type:`UPDATE`,payload:e})}destroy(){window.removeEventListener(`beforeunload`,this.handleBeforeUnload),this.confirmDialog.close(),this.bridge?.destroy(),this.bus.destroy(),this.bridge=null}};function pe(e){return typeof e==`object`&&!!e&&typeof e.code==`string`&&typeof e.message==`string`}exports.ZiilaWidget=fe,exports.isZiilaError=pe;
|
package/dist/index.esm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
var e = class {
|
|
2
|
-
constructor(e, t) {
|
|
2
|
+
constructor(e, t, n = !1) {
|
|
3
3
|
this.iframe = null, this.port = null, this.handleHandshake = (e) => {
|
|
4
4
|
let t = this.iframe?.contentWindow;
|
|
5
5
|
if (!t || e.source !== t || e.origin !== new URL(this.url).origin) return;
|
|
@@ -7,11 +7,11 @@ var e = class {
|
|
|
7
7
|
if (typeof n != "object" || !n || n.type !== "HANDSHAKE") return;
|
|
8
8
|
let r = new MessageChannel();
|
|
9
9
|
t.postMessage({ type: "HANDSHAKE_ACK" }, new URL(this.url).origin, [r.port2]), this.port = r.port1, this.port.start(), window.removeEventListener("message", this.handleHandshake), this.onPortReady(r.port1);
|
|
10
|
-
}, this.onPortReady = e, this.url = t ?? "https://widget.ziila.com";
|
|
10
|
+
}, this.onPortReady = e, this.url = t ?? "https://widget.ziila.com", this.standalone = n;
|
|
11
11
|
}
|
|
12
12
|
mount(e) {
|
|
13
13
|
let t = new URL(this.url);
|
|
14
|
-
t.searchParams.set("embedded", "1"), this.iframe = document.createElement("iframe"), this.iframe.src = t.toString(), this.iframe.title = "Ziila Shopping Assistant", this.iframe.setAttribute("allow", "microphone; autoplay"), this.iframe.style.cssText = "display:block;width:100%;height:100%;border:none;", e.appendChild(this.iframe), window.addEventListener("message", this.handleHandshake);
|
|
14
|
+
t.searchParams.set("embedded", this.standalone ? "0" : "1"), this.iframe = document.createElement("iframe"), this.iframe.src = t.toString(), this.iframe.title = "Ziila Shopping Assistant", this.iframe.setAttribute("allow", "microphone; autoplay"), this.iframe.style.cssText = "display:block;width:100%;height:100%;border:none;", e.appendChild(this.iframe), window.addEventListener("message", this.handleHandshake);
|
|
15
15
|
}
|
|
16
16
|
send(e) {
|
|
17
17
|
this.port?.postMessage(e);
|
|
@@ -295,7 +295,7 @@ var fe = class {
|
|
|
295
295
|
theme: r
|
|
296
296
|
}
|
|
297
297
|
});
|
|
298
|
-
}, this.config.widgetUrl), this.bridge.mount(t), window.addEventListener("beforeunload", this.handleBeforeUnload);
|
|
298
|
+
}, this.config.widgetUrl, this.config.standalone), this.bridge.mount(t), window.addEventListener("beforeunload", this.handleBeforeUnload);
|
|
299
299
|
}
|
|
300
300
|
handleConfirmRequest(e, t, n) {
|
|
301
301
|
this.confirmDialog.show({
|
|
@@ -5,7 +5,8 @@ export declare class MessageBridge {
|
|
|
5
5
|
private port;
|
|
6
6
|
private readonly onPortReady;
|
|
7
7
|
private readonly url;
|
|
8
|
-
|
|
8
|
+
private readonly standalone;
|
|
9
|
+
constructor(onPortReady: (port: MessagePort) => void, widgetUrl?: string, standalone?: boolean);
|
|
9
10
|
mount(container: HTMLElement): void;
|
|
10
11
|
private handleHandshake;
|
|
11
12
|
send(message: ParentMessage): void;
|
package/dist/src/types.d.ts
CHANGED
|
@@ -62,8 +62,10 @@ export interface ThemeStyles {
|
|
|
62
62
|
}
|
|
63
63
|
export interface ZiilaConfig {
|
|
64
64
|
apiKey: string;
|
|
65
|
-
/** Merchant cart id
|
|
66
|
-
|
|
65
|
+
/** Merchant cart id. Optional: pass it when the host page already owns a
|
|
66
|
+
* cart, and omit it for a guest who has none yet — the server then mints
|
|
67
|
+
* the cart on first contact and the widget adopts the id it reports. */
|
|
68
|
+
cartId?: string;
|
|
67
69
|
container: string | HTMLElement;
|
|
68
70
|
/** Conversation + UI locale. The widget resolves the merchant's agent for
|
|
69
71
|
* this locale itself (GET /agent) — there is no agent id in the config.
|
|
@@ -72,6 +74,23 @@ export interface ZiilaConfig {
|
|
|
72
74
|
/** Merchant-issued token identifying the signed-in shopper. Optional;
|
|
73
75
|
* when present it is forwarded on the WebSocket handshake. */
|
|
74
76
|
userToken?: string;
|
|
77
|
+
/** The merchant's storefront domain, e.g. `example.com`. Optional; when
|
|
78
|
+
* present it is forwarded on the WebSocket handshake, and the backend
|
|
79
|
+
* resolves how to search that shop from the domain alone. */
|
|
80
|
+
domainName?: string;
|
|
81
|
+
/** The shop's ecommerce platform, e.g. `shopify`. Forwarded on the handshake
|
|
82
|
+
* alongside `domainName`. A string rather than a union: the set is the
|
|
83
|
+
* backend's to extend, and a new platform must not need an SDK release. */
|
|
84
|
+
domainType?: string;
|
|
85
|
+
/** Host page is a bare shell, not a storefront: the widget owns the whole
|
|
86
|
+
* viewport, capping content to a column and rendering its own locale
|
|
87
|
+
* control. It cannot detect such a host itself — a shell is cross-origin. */
|
|
88
|
+
standalone?: boolean;
|
|
89
|
+
/** A showcase, not a shopping session — there is no real cart behind it.
|
|
90
|
+
* Exactly two effects: the header drops its cart button, and a cart change
|
|
91
|
+
* explains itself in the thread instead of reaching the server. Nothing
|
|
92
|
+
* security-relevant may be gated on it: any caller can set it. */
|
|
93
|
+
demo?: boolean;
|
|
75
94
|
widgetTitle?: string;
|
|
76
95
|
theme?: Partial<ThemeStyles>;
|
|
77
96
|
widgetUrl?: string;
|
package/dist/widget.iife.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(){var e=class{constructor(e,t){this.iframe=null,this.port=null,this.handleHandshake=e=>{let t=this.iframe?.contentWindow;if(!t||e.source!==t||e.origin!==new URL(this.url).origin)return;let n=e.data;if(typeof n!=`object`||!n||n.type!==`HANDSHAKE`)return;let r=new MessageChannel;t.postMessage({type:`HANDSHAKE_ACK`},new URL(this.url).origin,[r.port2]),this.port=r.port1,this.port.start(),window.removeEventListener(`message`,this.handleHandshake),this.onPortReady(r.port1)},this.onPortReady=e,this.url=t??`https://widget.ziila.com`}mount(e){let t=new URL(this.url);t.searchParams.set(`embedded`,`1`),this.iframe=document.createElement(`iframe`),this.iframe.src=t.toString(),this.iframe.title=`Ziila Shopping Assistant`,this.iframe.setAttribute(`allow`,`microphone; autoplay`),this.iframe.style.cssText=`display:block;width:100%;height:100%;border:none;`,e.appendChild(this.iframe),window.addEventListener(`message`,this.handleHandshake)}send(e){this.port?.postMessage(e)}destroy(){window.removeEventListener(`message`,this.handleHandshake),this.port?.close(),this.iframe?.remove(),this.iframe=null,this.port=null}},t=class{constructor(){this.handlers=new Map}on(e,t){return this.handlers.has(e)||this.handlers.set(e,new Set),this.handlers.get(e).add(t),()=>this.off(e,t)}off(e,t){this.handlers.get(e)?.delete(t)}emit(e,t){this.handlers.get(e)?.forEach(e=>e(t))}destroy(){this.handlers.clear()}},n=e=>`position:fixed;inset:0;z-index:2147483647;display:flex;align-items:center;justify-content:center;background:rgba(0,0,0,0.4);padding:24px;font-family:${e.fontFamily??`system-ui,-apple-system,sans-serif`};`,r=e=>`width:100%;max-width:24rem;border-radius:${e.borderRadius??`12px`};background:${e.backgroundColor??`#fff`};padding:16px;box-shadow:2px 4px 2px rgba(0,0,0,0.1);display:flex;flex-direction:column;gap:16px;outline:none;`,i=`display:flex;justify-content:flex-end;`,a=e=>`border:none;background:${e.mutedColor??`#f0f0f0`};border-radius:${e.buttonRadius??`12px`};width:32px;height:32px;display:flex;align-items:center;justify-content:center;cursor:pointer;font-size:14px;color:${e.mutedFg??`#555`};line-height:1;padding:0;`,o=`display:flex;flex-direction:column;gap:16px;align-items:center;`,ee=e=>`margin:0;font-size:24px;font-weight:700;color:${e.primaryColor??`#195f6b`};text-align:center;`,s=e=>`margin:0;font-size:16px;color:${e.foregroundColor??`#2d2d2d`};font-weight:500;line-height:27px;text-align:center;max-width:345px;`,c=`display:flex;gap:16px;padding:0 16px;width:100%;box-sizing:border-box;`,te=e=>`flex:1;border:1px solid ${e.primaryColor??`#195f6b`};border-radius:${e.buttonRadius??`8px`};background:${e.backgroundColor??`#fff`};color:${e.primaryColor??`#195f6b`};padding:16px;font-size:16px;font-weight:${e.buttonFontWeight??`500`};cursor:pointer;`,ne=(e,t)=>`flex:1;border:none;border-radius:${e.buttonRadius??`8px`};background:${t===`primary`?e.primaryColor??`#195f6b`:`#cc0000`};color:${t===`primary`?e.primaryFg??`#fff`:`#fff`};padding:16px;font-size:16px;font-weight:${e.buttonFontWeight??`500`};cursor:pointer;`,l=class{constructor(){this.overlay=null,this.pendingResolve=null,this.previousFocus=null}show(e){return this.pendingResolve?.(!1),this.pendingResolve=null,this.close(),new Promise(t=>{this.pendingResolve=t;let l=e=>{this.pendingResolve=null,this.close(),t(e)},u=e.tokens??{},d=document.createElement(`div`);d.style.cssText=n(u);let f=document.createElement(`div`);f.setAttribute(`role`,`dialog`),f.setAttribute(`aria-modal`,`true`),f.setAttribute(`aria-labelledby`,`ziila-confirm-title`),f.tabIndex=-1,f.style.cssText=r(u),f.dir=e.dir;let p=document.createElement(`div`);p.style.cssText=i;let m=document.createElement(`button`);m.type=`button`,m.setAttribute(`aria-label`,e.closeLabel),m.textContent=`✕`,m.style.cssText=a(u),m.addEventListener(`click`,()=>l(!1)),p.appendChild(m);let h=document.createElement(`div`);h.style.cssText=o;let g=document.createElement(`h2`);g.id=`ziila-confirm-title`,g.textContent=e.title,g.style.cssText=ee(u);let _=document.createElement(`p`);_.textContent=e.message,_.style.cssText=s(u);let v=document.createElement(`button`);v.type=`button`,v.textContent=e.cancelLabel,v.style.cssText=te(u),v.addEventListener(`click`,()=>l(!1));let y=document.createElement(`button`);y.type=`button`,y.textContent=e.confirmLabel,y.style.cssText=ne(u,e.tone??`destructive`),y.addEventListener(`click`,()=>l(!0));let b=document.createElement(`div`);b.style.cssText=c,b.append(v,y),h.append(g,_,b),f.append(p,h),d.appendChild(f);let x=[m,v,y];d.addEventListener(`keydown`,e=>{if(e.key===`Escape`){l(!1);return}if(e.key===`Tab`){e.preventDefault();let t=(x.indexOf(document.activeElement)+(e.shiftKey?-1:1)+x.length)%x.length;x[t].focus()}}),this.previousFocus=document.activeElement,this.overlay=d,document.body.appendChild(d),f.focus()})}close(){this.overlay?.remove(),this.overlay=null,this.previousFocus?.focus(),this.previousFocus=null}},u={primaryColor:[`--primary`,`--primary-color`,`--color-primary`,`--brand`,`--brand-color`,`--accent-color`,`--bs-primary`,`--mui-palette-primary-main`,`--ant-color-primary`],borderRadius:[`--radius`,`--border-radius`,`--rounded`,`--bs-border-radius`]},d=new Set([``,`transparent`,`inherit`,`initial`,`unset`,`none`,`rgba(0, 0, 0, 0)`,`rgb(0, 0, 0)`,`depends on user agent`]);function f(e){return!!e&&!d.has(e.trim().toLowerCase())}function p(e=document){let t=e.defaultView??globalThis,n=t.getComputedStyle(e.documentElement),r=e.body?t.getComputedStyle(e.body):null,i={};r&&(f(r.fontFamily)&&(i.fontFamily=r.fontFamily),f(r.backgroundColor)&&(i.backgroundColor=r.backgroundColor),f(r.color)&&(i.foregroundColor=r.color));for(let[e,t]of Object.entries(u))for(let r of t){let t=n.getPropertyValue(r).trim();if(f(t)){i[e]=t;break}}return i}function m(e,t={}){let n={...p(t.doc??document),...e?.tokens},r={};return Object.keys(n).length>0&&(r.tokens=n),e?.elements&&Object.keys(e.elements).length>0&&(r.elements=e.elements),r}var h={},g=[`en`,`ar`],_=`PARAGLIDE_LOCALE`,v=[`baseLocale`],y=[],b,x;function S(e){if(y.length===0)return;let t=typeof e==`string`?e:e.href;if(b===t)return x;let n=new URL(t,`http://dummy.com`),r;for(let e of y)if(new h(e.match,n.href).exec(n.href)){r=e;break}return b=t,x=r,r}function C(e){let t=S(e);return t&&t.exclude!==!0&&Array.isArray(t.strategy)?t.strategy:v}globalThis.__paraglide=globalThis.__paraglide??{},globalThis.__paraglide.ssr=globalThis.__paraglide.ssr??{};var w=!1,T=()=>{let e=v;typeof window<`u`&&window.location?.href&&(e=C(window.location.href));let t=re(e,typeof window<`u`?window.location?.href:void 0);if(t)return w||(w=!0,E(t,{reload:!1})),t;throw Error(`No locale found. Read the docs https://paraglidejs.com/errors#no-locale-found`)};function re(e,t){let n;for(let t of e){if(t===`baseLocale`)n=`en`;else if(j(t)&&A.has(t)){let e=A.get(t);if(e){let t=e.getLocale();if(t instanceof Promise)continue;if(t!==void 0)return O(t)}}let e=D(n);if(e)return e}}var ie=e=>{e?window.location.href=e:window.location.reload()},E=(e,t)=>{let n={reload:!0,...t},r;try{r=T()}catch{}let i=[],a=v;typeof window<`u`&&window.location?.href&&(a=C(window.location.href));for(let t of a)if(t===`baseLocale`)continue;else if(j(t)&&A.has(t)){let n=A.get(t);if(n){let r=n.setLocale(e);r instanceof Promise&&(r=r.catch(e=>{throw Error(`Custom strategy "${t}" setLocale failed.`,{cause:e})}),i.push(r))}}let o=()=>{n.reload&&window.location&&e!==r&&ie(void 0)};if(i.length)return Promise.all(i).then(()=>{o()});o()};function D(e){if(typeof e!=`string`)return;let t=e.toLowerCase();for(let e of g)if(e.toLowerCase()===t)return e}function O(e){let t=D(e);if(t)return t;throw Error(`Invalid locale: ${e}. Expected one of: ${g.join(`, `)}`)}var k=_.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`);RegExp(`(?:^|;\\s*)${k}=([^;]*)`);var A=new Map;function j(e){return typeof e==`string`&&/^custom-[A-Za-z0-9_-]+$/.test(e)}var M=()=>`End Chat`,N=()=>`إنهاء المحادثة`,P=((e={},t={})=>(t.locale??T())===`en`?M(e):N(e)),F=()=>`Are you sure you want to end this chat?`,I=()=>`هل أنت متأكد أنك تريد إنهاء هذه المحادثة؟`,L=((e={},t={})=>(t.locale??T())===`en`?F(e):I(e)),R=()=>`End Chat`,z=()=>`إنهاء المحادثة`,B=((e={},t={})=>(t.locale??T())===`en`?R(e):z(e)),V=()=>`Go to Checkout`,H=()=>`الذهاب للدفع`,U=((e={},t={})=>(t.locale??T())===`en`?V(e):H(e)),W=()=>`You will leave this chat and continue to the store's checkout page.`,G=()=>`سوف تغادر هذه المحادثة وتنتقل إلى صفحة الدفع في المتجر.`,ae=((e={},t={})=>(t.locale??T())===`en`?W(e):G(e)),K=()=>`Continue`,q=()=>`متابعة`,J=((e={},t={})=>(t.locale??T())===`en`?K(e):q(e)),oe=()=>`Cancel`,se=()=>`إلغاء`,ce=((e={},t={})=>(t.locale??T())===`en`?oe(e):se(e)),le=()=>`Close`,ue=()=>`إغلاق`,de=((e={},t={})=>(t.locale??T())===`en`?le(e):ue(e)),fe=new Set(g);function pe(e){return e&&fe.has(e)?e:`en`}function me(e){return e===`ar`?`rtl`:`ltr`}function he(e,t){let n={cancelLabel:ce({},{locale:t}),closeLabel:de({},{locale:t}),dir:me(t)};switch(e){case`checkout`:return{...n,title:U({},{locale:t}),message:ae({},{locale:t}),confirmLabel:J({},{locale:t}),tone:`primary`};case`endChat`:return{...n,title:P({},{locale:t}),message:L({},{locale:t}),confirmLabel:B({},{locale:t}),tone:`destructive`}}}function ge(e){let t;try{t=new URL(e,window.location.href)}catch{return null}return t.protocol!==`https:`&&t.protocol!==`http:`?null:t.href}var _e=class{get isDirty(){return this._isDirty}constructor(e){this.bridge=null,this.bus=new t,this.confirmDialog=new l,this.resolvedTheme={},this._isDirty=!1,this.handleBeforeUnload=e=>{this._isDirty&&(e.preventDefault(),e.returnValue=``)},this.handleMessage=e=>{let t=e.data;t.type===`READY`?this.bus.emit(`ready`):t.type===`EVENT`?this.bus.emit(t.event,t.payload):t.type===`DIRTY_STATE`?this._isDirty=t.isDirty:t.type===`CONFIRM_REQUEST`&&this.handleConfirmRequest(t.kind,t.locale,t.url)},this.config=e}mount(){if(this.bridge)return;let t=typeof this.config.container==`string`?document.querySelector(this.config.container):this.config.container;if(!t)throw Error(`Ziila: container not found: ${this.config.container}`);this.bridge=new e(e=>{e.onmessage=this.handleMessage;let{container:t,...n}=this.config,r=m(this.config.theme);this.resolvedTheme=r,this.bridge?.send({type:`INIT`,payload:{...n,theme:r}})},this.config.widgetUrl),this.bridge.mount(t),window.addEventListener(`beforeunload`,this.handleBeforeUnload)}handleConfirmRequest(e,t,n){this.confirmDialog.show({...he(e,pe(t)),tokens:this.resolvedTheme.tokens}).then(t=>{this.bridge?.send({type:`CONFIRM_RESULT`,kind:e,confirmed:t}),t&&e===`checkout`&&n&&this.navigateToCheckout(n)})}navigateToCheckout(e){let t=ge(e);t&&window.location.assign(t)}on(e,t){return this.bus.on(e,t)}off(e,t){this.bus.off(e,t)}emit(e,t){this.bridge?.send({type:`COMMAND`,command:e,payload:t})}update(e){this.config={...this.config,...e},`theme`in e&&(this.resolvedTheme=m(this.config.theme)),this.bridge?.send({type:`UPDATE`,payload:e})}destroy(){window.removeEventListener(`beforeunload`,this.handleBeforeUnload),this.confirmDialog.close(),this.bridge?.destroy(),this.bus.destroy(),this.bridge=null}},Y=null,ve=window.Ziila?.q??[],X=document.currentScript,Z=X instanceof HTMLScriptElement&&X.src?new URL(`..`,X.src).toString():void 0;function Q(e){Y?.destroy(),Y=new _e(e.widgetUrl?e:{...e,widgetUrl:Z}),Y.mount()}var $=function(e){Q(e)};$.on=function(e,t){if(!Y)throw Error(`Ziila: call Ziila(config) before Ziila.on()`);return Y.on(e,t)},$.off=function(e,t){Y?.off(e,t)},$.emit=function(e,t){Y?.emit(e,t)},$.update=function(e){Y?.update(e)},$.mount=function(){Y?.mount()},$.destroy=function(){Y?.destroy(),Y=null},Object.defineProperty($,"isDirty",{get:()=>Y?.isDirty??!1,enumerable:!0,configurable:!1}),window.Ziila=$,ve.forEach(Q)})();
|
|
1
|
+
(function(){var e=class{constructor(e,t,n=!1){this.iframe=null,this.port=null,this.handleHandshake=e=>{let t=this.iframe?.contentWindow;if(!t||e.source!==t||e.origin!==new URL(this.url).origin)return;let n=e.data;if(typeof n!=`object`||!n||n.type!==`HANDSHAKE`)return;let r=new MessageChannel;t.postMessage({type:`HANDSHAKE_ACK`},new URL(this.url).origin,[r.port2]),this.port=r.port1,this.port.start(),window.removeEventListener(`message`,this.handleHandshake),this.onPortReady(r.port1)},this.onPortReady=e,this.url=t??`https://widget.ziila.com`,this.standalone=n}mount(e){let t=new URL(this.url);t.searchParams.set(`embedded`,this.standalone?`0`:`1`),this.iframe=document.createElement(`iframe`),this.iframe.src=t.toString(),this.iframe.title=`Ziila Shopping Assistant`,this.iframe.setAttribute(`allow`,`microphone; autoplay`),this.iframe.style.cssText=`display:block;width:100%;height:100%;border:none;`,e.appendChild(this.iframe),window.addEventListener(`message`,this.handleHandshake)}send(e){this.port?.postMessage(e)}destroy(){window.removeEventListener(`message`,this.handleHandshake),this.port?.close(),this.iframe?.remove(),this.iframe=null,this.port=null}},t=class{constructor(){this.handlers=new Map}on(e,t){return this.handlers.has(e)||this.handlers.set(e,new Set),this.handlers.get(e).add(t),()=>this.off(e,t)}off(e,t){this.handlers.get(e)?.delete(t)}emit(e,t){this.handlers.get(e)?.forEach(e=>e(t))}destroy(){this.handlers.clear()}},n=e=>`position:fixed;inset:0;z-index:2147483647;display:flex;align-items:center;justify-content:center;background:rgba(0,0,0,0.4);padding:24px;font-family:${e.fontFamily??`system-ui,-apple-system,sans-serif`};`,r=e=>`width:100%;max-width:24rem;border-radius:${e.borderRadius??`12px`};background:${e.backgroundColor??`#fff`};padding:16px;box-shadow:2px 4px 2px rgba(0,0,0,0.1);display:flex;flex-direction:column;gap:16px;outline:none;`,i=`display:flex;justify-content:flex-end;`,a=e=>`border:none;background:${e.mutedColor??`#f0f0f0`};border-radius:${e.buttonRadius??`12px`};width:32px;height:32px;display:flex;align-items:center;justify-content:center;cursor:pointer;font-size:14px;color:${e.mutedFg??`#555`};line-height:1;padding:0;`,o=`display:flex;flex-direction:column;gap:16px;align-items:center;`,ee=e=>`margin:0;font-size:24px;font-weight:700;color:${e.primaryColor??`#195f6b`};text-align:center;`,s=e=>`margin:0;font-size:16px;color:${e.foregroundColor??`#2d2d2d`};font-weight:500;line-height:27px;text-align:center;max-width:345px;`,c=`display:flex;gap:16px;padding:0 16px;width:100%;box-sizing:border-box;`,te=e=>`flex:1;border:1px solid ${e.primaryColor??`#195f6b`};border-radius:${e.buttonRadius??`8px`};background:${e.backgroundColor??`#fff`};color:${e.primaryColor??`#195f6b`};padding:16px;font-size:16px;font-weight:${e.buttonFontWeight??`500`};cursor:pointer;`,ne=(e,t)=>`flex:1;border:none;border-radius:${e.buttonRadius??`8px`};background:${t===`primary`?e.primaryColor??`#195f6b`:`#cc0000`};color:${t===`primary`?e.primaryFg??`#fff`:`#fff`};padding:16px;font-size:16px;font-weight:${e.buttonFontWeight??`500`};cursor:pointer;`,l=class{constructor(){this.overlay=null,this.pendingResolve=null,this.previousFocus=null}show(e){return this.pendingResolve?.(!1),this.pendingResolve=null,this.close(),new Promise(t=>{this.pendingResolve=t;let l=e=>{this.pendingResolve=null,this.close(),t(e)},u=e.tokens??{},d=document.createElement(`div`);d.style.cssText=n(u);let f=document.createElement(`div`);f.setAttribute(`role`,`dialog`),f.setAttribute(`aria-modal`,`true`),f.setAttribute(`aria-labelledby`,`ziila-confirm-title`),f.tabIndex=-1,f.style.cssText=r(u),f.dir=e.dir;let p=document.createElement(`div`);p.style.cssText=i;let m=document.createElement(`button`);m.type=`button`,m.setAttribute(`aria-label`,e.closeLabel),m.textContent=`✕`,m.style.cssText=a(u),m.addEventListener(`click`,()=>l(!1)),p.appendChild(m);let h=document.createElement(`div`);h.style.cssText=o;let g=document.createElement(`h2`);g.id=`ziila-confirm-title`,g.textContent=e.title,g.style.cssText=ee(u);let _=document.createElement(`p`);_.textContent=e.message,_.style.cssText=s(u);let v=document.createElement(`button`);v.type=`button`,v.textContent=e.cancelLabel,v.style.cssText=te(u),v.addEventListener(`click`,()=>l(!1));let y=document.createElement(`button`);y.type=`button`,y.textContent=e.confirmLabel,y.style.cssText=ne(u,e.tone??`destructive`),y.addEventListener(`click`,()=>l(!0));let b=document.createElement(`div`);b.style.cssText=c,b.append(v,y),h.append(g,_,b),f.append(p,h),d.appendChild(f);let x=[m,v,y];d.addEventListener(`keydown`,e=>{if(e.key===`Escape`){l(!1);return}if(e.key===`Tab`){e.preventDefault();let t=(x.indexOf(document.activeElement)+(e.shiftKey?-1:1)+x.length)%x.length;x[t].focus()}}),this.previousFocus=document.activeElement,this.overlay=d,document.body.appendChild(d),f.focus()})}close(){this.overlay?.remove(),this.overlay=null,this.previousFocus?.focus(),this.previousFocus=null}},u={primaryColor:[`--primary`,`--primary-color`,`--color-primary`,`--brand`,`--brand-color`,`--accent-color`,`--bs-primary`,`--mui-palette-primary-main`,`--ant-color-primary`],borderRadius:[`--radius`,`--border-radius`,`--rounded`,`--bs-border-radius`]},d=new Set([``,`transparent`,`inherit`,`initial`,`unset`,`none`,`rgba(0, 0, 0, 0)`,`rgb(0, 0, 0)`,`depends on user agent`]);function f(e){return!!e&&!d.has(e.trim().toLowerCase())}function p(e=document){let t=e.defaultView??globalThis,n=t.getComputedStyle(e.documentElement),r=e.body?t.getComputedStyle(e.body):null,i={};r&&(f(r.fontFamily)&&(i.fontFamily=r.fontFamily),f(r.backgroundColor)&&(i.backgroundColor=r.backgroundColor),f(r.color)&&(i.foregroundColor=r.color));for(let[e,t]of Object.entries(u))for(let r of t){let t=n.getPropertyValue(r).trim();if(f(t)){i[e]=t;break}}return i}function m(e,t={}){let n={...p(t.doc??document),...e?.tokens},r={};return Object.keys(n).length>0&&(r.tokens=n),e?.elements&&Object.keys(e.elements).length>0&&(r.elements=e.elements),r}var h={},g=[`en`,`ar`],_=`PARAGLIDE_LOCALE`,v=[`baseLocale`],y=[],b,x;function S(e){if(y.length===0)return;let t=typeof e==`string`?e:e.href;if(b===t)return x;let n=new URL(t,`http://dummy.com`),r;for(let e of y)if(new h(e.match,n.href).exec(n.href)){r=e;break}return b=t,x=r,r}function C(e){let t=S(e);return t&&t.exclude!==!0&&Array.isArray(t.strategy)?t.strategy:v}globalThis.__paraglide=globalThis.__paraglide??{},globalThis.__paraglide.ssr=globalThis.__paraglide.ssr??{};var w=!1,T=()=>{let e=v;typeof window<`u`&&window.location?.href&&(e=C(window.location.href));let t=re(e,typeof window<`u`?window.location?.href:void 0);if(t)return w||(w=!0,E(t,{reload:!1})),t;throw Error(`No locale found. Read the docs https://paraglidejs.com/errors#no-locale-found`)};function re(e,t){let n;for(let t of e){if(t===`baseLocale`)n=`en`;else if(j(t)&&A.has(t)){let e=A.get(t);if(e){let t=e.getLocale();if(t instanceof Promise)continue;if(t!==void 0)return O(t)}}let e=D(n);if(e)return e}}var ie=e=>{e?window.location.href=e:window.location.reload()},E=(e,t)=>{let n={reload:!0,...t},r;try{r=T()}catch{}let i=[],a=v;typeof window<`u`&&window.location?.href&&(a=C(window.location.href));for(let t of a)if(t===`baseLocale`)continue;else if(j(t)&&A.has(t)){let n=A.get(t);if(n){let r=n.setLocale(e);r instanceof Promise&&(r=r.catch(e=>{throw Error(`Custom strategy "${t}" setLocale failed.`,{cause:e})}),i.push(r))}}let o=()=>{n.reload&&window.location&&e!==r&&ie(void 0)};if(i.length)return Promise.all(i).then(()=>{o()});o()};function D(e){if(typeof e!=`string`)return;let t=e.toLowerCase();for(let e of g)if(e.toLowerCase()===t)return e}function O(e){let t=D(e);if(t)return t;throw Error(`Invalid locale: ${e}. Expected one of: ${g.join(`, `)}`)}var k=_.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`);RegExp(`(?:^|;\\s*)${k}=([^;]*)`);var A=new Map;function j(e){return typeof e==`string`&&/^custom-[A-Za-z0-9_-]+$/.test(e)}var M=()=>`End Chat`,N=()=>`إنهاء المحادثة`,P=((e={},t={})=>(t.locale??T())===`en`?M(e):N(e)),F=()=>`Are you sure you want to end this chat?`,I=()=>`هل أنت متأكد أنك تريد إنهاء هذه المحادثة؟`,L=((e={},t={})=>(t.locale??T())===`en`?F(e):I(e)),R=()=>`End Chat`,z=()=>`إنهاء المحادثة`,B=((e={},t={})=>(t.locale??T())===`en`?R(e):z(e)),V=()=>`Go to Checkout`,H=()=>`الذهاب للدفع`,U=((e={},t={})=>(t.locale??T())===`en`?V(e):H(e)),W=()=>`You will leave this chat and continue to the store's checkout page.`,G=()=>`سوف تغادر هذه المحادثة وتنتقل إلى صفحة الدفع في المتجر.`,ae=((e={},t={})=>(t.locale??T())===`en`?W(e):G(e)),K=()=>`Continue`,q=()=>`متابعة`,J=((e={},t={})=>(t.locale??T())===`en`?K(e):q(e)),oe=()=>`Cancel`,se=()=>`إلغاء`,ce=((e={},t={})=>(t.locale??T())===`en`?oe(e):se(e)),le=()=>`Close`,ue=()=>`إغلاق`,de=((e={},t={})=>(t.locale??T())===`en`?le(e):ue(e)),fe=new Set(g);function pe(e){return e&&fe.has(e)?e:`en`}function me(e){return e===`ar`?`rtl`:`ltr`}function he(e,t){let n={cancelLabel:ce({},{locale:t}),closeLabel:de({},{locale:t}),dir:me(t)};switch(e){case`checkout`:return{...n,title:U({},{locale:t}),message:ae({},{locale:t}),confirmLabel:J({},{locale:t}),tone:`primary`};case`endChat`:return{...n,title:P({},{locale:t}),message:L({},{locale:t}),confirmLabel:B({},{locale:t}),tone:`destructive`}}}function ge(e){let t;try{t=new URL(e,window.location.href)}catch{return null}return t.protocol!==`https:`&&t.protocol!==`http:`?null:t.href}var _e=class{get isDirty(){return this._isDirty}constructor(e){this.bridge=null,this.bus=new t,this.confirmDialog=new l,this.resolvedTheme={},this._isDirty=!1,this.handleBeforeUnload=e=>{this._isDirty&&(e.preventDefault(),e.returnValue=``)},this.handleMessage=e=>{let t=e.data;t.type===`READY`?this.bus.emit(`ready`):t.type===`EVENT`?this.bus.emit(t.event,t.payload):t.type===`DIRTY_STATE`?this._isDirty=t.isDirty:t.type===`CONFIRM_REQUEST`&&this.handleConfirmRequest(t.kind,t.locale,t.url)},this.config=e}mount(){if(this.bridge)return;let t=typeof this.config.container==`string`?document.querySelector(this.config.container):this.config.container;if(!t)throw Error(`Ziila: container not found: ${this.config.container}`);this.bridge=new e(e=>{e.onmessage=this.handleMessage;let{container:t,...n}=this.config,r=m(this.config.theme);this.resolvedTheme=r,this.bridge?.send({type:`INIT`,payload:{...n,theme:r}})},this.config.widgetUrl,this.config.standalone),this.bridge.mount(t),window.addEventListener(`beforeunload`,this.handleBeforeUnload)}handleConfirmRequest(e,t,n){this.confirmDialog.show({...he(e,pe(t)),tokens:this.resolvedTheme.tokens}).then(t=>{this.bridge?.send({type:`CONFIRM_RESULT`,kind:e,confirmed:t}),t&&e===`checkout`&&n&&this.navigateToCheckout(n)})}navigateToCheckout(e){let t=ge(e);t&&window.location.assign(t)}on(e,t){return this.bus.on(e,t)}off(e,t){this.bus.off(e,t)}emit(e,t){this.bridge?.send({type:`COMMAND`,command:e,payload:t})}update(e){this.config={...this.config,...e},`theme`in e&&(this.resolvedTheme=m(this.config.theme)),this.bridge?.send({type:`UPDATE`,payload:e})}destroy(){window.removeEventListener(`beforeunload`,this.handleBeforeUnload),this.confirmDialog.close(),this.bridge?.destroy(),this.bus.destroy(),this.bridge=null}},Y=null,ve=window.Ziila?.q??[],X=document.currentScript,Z=X instanceof HTMLScriptElement&&X.src?new URL(`..`,X.src).toString():void 0;function Q(e){Y?.destroy(),Y=new _e(e.widgetUrl?e:{...e,widgetUrl:Z}),Y.mount()}var $=function(e){Q(e)};$.on=function(e,t){if(!Y)throw Error(`Ziila: call Ziila(config) before Ziila.on()`);return Y.on(e,t)},$.off=function(e,t){Y?.off(e,t)},$.emit=function(e,t){Y?.emit(e,t)},$.update=function(e){Y?.update(e)},$.mount=function(){Y?.mount()},$.destroy=function(){Y?.destroy(),Y=null},Object.defineProperty($,"isDirty",{get:()=>Y?.isDirty??!1,enumerable:!0,configurable:!1}),window.Ziila=$,ve.forEach(Q)})();
|