1var ie=Object.defineProperty,le=Object.defineProperties;var oe=Object.getOwnPropertyDescriptors;var Y=Object.getOwnPropertySymbols;var ue=Object.prototype.hasOwnProperty,de=Object.prototype.propertyIsEnumerable;var G=(e,s,t)=>s in e?ie(e,s,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[s]=t,F=(e,s)=>{for(var t in s||(s={}))ue.call(s,t)&&G(e,t,s[t]);if(Y)for(var t of Y(s))de.call(s,t)&&G(e,t,s[t]);return e},X=(e,s)=>le(e,oe(s));var b=(e,s,t)=>new Promise((p,l)=>{var o=g=>{try{d(t.next(g))}catch(m){l(m)}},f=g=>{try{d(t.throw(g))}catch(m){l(m)}},d=g=>g.done?p(g.value):Promise.resolve(g.value).then(o,f);d((t=t.apply(e,s)).next())});import{j as r}from"./vendor-mui-BGS3mHwM.js";import{r as a}from"./vendor-redux-FtDGt7Mz.js";/* empty css */import{A as fe,i as he}from"./ic-send-03-CN6Zu5op.js";import{W as pe}from"./WarningIcon-DbaJxr2G.js";import{kv as J,kw as Q,kx as P,bO as ge,ge as me}from"./index-D5apbUK-.js";import{c as xe,u as Ce}from"./vendor-react-CUpnpRPD.js";import"./vendor-stream-CynyArsg.js";import"./vendor-pdf-Ddbn4B6C.js";import"./vendor-graphql-CuoeCPzn.js";import"./vendor-media-BEZy7yMd.js";import"./vendor-radix-NiTG7895.js";import"./vendor-mui-pickers-BdXKbjEa.js";import"./vendor-forms-BcpGzS3-.js";const ye="in_app_chat:conversation:",K=50,z=e=>{var s,t;return{id:e.id,externalUid:(s=e.externalUid)!=null?s:null,renderedContent:e.renderedContent,aiRole:(t=e.aiRole)!=null?t:null,createdAt:e.createdAt,turnSeq:e.turnSeq}},Z=(e,s)=>{const t=new Map,p=l=>l.externalUid||l.id;return[...e,...s].forEach(l=>{const o=t.get(p(l));(!o||o.status)&&t.set(p(l),l)}),[...t.values()].sort((l,o)=>{var f,d;return l.createdAt!==o.createdAt?l.createdAt<o.createdAt?-1:1:((f=l.turnSeq)!=null?f:0)-((d=o.turnSeq)!=null?d:0)})},be=e=>{var W;const s=J(),{isConnected:t,initializeWebSocket:p}=Q(),[l,o]=a.useState(null),[f,d]=a.useState([]),[g,m]=a.useState(!0),[B,S]=a.useState(1),[U,D]=a.useState(1),[I,w]=a.useState(null),h=a.useRef(0),j=a.useRef(1),L=a.useRef(null),R=a.useRef(!1),[_,T]=a.useState(!1),M=a.useRef(null);M.current=(W=l==null?void 0:l.status)!=null?W:null;const v=a.useRef(0),x=a.useCallback(c=>{d(n=>Z(n,c))},[]),V=f.some(c=>c.status==="pending"),$=a.useRef(!1);$.current=V;const q=a.useCallback(c=>{v.current+=1,o(c)},[]),C=a.useCallback(()=>b(void 0,null,function*(){if(!e)return null;const c=h.current;v.current+=1;const n=v.current;try{const u=yield s.fetchInAppChatConversation({identityId:P,conversationId:e});return c===h.current&&n===v.current&&o(u),u}catch(u){return null}}),[s,e]),k=a.useRef(null),i=a.useCallback(()=>{var n;if(!e||M.current==="ACTIVE"||((n=k.current)==null?void 0:n.conversationId)===e)return;const c=C();k.current={conversationId:e,promise:c},c.finally(()=>{var u;((u=k.current)==null?void 0:u.promise)===c&&(k.current=null)})},[C,e]);a.useEffect(()=>{h.current+=1;const c=h.current,n=()=>c!==h.current;if(o(null),d([]),w(null),m(!0),j.current=1,L.current=null,!e)return m(!1),()=>{h.current+=1};p();let u;const E=()=>b(void 0,null,function*(){const{count:A}=yield s.summarizeInAppChatMessages({conversationId:e});if(n())return;const y=Math.max(1,Math.ceil(A/K));D(y),j.current=y,S(y);const O=yield s.fetchInAppChatMessages({conversationId:e,limit:K,page:y});n()||d(ae=>Z(ae.filter(ce=>ce.status),O.map(z)))});return b(void 0,null,function*(){C();const A=s.subscribeToChannel(`${ye}${e}`,y=>{n()||(x([z(y)]),i())},{onReconnectAck:()=>{n()||(w(null),E().catch(()=>{}),C())}});u=A.unsubscribe;try{yield A.ready}catch(y){n()||w("Couldnât connect to live chat. Retryingâ¦");return}if(!n())try{yield E()}catch(y){n()||w("Couldnât load messages.")}finally{n()||m(!1)}}),()=>{h.current+=1,u==null||u()}},[s,e,p,x,C,i]);const N=a.useCallback(()=>b(void 0,null,function*(){if(!e)return;const c=j.current-1;if(c<1)return;const n=h.current;try{const u=yield s.fetchInAppChatMessages({conversationId:e,limit:K,page:c});if(n!==h.current)return;x(u.map(z)),j.current=c,S(c)}catch(u){n===h.current&&w("Couldnât load older messages.")}}),[s,e,x]),H=a.useCallback((c,n)=>{if(!e)return;const u=h.current,E={id:n,externalUid:`in_app_chat:${n}`,renderedContent:{text:c},aiRole:"USER",createdAt:new Date().toISOString(),status:"pending",clientMessageId:n,text:c};x([E]),b(void 0,null,function*(){try{const A=yield s.sendInAppChatMessage({conversationId:e,identityId:P,content:{text:c},clientMessageId:n});if(u!==h.current)return;x([z(A)]),i()}catch(A){if(u!==h.current)return;d(y=>y.map(O=>O.clientMessageId===n?X(F({},O),{status:"failed"}):O)),i()}}).catch(()=>{})},[s,e,x,i]),ee=a.useCallback(c=>!c.trim()||!e||R.current?!1:(H(c,crypto.randomUUID()),!0),[e,H]),te=a.useCallback(c=>{const n=f.find(u=>u.clientMessageId===c);n!=null&&n.text&&(d(u=>u.map(E=>
1E.clientMessageId===c?X(F({},E),{status:"pending"}):E)),H(n.text,c))},[f,H]),se=a.useCallback(()=>b(void 0,null,function*(){if(!e)return!1;let c;for(let n=f.length-1;n>=0;n-=1)if(!f[n].status){c=f[n];break}if(!c||L.current===c.id)return!1;L.current=c.id;try{return yield s.markInAppChatConversationRead({identityId:P,conversationId:e,lastMessageId:c.id}),o(n=>n&&X(F({},n),{unreadCount:0})),!0}catch(n){return L.current=null,!1}}),[s,e,f]),ne=a.useCallback(()=>b(void 0,null,function*(){if(!e||R.current||$.current)return!1;R.current=!0,T(!0);try{const c=yield s.resolveInAppChatConversation({identityId:P,conversationId:e});return q(c),!0}catch(c){const n=yield C();return(n==null?void 0:n.status)==="RESOLVED"}finally{R.current=!1,T(!1)}}),[s,e,C,q]),re=B>1;return{summary:l,messages:f,hasPendingSend:V,isInitialLoading:g,hasOlder:re,fetchOlder:N,sendMessage:ee,retryMessage:te,refetchSummary:C,markRead:se,endChat:ne,isEndingChat:_,isConnected:t,error:I,newestPage:U}},we=()=>{const e=J(),{isConnected:s}=Q(),[t,p]=a.useState(0),l=a.useRef(0),o=a.useCallback(()=>b(void 0,null,function*(){l.current+=1;const d=l.current;try{const g=yield e.summarizeInAppChatUnread({identityId:P});if(d!==l.current)return;p(g.count)}catch(g){}}),[e]);a.useEffect(()=>(o(),()=>{l.current+=1}),[o]),a.useEffect(()=>{const d=()=>o();return window.addEventListener("focus",d),()=>window.removeEventListener("focus",d)},[o]);const f=a.useRef(s);return a.useEffect(()=>{s&&!f.current&&o(),f.current=s},[s,o]),{unreadCount:t,refetchUnread:o}},Se="data:image/svg+xml,%3csvg%20preserveAspectRatio='none'%20overflow='visible'%20style='display:%20block;'%20width='12.5'%20height='12.5'%20viewBox='0%200%2012.5%2012.5'%20fill='none'%20xmlns='http://www.w3.org/2000/svg'%3e%3cpath%20id='Icon'%20d='M11.75%200.75L0.75%2011.75M0.75%200.75L11.75%2011.75'%20stroke='var(--stroke-0,%20white)'%20stroke-width='1.5'%20stroke-linecap='round'%20stroke-linejoin='round'/%3e%3c/svg%3e",Re=e=>(e.aiRole||"").toLowerCase()==="user",Ne=e=>{var t;const s=e.renderedContent;return typeof s=="string"?s:(t=s==null?void 0:s.text)!=null?t:""},Ee=e=>{const s=new Date(e);return Number.isNaN(s.getTime())?"":ge(s,"h:mm a")},je={DRAFT:"sc-pill-neutral",ACTIVE:"sc-pill-success",RESOLVED:"sc-pill-info"},ve={ACTIVE:"AI assistant · replies instantly",RESOLVED:"Resolved",DRAFT:"Support"},Fe=()=>{var k;const{conversationId:e}=xe(),s=Ce(),{summary:t,messages:p,isInitialLoading:l,hasOlder:o,fetchOlder:f,sendMessage:d,retryMessage:g,markRead:m,endChat:B,isEndingChat:S,hasPendingSend:U,isConnected:D,error:I}=be(e!=null?e:null),{refetchUnread:w}=we(),[h,j]=a.useState(""),[L,R]=a.useState(!1),[_,T]=a.useState(null),M=a.useRef(null),v=a.useRef(0);a.useEffect(()=>{var i;p.length>v.current&&((i=M.current)==null||i.scrollIntoView({behavior:"smooth",block:"nearest"})),v.current=p.length},[p.length]),a.useEffect(()=>{if(l||p.length===0||typeof document!="undefined"&&document.hidden)return;const i=setTimeout(()=>{m().then(N=>{N&&w()}).catch(()=>{})},400);return()=>clearTimeout(i)},[l,p,m,w]);const x=()=>{h.trim()&&(me(),d(h)&&j(""))},V=()=>b(void 0,null,function*(){T(null);const i=yield B();R(!1),i?w():T("Couldnât end the chat. Please try again.")}),$=(t==null?void 0:t.status)==="RESOLVED",q=(t==null?void 0:t.context)&&(t.context.name||t.context.reference||t.context.kind),C=ve[(t==null?void 0:t.status)||""]||"Support";return r.jsxs("div",{className:"cnh-chat sc-screen sc-screen-chat",children:[r.jsx(fe,{subtitle:C,showDot:D&&(t==null?void 0:t.status)==="ACTIVE",onBack:()=>s(-1),action:(t==null?void 0:t.status)==="ACTIVE"?r.jsxs("button",{type:"button",className:"sc-hdr-end",onClick:()=>R(!0),disabled:S||U,children:["End Chat",r.jsx("span",{className:"sc-hdr-end-x","aria-hidden":!0,children:r.jsx("img",{src:Se,alt:""})})]}):void 0}),L&&r.jsx("div",{className:"sc-modal-overlay",role:"presentation",children:r.jsxs("div",{className:"sc-modal sc-modal-center",role:"dialog","aria-modal":"true","aria-labelledby":"sc-end-title",children:[r.jsx(pe,{}),r.jsxs("div",{className:"sc-modal-copy",children:[r.jsx("div",{className:"sc-modal-title",id:"sc-end-title",children:"Are you sure?"}),r.jsx("p",{className:"sc-modal-desc",children:"Are you sure you want to end this chat? You can always re-open it or start a new chat"})]}),r.jsxs("div",{className:"sc-modal-actions-center",children:[r.jsx("button",{type:"button",className:"sc-btn sc-btn-secondary",onClick:()=>R(!1),disabled:S,children:"Keep It"}),r.jsx("button",{type:"button",className:"sc-btn sc-btn-primary",onClick:V,disabled:S||U,children:S?"Endingâ¦":"End Chat"})]})]})}),(t==null?void 0:t.context)&&r.jsxs("div",{className:"sc-ctx-strip",children:[r.jsx("div",{className:"sc-ctx-icon",children:"ð¦"}),r.jsxs("div",{style:{minWidth:0,flex:1},children:[r.jsxs("div",{className:"sc-ctx-sub",style:{overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:[t.context.kind,t.context.reference?` · #${t.context.reference}`:""]}),r.jsxs("div",{className:"sc-ctx-title",style:{overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:[q,(k=t.topic)!=null&&k.label?` · ${t.topic.label}`:""]})]}),t.status&&r.jsx("span",{className:`sc-pill ${je[t.status]||"sc-pill-neutral"}`,children:t.status})]}),r.jsx("div",{className:"sc-msg-region",children:r.jsxs("div",{className:"sc-msg-scroll",children:[o&&r.jsx("button",{type:"button",className:"sc-btn sc-btn-ghost sc-btn-sm",style:{alignSelf:"center",color:"var(--sc-cta)"},onClick:f,children:"Load older messages"}),l&&r.jsx("p",{style:{textAlign:"center",fontSize:13,color:"var(--sc-fg-3)",padding:"24px 0"},children:"Loadingâ¦"}),p.map(i=>{const N=Re(i);return r.jsxs("div",{className:`sc-row ${N?"right":"left"}`,children:[!N&&r.jsx("div",{className:"sc-msg-avatar",children:"CNH"}),r.jsxs("div",{className:`sc-col ${N?"right":"left"}`,children:[r.jsx("div",{className:`sc-bubble ${N?"user":"agent"} ${i.status==="failed"?"failed":""}`,children:Ne(i)}),i.status==="failed"?r.jsx("button",{type:"button",className:"sc-retry",onClick:()=>i.clientMessageId&&g(i.clientMessageId),children:"Failed â tap to retry"}):r.jsx("span",{className:"sc-msg-time",children:i.status==="pending"?"Sendingâ¦":Ee(i.createdAt)})]})]},i.externalUid||i.id)})]})}),r.jsxs("div",{className:"sc-chat-band",children:[_&&r.jsx("div",{className:"sc-inline-error",children:_}),!D&&r.jsx("div",{className:"sc-error-note",children:"Reconnectingâ¦"}),I&&r.jsx("div",{className:"sc-error-note",children:I}),$&&r.jsx("div",{className:"sc-hint",children:"This chat was resolved. Reply below to reopen it."})]}),r.jsxs("div",{className:"sc-composer",children:[r.jsx("textarea",{className:"sc-composer-input",value:h,onChange:i=>j(i.target.value),onKeyDown:i=>{i.key==="Enter"&&!i.shiftKey&&(i.preventDefault(),x())},rows:1,placeholder:"Type a message"}),r.jsxs("button",{type:"button",className:"sc-send",onClick:x,disabled:!h.trim()||S,children:["Send",r.jsx("span",{className:"sc-send-icon","aria-hidden":!0,
1children:r.jsx("img",{src:he,alt:""})})]})]}),r.jsx("div",{ref:M})]})};export{Fe as default};
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.