1import{k as d,G as U,T as _,A as f}from"./entry.68c30b5d.js";function S(h={autoShow:!0,initImmediately:!1}){const u=d(!1),i=d(!1),l=d(!1);let s=d(null);try{const e=useCustomerAccount==null?void 0:useCustomerAccount();e!=null&&e.customer&&(s=e.customer)}catch{console.debug("ð useCustomerAccount not available, running Zendesk in anonymous mode")}const m=async()=>{const e=document.createElement("script");e.id="ze-snippet",e.src="https://static.zdassets.com/ekr/snippet.js?key=639aeb37-4e68-4cea-8f8f-1761b2110f3a",e.async=!0,document.body.appendChild(e),e.onload=async()=>{if(u.value=!0,window.zE)try{window.zE("messenger","hide"),console.debug("â
Zendesk Messenger detected"),k(),await r()}catch(n){console.error("Zendesk Messenger not available:",n)}}};h.initImmediately?m():U(()=>{m()}),_(()=>s.value,async(e,n)=>{u.value&&e&&(e!=null&&e.email)&&(e!=null&&e.shopify_customer_id)&&!i.value&&n!==e&&(console.debug("ð¤ Customer data loaded, auto-authenticating with Zendesk"),await r())},{immediate:!0});const r=async()=>{var e,n,o,t;if(s.value&&((e=s.value)!=null&&e.email)&&((n=s.value)!=null&&n.shopify_customer_id)&&!i.value?(console.debug("ð Auto-authenticating logged-in user"),console.debug("ð§ Email:",s.value.email),console.debug("ð Customer ID:",s.value.shopify_customer_id),await w({email:s.value.email,customerId:s.value.shopify_customer_id,firstName:s.value.first_name})):!s.value||!((o=s.value)!=null&&o.email)||!((t=s.value)!=null&&t.shopify_customer_id)?console.debug("ð No customer data available, Zendesk will run in anonymous mode"):console.debug("â
User already authenticated with Zendesk"),h.autoShow)try{window.zE("messenger","show"),console.debug("ðï¸ Zendesk widget is now visible")}catch(c){console.error("Error showing Zendesk widget:",c)}else console.debug("ð autoShow disabled, Zendesk widget will remain hidden")},k=()=>{console.debug("ð§ Setting up Zendesk Messenger event listeners...");try{window.zE("messenger:on","loginSuccessful",()=>{console.debug("ð Zendesk: Login successful"),console.debug("ð Ultimate.ai getUser action should now have access to authenticated user data")}),window.zE("messenger:on","loginFailed",e=>{console.debug("â Zendesk: Login failed",e)}),window.zE("messenger:on","open",()=>{console.debug("ð Zendesk: Widget opened")}),window.zE("messenger:on","close",()=>{console.debug("ð Zendesk: Widget closed")}),window.zE("messenger:on","unreadCountChanged",e=>{console.debug("ð¬ Zendesk: Unread count changed:",e)}),console.debug("â
Zendesk Messenger event listeners set up")}catch(e){console.debug("â ï¸ Some Zendesk event listeners failed to set up:",e)}},b=async(e,n,o)=>{try{return{token:(await $fetch("/api/zendesk-auth",{method:"POST",body:{email:e,customerId:n,firstName:o}})).token}}catch(t){return console.debug("Zendesk JWT authentication not available (likely in ecommerce app):",t.message),{token:null}}},g=async e=>{console.debug("â
User authenticated with data:",e)},v=async(e,n,o)=>{if(!(!window.zE||l.value))try{l.value=!0;const{token:t}=await b(e,n,o);if(!t){console.debug("ð No JWT token available, user will remain anonymous"),i.value=!1;return}console.debug("ð§ About to call loginUser with token length:",t.length),window.zE("messenger","loginUser",function(a){console.debug("ð§ JWT callback executed, providing token"),a(t)},function(a){if(a)console.error("Zendesk login failed:",a),console.error("Error details:",{type:a.type,reason:a.reason,message:a.message}),i.value=!1;else{console.debug("â
Zendesk login callback successful"),console.debug("ð Ultimate.ai Debug - Authentication successful!"),console.debug("ð JWT payload contained:",{external_id:n,user_email:e,user_first_name:o,name:o||e.split("@")[0]}),console.debug("ð Ultimate.ai getUser action should now work with variables:"),console.debug("ð - {{customerEmail}} should be:",e),console.debug("ð - {{customerName}} should be:",o||e.split("@")[0]),console.debug("ð - {{customerExternalId}} should be:",n);const z={name:o||e.split("@")[0],email:e,customerId:n};g(z),i.value=!0,console.debug("â
Authentication completed successfully")}})}catch(t){console.error("Failed to authenticate with Zendesk:",t),i.value=!1}finally{l.value=!1}},y=async e=>{var n,o;if(window.zE){e==null||e.preventDefault(),s.value&&((n=s.value)!=null&&n.email)&&((o=s.value)!=null&&o.shopify_customer_id)&&!i.value&&!l.value&&(console.debug("ð User is logged in but not authenticated, attempting authentication..."),await r());try{window.zE("messenger","show"),window.zE("messenger","open"),console.debug("ð Opened Zen
1desk widget")}catch(t){console.error("Error showing Zendesk messenger:",t)}}},p=()=>{if(window.zE)try{window.zE("messenger","hide")}catch(e){console.error("Error hiding Zendesk messenger:",e)}},w=async e=>{try{const{token:n}=await b(e.email,e.customerId,e.firstName);n?(await new Promise((o,t)=>{window.zE("messenger","loginUser",function(a){a(n)},function(a){a?(console.error("Messenger login failed:",a),t(a)):(console.debug("â
Messenger login successful"),console.debug("ð Ultimate.ai Debug - Authentication successful!"),console.debug("ð User should now be authenticated and Ultimate.ai getUser should work"),o())})}),await g(e),i.value=!0):(console.debug("ð No JWT token available in initializeAuthenticatedZendesk, staying anonymous"),i.value=!1)}catch(n){throw console.error("Failed to initialize authenticated Zendesk:",n),n}},Z=f(()=>u.value&&!l.value),E=f(()=>u.value?l.value?"authenticating":i.value?"authenticated":"anonymous":"loading");return{isZdLoaded:u,isAuthenticated:i,isAuthenticating:l,isZendeskReady:Z,zendeskStatus:E,showZd:y,hideZd:p,authenticateUser:v,setUserDataInWidget:g,initializeAuthenticatedZendesk:w}}export{S as u};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.