@defra/flood-webchat 1.0.25 → 1.0.27
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/client.js +1 -1
- package/package.json +1 -1
package/dist/client.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{"use strict";const e={n:t=>{const a=t&&t.__esModule?()=>t.default:()=>t;return e.d(a,{a}),a},d:(t,a)=>{if(Array.isArray(a))for(var o=0;o<a.length;){var n=a[o++],s=a[o++];e.o(t,n)?0===s&&o++:0===s?Object.defineProperty(t,n,{enumerable:!0,value:a[o++]}):Object.defineProperty(t,n,{enumerable:!0,get:s})}else for(var n in a)e.o(a,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:a[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}};let t={};e.r(t),e.d(t,{init:()=>Q});const a=require("react"),o=require("react-dom/client"),n=require("@nice-devone/nice-cxone-chat-web-sdk"),s=(...e)=>e.filter(e=>e&&"string"==typeof e).join(" "),i=require("react-dom"),r=require("luxon"),d=require("html-react-parser");var l=e.n(d);const c=/(?:www|https?)[^\s]+/gi,u=e=>(e.match(c)&&(e=e.replace(c,e=>`<a class="govuk-link" href="${e}" target="_blank" rel="noreferrer">${e.replace(/https?:\/\//gi,"")}</a>`)),l()(e)),h=e=>({id:e.id,text:e.messageContent?.text,createdAt:new Date(e.createdAt),user:e.authorEndUserIdentity?.fullName?.trim()||null,assignee:e.authorUser?.firstName||null,direction:e.direction}),v=e=>{const t=r.DateTime.local();t.setZone("Europe/London");let a=`Floodline webchat transcript, ${t.toFormat("HH:mm:ss a, dd LLLL yyyy")}\n\n`;for(const t of e){const{text:e,direction:o,user:n,assignee:s,createdAt:i}=t,d="inbound"===o?n:`${s} (Floodline adviser)`;a+=`[${r.DateTime.fromJSDate(new Date(i)).setZone("Europe/London").toFormat("HH:mm:ss a, dd LLLL yyyy")}] ${d}: \n${e}\n\n`}return a=a.replace(/<a\b[^>]*>/i,"").replace(/<\/a>/i,""),encodeURIComponent(a)},g="webchat_settings",m="webchat_customer_id",E="webchat_thread_id",w={SET_CHAT_VISIBILITY:(e,t)=>({...e,isChatOpen:t}),SET_AVAILABILITY:(e,t)=>({...e,availability:t}),SET_SETTINGS:(e,t)=>(t?window.localStorage.setItem(g,JSON.stringify(t)):window.localStorage.removeItem(g),{...e,settings:t}),SET_CUSTOMER_ID:(e,t)=>(t?window.localStorage.setItem(m,t):window.localStorage.removeItem(m),{...e,customerId:t}),SET_THREAD_ID:(e,t)=>(t?window.localStorage.setItem(E,t):window.localStorage.removeItem(E),{...e,threadId:t}),SET_THREAD:(e,t)=>({...e,thread:t}),SET_MESSAGE:(e,t)=>({...e,message:h(t),messages:[...e.messages,h(t)]}),SET_MESSAGES:(e,t)=>{return{...e,messages:(a=t,a.map(e=>h(e))).reverse()};var a},SET_AGENT:(e,t)=>({...e,agent:t}),SET_AGENT_TYPING:(e,t)=>({...e,isAgentTyping:t}),SET_AGENT_STATUS:(e,t)=>({...e,agentStatus:t}),SET_UNSEEN_COUNT:(e,t)=>({...e,unseenCount:t}),SET_INSTIGATOR_ID:(e,t)=>({...e,instigatorId:t}),SET_LIVE_REGION_TEXT:(e,t)=>({...e,liveRegionText:t}),TOGGLE_IS_MOBILE:(e,t)=>({...e,isMobile:t}),TOGGLE_IS_KEYBOARD:(e,t)=>({...e,isKeyboard:t})},b={availability:null,customerId:null,threadId:null,thread:null,messages:[],unseenCount:0,instigatorId:null,agent:null,agentStatus:null,isAgentTyping:!1,isChatOpen:!1,isMobile:window.matchMedia("(max-width: 640px)").matches,isKeyboard:!1,settings:{audio:!0,scroll:!0}},y=(e,t)=>{const{type:a,payload:o}=t,n=w[a];return n?n.bind(void 0,e,o)():e},f=require("react/jsx-dev-runtime"),x=(0,a.createContext)(b),p=({sdk:e,availability:t,playSound:o,children:s})=>{const[i,r]=(0,a.useReducer)(y,b),d=e=>{r({type:"SET_AGENT",payload:e.detail.data.inboxAssignee}),r({type:"SET_AGENT_STATUS",payload:e.detail.data.contact.status}),r({type:"SET_UNSEEN_COUNT",payload:e.detail.data.contact.customerStatistics.unseenMessagesCount})},l=e=>{r({type:"SET_AGENT",payload:e.detail.data.inboxAssignee}),r({type:"SET_AGENT_STATUS",payload:e.detail.data.case.status})},c=e=>{e?.detail?.data?.user&&r({type:"SET_AGENT",payload:e.detail.data.user}),r({type:"SET_AGENT_TYPING",payload:!0})},u=()=>{r({type:"SET_AGENT_TYPING",payload:!1}),r({type:"SET_LIVE_REGION_TEXT"})},h=e=>{r({type:"SET_MESSAGE",payload:e.detail.data.message}),r({type:"SET_AGENT_STATUS",payload:e.detail.data.case.status}),r({type:"SET_UNSEEN_COUNT",payload:e.detail.data.case.customerStatistics.unseenMessagesCount}),JSON.parse(window.localStorage.getItem(g)).audio&&"outbound"===e.detail.data.message.direction&&o&&o()},v=e=>{r({type:"SET_AGENT_STATUS",payload:e.detail.data.case.status}),e.detail.data.inboxAssignee&&r({type:"SET_AGENT",payload:e.detail.data.inboxAssignee})},w=e=>{r({type:"TOGGLE_IS_MOBILE",payload:e.matches})},p=()=>{r({type:"TOGGLE_IS_KEYBOARD",payload:!0})},k=()=>{r({type:"TOGGLE_IS_KEYBOARD",payload:!1})};(0,a.useEffect)(()=>(e.onChatEvent(n.ChatEvent.LIVECHAT_RECOVERED,d),e.onChatEvent(n.ChatEvent.MESSAGE_CREATED,h),e.onChatEvent(n.ChatEvent.AGENT_TYPING_STARTED,c),e.onChatEvent(n.ChatEvent.AGENT_TYPING_ENDED,u),e.onChatEvent(n.ChatEvent.ASSIGNED_AGENT_CHANGED,l),e.onChatEvent(n.ChatEvent.CONTACT_STATUS_CHANGED,v),window.matchMedia("(max-width: 640px)").addEventListener("change",w),window.addEventListener("keydown",p),window.addEventListener("pointerdown",k),()=>{window.removeEventListener("change",w),window.removeEventListener("keydown",p),window.removeEventListener("pointerdown",k)}),[e]),(0,a.useEffect)(()=>{r({type:"SET_AVAILABILITY",payload:t}),V(window.localStorage.getItem(m)),j(window.localStorage.getItem(E)),N(JSON.parse(window.localStorage.getItem(g))||i.settings)},[]),(0,a.useEffect)(()=>{"#webchat"===window.location.hash&&D(!0);const e=()=>{"#webchat"===window.location.hash?D(!0):D(!1)};return window.addEventListener("popstate",e),()=>{window.removeEventListener("popstate",e)}},[]);const D=e=>{r({type:"SET_CHAT_VISIBILITY",payload:e})},V=e=>{r({type:"SET_CUSTOMER_ID",payload:e})},j=e=>{r({type:"SET_THREAD_ID",payload:e})},S=e=>{r({type:"SET_THREAD",payload:e})},_=e=>{r({type:"SET_MESSAGES",payload:e})},N=e=>{r({type:"SET_SETTINGS",payload:e})},T=e=>{r({type:"SET_UNSEEN_COUNT",payload:e})},C=e=>{r({type:"SET_INSTIGATOR_ID",payload:e})},I=e=>{r({type:"SET_LIVE_REGION_TEXT",payload:e})},A=(0,a.useMemo)(()=>({...i,sdk:e,setSettings:N,setCustomerId:V,setThreadId:j,setThread:S,setMessages:_,setUnseenCount:T,setChatVisibility:D,setInstigatorId:C,setLiveRegionText:I,onLiveChatRecovered:d,onAssignedAgentChanged:l,onAgentTypingStarted:c,onAgentTypingEnded:u,onMessageCreated:h,onContactStatusChanged:v}));return(0,f.jsxDEV)(x.Provider,{value:A,children:s},void 0,!1)},k=()=>(0,a.useContext)(x),D=()=>{const e=window.location.href.split("#")[0];window.history.pushState({history:!0},null,`${e}#webchat`)},V=()=>{if(window.history.state?.history)return window.history.back();const e=window.location.href.split("#")[0];return window.history.replaceState(null,null,e)};function j(){const{thread:e,threadId:t,setChatVisibility:a,setUnseenCount:o,isMobile:n}=k(),s=n=>{n.preventDefault(),a(!1),V(),t&&(e?.lastMessageSeen(),o(0))},i=(0,f.jsxDEV)("button",{className:"wc-header__back","aria-label":"Close the webchat",onClick:s,children:(0,f.jsxDEV)("svg",{"aria-hidden":"true",focusable:"false",width:"20",height:"20",viewBox:"0 0 20 20",children:(0,f.jsxDEV)("path",{d:"M4.828,11L12.314,18.485L10.899,19.899L1,10L10.899,0.101L12.314,1.515L4.828,9L19,9L19,11L4.828,11Z",fill:"currentColor"},void 0,!1)},void 0,!1)},void 0,!1),r=(0,f.jsxDEV)("button",{className:"wc-header__close","aria-label":"Close the webchat",onClick:s,children:(0,f.jsxDEV)("svg",{"aria-hidden":"true",focusable:"false",width:"20",height:"20",viewBox:"0 0 20 20",children:(0,f.jsxDEV)("path",{d:"M10,8.6L15.6,3L17,4.4L11.4,10L17,15.6L15.6,17L10,11.4L4.4,17L3,15.6L8.6,10L3,4.4L4.4,3L10,8.6Z",fill:"currentColor"},void 0,!1)},void 0,!1)},void 0,!1),d=(0,f.jsxDEV)("button",{className:"wc-header__hide","aria-label":"Minimise the webchat",onClick:s,children:(0,f.jsxDEV)("svg",{"aria-hidden":"true",focusable:"false",width:"20",height:"20",viewBox:"0 0 20 20",children:(0,f.jsxDEV)("path",{d:"M10 14.4l-7-7L4.4 6l5.6 5.6L15.6 6 17 7.4l-7 7z",fill:"currentColor"},void 0,!1)},void 0,!1)},void 0,!1),l=n&&window.history.state,c=n&&!window.history.state;let u=r;return t&&(u=d,c&&(u=d)),!t&&c&&(u=r),l&&(u=null),(0,f.jsxDEV)("div",{className:"wc-header",children:[l?i:null,(0,f.jsxDEV)("h2",{id:"wc-header",className:"wc-header__title",children:"Floodline webchat"},void 0,!1),u]},void 0,!0)}function S({onContinue:e}){return(0,f.jsxDEV)(f.Fragment,{children:[(0,f.jsxDEV)(j,{},void 0,!1),(0,f.jsxDEV)("div",{className:"wc-body",children:(0,f.jsxDEV)("div",{className:"wc-content",children:[(0,f.jsxDEV)("h3",{id:"wc-subtitle",className:"wc-heading","aria-live":"polite",children:"What you can use webchat for"},void 0,!1),(0,f.jsxDEV)("p",{children:"Webchat lets you talk directly to a Floodline adviser."},void 0,!1),(0,f.jsxDEV)("p",{children:"You can use webchat to get:"},void 0,!1),(0,f.jsxDEV)("ul",{className:"wc-list",children:[(0,f.jsxDEV)("li",{children:"current flood warnings and alerts in your area"},void 0,!1),(0,f.jsxDEV)("li",{children:"information on the flood warning service"},void 0,!1),(0,f.jsxDEV)("li",{children:"advice on what to do before, during and after a flood"},void 0,!1)]},void 0,!0),(0,f.jsxDEV)("p",{children:["There are other ways to ",(0,f.jsxDEV)("a",{className:"govuk-link",href:"https://www.gov.uk/get-flood-warnings",target:"_blank",rel:"noreferrer",children:"get flood warnings"},void 0,!1)," and ",(0,f.jsxDEV)("a",{className:"govuk-link",href:"https://get-flood-warnings.service.gov.uk/sign-in",target:"_blank",rel:"noreferrer",children:"manage your flood warnings account"},void 0,!1),"."]},void 0,!0),(0,f.jsxDEV)("p",{children:["Do not use webchat to ",(0,f.jsxDEV)("a",{className:"govuk-link",href:"https://www.gov.uk/report-flood-cause",target:"_blank",rel:"noreferrer",children:"report a flood"},void 0,!1),"."]},void 0,!0),(0,f.jsxDEV)("button",{className:"govuk-button wc-button govuk-!-margin-top-1","data-module":"govuk-button",onClick:e,children:"Continue"},void 0,!1),(0,f.jsxDEV)("h3",{className:"wc-heading",children:"Other flood information"},void 0,!1),(0,f.jsxDEV)("p",{children:"Call Floodline for all other flood and flood warning information."},void 0,!1),(0,f.jsxDEV)("p",{children:[(0,f.jsxDEV)("strong",{children:"Floodline helpline"},void 0,!1),(0,f.jsxDEV)("br",{},void 0,!1),"Telephone: 0345 988 1188",(0,f.jsxDEV)("br",{},void 0,!1),"Textphone: 0345 602 6340",(0,f.jsxDEV)("br",{},void 0,!1),"Open 24 hours a day, 7 days a week",(0,f.jsxDEV)("br",{},void 0,!1),(0,f.jsxDEV)("a",{className:"govuk-link",href:"https://gov.uk/call-charges",target:"_blank",rel:"noreferrer",children:"Find out more about call charges"},void 0,!1)]},void 0,!0)]},void 0,!0)},void 0,!1)]},void 0,!0)}const _=require("uuid"),N=e=>{const t=async()=>e.connect();return{connect:t,fetchCustomerId:async()=>(await t(),e.getCustomer().getId()),fetchThread:async a=>(await t(),e.getThread(a)),fetchMessages:async e=>{await t();const a=await e.recover(),o=[];let n=a.messages;for(;n.length;){n.map(e=>o.push(e));try{n=(await e.loadMoreMessages()).data.messages}catch(e){n=[]}}return o}}},T=({errors:e})=>{const t=Object.keys(e),a=e=>{e.preventDefault();const t=e.target.getAttribute("data-key");document.querySelector(`#wc-${t}`).focus()};return 0===t.length?null:(0,f.jsxDEV)("div",{className:"govuk-error-summary govuk-!-static-margin-bottom-7","data-module":"govuk-error-summary",tabIndex:"-1",children:(0,f.jsxDEV)("div",{role:"alert",children:[(0,f.jsxDEV)("h2",{id:"wc-error",className:"wc-error-summary__title govuk-error-summary__title",children:"There is a problem"},void 0,!1),(0,f.jsxDEV)("div",{className:"govuk-error-summary__body",children:(0,f.jsxDEV)("ul",{className:"govuk-list govuk-error-summary__list wc-error-summary__list",children:t.map(t=>(0,f.jsxDEV)("li",{children:(0,f.jsxDEV)("a",{href:"#","data-key":t,onClick:a,children:e[t]},void 0,!1)},t,!1))},void 0,!1)},void 0,!1)]},void 0,!0)},void 0,!1)},C=500;function I({onPreChatScreen:e}){const{sdk:t,setCustomerId:o,setThreadId:n,setThread:i,setLiveRegionText:r}=k(),{fetchCustomerId:d,fetchThread:l}=N(t),[c,u]=(0,a.useState)({}),[h,v]=(0,a.useState)(0),[g,m]=(0,a.useState)(!1),E=(0,a.useRef)(),w=(0,a.useRef)(),b=C>=h;let y=`You have ${C-h} characters remaining`;b||(y=`You have ${h-C} characters too many`),(0,a.useEffect)(()=>{Object.keys(c).length&&document.querySelector(".govuk-error-summary")?.focus()},[c]);const x=g?"Requesting...":"Request chat";return(0,a.useEffect)(()=>{g&&r(x)},[g]),(0,f.jsxDEV)(f.Fragment,{children:[(0,f.jsxDEV)(j,{},void 0,!1),(0,f.jsxDEV)("div",{className:"wc-body",children:(0,f.jsxDEV)("div",{className:"wc-content",children:[(0,f.jsxDEV)("a",{href:"#",className:"wc-back-link govuk-back-link",onClick:e,children:"What you can use webchat for"},void 0,!1),(0,f.jsxDEV)(T,{errors:c},void 0,!1),(0,f.jsxDEV)("h3",{id:"wc-subtitle",className:"wc-heading","aria-live":"polite",children:"Your name and question"},void 0,!1),(0,f.jsxDEV)("form",{children:[(0,f.jsxDEV)("div",{className:s("wc-form-group","govuk-form-group",c.name&&"govuk-form-group--error"),children:[(0,f.jsxDEV)("label",{className:"wc-label govuk-label",htmlFor:"wc-name",children:"Your name"},void 0,!1),c.name&&(0,f.jsxDEV)("p",{className:"govuk-error-message",children:[(0,f.jsxDEV)("span",{className:"govuk-visually-hidden",children:"Error:"},void 0,!1)," ",c.name]},void 0,!0),(0,f.jsxDEV)("input",{ref:E,className:s("wc-input","govuk-input",c.name&&"govuk-input--error"),id:"wc-name",name:"name",type:"text","data-testid":"request-chat-user-name"},void 0,!1)]},void 0,!0),(0,f.jsxDEV)("div",{className:"wc-form-group govuk-character-count","data-module":"govuk-character-count","data-maxlength":"500",children:(0,f.jsxDEV)("div",{className:s("govuk-form-group",c.question&&"govuk-form-group--error"),children:[(0,f.jsxDEV)("label",{className:"wc-label govuk-label",htmlFor:"wc-question",children:"Your question"},void 0,!1),c.question&&(0,f.jsxDEV)("p",{className:"govuk-error-message",children:[(0,f.jsxDEV)("span",{className:"govuk-visually-hidden",children:"Error:"},void 0,!1),c.question]},void 0,!0),(0,f.jsxDEV)("textarea",{ref:w,id:"wc-question",name:"question",rows:"5","aria-describedby":"wc-question-info",onChange:e=>{v(e.target.value.length)},className:s("wc-textarea","govuk-textarea","govuk-js-character-count",!b||c.question?"govuk-textarea--error":""),"data-testid":"request-chat-user-question"},void 0,!1),(0,f.jsxDEV)("div",{id:"wc-question-info",className:"wc-hint govuk-hint govuk-char-count__msg",style:{color:b?"":"#d4351c"},"aria-hidden":"true",children:y},void 0,!1),(0,f.jsxDEV)("div",{className:"govuk-character-count__sr-status govuk-visually-hidden","aria-live":"polite",children:y},void 0,!1)]},void 0,!0)},void 0,!1),(0,f.jsxDEV)("div",{className:"wc-inset-text govuk-inset-text",children:["By selecting 'Request chat' you agree to the terms of our ",(0,f.jsxDEV)("a",{href:"https://check-for-flooding.service.gov.uk/privacy-notice",target:"_blank",rel:"noreferrer",className:"govuk-link",children:"privacy notice"},void 0,!1),"."]},void 0,!0),(0,f.jsxDEV)("button",{className:"wc-button govuk-button govuk-!-margin-top-1","data-module":"govuk-button",onClick:async e=>{if(e.preventDefault(),g)return;const a={};if(0===E.current.value.length&&(a.name="Enter your name"),0===w.current.value.length&&(a.question="Enter your question"),w.current.value.length>C&&(a.question="Your question must be 500 characters or less"),0===Object.keys(a).length)try{m(!0);const e=_.v4(),a=await d(),s=await l(e);t.getCustomer().setName(E.current.value),await s.startChat(w.current.value||"Begin conversation"),o(a),n(e),i(s),s.setCustomField("threadid",e)}catch(e){console.log("[Request chat Error]",e)}u(a)},"aria-disabled":g,children:x},void 0,!1)]},void 0,!0)]},void 0,!0)},void 0,!1)]},void 0,!0)}function A({children:e}){return(0,f.jsxDEV)("div",{className:"wc-footer",children:e},void 0,!1)}function L({message:e,previousMessage:t}){const a="outbound"===e.direction,o=e?.direction===t?.direction;return(0,f.jsxDEV)("li",{className:s("wc-chat__message",a?"outbound":"inbound"),children:[(0,f.jsxDEV)("div",{className:"wc-chat__from",children:[o?null:(0,f.jsxDEV)("span",{className:s(a?"outbound":"inbound"),children:a?e.assignee:"You"},void 0,!1),(0,f.jsxDEV)("span",{className:"govuk-visually-hidden",children:":"},void 0,!1)]},void 0,!0),(0,f.jsxDEV)("div",{className:s("wc-chat__text",a?"outbound":"inbound"),children:u(e.text)},void 0,!1)]},void 0,!0)}const G=(e,t)=>{(0,a.useEffect)(()=>{if(e){e.style.height="0px";const t=e.scrollHeight;e.style.height=`${t}px`}},[e,t])},q=(e,t,a)=>{if("UNAVAILABLE"===e)return"Webchat is not currently available";if(!t)return"Connecting to Floodline";switch(t){case"closed":case"resolved":return a?`${a} ended the session`:"Session ended by advisor";default:return a?`You are speaking with ${a}`:"Waiting for an adviser"}};function O({onEndChatScreen:e,onSettingsScreen:t}){const{availability:o,thread:n,messages:s,agent:i,agentStatus:r,isAgentTyping:d,isChatOpen:l,settings:c,isKeyboard:u,setLiveRegionText:h}=k(),[g,m]=(0,a.useState)(""),[E,w]=(0,a.useState)(!1),b=(0,a.useRef)();G(b.current,g),(0,a.useEffect)(()=>{if(c.scroll&&0!==s.length){const e=document.querySelector(".wc-body");e.scrollTop=e.scrollHeight}},[s,d]),(0,a.useEffect)(()=>{const e=document.querySelector(".wc-form__label");0===g.length?e.classList.remove("govuk-visually-hidden"):e.classList.add("govuk-visually-hidden")},[g]);const y=[...s].reverse().find(e=>"outbound"===e.direction&&e.assignee),x=i?.nickname||i?.firstName||y?.assignee,p=q(o,r,x);(0,a.useEffect)(()=>(p&&h(`Floodline webchat - ${p}`),()=>{h()}),[p]),(0,a.useEffect)(()=>(d&&l&&h(`${x} is typing...`),()=>{h()}),[d,l]),(0,a.useEffect)(()=>{const e=s[s.length-1];return"closed"!==r&&"outbound"===e?.direction&&h(`${x} said: ${e.text}`),()=>{h()}},[s]);const D=()=>{if(0!==b.current.value.length&&"closed"!==r){try{const e=b.current.value.trim();n.sendTextMessage(e),h(`You said: ${e}`)}catch(e){console.log("[Chat Error] sendMessage",e)}m("")}},V=()=>{const e=v(s);document.querySelector("#transcript-download").setAttribute("href",`data:text/plain;charset=utf-8,${e}`)},S=a=>{if("Enter"!==a.key||a.shiftKey)" "===a.key?("end-chat"===a.target.id&&e(a),"wc-settings"===a.target.id&&t(a)):(n.keystroke(),setTimeout(()=>{n.stopTyping()},4e3));else switch(a.target.id){case"text-area":a.preventDefault(),D();break;case"end-chat":e(a);break;case"wc-settings":t(a);break;case"transcript-download":V()}};return(0,f.jsxDEV)(f.Fragment,{children:[(0,f.jsxDEV)(j,{},void 0,!1),(0,f.jsxDEV)("div",{className:"wc-status",children:[(0,f.jsxDEV)("p",{className:"wc-status__availability",children:p},void 0,!1),(0,f.jsxDEV)("a",{id:"end-chat",className:"wc-status__link",href:"#","data-module":"govuk-button",role:"button",onClick:e,onKeyDown:S,children:"End chat"},void 0,!1)]},void 0,!0),(0,f.jsxDEV)("div",{className:"wc-body",tabIndex:"0",children:(0,f.jsxDEV)("ul",{className:"wc-chat",children:[s.length?s.map((e,t)=>(0,f.jsxDEV)(L,{message:e,previousMessage:s[t-1]},e.id,!1)):null,d?(0,f.jsxDEV)("li",{className:"wc-chat__message outbound",children:[(0,f.jsxDEV)("div",{className:"wc-chat__from",children:[x," is typing"]},void 0,!0),(0,f.jsxDEV)("div",{className:"wc-chat__text outbound",children:(0,f.jsxDEV)("svg",{width:"28",height:"16",x:"0px",y:"0px",viewBox:"0 0 28 16",children:[(0,f.jsxDEV)("circle",{stroke:"none",cx:"3",cy:"8",r:"3",fill:"currentColor"},void 0,!1),(0,f.jsxDEV)("circle",{stroke:"none",cx:"14",cy:"8",r:"3",fill:"currentColor"},void 0,!1),(0,f.jsxDEV)("circle",{stroke:"none",cx:"25",cy:"8",r:"3",fill:"currentColor"},void 0,!1)]},void 0,!0)},void 0,!1)]},void 0,!0):null]},void 0,!0)},void 0,!1),(0,f.jsxDEV)(A,{children:[(0,f.jsxDEV)("form",{className:"wc-form"+(E?" wc-focus-within":""),noValidate:!0,onSubmit:e=>{e.preventDefault(),D()},children:[(0,f.jsxDEV)("label",{className:"govuk-label wc-form__label",htmlFor:"wc-form-textarea",children:["Your message",(0,f.jsxDEV)("span",{className:"govuk-visually-hidden",children:" (enter key submits)"},void 0,!1)]},void 0,!0),(0,f.jsxDEV)("textarea",{ref:b,rows:"1","aria-required":"true",className:"wc-form__textarea",id:"text-area",name:"message",onChange:e=>{m(e.target?.value)},onKeyDown:S,onFocus:()=>{w(u)},onBlur:()=>{w(!1)},value:g},void 0,!1),(0,f.jsxDEV)("input",{type:"submit",className:"wc-form__button govuk-button",value:"Send","data-prevent-double-click":"true"},void 0,!1)]},void 0,!0),(0,f.jsxDEV)("div",{className:"wc-footer__settings",children:[(0,f.jsxDEV)("a",{href:"#",id:"wc-settings",className:"wc-footer__settings-link","data-module":"govuk-button",onKeyDown:S,onClick:t,children:"Settings"},void 0,!1),(0,f.jsxDEV)("a",{href:"#",id:"transcript-download",className:"wc-footer__settings-link","data-module":"govuk-button",download:"floodline-webchat-transcript.txt",onKeyDown:S,onClick:V,children:"Save chat"},void 0,!1)]},void 0,!0)]},void 0,!0)]},void 0,!0)}function M(){return(0,f.jsxDEV)(f.Fragment,{children:[(0,f.jsxDEV)(j,{},void 0,!1),(0,f.jsxDEV)("div",{className:"wc-body",children:[(0,f.jsxDEV)("h3",{id:"wc-subtitle",className:"govuk-heading-m",children:"Webchat is currently not available"},void 0,!1),(0,f.jsxDEV)("p",{className:"govuk-body-s",children:"Try again later, or call Floodline:"},void 0,!1),(0,f.jsxDEV)("p",{className:"govuk-body-s",children:[(0,f.jsxDEV)("strong",{children:"Floodline helpline"},void 0,!1),(0,f.jsxDEV)("br",{},void 0,!1),"Telephone: 0345 988 1188",(0,f.jsxDEV)("br",{},void 0,!1),"Textphone: 0345 602 6340",(0,f.jsxDEV)("br",{},void 0,!1),"Open 24 hours a day, 7 days a week",(0,f.jsxDEV)("br",{},void 0,!1),(0,f.jsxDEV)("a",{className:"govuk-link",href:"https://gov.uk/call-charges",children:"Find out more about call charges"},void 0,!1)]},void 0,!0),(0,f.jsxDEV)("p",{className:"govuk-body-s",children:"We're running webchat as a trial, it will not always be available."},void 0,!1)]},void 0,!0)]},void 0,!0)}function R({onChatScreen:e,onEndChatConfirm:t}){const{setCustomerId:o,setThreadId:n,setMessages:s,agentStatus:i,thread:r,threadId:d,setUnseenCount:l,isKeyboard:c}=k(),u=async e=>{e.preventDefault(),window.localStorage.setItem("tmpThreadId",d),n(),s([]),o(),r.lastMessageSeen(),l(0),"closed"!==i&&r.endChat(),t(e)};(0,a.useEffect)(()=>{c&&setTimeout(()=>{document.querySelector("#confirm-endchat").focus()},10)},[]);const h=t=>{"Enter"!==t.key&&" "!==t.key||"confirm-endchat"!==t.target.id||u(t),"Enter"!==t.key&&" "!==t.key||"resume-chat"!==t.target.id||e(t)};return(0,f.jsxDEV)(f.Fragment,{children:[(0,f.jsxDEV)(j,{},void 0,!1),(0,f.jsxDEV)("div",{className:"wc-body",children:(0,f.jsxDEV)("div",{className:"wc-content",children:[(0,f.jsxDEV)("h3",{id:"wc-subtitle",className:"wc-heading","aria-live":"polite",children:"Are you sure you want to end the chat?"},void 0,!1),(0,f.jsxDEV)("div",{className:"govuk-button-group",children:[(0,f.jsxDEV)("a",{id:"confirm-endchat",href:"#endChat",role:"button",className:"wc-button govuk-button","data-module":"govuk-button",onClick:u,onKeyDown:h,children:"Yes, end chat"},void 0,!1),(0,f.jsxDEV)("a",{id:"resume-chat",href:"#resumeChat",role:"button",className:"wc-link govuk-link","data-module":"govuk-button",onClick:e,onKeyDown:h,children:"No, resume chat"},void 0,!1)]},void 0,!0)]},void 0,!0)},void 0,!1)]},void 0,!0)}function F({onCancel:e}){const{settings:t,setSettings:o}=k(),[n,s]=(0,a.useState)(t.audio),[i,r]=(0,a.useState)(t.scroll),d=t=>{t.preventDefault(),o({audio:n,scroll:i}),e(t)},l=t=>{"Enter"!==t.key&&" "!==t.key||"settings-save"!==t.target.id?"Enter"!==t.key&&" "!==t.key||"settings-cancel"!==t.target.id||e(t):d(t)};return(0,f.jsxDEV)(f.Fragment,{children:[(0,f.jsxDEV)(j,{},void 0,!1),(0,f.jsxDEV)("div",{className:"wc-body",children:(0,f.jsxDEV)("div",{className:"wc-content",children:[(0,f.jsxDEV)("fieldset",{className:"govuk-fieldset govuk-!-margin-bottom-4",children:[(0,f.jsxDEV)("legend",{className:"govuk-fieldset__legend govuk-fieldset__legend",children:(0,f.jsxDEV)("h3",{id:"wc-subtitle",className:"wc-heading",children:"Change settings"},void 0,!1)},void 0,!1),(0,f.jsxDEV)("div",{className:"govuk-checkboxes govuk-checkboxes--small","data-module":"govuk-checkboxes",children:[(0,f.jsxDEV)("div",{className:"govuk-checkboxes__item",children:[(0,f.jsxDEV)("input",{id:"audio",name:"audio",type:"checkbox",value:"audio",className:"govuk-checkboxes__input",defaultChecked:n,onChange:()=>s(!n)},void 0,!1),(0,f.jsxDEV)("label",{className:"wc-label govuk-label govuk-checkboxes__label",htmlFor:"audio",children:"Play a sound when receiving a new message"},void 0,!1)]},void 0,!0),(0,f.jsxDEV)("div",{className:"govuk-checkboxes__item",children:[(0,f.jsxDEV)("input",{id:"scroll",name:"scroll",type:"checkbox",value:"scroll",className:"govuk-checkboxes__input",defaultChecked:i,onChange:()=>r(!i)},void 0,!1),(0,f.jsxDEV)("label",{className:"wc-label govuk-label govuk-checkboxes__label",htmlFor:"scroll",children:"Scroll automatically to a new message"},void 0,!1)]},void 0,!0)]},void 0,!0)]},void 0,!0),(0,f.jsxDEV)("div",{className:"govuk-button-group",children:[(0,f.jsxDEV)("a",{id:"settings-save",href:"#",className:"wc-button govuk-button","data-module":"govuk-button",role:"button",onClick:d,onKeyDown:l,children:"Save"},void 0,!1),(0,f.jsxDEV)("a",{id:"settings-cancel",href:"#",className:"wc-link govuk-link","data-module":"govuk-button",role:"button",onClick:e,onKeyDown:l,children:"Cancel"},void 0,!1)]},void 0,!0)]},void 0,!0)},void 0,!1)]},void 0,!0)}function U({onCancel:e}){const t=()=>{const e=window.localStorage.getItem("tmpThreadId");window.location.href=`https://defragroup.eu.qualtrics.com/jfe/form/SV_8dgFSJcxxIfqx5Y?Id=${e}&Source=${window.location.href}`,window.localStorage.removeItem("tmpThreadId")},a=async t=>{window.localStorage.removeItem("tmpThreadId"),e(t)},o=e=>{"Enter"!==e.key&&" "!==e.key||("feedback-close"===e.target.id&&a(e),"feedback-send"===e.target.id&&t())};return(0,f.jsxDEV)(f.Fragment,{children:[(0,f.jsxDEV)(j,{},void 0,!1),(0,f.jsxDEV)("div",{className:"wc-body",children:(0,f.jsxDEV)("div",{className:"wc-content",children:[(0,f.jsxDEV)("h3",{id:"wc-subtitle",className:"wc-heading",children:"Give feedback on Floodline webchat"},void 0,!1),(0,f.jsxDEV)("p",{children:["We're running webchat as a trial. you can ",(0,f.jsxDEV)("a",{id:"feedback-send",className:"govuk-link",href:"#",target:"_blank",rel:"noreferrer",onKeyDown:o,onClick:t,children:"give feedback"},void 0,!1)," to help us improve it."]},void 0,!0),(0,f.jsxDEV)("p",{children:(0,f.jsxDEV)("a",{id:"feedback-close",className:"wc-link govuk-link",href:"#","data-module":"govuk-button",role:"button",onKeyDown:o,onClick:a,children:"Close"},void 0,!1)},void 0,!1)]},void 0,!0)},void 0,!1)]},void 0,!0)}const B=require("lodash.debounce");var Y=e.n(B);const $=()=>{const{agentStatus:e,liveRegionText:t,setLiveRegionText:o}=k(),[n,s]=(0,a.useState)(),[i,r]=(0,a.useState)(),d="closed"===e||null===e,l=()=>{s(),r()},c=(0,a.useCallback)(Y()(e=>{n?r(e):s(e)},2e3),[n,i]);return(0,a.useEffect)(()=>(t&&(l(),c(t)),d&&(l(),o()),()=>{l(),o()}),[t]),(0,f.jsxDEV)(f.Fragment,{children:[(0,f.jsxDEV)("div",{className:"wc-live",role:"status","aria-atomic":"true",children:n},void 0,!1),(0,f.jsxDEV)("div",{className:"wc-live",role:"status","aria-atomic":"true",children:i},void 0,!1)]},void 0,!0)},K="aria-hidden",H="data-wc-inert",P=e=>{for(const t of document.body.children)"wc-panel"!==t.id&&(e&&!t.getAttribute(K)?(t.setAttribute(K,"true"),t.setAttribute(H,"")):t.hasAttribute(H)&&(t.removeAttribute(K),t.removeAttribute(H)))},W=e=>{const[t,o]=(0,a.useState)([]),n=(0,a.useCallback)(e=>{const a=document.querySelector("#wc-panel");"Tab"===e.key&&(a&&!document.activeElement.closest("#wc-panel")&&a.focus(),e.shiftKey?(document.activeElement===t[0]||document.activeElement===document.querySelector("#wc-panel"))&&(t[t.length-1].focus(),e.preventDefault()):document.activeElement===t[t.length-1]&&(t[0].focus(),e.preventDefault()))},[t]);(0,a.useEffect)(()=>{o((()=>{const e=document.body.querySelectorAll(["#wc-panel a:not([disabled])","#wc-panel button:not([disabled])","#wc-panel select:not([disabled])","#wc-panel input:not([disabled])","#wc-panel textarea:not([disabled])",'#wc-panel *[tabindex="0"]:not([disabled])'].join(","));return Array.from(e).filter(e=>!e.closest("[hidden]")&&!e.closest('[aria-hidden="true"]'))})())},[e]),(0,a.useEffect)(()=>(P(!0),document.querySelector("#wc-panel").focus(),document.addEventListener("keydown",n),()=>{P(),document.removeEventListener("keydown",n)}),[t])};function J(){const{sdk:e,availability:t,instigatorId:o,thread:n,threadId:r,setThread:d,setThreadId:l,setChatVisibility:c,setMessages:u,setUnseenCount:h,isMobile:v,isKeyboard:g}=k(),{fetchThread:m,fetchMessages:E}=N(e),[w,b]=(0,a.useState)(r?2:0);W(w);const y=(0,a.useCallback)(e=>{"Escape"!==e.key&&"Esc"!==e.key||(r&&(n?.lastMessageSeen(),h(0)),c(!1),V())},[n,r,h,c]);(0,a.useEffect)(()=>()=>{document.getElementById(o)?.focus()},[]),(0,a.useEffect)(()=>(document.body.classList.remove("wc-u-hidden"),document.getElementsByTagName("html")[0].classList.add("wc-u-html"),document.body.classList.add("wc-u-body"),()=>{document.getElementsByTagName("html")[0].classList.remove("wc-u-html"),document.body.classList.remove("wc-u-body")}),[v]),(0,a.useEffect)(()=>(document.addEventListener("keydown",y),()=>{document.removeEventListener("keydown",y)}),[y,c]),(0,a.useEffect)(()=>{r&&(n?(n.lastMessageSeen(),h(0),b(2)):(async()=>{try{const e=await m(r);d(e);const t=await E(e);u(t)}catch(e){console.log("[Chat Error] fetchThread",e),l(),d(),b(0)}})())},[n,r]);const x=e=>t=>{t.preventDefault(),b(e)},p=x(0),D=x(1),j=x(2),_=x(3),T=x(4),C=x(5);let A;switch(w){case 0:default:A=(0,f.jsxDEV)(S,{onContinue:D},void 0,!1);break;case 1:A=(0,f.jsxDEV)(I,{onPreChatScreen:p},void 0,!1);break;case 2:A=(0,f.jsxDEV)(O,{onSettingsScreen:C,onEndChatScreen:_},void 0,!1);break;case 3:A=(0,f.jsxDEV)(R,{onChatScreen:j,onEndChatConfirm:T},void 0,!1);break;case 4:A=(0,f.jsxDEV)(U,{onCancel:()=>{c(!1),V()}},void 0,!1);break;case 5:A=(0,f.jsxDEV)(F,{onCancel:j},void 0,!1)}"UNAVAILABLE"===t&&(A=(0,f.jsxDEV)(M,{},void 0,!1));const L=(0,f.jsxDEV)("div",{id:"wc-panel",role:"dialog",className:s("wc-panel",g&&"wc-focus-visible"),tabIndex:"-1","aria-modal":"true","aria-labelledby":"wc-header wc-subtitle",children:[(0,f.jsxDEV)("div",{className:"wc-panel__inner",children:A},void 0,!1),(0,f.jsxDEV)($,{},void 0,!1)]},void 0,!0);return(0,f.jsxDEV)(f.Fragment,{children:(0,i.createPortal)(L,document.body)},void 0,!1)}const X=()=>{const{threadId:e,setInstigatorId:t}=k();if(!e)return null;const a=document.getElementById("webchat-skip-link-container");return a?(0,f.jsxDEV)(f.Fragment,{children:(0,i.createPortal)((0,f.jsxDEV)("a",{id:"webchat-skip-link",href:"#webchat",className:"govuk-skip-link","data-module":"govuk-skip-link",onClick:e=>{e.preventDefault(),t(e.target.id),window.location.hash="#webchat"},"data-wc-skiplink":!0,"data-wc-open-btn":!0,children:"Skip to webchat"},void 0,!1),a)},void 0,!1):null};function Z(){const{availability:e,isChatOpen:t,setChatVisibility:o,unseenCount:n,threadId:i,setUnseenCount:r,setInstigatorId:d,setLiveRegionText:l}=k(),c=(0,a.useRef)(),u=n>0&&!t,h=n>1?"new messages":"new message",v=e=>{e.preventDefault(),r(0),o(!t),d(e.target.id),t?V():D()},g=e=>{" "===e.key&&e.preventDefault()},m=e=>{" "===e.key&&(r(0),o(!t))};switch((0,a.useEffect)(()=>(u&&l(`Floodline webchat - ${n} ${h}`),()=>{l()}),[n,t]),(0,a.useEffect)(()=>{const a=()=>{if(!i)return;if("AVAILABLE"!==e)return;const a=c.current.parentElement.getBoundingClientRect().top+35>(window.innerHeight||document.documentElement.clientHeight),o=!t&&a;document.documentElement.classList.toggle("wc-u-scroll-padding",o),document.body.classList.toggle("wc-u-scroll-padding",o),c.current.classList.toggle("wc-availability--fixed",o)};return document.addEventListener("scroll",a),a(),()=>{document.removeEventListener("scroll",a)}},[i,t,e]),e){case"AVAILABLE":return(0,f.jsxDEV)(f.Fragment,{children:[(0,f.jsxDEV)("div",{className:s("wc-availability",t&&"wc-open"),ref:c,children:(0,f.jsxDEV)("div",{className:"wc-availability__inner",children:(0,f.jsxDEV)("a",{id:"webchat-start-chat-link",className:"wc-availability__link",href:"#webchat",draggable:"false",onClick:v,onKeyUp:m,onKeyDown:g,role:"button","data-module":"govuk-button",children:[i?(0,f.jsxDEV)(f.Fragment,{children:"Show chat"},void 0,!1):(0,f.jsxDEV)(f.Fragment,{children:"Start chat"},void 0,!1),u?(0,f.jsxDEV)("span",{className:"wc-availability__unseen",children:[n,(0,f.jsxDEV)("span",{className:"govuk-visually-hidden",children:[" ",h]},void 0,!0)]},void 0,!0):null]},void 0,!0)},void 0,!1)},void 0,!1),t?(0,f.jsxDEV)(J,{},void 0,!1):(0,f.jsxDEV)($,{},void 0,!1),(0,f.jsxDEV)(X,{},void 0,!1)]},void 0,!0);case"EXISTING":case"UNAVAILABLE":return(0,f.jsxDEV)("p",{className:"govuk-body",children:"When it is available, a 'start chat' link will appear."},void 0,!1);default:return(0,f.jsxDEV)("p",{className:"govuk-body",children:"Checking availability"},void 0,!1)}}const z=async e=>{const t=new(window.AudioContext||window.webkitAudioContext);if("suspended"===t.state){const e=["touchstart","touchend","mousedown","wheel","keydown","click"],a=o=>{e.forEach(e=>{document.body.removeEventListener(e,a)}),t.resume()};e.forEach(e=>{document.body.addEventListener(e,a,!1)})}const a=await fetch(e),o=await a.arrayBuffer(),n=await t.decodeAudioData(o);return()=>{const e=t.createBufferSource();e.buffer=n,e.connect(t.destination),e.start()}};async function Q(e,t){const a=new n.ChatSdk({brandId:t.brandId,channelId:t.channelId,customerId:globalThis.localStorage.getItem(m)||"",environment:t.environment,isLivechat:!0,cacheStorage:new n.CacheStorage(globalThis.localStorage),storage:globalThis.localStorage}),s=(0,o.createRoot)(e);let i,r;try{const e=await async function(e){const t=await fetch(e);return{availability:(await t.json()).availability||"UNAVAILABLE"}}(t.availabilityEndpoint);r=await z(t.audioUrl),i=e.availability}catch(e){i="UNAVAILABLE"}s.render((0,f.jsxDEV)(p,{sdk:a,availability:i,playSound:r,children:(0,f.jsxDEV)(Z,{},void 0,!1)},void 0,!1))}module.exports=t})();
|
|
1
|
+
(()=>{"use strict";const e={n:t=>{const a=t&&t.__esModule?()=>t.default:()=>t;return e.d(a,{a}),a},d:(t,a)=>{if(Array.isArray(a))for(var s=0;s<a.length;){var n=a[s++],o=a[s++];e.o(t,n)?0===o&&s++:0===o?Object.defineProperty(t,n,{enumerable:!0,value:a[s++]}):Object.defineProperty(t,n,{enumerable:!0,get:o})}else for(var n in a)e.o(a,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:a[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}};let t={};e.r(t),e.d(t,{init:()=>Q});const a=require("react"),s=require("react-dom/client"),n=require("@nice-devone/nice-cxone-chat-web-sdk"),o=(...e)=>e.filter(e=>e&&"string"==typeof e).join(" "),r=require("react-dom"),i=require("luxon"),l=require("html-react-parser");var c=e.n(l);const d=/(?:www|https?)[^\s]+/gi,u=e=>(e.match(d)&&(e=e.replace(d,e=>`<a class="govuk-link" href="${e}" target="_blank" rel="noreferrer">${e.replace(/https?:\/\//gi,"")}</a>`)),c()(e)),h=e=>({id:e.id,text:e.messageContent?.text,createdAt:new Date(e.createdAt),user:e.authorEndUserIdentity?.fullName?.trim()||null,assignee:e.authorUser?.firstName||null,direction:e.direction}),g=e=>{const t=i.DateTime.local();t.setZone("Europe/London");let a=`Floodline webchat transcript, ${t.toFormat("HH:mm:ss a, dd LLLL yyyy")}\n\n`;for(const t of e){const{text:e,direction:s,user:n,assignee:o,createdAt:r}=t,l="inbound"===s?n:`${o} (Floodline adviser)`;a+=`[${i.DateTime.fromJSDate(new Date(r)).setZone("Europe/London").toFormat("HH:mm:ss a, dd LLLL yyyy")}] ${l}: \n${e}\n\n`}return a=a.replace(/<a\b[^>]*>/i,"").replace(/<\/a>/i,""),encodeURIComponent(a)},m="webchat_settings",w="webchat_customer_id",v="webchat_thread_id",b={SET_CHAT_VISIBILITY:(e,t)=>({...e,isChatOpen:t}),SET_AVAILABILITY:(e,t)=>({...e,availability:t}),SET_SETTINGS:(e,t)=>(t?window.localStorage.setItem(m,JSON.stringify(t)):window.localStorage.removeItem(m),{...e,settings:t}),SET_CUSTOMER_ID:(e,t)=>(t?window.localStorage.setItem(w,t):window.localStorage.removeItem(w),{...e,customerId:t}),SET_THREAD_ID:(e,t)=>(t?window.localStorage.setItem(v,t):window.localStorage.removeItem(v),{...e,threadId:t}),SET_THREAD:(e,t)=>({...e,thread:t}),SET_MESSAGE:(e,t)=>({...e,message:h(t),messages:[...e.messages,h(t)]}),SET_MESSAGES:(e,t)=>{return{...e,messages:(a=t,a.map(e=>h(e))).reverse()};var a},SET_AGENT:(e,t)=>({...e,agent:t}),SET_AGENT_TYPING:(e,t)=>({...e,isAgentTyping:t}),SET_AGENT_STATUS:(e,t)=>({...e,agentStatus:t}),SET_UNSEEN_COUNT:(e,t)=>({...e,unseenCount:t}),SET_INSTIGATOR_ID:(e,t)=>({...e,instigatorId:t}),SET_LIVE_REGION_TEXT:(e,t)=>({...e,liveRegionText:t}),TOGGLE_IS_MOBILE:(e,t)=>({...e,isMobile:t}),TOGGLE_IS_KEYBOARD:(e,t)=>({...e,isKeyboard:t})},y={availability:null,customerId:null,threadId:null,thread:null,messages:[],unseenCount:0,instigatorId:null,agent:null,agentStatus:null,isAgentTyping:!1,isChatOpen:!1,isMobile:window.matchMedia("(max-width: 640px)").matches,isKeyboard:!1,settings:{audio:!0,scroll:!0}},f=(e,t)=>{const{type:a,payload:s}=t,n=b[a];return n?n.bind(void 0,e,s)():e},x=require("react/jsx-runtime"),p=(0,a.createContext)(y),k=({sdk:e,availability:t,playSound:s,children:o})=>{const[r,i]=(0,a.useReducer)(f,y),l=e=>{i({type:"SET_AGENT",payload:e.detail.data.inboxAssignee}),i({type:"SET_AGENT_STATUS",payload:e.detail.data.contact.status}),i({type:"SET_UNSEEN_COUNT",payload:e.detail.data.contact.customerStatistics.unseenMessagesCount})},c=e=>{i({type:"SET_AGENT",payload:e.detail.data.inboxAssignee}),i({type:"SET_AGENT_STATUS",payload:e.detail.data.case.status})},d=e=>{e?.detail?.data?.user&&i({type:"SET_AGENT",payload:e.detail.data.user}),i({type:"SET_AGENT_TYPING",payload:!0})},u=()=>{i({type:"SET_AGENT_TYPING",payload:!1}),i({type:"SET_LIVE_REGION_TEXT"})},h=e=>{i({type:"SET_MESSAGE",payload:e.detail.data.message}),i({type:"SET_AGENT_STATUS",payload:e.detail.data.case.status}),i({type:"SET_UNSEEN_COUNT",payload:e.detail.data.case.customerStatistics.unseenMessagesCount}),JSON.parse(window.localStorage.getItem(m)).audio&&"outbound"===e.detail.data.message.direction&&s&&s()},g=e=>{i({type:"SET_AGENT_STATUS",payload:e.detail.data.case.status}),e.detail.data.inboxAssignee&&i({type:"SET_AGENT",payload:e.detail.data.inboxAssignee})},b=e=>{i({type:"TOGGLE_IS_MOBILE",payload:e.matches})},k=()=>{i({type:"TOGGLE_IS_KEYBOARD",payload:!0})},E=()=>{i({type:"TOGGLE_IS_KEYBOARD",payload:!1})};(0,a.useEffect)(()=>(e.onChatEvent(n.ChatEvent.LIVECHAT_RECOVERED,l),e.onChatEvent(n.ChatEvent.MESSAGE_CREATED,h),e.onChatEvent(n.ChatEvent.AGENT_TYPING_STARTED,d),e.onChatEvent(n.ChatEvent.AGENT_TYPING_ENDED,u),e.onChatEvent(n.ChatEvent.ASSIGNED_AGENT_CHANGED,c),e.onChatEvent(n.ChatEvent.CONTACT_STATUS_CHANGED,g),window.matchMedia("(max-width: 640px)").addEventListener("change",b),window.addEventListener("keydown",k),window.addEventListener("pointerdown",E),()=>{window.removeEventListener("change",b),window.removeEventListener("keydown",k),window.removeEventListener("pointerdown",E)}),[e]),(0,a.useEffect)(()=>{i({type:"SET_AVAILABILITY",payload:t}),S(window.localStorage.getItem(w)),_(window.localStorage.getItem(v)),C(JSON.parse(window.localStorage.getItem(m))||r.settings)},[]),(0,a.useEffect)(()=>{"#webchat"===window.location.hash&&j(!0);const e=()=>{"#webchat"===window.location.hash?j(!0):j(!1)};return window.addEventListener("popstate",e),()=>{window.removeEventListener("popstate",e)}},[]);const j=e=>{i({type:"SET_CHAT_VISIBILITY",payload:e})},S=e=>{i({type:"SET_CUSTOMER_ID",payload:e})},_=e=>{i({type:"SET_THREAD_ID",payload:e})},N=e=>{i({type:"SET_THREAD",payload:e})},T=e=>{i({type:"SET_MESSAGES",payload:e})},C=e=>{i({type:"SET_SETTINGS",payload:e})},I=e=>{i({type:"SET_UNSEEN_COUNT",payload:e})},A=e=>{i({type:"SET_INSTIGATOR_ID",payload:e})},L=e=>{i({type:"SET_LIVE_REGION_TEXT",payload:e})},D=(0,a.useMemo)(()=>({...r,sdk:e,setSettings:C,setCustomerId:S,setThreadId:_,setThread:N,setMessages:T,setUnseenCount:I,setChatVisibility:j,setInstigatorId:A,setLiveRegionText:L,onLiveChatRecovered:l,onAssignedAgentChanged:c,onAgentTypingStarted:d,onAgentTypingEnded:u,onMessageCreated:h,onContactStatusChanged:g}));return(0,x.jsx)(p.Provider,{value:D,children:o})},E=()=>(0,a.useContext)(p),j=()=>{const e=window.location.href.split("#")[0];window.history.pushState({history:!0},null,`${e}#webchat`)},S=()=>{if(window.history.state?.history)return window.history.back();const e=window.location.href.split("#")[0];return window.history.replaceState(null,null,e)};function _(){const{thread:e,threadId:t,setChatVisibility:a,setUnseenCount:s,isMobile:n}=E(),o=n=>{n.preventDefault(),a(!1),S(),t&&(e?.lastMessageSeen(),s(0))},r=(0,x.jsx)("button",{className:"wc-header__back","aria-label":"Close the webchat",onClick:o,children:(0,x.jsx)("svg",{"aria-hidden":"true",focusable:"false",width:"20",height:"20",viewBox:"0 0 20 20",children:(0,x.jsx)("path",{d:"M4.828,11L12.314,18.485L10.899,19.899L1,10L10.899,0.101L12.314,1.515L4.828,9L19,9L19,11L4.828,11Z",fill:"currentColor"})})}),i=(0,x.jsx)("button",{className:"wc-header__close","aria-label":"Close the webchat",onClick:o,children:(0,x.jsx)("svg",{"aria-hidden":"true",focusable:"false",width:"20",height:"20",viewBox:"0 0 20 20",children:(0,x.jsx)("path",{d:"M10,8.6L15.6,3L17,4.4L11.4,10L17,15.6L15.6,17L10,11.4L4.4,17L3,15.6L8.6,10L3,4.4L4.4,3L10,8.6Z",fill:"currentColor"})})}),l=(0,x.jsx)("button",{className:"wc-header__hide","aria-label":"Minimise the webchat",onClick:o,children:(0,x.jsx)("svg",{"aria-hidden":"true",focusable:"false",width:"20",height:"20",viewBox:"0 0 20 20",children:(0,x.jsx)("path",{d:"M10 14.4l-7-7L4.4 6l5.6 5.6L15.6 6 17 7.4l-7 7z",fill:"currentColor"})})}),c=n&&window.history.state,d=n&&!window.history.state;let u=i;return t&&(u=l,d&&(u=l)),!t&&d&&(u=i),c&&(u=null),(0,x.jsxs)("div",{className:"wc-header",children:[c?r:null,(0,x.jsx)("h2",{id:"wc-header",className:"wc-header__title",children:"Floodline webchat"}),u]})}function N({onContinue:e}){return(0,x.jsxs)(x.Fragment,{children:[(0,x.jsx)(_,{}),(0,x.jsx)("div",{className:"wc-body",children:(0,x.jsxs)("div",{className:"wc-content",children:[(0,x.jsx)("h3",{id:"wc-subtitle",className:"wc-heading","aria-live":"polite",children:"What you can use webchat for"}),(0,x.jsx)("p",{children:"Webchat lets you talk directly to a Floodline adviser."}),(0,x.jsx)("p",{children:"You can use webchat to get:"}),(0,x.jsxs)("ul",{className:"wc-list",children:[(0,x.jsx)("li",{children:"current flood warnings and alerts in your area"}),(0,x.jsx)("li",{children:"information on the flood warning service"}),(0,x.jsx)("li",{children:"advice on what to do before, during and after a flood"})]}),(0,x.jsxs)("p",{children:["There are other ways to ",(0,x.jsx)("a",{className:"govuk-link",href:"https://www.gov.uk/get-flood-warnings",target:"_blank",rel:"noreferrer",children:"get flood warnings"})," and ",(0,x.jsx)("a",{className:"govuk-link",href:"https://get-flood-warnings.service.gov.uk/sign-in",target:"_blank",rel:"noreferrer",children:"manage your flood warnings account"}),"."]}),(0,x.jsxs)("p",{children:["Do not use webchat to ",(0,x.jsx)("a",{className:"govuk-link",href:"https://www.gov.uk/report-flood-cause",target:"_blank",rel:"noreferrer",children:"report a flood"}),"."]}),(0,x.jsx)("button",{className:"govuk-button wc-button govuk-!-margin-top-1","data-module":"govuk-button",onClick:e,children:"Continue"}),(0,x.jsx)("h3",{className:"wc-heading",children:"Other flood information"}),(0,x.jsx)("p",{children:"Call Floodline for all other flood and flood warning information."}),(0,x.jsxs)("p",{children:[(0,x.jsx)("strong",{children:"Floodline helpline"}),(0,x.jsx)("br",{}),"Telephone: 0345 988 1188",(0,x.jsx)("br",{}),"Textphone: 0345 602 6340",(0,x.jsx)("br",{}),"Open 24 hours a day, 7 days a week",(0,x.jsx)("br",{}),(0,x.jsx)("a",{className:"govuk-link",href:"https://gov.uk/call-charges",target:"_blank",rel:"noreferrer",children:"Find out more about call charges"})]})]})})]})}const T=require("uuid"),C=e=>{const t=async()=>e.connect();return{connect:t,fetchCustomerId:async()=>(await t(),e.getCustomer().getId()),fetchThread:async a=>(await t(),e.getThread(a)),fetchMessages:async e=>{await t();const a=await e.recover(),s=[];let n=a.messages;for(;n.length;){n.map(e=>s.push(e));try{n=(await e.loadMoreMessages()).data.messages}catch(e){n=[]}}return s}}},I=({errors:e})=>{const t=Object.keys(e),a=e=>{e.preventDefault();const t=e.target.getAttribute("data-key");document.querySelector(`#wc-${t}`).focus()};return 0===t.length?null:(0,x.jsx)("div",{className:"govuk-error-summary govuk-!-static-margin-bottom-7","data-module":"govuk-error-summary",tabIndex:"-1",children:(0,x.jsxs)("div",{role:"alert",children:[(0,x.jsx)("h2",{id:"wc-error",className:"wc-error-summary__title govuk-error-summary__title",children:"There is a problem"}),(0,x.jsx)("div",{className:"govuk-error-summary__body",children:(0,x.jsx)("ul",{className:"govuk-list govuk-error-summary__list wc-error-summary__list",children:t.map(t=>(0,x.jsx)("li",{children:(0,x.jsx)("a",{href:"#","data-key":t,onClick:a,children:e[t]})},t))})})]})})},A=500;function L({onPreChatScreen:e}){const{sdk:t,setCustomerId:s,setThreadId:n,setThread:r,setLiveRegionText:i}=E(),{fetchCustomerId:l,fetchThread:c}=C(t),[d,u]=(0,a.useState)({}),[h,g]=(0,a.useState)(0),[m,w]=(0,a.useState)(!1),v=(0,a.useRef)(),b=(0,a.useRef)(),y=A>=h;let f=`You have ${A-h} characters remaining`;y||(f=`You have ${h-A} characters too many`),(0,a.useEffect)(()=>{Object.keys(d).length&&document.querySelector(".govuk-error-summary")?.focus()},[d]);const p=m?"Requesting...":"Request chat";return(0,a.useEffect)(()=>{m&&i(p)},[m]),(0,x.jsxs)(x.Fragment,{children:[(0,x.jsx)(_,{}),(0,x.jsx)("div",{className:"wc-body",children:(0,x.jsxs)("div",{className:"wc-content",children:[(0,x.jsx)("a",{href:"#",className:"wc-back-link govuk-back-link",onClick:e,children:"What you can use webchat for"}),(0,x.jsx)(I,{errors:d}),(0,x.jsx)("h3",{id:"wc-subtitle",className:"wc-heading","aria-live":"polite",children:"Your name and question"}),(0,x.jsxs)("form",{children:[(0,x.jsxs)("div",{className:o("wc-form-group","govuk-form-group",d.name&&"govuk-form-group--error"),children:[(0,x.jsx)("label",{className:"wc-label govuk-label",htmlFor:"wc-name",children:"Your name"}),d.name&&(0,x.jsxs)("p",{className:"govuk-error-message",children:[(0,x.jsx)("span",{className:"govuk-visually-hidden",children:"Error:"})," ",d.name]}),(0,x.jsx)("input",{ref:v,className:o("wc-input","govuk-input",d.name&&"govuk-input--error"),id:"wc-name",name:"name",type:"text","data-testid":"request-chat-user-name"})]}),(0,x.jsx)("div",{className:"wc-form-group govuk-character-count","data-module":"govuk-character-count","data-maxlength":"500",children:(0,x.jsxs)("div",{className:o("govuk-form-group",d.question&&"govuk-form-group--error"),children:[(0,x.jsx)("label",{className:"wc-label govuk-label",htmlFor:"wc-question",children:"Your question"}),d.question&&(0,x.jsxs)("p",{className:"govuk-error-message",children:[(0,x.jsx)("span",{className:"govuk-visually-hidden",children:"Error:"}),d.question]}),(0,x.jsx)("textarea",{ref:b,id:"wc-question",name:"question",rows:"5","aria-describedby":"wc-question-info",onChange:e=>{g(e.target.value.length)},className:o("wc-textarea","govuk-textarea","govuk-js-character-count",!y||d.question?"govuk-textarea--error":""),"data-testid":"request-chat-user-question"}),(0,x.jsx)("div",{id:"wc-question-info",className:"wc-hint govuk-hint govuk-char-count__msg",style:{color:y?"":"#d4351c"},"aria-hidden":"true",children:f}),(0,x.jsx)("div",{className:"govuk-character-count__sr-status govuk-visually-hidden","aria-live":"polite",children:f})]})}),(0,x.jsxs)("div",{className:"wc-inset-text govuk-inset-text",children:["By selecting 'Request chat' you agree to the terms of our ",(0,x.jsx)("a",{href:"https://check-for-flooding.service.gov.uk/privacy-notice",target:"_blank",rel:"noreferrer",className:"govuk-link",children:"privacy notice"}),"."]}),(0,x.jsx)("button",{className:"wc-button govuk-button govuk-!-margin-top-1","data-module":"govuk-button",onClick:async e=>{if(e.preventDefault(),m)return;const a={};if(0===v.current.value.length&&(a.name="Enter your name"),0===b.current.value.length&&(a.question="Enter your question"),b.current.value.length>A&&(a.question="Your question must be 500 characters or less"),0===Object.keys(a).length)try{w(!0);const e=T.v4(),a=await l(),o=await c(e);t.getCustomer().setName(v.current.value),await o.startChat(b.current.value||"Begin conversation"),s(a),n(e),r(o),o.setCustomField("threadid",e)}catch(e){console.log("[Request chat Error]",e)}u(a)},"aria-disabled":m,children:p})]})]})})]})}function D({children:e}){return(0,x.jsx)("div",{className:"wc-footer",children:e})}function G({message:e,previousMessage:t}){const a="outbound"===e.direction,s=e?.direction===t?.direction;return(0,x.jsxs)("li",{className:o("wc-chat__message",a?"outbound":"inbound"),children:[(0,x.jsxs)("div",{className:"wc-chat__from",children:[s?null:(0,x.jsx)("span",{className:o(a?"outbound":"inbound"),children:a?e.assignee:"You"}),(0,x.jsx)("span",{className:"govuk-visually-hidden",children:":"})]}),(0,x.jsx)("div",{className:o("wc-chat__text",a?"outbound":"inbound"),children:u(e.text)})]})}const q=(e,t)=>{(0,a.useEffect)(()=>{if(e){e.style.height="0px";const t=e.scrollHeight;e.style.height=`${t}px`}},[e,t])},O=(e,t,a)=>{if("UNAVAILABLE"===e)return"Webchat is not currently available";if(!t)return"Connecting to Floodline";switch(t){case"closed":case"resolved":return a?`${a} ended the session`:"Session ended by advisor";default:return a?`You are speaking with ${a}`:"Waiting for an adviser"}};function M({onEndChatScreen:e,onSettingsScreen:t}){const{availability:s,thread:n,messages:o,agent:r,agentStatus:i,isAgentTyping:l,isChatOpen:c,settings:d,isKeyboard:u,setLiveRegionText:h}=E(),[m,w]=(0,a.useState)(""),[v,b]=(0,a.useState)(!1),y=(0,a.useRef)();q(y.current,m),(0,a.useEffect)(()=>{if(d.scroll&&0!==o.length){const e=document.querySelector(".wc-body");e.scrollTop=e.scrollHeight}},[o,l]),(0,a.useEffect)(()=>{const e=document.querySelector(".wc-form__label");0===m.length?e.classList.remove("govuk-visually-hidden"):e.classList.add("govuk-visually-hidden")},[m]);const f=[...o].reverse().find(e=>"outbound"===e.direction&&e.assignee),p=r?.nickname||r?.firstName||f?.assignee,k=O(s,i,p);(0,a.useEffect)(()=>(k&&h(`Floodline webchat - ${k}`),()=>{h()}),[k]),(0,a.useEffect)(()=>(l&&c&&h(`${p} is typing...`),()=>{h()}),[l,c]),(0,a.useEffect)(()=>{const e=o[o.length-1];return"closed"!==i&&"outbound"===e?.direction&&h(`${p} said: ${e.text}`),()=>{h()}},[o]);const j=()=>{if(0!==y.current.value.length&&"closed"!==i){try{const e=y.current.value.trim();n.sendTextMessage(e),h(`You said: ${e}`)}catch(e){console.log("[Chat Error] sendMessage",e)}w("")}},S=()=>{const e=g(o);document.querySelector("#transcript-download").setAttribute("href",`data:text/plain;charset=utf-8,${e}`)},N=a=>{if("Enter"!==a.key||a.shiftKey)" "===a.key?("end-chat"===a.target.id&&e(a),"wc-settings"===a.target.id&&t(a)):(n.keystroke(),setTimeout(()=>{n.stopTyping()},4e3));else switch(a.target.id){case"text-area":a.preventDefault(),j();break;case"end-chat":e(a);break;case"wc-settings":t(a);break;case"transcript-download":S()}};return(0,x.jsxs)(x.Fragment,{children:[(0,x.jsx)(_,{}),(0,x.jsxs)("div",{className:"wc-status",children:[(0,x.jsx)("p",{className:"wc-status__availability",children:k}),(0,x.jsx)("a",{id:"end-chat",className:"wc-status__link",href:"#","data-module":"govuk-button",role:"button",onClick:e,onKeyDown:N,children:"End chat"})]}),(0,x.jsx)("div",{className:"wc-body",tabIndex:"0",children:(0,x.jsxs)("ul",{className:"wc-chat",children:[o.length?o.map((e,t)=>(0,x.jsx)(G,{message:e,previousMessage:o[t-1]},e.id)):null,l?(0,x.jsxs)("li",{className:"wc-chat__message outbound",children:[(0,x.jsxs)("div",{className:"wc-chat__from",children:[p," is typing"]}),(0,x.jsx)("div",{className:"wc-chat__text outbound",children:(0,x.jsxs)("svg",{width:"28",height:"16",x:"0px",y:"0px",viewBox:"0 0 28 16",children:[(0,x.jsx)("circle",{stroke:"none",cx:"3",cy:"8",r:"3",fill:"currentColor"}),(0,x.jsx)("circle",{stroke:"none",cx:"14",cy:"8",r:"3",fill:"currentColor"}),(0,x.jsx)("circle",{stroke:"none",cx:"25",cy:"8",r:"3",fill:"currentColor"})]})})]}):null]})}),(0,x.jsxs)(D,{children:[(0,x.jsxs)("form",{className:"wc-form"+(v?" wc-focus-within":""),noValidate:!0,onSubmit:e=>{e.preventDefault(),j()},children:[(0,x.jsxs)("label",{className:"govuk-label wc-form__label",htmlFor:"wc-form-textarea",children:["Your message",(0,x.jsx)("span",{className:"govuk-visually-hidden",children:" (enter key submits)"})]}),(0,x.jsx)("textarea",{ref:y,rows:"1","aria-required":"true",className:"wc-form__textarea",id:"text-area",name:"message",onChange:e=>{w(e.target?.value)},onKeyDown:N,onFocus:()=>{b(u)},onBlur:()=>{b(!1)},value:m}),(0,x.jsx)("input",{type:"submit",className:"wc-form__button govuk-button",value:"Send","data-prevent-double-click":"true"})]}),(0,x.jsxs)("div",{className:"wc-footer__settings",children:[(0,x.jsx)("a",{href:"#",id:"wc-settings",className:"wc-footer__settings-link","data-module":"govuk-button",onKeyDown:N,onClick:t,children:"Settings"}),(0,x.jsx)("a",{href:"#",id:"transcript-download",className:"wc-footer__settings-link","data-module":"govuk-button",download:"floodline-webchat-transcript.txt",onKeyDown:N,onClick:S,children:"Save chat"})]})]})]})}function R(){return(0,x.jsxs)(x.Fragment,{children:[(0,x.jsx)(_,{}),(0,x.jsxs)("div",{className:"wc-body",children:[(0,x.jsx)("h3",{id:"wc-subtitle",className:"govuk-heading-m",children:"Webchat is currently not available"}),(0,x.jsx)("p",{className:"govuk-body-s",children:"Try again later, or call Floodline:"}),(0,x.jsxs)("p",{className:"govuk-body-s",children:[(0,x.jsx)("strong",{children:"Floodline helpline"}),(0,x.jsx)("br",{}),"Telephone: 0345 988 1188",(0,x.jsx)("br",{}),"Textphone: 0345 602 6340",(0,x.jsx)("br",{}),"Open 24 hours a day, 7 days a week",(0,x.jsx)("br",{}),(0,x.jsx)("a",{className:"govuk-link",href:"https://gov.uk/call-charges",children:"Find out more about call charges"})]}),(0,x.jsx)("p",{className:"govuk-body-s",children:"We're running webchat as a trial, it will not always be available."})]})]})}function F({onChatScreen:e,onEndChatConfirm:t}){const{setCustomerId:s,setThreadId:n,setMessages:o,agentStatus:r,thread:i,threadId:l,setUnseenCount:c,isKeyboard:d}=E(),u=async e=>{e.preventDefault(),window.localStorage.setItem("tmpThreadId",l),n(),o([]),s(),i.lastMessageSeen(),c(0),"closed"!==r&&i.endChat(),t(e)};(0,a.useEffect)(()=>{d&&setTimeout(()=>{document.querySelector("#confirm-endchat").focus()},10)},[]);const h=t=>{"Enter"!==t.key&&" "!==t.key||"confirm-endchat"!==t.target.id||u(t),"Enter"!==t.key&&" "!==t.key||"resume-chat"!==t.target.id||e(t)};return(0,x.jsxs)(x.Fragment,{children:[(0,x.jsx)(_,{}),(0,x.jsx)("div",{className:"wc-body",children:(0,x.jsxs)("div",{className:"wc-content",children:[(0,x.jsx)("h3",{id:"wc-subtitle",className:"wc-heading","aria-live":"polite",children:"Are you sure you want to end the chat?"}),(0,x.jsxs)("div",{className:"govuk-button-group",children:[(0,x.jsx)("a",{id:"confirm-endchat",href:"#endChat",role:"button",className:"wc-button govuk-button","data-module":"govuk-button",onClick:u,onKeyDown:h,children:"Yes, end chat"}),(0,x.jsx)("a",{id:"resume-chat",href:"#resumeChat",role:"button",className:"wc-link govuk-link","data-module":"govuk-button",onClick:e,onKeyDown:h,children:"No, resume chat"})]})]})})]})}function U({onCancel:e}){const{settings:t,setSettings:s}=E(),[n,o]=(0,a.useState)(t.audio),[r,i]=(0,a.useState)(t.scroll),l=t=>{t.preventDefault(),s({audio:n,scroll:r}),e(t)},c=t=>{"Enter"!==t.key&&" "!==t.key||"settings-save"!==t.target.id?"Enter"!==t.key&&" "!==t.key||"settings-cancel"!==t.target.id||e(t):l(t)};return(0,x.jsxs)(x.Fragment,{children:[(0,x.jsx)(_,{}),(0,x.jsx)("div",{className:"wc-body",children:(0,x.jsxs)("div",{className:"wc-content",children:[(0,x.jsxs)("fieldset",{className:"govuk-fieldset govuk-!-margin-bottom-4",children:[(0,x.jsx)("legend",{className:"govuk-fieldset__legend govuk-fieldset__legend",children:(0,x.jsx)("h3",{id:"wc-subtitle",className:"wc-heading",children:"Change settings"})}),(0,x.jsxs)("div",{className:"govuk-checkboxes govuk-checkboxes--small","data-module":"govuk-checkboxes",children:[(0,x.jsxs)("div",{className:"govuk-checkboxes__item",children:[(0,x.jsx)("input",{id:"audio",name:"audio",type:"checkbox",value:"audio",className:"govuk-checkboxes__input",defaultChecked:n,onChange:()=>o(!n)}),(0,x.jsx)("label",{className:"wc-label govuk-label govuk-checkboxes__label",htmlFor:"audio",children:"Play a sound when receiving a new message"})]}),(0,x.jsxs)("div",{className:"govuk-checkboxes__item",children:[(0,x.jsx)("input",{id:"scroll",name:"scroll",type:"checkbox",value:"scroll",className:"govuk-checkboxes__input",defaultChecked:r,onChange:()=>i(!r)}),(0,x.jsx)("label",{className:"wc-label govuk-label govuk-checkboxes__label",htmlFor:"scroll",children:"Scroll automatically to a new message"})]})]})]}),(0,x.jsxs)("div",{className:"govuk-button-group",children:[(0,x.jsx)("a",{id:"settings-save",href:"#",className:"wc-button govuk-button","data-module":"govuk-button",role:"button",onClick:l,onKeyDown:c,children:"Save"}),(0,x.jsx)("a",{id:"settings-cancel",href:"#",className:"wc-link govuk-link","data-module":"govuk-button",role:"button",onClick:e,onKeyDown:c,children:"Cancel"})]})]})})]})}function B({onCancel:e}){const t=()=>{const e=window.localStorage.getItem("tmpThreadId");window.location.href=`https://defragroup.eu.qualtrics.com/jfe/form/SV_8dgFSJcxxIfqx5Y?Id=${e}&Source=${window.location.href}`,window.localStorage.removeItem("tmpThreadId")},a=async t=>{window.localStorage.removeItem("tmpThreadId"),e(t)},s=e=>{"Enter"!==e.key&&" "!==e.key||("feedback-close"===e.target.id&&a(e),"feedback-send"===e.target.id&&t())};return(0,x.jsxs)(x.Fragment,{children:[(0,x.jsx)(_,{}),(0,x.jsx)("div",{className:"wc-body",children:(0,x.jsxs)("div",{className:"wc-content",children:[(0,x.jsx)("h3",{id:"wc-subtitle",className:"wc-heading",children:"Give feedback on Floodline webchat"}),(0,x.jsxs)("p",{children:["We're running webchat as a trial. you can ",(0,x.jsx)("a",{id:"feedback-send",className:"govuk-link",href:"#",target:"_blank",rel:"noreferrer",onKeyDown:s,onClick:t,children:"give feedback"})," to help us improve it."]}),(0,x.jsx)("p",{children:(0,x.jsx)("a",{id:"feedback-close",className:"wc-link govuk-link",href:"#","data-module":"govuk-button",role:"button",onKeyDown:s,onClick:a,children:"Close"})})]})})]})}const Y=require("lodash.debounce");var $=e.n(Y);const K=()=>{const{agentStatus:e,liveRegionText:t,setLiveRegionText:s}=E(),[n,o]=(0,a.useState)(),[r,i]=(0,a.useState)(),l="closed"===e||null===e,c=()=>{o(),i()},d=(0,a.useCallback)($()(e=>{n?i(e):o(e)},2e3),[n,r]);return(0,a.useEffect)(()=>(t&&(c(),d(t)),l&&(c(),s()),()=>{c(),s()}),[t]),(0,x.jsxs)(x.Fragment,{children:[(0,x.jsx)("div",{className:"wc-live",role:"status","aria-atomic":"true",children:n}),(0,x.jsx)("div",{className:"wc-live",role:"status","aria-atomic":"true",children:r})]})},V="aria-hidden",H="data-wc-inert",P=e=>{for(const t of document.body.children)"wc-panel"!==t.id&&(e&&!t.getAttribute(V)?(t.setAttribute(V,"true"),t.setAttribute(H,"")):t.hasAttribute(H)&&(t.removeAttribute(V),t.removeAttribute(H)))},W=e=>{const[t,s]=(0,a.useState)([]),n=(0,a.useCallback)(e=>{const a=document.querySelector("#wc-panel");"Tab"===e.key&&(a&&!document.activeElement.closest("#wc-panel")&&a.focus(),e.shiftKey?(document.activeElement===t[0]||document.activeElement===document.querySelector("#wc-panel"))&&(t[t.length-1].focus(),e.preventDefault()):document.activeElement===t[t.length-1]&&(t[0].focus(),e.preventDefault()))},[t]);(0,a.useEffect)(()=>{s((()=>{const e=document.body.querySelectorAll(["#wc-panel a:not([disabled])","#wc-panel button:not([disabled])","#wc-panel select:not([disabled])","#wc-panel input:not([disabled])","#wc-panel textarea:not([disabled])",'#wc-panel *[tabindex="0"]:not([disabled])'].join(","));return Array.from(e).filter(e=>!e.closest("[hidden]")&&!e.closest('[aria-hidden="true"]'))})())},[e]),(0,a.useEffect)(()=>(P(!0),document.querySelector("#wc-panel").focus(),document.addEventListener("keydown",n),()=>{P(),document.removeEventListener("keydown",n)}),[t])};function J(){const{sdk:e,availability:t,instigatorId:s,thread:n,threadId:i,setThread:l,setThreadId:c,setChatVisibility:d,setMessages:u,setUnseenCount:h,isMobile:g,isKeyboard:m}=E(),{fetchThread:w,fetchMessages:v}=C(e),[b,y]=(0,a.useState)(i?2:0);W(b);const f=(0,a.useCallback)(e=>{"Escape"!==e.key&&"Esc"!==e.key||(i&&(n?.lastMessageSeen(),h(0)),d(!1),S())},[n,i,h,d]);(0,a.useEffect)(()=>()=>{document.getElementById(s)?.focus()},[]),(0,a.useEffect)(()=>(document.body.classList.remove("wc-u-hidden"),document.getElementsByTagName("html")[0].classList.add("wc-u-html"),document.body.classList.add("wc-u-body"),()=>{document.getElementsByTagName("html")[0].classList.remove("wc-u-html"),document.body.classList.remove("wc-u-body")}),[g]),(0,a.useEffect)(()=>(document.addEventListener("keydown",f),()=>{document.removeEventListener("keydown",f)}),[f,d]),(0,a.useEffect)(()=>{i&&(n?(n.lastMessageSeen(),h(0),y(2)):(async()=>{try{const e=await w(i);l(e);const t=await v(e);u(t)}catch(e){console.log("[Chat Error] fetchThread",e),c(),l(),y(0)}})())},[n,i]);const p=e=>t=>{t.preventDefault(),y(e)},k=p(0),j=p(1),_=p(2),T=p(3),I=p(4),A=p(5);let D;switch(b){case 0:default:D=(0,x.jsx)(N,{onContinue:j});break;case 1:D=(0,x.jsx)(L,{onPreChatScreen:k});break;case 2:D=(0,x.jsx)(M,{onSettingsScreen:A,onEndChatScreen:T});break;case 3:D=(0,x.jsx)(F,{onChatScreen:_,onEndChatConfirm:I});break;case 4:D=(0,x.jsx)(B,{onCancel:()=>{d(!1),S()}});break;case 5:D=(0,x.jsx)(U,{onCancel:_})}"UNAVAILABLE"===t&&(D=(0,x.jsx)(R,{}));const G=(0,x.jsxs)("div",{id:"wc-panel",role:"dialog",className:o("wc-panel",m&&"wc-focus-visible"),tabIndex:"-1","aria-modal":"true","aria-labelledby":"wc-header wc-subtitle",children:[(0,x.jsx)("div",{className:"wc-panel__inner",children:D}),(0,x.jsx)(K,{})]});return(0,x.jsx)(x.Fragment,{children:(0,r.createPortal)(G,document.body)})}const X=()=>{const{threadId:e,setInstigatorId:t}=E();if(!e)return null;const a=document.getElementById("webchat-skip-link-container");return a?(0,x.jsx)(x.Fragment,{children:(0,r.createPortal)((0,x.jsx)("a",{id:"webchat-skip-link",href:"#webchat",className:"govuk-skip-link","data-module":"govuk-skip-link",onClick:e=>{e.preventDefault(),t(e.target.id),window.location.hash="#webchat"},"data-wc-skiplink":!0,"data-wc-open-btn":!0,children:"Skip to webchat"}),a)}):null};function Z(){const{availability:e,isChatOpen:t,setChatVisibility:s,unseenCount:n,threadId:r,setUnseenCount:i,setInstigatorId:l,setLiveRegionText:c}=E(),d=(0,a.useRef)(),u=n>0&&!t,h=n>1?"new messages":"new message",g=e=>{e.preventDefault(),i(0),s(!t),l(e.target.id),t?S():j()},m=e=>{" "===e.key&&e.preventDefault()},w=e=>{" "===e.key&&(i(0),s(!t))};switch((0,a.useEffect)(()=>(u&&c(`Floodline webchat - ${n} ${h}`),()=>{c()}),[n,t]),(0,a.useEffect)(()=>{const a=()=>{if(!r)return;if("AVAILABLE"!==e)return;const a=d.current.parentElement.getBoundingClientRect().top+35>(window.innerHeight||document.documentElement.clientHeight),s=!t&&a;document.documentElement.classList.toggle("wc-u-scroll-padding",s),document.body.classList.toggle("wc-u-scroll-padding",s),d.current.classList.toggle("wc-availability--fixed",s)};return document.addEventListener("scroll",a),a(),()=>{document.removeEventListener("scroll",a)}},[r,t,e]),e){case"AVAILABLE":return(0,x.jsxs)(x.Fragment,{children:[(0,x.jsx)("div",{className:o("wc-availability",t&&"wc-open"),ref:d,children:(0,x.jsx)("div",{className:"wc-availability__inner",children:(0,x.jsxs)("a",{id:"webchat-start-chat-link",className:"wc-availability__link",href:"#webchat",draggable:"false",onClick:g,onKeyUp:w,onKeyDown:m,role:"button","data-module":"govuk-button",children:[r?(0,x.jsx)(x.Fragment,{children:"Show chat"}):(0,x.jsx)(x.Fragment,{children:"Start chat"}),u?(0,x.jsxs)("span",{className:"wc-availability__unseen",children:[n,(0,x.jsxs)("span",{className:"govuk-visually-hidden",children:[" ",h]})]}):null]})})}),t?(0,x.jsx)(J,{}):(0,x.jsx)(K,{}),(0,x.jsx)(X,{})]});case"EXISTING":case"UNAVAILABLE":return(0,x.jsx)("p",{className:"govuk-body",children:"When it is available, a 'start chat' link will appear."});default:return(0,x.jsx)("p",{className:"govuk-body",children:"Checking availability"})}}const z=async e=>{const t=new(window.AudioContext||window.webkitAudioContext);if("suspended"===t.state){const e=["touchstart","touchend","mousedown","wheel","keydown","click"],a=s=>{e.forEach(e=>{document.body.removeEventListener(e,a)}),t.resume()};e.forEach(e=>{document.body.addEventListener(e,a,!1)})}const a=await fetch(e),s=await a.arrayBuffer(),n=await t.decodeAudioData(s);return()=>{const e=t.createBufferSource();e.buffer=n,e.connect(t.destination),e.start()}};async function Q(e,t){const a=new n.ChatSdk({brandId:t.brandId,channelId:t.channelId,customerId:globalThis.localStorage.getItem(w)||"",environment:t.environment,isLivechat:!0,cacheStorage:new n.CacheStorage(globalThis.localStorage),storage:globalThis.localStorage}),o=(0,s.createRoot)(e);let r,i;try{const e=await async function(e){const t=await fetch(e);return{availability:(await t.json()).availability||"UNAVAILABLE"}}(t.availabilityEndpoint);i=await z(t.audioUrl),r=e.availability}catch(e){r="UNAVAILABLE"}o.render((0,x.jsx)(k,{sdk:a,availability:r,playSound:i,children:(0,x.jsx)(Z,{})}))}module.exports=t})();
|