import{r,_ as O}from"./wp-i18n-shim-DUX0OV_V.js";var I;const v=(((I=window.radius_booking_chatbot_param)==null?void 0:I.rest_root)||"/wp-json/")+"radius-booking/v1";async function C(s,g=[],t="",e){const _={message:s,history:g};e&&(_.completed_steps=e);const n=await fetch(`${v}/chatbot/message`,{method:"POST",headers:{"Content-Type":"application/json","X-WP-Nonce":t},credentials:"same-origin",body:JSON.stringify(_)});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 h="rtrb_chatbot_messages",E="rtrb_chatbot_version",w="2",N=20;function A({welcomeMessage:s,nonce:g}){const[t,e]=r.useState(()=>{try{if(sessionStorage.getItem(E)!==w)sessionStorage.removeItem(h),sessionStorage.setItem(E,w);else{const f=sessionStorage.getItem(h);if(f){const i=JSON.parse(f);if(Array.isArray(i)&&i.length>0)return i}}}catch{}return s?[{role:"assistant",content:s}]:[]}),[_,n]=r.useState(!1),o=r.useRef(!1),a=r.useRef(t);a.current=t,r.useEffect(()=>{try{sessionStorage.setItem(h,JSON.stringify(t))}catch{}},[t]);const l=r.useCallback(async d=>{var i;if(!d.trim()||o.current)return;o.current=!0;const f={role:"user",content:d};e(c=>[...c,f]),n(!0);try{const c=[...a.current,f],y=c.slice(-N).map(({role:u,content:R})=>({role:u,content:R})),p=[];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&&!p.includes(S)&&p.push(S)}const m=await C(d,y,g,p.length>0?p: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:O("Sorry, I'm having trouble responding right now. Please try again.","radius-booking")}])}finally{n(!1),o.current=!1}},[g]),k=r.useCallback(()=>{e(s?[{role:"assistant",content:s}]:[]),o.current=!1;try{sessionStorage.removeItem(h)}catch{}},[s]);return{messages:t,isLoading:_,sendMessage:l,clearMessages:k}}export{A as u};
//# sourceMappingURL=useChat-TMApkGM8.js.map
