import{r as i,j as d,a as st}from"./vendor-react-CVHocMAO.js";import{F as E,bn as ot,d7 as it,c5 as at}from"./index-DXdAv5n3.js";const N=(o,p=!1)=>{if(!o||!o.trim())return"";const x=o.split(":");if(x.length!==2)return"";const w=parseInt(x[0],10),y=parseInt(x[1],10);if(isNaN(w)||isNaN(y)||w<0||w>=24||y<0||y>=60)return"";const v=w>=12?"PM":"AM",R=`${w%12||12}:${y.toString().padStart(2,"0")} ${v}`;return p?`${R} (next day)`:R},ct=({value:o,onChange:p,onInvalidTime:x,placeholder:w="Add time",className:y,disabled:v=!1,minTime:F,maxTime:R,options:j})=>{const U=t=>{if(!t)return;const r=t.match(/^(\d{1,2}):(\d{2})/);if(!r)return;const e=Number(r[1]),n=Number(r[2]);if(!(Number.isNaN(e)||Number.isNaN(n)||e<0||e>23||n<0||n>59))return`${String(e).padStart(2,"0")}:${String(n).padStart(2,"0")}`},u=U(F),a=U(R),W=i.useMemo(()=>!!(u&&a&&a<u),[u,a]),[l,T]=i.useState(!1),[b,c]=i.useState(N(o)),[$,q]=i.useState(null),[M,X]=i.useState(!1),I=i.useRef(null),O=i.useRef(null),L=i.useRef(null),k=i.useRef(null),B=i.useRef(!1),Y=()=>{const t=[];for(let r=0;r<24;r++)for(let e=0;e<60;e+=15){const n=r.toString().padStart(2,"0"),m=e.toString().padStart(2,"0");t.push(`${n}:${m}`)}return t},G=i.useMemo(()=>Y(),[]),J=i.useMemo(()=>G.filter(t=>u&&a&&a<u?t>=u||t<=a:!(u&&t<u||a&&t>a)),[G,u,a]),f=i.useMemo(()=>Array.isArray(j)&&j.length>0?j:J.map(t=>({value:t,isNextDay:!!(W&&a&&t<=a)})),[j,J,W,a]),H=i.useMemo(()=>f.find(t=>t.value===o),[f,o]),S=i.useMemo(()=>o?N(o,!!(H!=null&&H.isNextDay)):"",[o,H]);i.useEffect(()=>{c(o?S:"")},[o,S]);const P=i.useMemo(()=>{if(!l)return[];if(!b.trim()||b===S)return f;const t=b.trim(),r=t.match(/^(\d{1,2}):?(\d{0,2})$/);if(!r){const V=t.toLowerCase();return f.filter(h=>N(h.value,!!h.isNextDay).toLowerCase().includes(V))}const e=parseInt(r[1],10),n=r[2]||"",m=n.length>0,s=m?parseInt(n.padEnd(2,"0"),10):null;if(isNaN(e)||e<0||e>23||s!==null&&(isNaN(s)||s<0||s>59))return f;const g=f.filter(V=>{const[h,z]=V.value.split(":").map(Number);if(s!==null&&m){const D=Math.round(s/15)*15;if(e>=1&&e<=12){if(h===e&&z===D)return!0;const nt=e===12?0:e+12;if(h===nt&&z===D)return!0}else if(h===e&&z===D)return!0;return!1}if(e>=1&&e<=12){const D=e===12?0:e+12;return h===e||h===D}else return h===e});return g.length>0?g:f},[l,b,f,S]);i.useEffect(()=>{(()=>{var e;const r=!!((e=I.current)!=null&&e.closest('[role="dialog"]'));X(r)})()},[]),i.useEffect(()=>{if(l&&!M){const t=()=>{const r=I.current??O.current;if(!r)return;const e=r.getBoundingClientRect();q({top:e.bottom+4,left:e.left,width:e.width})};return t(),window.addEventListener("resize",t),window.addEventListener("scroll",t,!0),()=>{window.removeEventListener("resize",t),window.removeEventListener("scroll",t,!0)}}else q(null)},[l,M]),i.useEffect(()=>{l&&o&&k.current&&setTimeout(()=>{const t=P.findIndex(r=>r.value===o);if(t>=0&&k.current){const r=k.current.children[t];r&&r.scrollIntoView({block:"nearest",behavior:"smooth"})}},0)},[l,o,P]),i.useEffect(()=>{const t=r=>{I.current&&!I.current.contains(r.target)&&L.current&&!L.current.contains(r.target)&&(T(!1),c(o?N(o):""))};if(l)return document.addEventListener("mousedown",t),()=>document.removeEventListener("mousedown",t)},[l,o]);const K=t=>{if(!t.trim())return null;const r=t.match(/^(\d{1,2}):?(\d{0,2})\s*(AM|PM|am|pm)$/i);if(r){let n=parseInt(r[1],10);const m=r[2]||"0";let s=parseInt(m,10);const g=r[3].toUpperCase();if(n>=1&&n<=12&&s>=0&&s<60)return s=Math.round(s/15)*15,s===60&&(s=0,n=n%12+1),g==="PM"&&n!==12&&(n+=12),g==="AM"&&n===12&&(n=0),`${n.toString().padStart(2,"0")}:${s.toString().padStart(2,"0")}`}const e=t.match(/^(\d{1,2}):?(\d{0,2})$/);if(e&&!t.match(/\s*(AM|PM|am|pm)/i)){const n=parseInt(e[1],10),m=e[2]||"0";let s=parseInt(m,10);if(n>=0&&n<24&&s>=0&&s<60){s=Math.round(s/15)*15;let g=n;return s===60&&(s=0,g=(n+1)%24),`${g.toString().padStart(2,"0")}:${s.toString().padStart(2,"0")}`}}return null},A=t=>f.length>0?f.some(e=>e.value===t):!!(u&&a&&a<u)?t>=u||t<=a:(!u||t>=u)&&(!a||t<=a),Z=t=>{const r=t.target.value;if(c(r),r.trim()===""){p("");return}const e=K(r);e&&A(e)&&p(e)},_=()=>{if(B.current){B.current=!1;return}const t=K(b);if(t){if(!A(t)){x==null||x("Selected time is outside the available booking window."),o&&A(o)?c(S):(c(""),p(""));return}const[e,n]=t.split(":").map(Number),m=Math.round(n/15)*15,s=`${e.toString().padStart(2,"0")}:${m.toString().padStart(2,"0")}`;c(N(s)),p(s)}else b.trim()===""?(p(""),c("")):o?c(S):(c(""),p(""))},tt=()=>{T(!0)},et=t=>{B.current=!0,p(t.value),c(N(t.value,!!t.isNextDay)),T(!1),setTimeout(()=>{var r;(r=O.current)==null||r.blur(),B.current=!1},150)},rt=()=>{var t;v||(T(!l),l||(t=O.current)==null||t.focus())},C=180,Q=d.jsx("div",{ref:L,className:E("bg-white border border-gray-200 rounded-lg shadow-lg overflow-hidden",M?"absolute z-[60] mt-1 w-full":"fixed z-[20000]"),style:M?{maxHeight:`${C}px`}:$?{top:$.top,left:$.left,width:$.width,maxHeight:`${C}px`}:void 0,children:d.jsxs("div",{ref:k,className:"overflow-y-auto",style:{maxHeight:`${C}px`},children:[P.map(t=>{const r=o===t.value;return d.jsx("button",{type:"button",onMouseDown:e=>{e.preventDefault()},onClick:()=>et(t),className:E("w-full px-4 py-2 text-left text-sm hover:bg-gray-50 transition-colors",r&&"bg-brand/10 text-brand font-medium"),children:N(t.value,!!t.isNextDay)},`${t.value}-${t.isNextDay?"next":"same"}`)}),P.length===0&&d.jsx("div",{className:"px-4 py-3 text-sm text-gray-500",children:"No times available for this date."})]})});return d.jsxs("div",{className:E("relative",y),ref:I,children:[d.jsxs("div",{className:"relative",children:[d.jsx("div",{className:"absolute left-3 top-1/2 -translate-y-1/2 text-gray-400",children:d.jsx(ot,{className:"h-4 w-4"})}),d.jsx("input",{ref:O,type:"text",value:b,onChange:Z,onBlur:_,onFocus:tt,placeholder:w,disabled:v,className:E("w-full pl-10 pr-10 py-2.5 rounded-lg border border-gray-300","focus:outline-none focus:ring-2 focus:ring-brand focus:border-transparent","disabled:bg-gray-100 disabled:cursor-not-allowed","text-base sm:text-sm")}),d.jsx("button",{type:"button",onClick:rt,disabled:v,className:E("absolute right-2 top-1/2 -translate-y-1/2 text-gray-400 hover:text-gray-600","disabled:cursor-not-allowed"),children:l?d.jsx(it,{className:"h-4 w-4"}):d.jsx(at,{className:"h-4 w-4"})})]}),l&&M&&Q,l&&!M&&$&&st.createPortal(Q,document.body)]})};export{ct as T};