import{r}from"./client-BbMi5ewG.js";var I;const k=(((I=window.radius_booking_chatbot_param)==null?void 0:I.rest_root)||"/wp-json/")+"radius-booking/v1";async function v(s,g=[],t="",e){const p={message:s,history:g};e&&(p.completed_steps=e);const n=await fetch(`${k}/chatbot/message`,{method:"POST",headers:{"Content-Type":"application/json","X-WP-Nonce":t},credentials:"same-origin",body:JSON.stringify(p)});if(!n.ok){const l=await n.json().catch(()=>({}));throw new Error((l==null?void 0:l.message)||`Request failed with status ${n.status}`)}const o=await n.json(),a=o.data||o;return typeof a=="object"&&a!==null?a:{reply:String(a||"")}}const _="rtrb_chatbot_messages",E="rtrb_chatbot_version",w="2",C=20;function T({welcomeMessage:s,nonce:g}){const[t,e]=r.useState(()=>{try{if(sessionStorage.getItem(E)!==w)sessionStorage.removeItem(_),sessionStorage.setItem(E,w);else{const d=sessionStorage.getItem(_);if(d){const i=JSON.parse(d);if(Array.isArray(i)&&i.length>0)return i}}}catch{}return s?[{role:"assistant",content:s}]:[]}),[p,n]=r.useState(!1),o=r.useRef(!1),a=r.useRef(t);a.current=t,r.useEffect(()=>{try{sessionStorage.setItem(_,JSON.stringify(t))}catch{}},[t]);const l=r.useCallback(async f=>{var i;if(!f.trim()||o.current)return;o.current=!0;const d={role:"user",content:f};e(c=>[...c,d]),n(!0);try{const c=[...a.current,d],y=c.slice(-C).map(({role:u,content:R})=>({role:u,content:R})),h=[];for(const u of c)if((i=u.ui_data)!=null&&i.type){const S={services:"service_shown",employees:"staff_shown",date_picker:"dates_shown",availability:"timeslots_shown",booking_confirmed:"booking_created"}[u.ui_data.type];S&&!h.includes(S)&&h.push(S)}const m=await v(f,y,g,h.length>0?h:void 0),b={role:"assistant",content:m.reply};m.ui_data&&(b.ui_data=m.ui_data),e(u=>[...u,b])}catch(c){console.error("[Chatbot] Send message error:",c),e(y=>[...y,{role:"assistant",content:"Sorry, I'm having trouble responding right now. Please try again."}])}finally{n(!1),o.current=!1}},[g]),O=r.useCallback(()=>{e(s?[{role:"assistant",content:s}]:[]),o.current=!1;try{sessionStorage.removeItem(_)}catch{}},[s]);return{messages:t,isLoading:p,sendMessage:l,clearMessages:O}}export{T as u};
//# sourceMappingURL=useChat-bwGbsjhF.js.map
