import{b as Re,r,s as Qe,q as We,Y as ae,h as pe,e as be,j as e,B as C,f as A,d as Be,Z as es,_ as ss,$ as ts}from"./index-DLIdDvtv.js";import{D as ve,a as Ce,c as Se,d as we,b as as}from"./dialog-Cz2djAlK.js";import{I as ne}from"./input-hEr-DKad.js";import{C as De}from"./calendar-CC4EfbAP.js";import{P as Fe,a as Pe,b as Ie}from"./popover-DoWpMTtN.js";import{S as Te,a as Ee,b as qe,c as Me,d as Ae}from"./select-0XKWeVb4.js";import{p as ns,t as rs,u as is,C as ls,v as Oe,c as os}from"./CustomFieldRenderer-kL_AcfC9.js";import{u as cs}from"./use-category-margins-CW9Ojhj6.js";import{S as ms}from"./skeleton-D1cfBzST.js";import{C as re}from"./check-56-l036b.js";import{C as _e}from"./calendar-DwFQA7Pt.js";import{C as ds}from"./clock-BvxlWn2P.js";import{U as us}from"./users-Bt6dZUqf.js";import{S as $e}from"./send-BsOECO4t.js";import{S as ke}from"./shopping-cart-CV6-c52z.js";const fs=["08:00","09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00","19:00"];function xs({open:x,onOpenChange:g,product:s,timeSlots:S,priceTiers:h,tierTotal:w,tierBreakdown:d,maxPax:O,isOnRequest:_}){var ye;const $=S&&S.length>0?S:fs,{addItem:R}=Re(),{data:D}=cs(),K=D!==void 0,q=(D==null?void 0:D.transfer)??0,F=(h||[]).some(t=>t.type==="group_flat"),k=(d||[]).reduce((t,a)=>t+(Number(a.count)||0)*(Number(a.price)||0),0),j=F?(d||[]).reduce((t,a)=>t+(Number(a.price)||0),0):0,J=w!=null&&Number.isFinite(Number(w))?Math.max(0,Number(w)):0,N=F?j>0?j:J:k>0?k:J,o=!!(h&&h.length>0&&N>0),c=s.type==="transfer"&&!o,P=c&&s.priceType==="per_item",y=c&&!P,[U,V]=r.useState(null);r.useEffect(()=>{if(!x||!c)return;const t=s.cutoff,a=t==null?null:Number(t);if(a!=null&&Number.isFinite(a)&&a>0){V(a);return}let l=!1;return(async()=>{try{const{data:n,error:f}=await Qe.from("product").select("cutoff").eq("id",s.id).maybeSingle();if(l)return;if(f){console.warn("Could not load transfer cutoff:",f),V(null);return}const m=n==null?void 0:n.cutoff,E=m==null||!Number.isFinite(Number(m))||Number(m)<=0?null:Number(m);V(E)}catch(n){if(l)return;console.warn("Could not load transfer cutoff:",n),V(null)}})(),()=>{l=!0}},[c,x,s.cutoff,s.id]);const Q=r.useMemo(()=>{const t=c?U:s.cutoff,a=t==null?null:Number(t);return a==null||!Number.isFinite(a)||a<=0?null:a},[c,s.cutoff,U]),I=r.useMemo(()=>{const t=new Date,a=c&&Q!=null?We(t,Q):t;return ae(a)},[Q,c]),Je=r.useMemo(()=>c?I:pe(new Date,7),[c,I]),[i,ie]=r.useState(Je),[u,le]=r.useState(s.showEndDate?pe(new Date,10):void 0),[W,Ue]=r.useState($[0]||"10:00"),[p,Ve]=r.useState(2),[B,oe]=r.useState(!1),[ee,M]=r.useState(null),[b,ce]=r.useState({}),[Ge,G]=r.useState({}),[se,me]=r.useState(""),[L,de]=r.useState(""),[ue,fe]=r.useState(""),[Z,z]=r.useState(""),[X,T]=r.useState(""),[xe,he]=r.useState(!1),[Le,je]=r.useState(!1),Ze=(typeof s.category=="string"&&s.category.trim()!==""?s.category.trim():ns(s.type))??"",te=s.type==="transfer"?rs(s.origin,s.destination):void 0,ge=_&&te!=="city_to_airport"&&te!=="city_to_city",{data:ze}=is({scope:"product",productCategory:Ze,productId:s.id,transferSubcategory:te}),v=ze??[];r.useEffect(()=>{x&&(ce({}),G({}),me(""),de(""),fe(""),z(""),T(""),je(!1))},[x,s.id]);const Xe=(()=>{const t=Number(O),a=Number.isFinite(t)&&t>0?t:8,l=Math.min(12,Math.max(1,a));return Array.from({length:l},(n,f)=>f+1)})(),He=async()=>{if(!i){M("Please select a date");return}M(null);const t=Oe(v,b);if(Object.keys(t).length>0){G(t);return}let a=!1;if(se.trim()?z(""):(z("Name is required"),a=!0),L.trim()?L.includes("@")?T(""):(T("Enter a valid email address"),a=!0):(T("Email is required"),a=!0),a)return;const l=v.filter(m=>b[m.field_name]!=null&&b[m.field_name]!=="").map(m=>({label:m.label,value:String(b[m.field_name])})),n="https://fsustnroyahxlefsvwkl.supabase.co",f="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImZzdXN0bnJveWFoeGxlZnN2d2tsIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzQ5NzY1NDksImV4cCI6MjA5MDU1MjU0OX0.Ju_68r5ZtiOb_krIjedJuFVpLT2pcxQ5_xaef2pP6Xg";he(!0);try{const m=await fetch(`${n}/functions/v1/submit-transfer-request`,{method:"POST",headers:{"Content-Type":"application/json",apikey:f,Authorization:`Bearer ${f}`},body:JSON.stringify({transferName:s.title,origin:s.origin,destination:s.destination,date:A(i,"PPP"),time:s.showTime?W:void 0,travelers:p,customFields:l,contactName:se.trim(),contactEmail:L.trim(),contactPhone:ue.trim()||void 0,estimatedPrice:Y!=null?`$${Y.toFixed(2)}`:void 0,source_page:window.location.pathname})}),E=await m.json().catch(()=>({}));if(!m.ok||!E.success)throw new Error(E.error||"Unable to send request. Please try again.");je(!0)}catch(m){const E=m instanceof Error?m.message:"Unable to send request. Please try again.";T(E)}finally{he(!1)}},Ye=()=>{if(!i){M("Please select a date");return}M(null);const t=Oe(v,b);if(Object.keys(t).length>0){G(t);return}const a=s.showEndDate&&i&&u?Math.max(1,Math.ceil((u.getTime()-i.getTime())/(1e3*60*60*24))):1,l={id:`${s.id}-${Date.now()}`,productId:s.id,productGroupId:s.productGroupId,type:s.type,title:s.title,subtitle:o&&d?d.map(n=>`${n.count} × ${n.label}`).join(", "):s.subtitle,imageUrl:s.imageUrl,startDate:i?A(i,"yyyy-MM-dd"):void 0,endDate:u?A(u,"yyyy-MM-dd"):void 0,time:s.showTime?W:void 0,quantity:o||y?1:p,unitPrice:o?N:s.unitPrice*a,currency:"USD",status:s.status,provider:s.provider||"MOCK",quantityLabel:s.quantityLabel,metadata:(()=>{const n={};return o&&(d!=null&&d.length)&&(n.tierBreakdown=d.map(f=>({label:String(f.label??""),count:Number(f.count)||0,price:Number(f.price)||0}))),o&&(n.tierTotalPrice=N),o&&F&&(n.priceByGroup=!0),o&&h&&h.length>0&&(n.catalogPriceTiers=h),y&&(n.itemType="transfer",n.guestCount=p),P&&(n.itemType="transfer",n.guestCount=p),s.type==="transfer"&&(s.origin&&(n.transferOrigin=s.origin),s.destination&&(n.transferDestination=s.destination)),v.length>0&&(n.customFieldValues=b,n.customFieldDefinitions=os(v)),Object.keys(n).length?n:void 0})()};R(l),oe(!0),setTimeout(()=>{oe(!1),g(!1)},1500)},Ke=s.pricingBasis==="retail";r.useEffect(()=>{c&&((!i||ae(i)<I)&&ie(I),s.showEndDate&&u&&i&&u<i&&le(void 0))},[u,c,I,s.showEndDate,i]);const H=s.showEndDate&&i&&u?Math.max(1,Math.ceil((u.getTime()-i.getTime())/(1e3*60*60*24))):1,Ne=o?N:y?s.unitPrice*H:s.unitPrice*p*H,Y=o?Ne:c&&!Ke?K?y?be(s.unitPrice,q)*H:be(s.unitPrice,q)*p*H:null:Ne;return Le?e.jsx(ve,{"data-name":"BookingModal",open:x,onOpenChange:g,children:e.jsxs(Ce,{className:"sm:max-w-md flex flex-col items-center text-center gap-6 py-10 select-none",children:[e.jsx("div",{className:"flex items-center justify-center w-16 h-16 rounded-full bg-green-100",children:e.jsx(re,{className:"w-8 h-8 text-green-600"})}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(Se,{className:"font-serif text-xl",children:"Request Received"}),e.jsx(we,{className:"font-sans text-sm text-muted-foreground",children:"We received your request and will be in touch shortly to confirm your transfer."})]}),e.jsx(C,{variant:"accent",className:"w-full",onClick:()=>g(!1),children:"OK"})]})}):e.jsx(ve,{"data-name":"BookingModal",open:x,onOpenChange:g,children:e.jsxs(Ce,{className:"sm:max-w-md flex flex-col max-h-[85vh]",children:[e.jsxs(as,{children:[e.jsx(Se,{className:"font-serif text-xl",children:s.title}),e.jsx(we,{className:"font-sans text-sm",children:s.subtitle})]}),e.jsxs("div",{className:"overflow-y-auto flex-1 space-y-6 py-4 pr-3 pl-3",children:[ge&&e.jsxs("div",{className:"space-y-4 pb-6 border-b border-border pl-1",children:[e.jsx("p",{className:"text-sm font-sans font-medium text-foreground",children:"Your contact information"}),e.jsxs("div",{className:"space-y-1",children:[e.jsxs("label",{className:"text-sm font-sans font-medium flex items-center gap-0.5",children:["Name",e.jsx("span",{className:"text-destructive",children:"*"})]}),e.jsx(ne,{value:se,onChange:t=>{me(t.target.value),Z&&z("")},placeholder:"Full name",className:Z?"border-destructive":""}),Z&&e.jsx("p",{className:"text-destructive text-xs",children:Z})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsxs("label",{className:"text-sm font-sans font-medium flex items-center gap-0.5",children:["Email",e.jsx("span",{className:"text-destructive",children:"*"})]}),e.jsx(ne,{type:"email",value:L,onChange:t=>{de(t.target.value),X&&T("")},placeholder:"your@email.com",className:X?"border-destructive":""}),X&&e.jsx("p",{className:"text-destructive text-xs",children:X})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx("label",{className:"text-sm font-sans font-medium",children:"Phone"}),e.jsx(ne,{type:"tel",value:ue,onChange:t=>fe(t.target.value),placeholder:"+1 (optional)"})]})]}),e.jsxs("div",{children:[e.jsxs("label",{className:"text-sm font-sans font-medium flex items-center gap-0.5 mb-2",children:[s.showEndDate?"Check-in Date":"Date",e.jsx("span",{className:"text-destructive",children:"*"})]}),e.jsxs(Fe,{children:[e.jsx(Pe,{asChild:!0,children:e.jsxs(C,{variant:"outline",className:Be("w-full justify-start text-left font-normal",ee&&"border-destructive"),children:[e.jsx(_e,{className:"mr-2 h-4 w-4"}),i?A(i,"PPP"):"Select date"]})}),e.jsx(Ie,{className:"w-auto p-0",align:"start",children:e.jsx(De,{mode:"single",selected:i,onSelect:t=>{ie(t),t&&M(null)},disabled:t=>ae(t)<I,initialFocus:!0})})]}),ee&&e.jsx("p",{className:"text-destructive text-xs mt-1",children:ee})]}),s.showEndDate&&e.jsxs("div",{children:[e.jsx("label",{className:"text-sm font-sans font-medium mb-2 block",children:"Check-out Date"}),e.jsxs(Fe,{children:[e.jsx(Pe,{asChild:!0,children:e.jsxs(C,{variant:"outline",className:"w-full justify-start text-left font-normal",children:[e.jsx(_e,{className:"mr-2 h-4 w-4"}),u?A(u,"PPP"):"Select date"]})}),e.jsx(Ie,{className:"w-auto p-0",align:"start",children:e.jsx(De,{mode:"single",selected:u,onSelect:le,disabled:t=>t<(i||new Date),initialFocus:!0})})]})]}),s.showTime&&e.jsxs("div",{className:"pl-1",children:[e.jsxs("label",{className:"text-sm font-sans font-medium flex items-center gap-0.5 mb-2",children:["Time",e.jsx("span",{className:"text-destructive",children:"*"})]}),e.jsxs(Te,{value:W,onValueChange:Ue,children:[e.jsxs(Ee,{className:"w-full",children:[e.jsx(ds,{className:"mr-2 h-4 w-4"}),e.jsx(qe,{})]}),e.jsx(Me,{children:$.map(t=>e.jsx(Ae,{value:t,children:es(t)},t))})]})]}),!o&&e.jsxs("div",{className:"pl-1",children:[e.jsxs("label",{className:"text-sm font-sans font-medium flex items-center gap-0.5 mb-2",children:[P?((ye=s.quantityLabel)==null?void 0:ye.trim())||"Quantity":s.type==="accommodation"?"Guests":"Travelers",e.jsx("span",{className:"text-destructive",children:"*"})]}),e.jsxs(Te,{value:p.toString(),onValueChange:t=>Ve(parseInt(t)),children:[e.jsxs(Ee,{className:"w-full",children:[e.jsx(us,{className:"mr-2 h-4 w-4"}),e.jsx(qe,{})]}),e.jsx(Me,{children:Xe.map(t=>{var a,l;return e.jsx(Ae,{value:t.toString(),children:P?`${t} ${t===1?((a=s.quantityLabel)==null?void 0:a.trim())||"item":((l=s.quantityLabel)==null?void 0:l.trim())||"items"}`:`${t} ${t===1?"person":"people"}`},t)})})]})]}),o&&d&&d.length>0&&e.jsx("div",{className:"bg-secondary/30 rounded-sm p-3 space-y-1",children:d.map(t=>e.jsxs("div",{className:"flex justify-between text-xs font-sans text-muted-foreground",children:[e.jsxs("span",{children:[t.count," × ",t.label]}),e.jsxs("span",{children:["$",(t.count*t.price).toFixed(2)]})]},t.label))}),v.length>0&&e.jsx(ls,{fields:v,values:b,onChange:(t,a)=>{ce(l=>({...l,[t]:a})),G(l=>{if(!l[t])return l;const n={...l};return delete n[t],n})},errors:Ge,singleColumn:!0,className:"gap-6 pl-1"})]}),e.jsxs("div",{className:"border-t border-border pt-4 space-y-4",children:[e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsx("span",{className:"font-sans text-muted-foreground",children:"Total"}),e.jsx("span",{className:"text-2xl font-serif font-medium text-foreground inline-flex items-center min-h-[2rem]",children:Y!=null?e.jsxs(e.Fragment,{children:["$",Y.toFixed(2)]}):e.jsx(ms,{className:"h-9 w-28"})})]}),ge?e.jsx(C,{onClick:()=>void He(),className:"w-full",variant:"accent",disabled:xe,children:xe?e.jsxs(e.Fragment,{children:[e.jsx($e,{className:"w-4 h-4 mr-2 animate-pulse"}),"Sending..."]}):e.jsxs(e.Fragment,{children:[e.jsx($e,{className:"w-4 h-4 mr-2"}),"Request Now"]})}):e.jsx(C,{onClick:Ye,className:"w-full",variant:B?"default":"accent",disabled:B,children:B?e.jsxs(e.Fragment,{children:[e.jsx(re,{className:"w-4 h-4 mr-2"}),"Added to Cart!"]}):e.jsxs(e.Fragment,{children:[e.jsx(ke,{className:"w-4 h-4 mr-2"}),"Add to Cart"]})})]})]})})}function Ts({product:x,variant:g="default",size:s="sm",label:S="Book Now",className:h,timeSlots:w,priceTiers:d,tierTotal:O,tierBreakdown:_,minPax:$,maxPax:R,maxAdults:D,maxChildren:K,inventoryQuantity:q,disabled:F,isOnRequest:k,inlineBooking:j}){const[J,N]=r.useState(!1),[o,c]=r.useState(!1),{addItem:P}=Re(),y=()=>{if(!j)return;const U=ss({product:x,startDate:j.startDate,endDate:j.endDate,selectedTime:j.selectedTime,priceTiers:d,tierTotal:O,tierBreakdown:_,minPax:$,maxPax:R,maxAdults:D,maxChildren:K,inventoryQuantity:q,quantity:x.priceType==="per_item"?1:2});P(U),ts.success("Added to cart"),c(!0),window.setTimeout(()=>c(!1),1500)};return j?e.jsx(C,{"data-name":"AddToCartButton",variant:o?"default":g,size:s,onClick:y,disabled:o||F,className:h,children:o?e.jsxs(e.Fragment,{children:[e.jsx(re,{className:"w-4 h-4 mr-2"}),"Added to Cart!"]}):e.jsxs(e.Fragment,{children:[e.jsx(ke,{className:"w-4 h-4 mr-2"}),S]})}):e.jsxs(e.Fragment,{children:[e.jsx(C,{"data-name":"AddToCartButton",variant:g,size:s,onClick:()=>N(!0),disabled:F,className:h,children:S}),e.jsx(xs,{open:J,onOpenChange:N,product:x,timeSlots:w,priceTiers:d,tierTotal:O,tierBreakdown:_,maxPax:R,inventoryQuantity:q,isOnRequest:k})]})}export{Ts as A};