1import{j as e}from"./data-DN-eP4ae.js";import{k as Y,r as g}from"./react-BUOzgyp0.js";import{s as $,i as q,V as J,a4 as L,$ as U,B as I,bA as V,b0 as Z}from"./index-6GUxm5Tl.js";import{d as O,C as G}from"./calendar-4kXG8opa.js";import{C as R,a as A,b as K,c as Q}from"./card-Bpoa90g8.js";import{u as W}from"./usePublicOrgBranding-CJd6EbdP.js";import{A as X,s as ee}from"./recent-bookings-91GutVZc.js";import{f as k,s as te}from"./format-BOCe1Q81.js";import{g as z}from"./getDay-BYaKWRsU.js";import{a as F}from"./addDays-FNsJkNgj.js";import"./documents-Bju1cS7G.js";import"./charts-BjVT6r80.js";import"./addMonths-CBp3ZtZ_.js";import"./isSameDay-BZkPoTMS.js";import"./startOfMonth-kO_ql4le.js";import"./subDays-BHo4iHhB.js";import"./chevron-left-BO8s0r98.js";import"./calendar-plus-Y9ZLRDRx.js";function se(x,f,m,M,h){const a=x.filter(t=>t.is_available);if(a.length===0)return[];const D=k(h,"yyyy-MM-dd"),l=M.filter(t=>t.booked_date===D),b=[];for(const t of a){const[B,i]=t.start_time.split(":").map(Number),[C,u]=t.end_time.split(":").map(Number),w=B*60+i,s=C*60+u;let p=w;for(;p+f<=s;){const y=`${Math.floor(p/60).toString().padStart(2,"0")}:${(p%60).toString().padStart(2,"0")}`,v=`${Math.floor((p+f)/60).toString().padStart(2,"0")}:${((p+f)%60).toString().padStart(2,"0")}`;!l.some(N=>y<N.end_time&&v>N.start_time)&&!b.includes(y)&&b.push(y),p+=f+m}}return b.sort(),b}function je(){const{token:x}=Y(),[f,m]=g.useState("loading"),[M,h]=g.useState(""),[a,D]=g.useState(null),[l,b]=g.useState(null),[t,B]=g.useState(null),[i,C]=g.useState(),[u,w]=g.useState(null),{branding:s,brandStyle:p}=W(a==null?void 0:a.org_id);g.useEffect(()=>{if(!x){m("invalid"),h("No reschedule token provided.");return}(async()=>{var d;const{data:n,error:o}=await $.from("booking_reschedule_tokens").select("*, bookings(*, meeting_types(*))").eq("token",x).maybeSingle();if(o||!n){m("invalid"),h("This reschedule link is invalid.");return}if(n.used){m("invalid"),h("This reschedule link has already been used.");return}if(n.expires_at&&new Date(n.expires_at)<new Date){m("invalid"),h("This reschedule link has expired. Please contact the host to get a new one.");return}const r=n.bookings;if(!r){m("invalid"),h("Booking not found.");return}if(r.status==="cancelled"){m("invalid"),h("This booking has been cancelled.");return}D(r),b(r.meeting_types);const _=(d=r.meeting_types)==null?void 0:d.booking_settings_id;if(_){const{data:S}=await $.rpc("get_public_booking_availability",{_booking_settings_id:_}),{data:T}=await $.rpc("get_public_booked_slots",{_booking_settings_id:_,_from:k(new Date,"yyyy-MM-dd"),_to:k(new Date(Date.now()+60*864e5),"yyyy-MM-dd")}),H=(T||[]).map(c=>({booked_date:String(c.booked_date),start_time:String(c.start_time).slice(0,5),end_time:String(c.end_time).slice(0,5),team_member_id:c.team_member_id??null})),E=c=>{const j=c.split(":");return j.length>=2?`${j[0]}:${j[1]}`:c};B({settings:{id:_,user_id:r.host_user_id||r.user_id,org_id:r.org_id,slug:"",buffer_minutes:10,booking_window_days:30,notifications_enabled:!0,reminder_hours:[24,1]},hostUser:null,meetingTypes:[],availability:(S||[]).map(c=>({...c,start_time:E(c.start_time),end_time:E(c.end_time)})),existingBookings:H,offers:[],teamMembers:[],services:[],branches:[],staff:[]})}m("select")})()},[x]);const y=g.useMemo(()=>{if(!i||!l||!t)return[];const n=z(i),o=t.availability.filter(r=>r.day_of_week===n);return se(o,l.duration_minutes,t.settings.buffer_minutes,t.existingBookings,i)},[i,l,t]),v=g.useMemo(()=>{if(!i||!u||!l)return null;const[n,o]=u.split(":").map(Number),r=new Date(i);r.setHours(n,o,0,0);const _=new Date(r.getTime()+l.duration_minutes*6e4),d=a||{};return{title:`${l.name}${s!=null&&s.business_name?` - ${s.business_name}`:""}`,description:[s!=null&&s.business_name?`Booked with ${s.business_name}`:"",d.booking_code?`Booking ID: ${d.booking_code}`:""].filter(Boolean).join(` 2`),start:r,end:_,location:d.meeting_link||void 0,url:d.meeting_link||void 0,organizerName:(s==null?void 0:s.business_name)||void 0,attendeeName:d.guest_name||d.contact_name||void 0,attendeeEmail:d.guest_email||d.contact_email||void 0,uid:d.booking_code||void 0}},[i,u,l,a,s]),P=g.useMemo(()=>
2{if(!t)return;const n=new Set(t.availability.filter(o=>o.is_available).map(o=>o.day_of_week));return o=>O(o,te(new Date))||O(F(new Date,t.settings.booking_window_days),o)?!0:!n.has(z(o))},[t]),N=async()=>{var n,o;if(!(!i||!u||!a||!l)){m("confirming");try{const r=l.duration_minutes,[_,d]=u.split(":").map(Number),S=_*60+d+r,T=`${Math.floor(S/60).toString().padStart(2,"0")}:${(S%60).toString().padStart(2,"0")}`,c=await(await fetch("https://undefined.supabase.co/functions/v1/reschedule-booking",{method:"POST",headers:{"Content-Type":"application/json",apikey:void 0},body:JSON.stringify({token:x,new_date:k(i,"yyyy-MM-dd"),new_start_time:u,new_end_time:T})})).json();if(c.success){if(a!=null&&a.booking_code){const j=new Date(i);j.setHours(_,d,0,0),ee({code:a.booking_code,title:l.name,business:((n=t==null?void 0:t.settings)==null?void 0:n.business_name)||null,startsAt:j.toISOString(),timeZone:((o=t==null?void 0:t.settings)==null?void 0:o.timezone)||null,slug:null})}m("done")}else h(c.error||"Failed to reschedule."),m("invalid")}catch{h("Something went wrong. Please try again."),m("invalid")}}};return f==="loading"?e.jsx("div",{className:"min-h-screen flex items-center justify-center bg-background p-4",children:e.jsx(q,{className:"h-96 w-full max-w-lg"})}):f==="invalid"?e.jsx("div",{className:"min-h-screen flex items-center justify-center bg-background p-4",children:e.jsx(R,{className:"max-w-md w-full",children:e.jsxs(A,{className:"p-8 text-center",children:[e.jsx(J,{className:"h-12 w-12 text-destructive/60 mx-auto mb-4"}),e.jsx("h2",{className:"text-lg font-semibold",children:"Cannot Reschedule"}),e.jsx("p",{className:"text-sm text-muted-foreground mt-1",children:M})]})})}):f==="done"?e.jsx("div",{className:"min-h-screen flex items-center justify-center bg-background p-4",children:e.jsx(R,{className:"max-w-md w-full",children:e.jsxs(A,{className:"p-8 text-center space-y-3",children:[e.jsx(L,{className:"h-16 w-16 text-primary mx-auto"}),e.jsx("h3",{className:"text-lg font-semibold",children:"Booking Rescheduled!"}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:["Your appointment has been moved to"," ",e.jsx("strong",{children:i&&k(i,"EEEE, d MMMM")})," at ",e.jsx("strong",{children:u}),"."]}),v&&e.jsx("div",{className:"flex justify-center",children:e.jsx(X,{event:v,filename:"rescheduled-appointment.ics"})}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"A confirmation email has been sent."})]})})}):e.jsx("div",{className:"min-h-screen bg-background flex items-start justify-center p-4 pt-8 md:pt-16",style:p,children:e.jsxs(R,{className:"w-full max-w-lg",children:[e.jsxs(K,{className:"text-center pb-2",children:[(s==null?void 0:s.logo_url)&&e.jsx("img",{src:s.logo_url,alt:`${s.business_name||"Business"} logo`,className:"h-10 mx-auto mb-2 object-contain"}),(s==null?void 0:s.business_name)&&e.jsx("p",{className:"text-xs font-medium uppercase tracking-wide text-primary",children:s.business_name}),e.jsx(Q,{children:"Reschedule Your Booking"}),a&&e.jsxs("div",{className:"text-sm text-muted-foreground space-y-1 mt-2",children:[e.jsxs("p",{children:["Current: ",e.jsx("strong",{children:a.booked_date})," at ",e.jsx("strong",{children:a.start_time})]}),l&&e.jsxs(U,{variant:"secondary",children:[l.name," · ",l.duration_minutes," min"]})]})]}),e.jsxs(A,{className:"space-y-4",children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"Pick a new date and time below:"}),e.jsx("div",{className:"flex justify-center",children:e.jsx(G,{mode:"single",selected:i,onSelect:n=>{C(n),w(null)},disabled:P,fromDate:new Date,toDate:t?F(new Date,t.settings.booking_window_days):void 0})}),i&&e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium mb-2",children:k(i,"EEEE, d MMMM")}),y.length===0?e.jsx("p",{className:"text-xs text-muted-foreground",children:"No available slots on this day."}):e.jsxs(e.Fragment,{children:[e.jsx("p",{className:"text-xs text-muted-foreground mb-3",children:"Select an available time slot below to continue."}),e.jsx("div",{className:"grid grid-cols-3 sm:grid-cols-4 gap-2",children:y.map(n=>e.jsx(I,{variant:u===n?"default":"outline",size:"sm",onClick:()=>w(n),className:"text-xs",children:n},n))})]}),u&&e.jsxs(I,{className:"w-full mt-4",onClick:N,disabled:f==="confirming",children:[f==="confirming"?e.jsx(V,{className:"h-4 w-4 mr-2 animate-spin"}):e.jsx(Z,{className:"h-4 w-4 mr-2"}),"Confirm Reschedule"]})]})]})]})})}export{je 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.