import{c,r as n,a as i,j as m,F as ae}from"./provider-jGJDpSsD.js";import{C as oe}from"./client-only-BiA9gwQP.js";import{w as re}from"./with-authenticated-N--UqYKj.js";import{u as X,A}from"./use-app-path-BUM05-P1.js";import{t as Y,T as L}from"./typography-D5FAW5Sb.js";import{C as ne,H as ce,B as me,A as le,R as de}from"./form-layout-CrvlIo6S.js";import{c as E,g as z,b as ue,u as pe}from"./formik.esm-BmwmIU_j.js";import{F as R,L as G,R as fe}from"./form-De4ZT-Bs.js";import{B as N}from"./button-DGOdsXw0.js";import{S as J}from"./select-input-DH3WXN0H.js";import{S as he,a as ge,t as ye,b as Ce}from"./actions-DgJT9sTu.js";import{S as $}from"./index-C1Hwvbl2.js";import{F as Se}from"./feedback-message-BohwyaSB.js";import{a as ve}from"./index-DAQNz6Jx.js";import{u as V}from"./useTranslation-CtY5AHSk.js";import{d as _,U as Z,f as M,b as ee,ac as be,ai as P,aj as ke}from"./index-BVE0rL7V.js";import{u as Pe}from"./use-appointments-CbWgUa3E.js";import{u as Ae}from"./useQuery-aJojuJxG.js";import{a as xe}from"./index-cxbZx-ve.js";import{g as Te,i as De,B as Fe,A as Oe,j as we}from"./responsive-JY1roPos.js";import{u as Ve}from"./index-DTLMCL9z.js";import{D as K}from"./datetime-HslA5HLw.js";import"./polyfills-B5VgeUBU.js";import"./auth-client-manager-DQSHzvin.js";import"./index.esm2017-RCKYhhsE.js";import"./create-search-params-duyN7nPD.js";import"./app-container-BcuS2rzE.js";import"./app-bar-BhdUqI9w.js";import"./index-CiF965Dc.js";import"./index-BtT5uCNV.js";import"./index-DmZ0qsS8.js";import"./scroll-to-top-elOVGYY1.js";import"./_Uint8Array-DAX1LXOP.js";import"./_baseClone-C16dVnB9.js";import"./index-Dc9IlWZS.js";import"./index-DZPvrtfD.js";import"./icons-e83Zchnl.js";import"./preload-helper-DEPslLRF.js";import"./objectSpread2-DfNl4N-c.js";import"./objectWithoutProperties-Bpj9LcIY.js";import"./iterableToArray-weAN6v47.js";import"./setPrototypeOf-DgZC2w_0.js";import"./floating-ui.dom.browser.min-ByDLuf66.js";import"./date-picker-56ICZx1U.js";import"./index-CmT6Gk3z.js";import"./button-icon-CnvuPCRK.js";import"./toggle-group-hbKUDdJB.js";import"./index-DB8v_Xz6.js";import"./index-Dj4TLTKl.js";import"./index-ChMVmGJe.js";import"./_checkForMethod-D5z6pDsq.js";import"./_xfBase-ChDvHdvA.js";import"./type-CS1H_1Vj.js";import"./index-CzbPQDt8.js";import"./utils-Cv9PmFJz.js";import"./QueryClientProvider-B99TdaqV.js";import"./media-query-hooks-gd_xrKAI.js";import"./index-5IrQH2c7.js";import"./useForkRef-DegKsY9x.js";function Be(){const t=_(Z);return Ae([],()=>t.getAvailableCommunitiesForBooking(),{refetchOnReconnect:!1,refetchOnWindowFocus:!1,suspense:!1})}const w={actions:{invalid:t=>c({visibility:"hidden"})},body:t=>c({display:"flex",flexDirection:"column",gap:t.spaces.xl2}),container:t=>c({display:"flex",flexDirection:"column",marginBlock:t.spaces.xl5,marginInline:t.spaces.xl5,[Y(t)]:{marginBlock:t.spaces.xl8,marginInline:t.spaces.xl8}}),content:t=>c({paddingInlineEnd:t.spaces.xl2,[Y(t)]:{height:"100%",maxHeight:"85vh",maxWidth:"480px",overflow:"auto",width:"480px"}}),description:t=>c({color:t.colors.text.secondary,marginBlockStart:t.spaces.md,width:"100%"}),timePicker:{base:c({[`& .${he.slotPickerWrapper}`]:{flex:"1 1 auto"}}),invalid:c({display:"none"})}},Ie=E().shape({community:E().required(),salesCenter:E().required(),selectedDate:z().required(),selectedPartOfDay:ue().required(),selectedStartTime:z().required()}),Ee=({defaultCommunity:t,submitting:l,onChange:d,onSubmit:p,fetchingComsAndCenters:x,allCommunities:u,allSalesCenters:C,fetchingSlots:T,slots:B,error:f})=>{const r=V().t,b=V(void 0,{keyPrefix:"common"}).t,e=pe({initialValues:{},onSubmit:async a=>{e.isValid&&await(p==null?void 0:p(a))},validationSchema:Ie});n.useEffect(M(()=>{if(e.values.community||!t||!(u!=null&&u.length))return;const a=u.find(h=>h.value.toLowerCase()===t.toLowerCase());e.setFieldValue("community",a)}),[u]),n.useEffect(()=>{d==null||d(e.values)},[e.values]),n.useEffect(()=>{e.setFieldValue("selectedDate",void 0),e.setFieldValue("selectedPartOfDay",void 0),e.setFieldValue("selectedStartTime",void 0)},[e.values.salesCenter,e.values.community]),n.useEffect(()=>{const a=C==null?void 0:C.find(h=>h.isDefault);e.setFieldValue("salesCenter",a)},[C]);const D=()=>{var a,h;return((h=(a=e.values.community)==null?void 0:a.value)==null?void 0:h.toLowerCase())===ve.ALAROUS?null:m(R,{name:"salesCenter",serverInvalid:!!e.errors.salesCenter&&!!e.touched.salesCenter,children:[i(G,{children:r("features.bookings.visitSaleCenter.whichSaleCenter")}),i(J,{placeholder:r("features.bookings.visitSaleCenter.selectSalesCenter"),...e.getFieldProps("salesCenter"),onChange:g=>e.setFieldValue("salesCenter",g),filterOption:g=>!!(g.label&&g.value),options:C,menuPlacement:"auto",menuPortalTarget:document.body})]})},k=!e.values.salesCenter||e.errors.salesCenter||!e.values.community||e.errors.community,F=m("div",{children:[i($,{height:24,width:"50%"}),i($,{height:36,css:{marginBlock:12}}),i($,{height:36,css:{marginBlock:12}})]});return i(de,{children:i(ne,{children:m(fe,{onSubmit:e.handleSubmit,children:[i(ce,{children:i(L,{variant:"h3",children:r("features.bookings.scheduleSelectSlot.salesCentreVisit")})}),m(me,{css:w.body,children:[f?i(Se,{message:f,variant:"error"}):null,x?F:m(R,{name:"community",serverInvalid:!!e.errors.community&&!!e.touched.community,children:[i(G,{children:r("features.bookings.visitSaleCenter.whichCommunity")}),i(J,{placeholder:"...",...e.getFieldProps("community"),onChange:a=>e.setFieldValue("community",a),options:u,menuPlacement:"auto",menuPortalTarget:document.body})]}),x?F:D(),i(R,{name:"selectedStartTime",serverInvalid:!!e.errors.selectedStartTime&&!!e.touched.selectedStartTime,css:[w.timePicker.base,k&&w.timePicker.invalid],children:i(ge,{loading:T,slots:B,selectedDate:e.values.selectedDate,onDateChange:a=>e.setFieldValue("selectedDate",a),selectedPartOfDay:e.values.selectedPartOfDay,onPartOfDayChange:a=>e.setFieldValue("selectedPartOfDay",a),selectedStartTime:e.values.selectedStartTime,onStartTimeChange:a=>e.setFieldValue("selectedStartTime",a)})})]}),i(le,{children:i(N,{css:[k&&w.actions.invalid],size:"lg",variant:"primary","data-testid":"confirm",disabled:!e.isValid||e.isSubmitting,loading:l||e.isSubmitting,type:"submit",onClick:e.submitForm,children:b("confirm")})})]})})})},v={actions:t=>c({flexDirection:"column",gap:t.spaces.md}),button:c({width:"100%"}),text:t=>c({marginBlock:t.spaces.lg}),title:t=>c({lineHeight:t.typography.subtitleXL.lineHeight}),wrapper:()=>c({width:500})};function Re({open:t}){const{t:l}=V(),d=ee(),p=X();return m(we,{open:t,children:[i(Te,{}),m(De,{css:v.wrapper,children:[m(Fe,{children:[i(L,{variant:"bodyL",css:v.title,children:l("features.bookings.bookOneAppointment.sorry")}),i(L,{variant:"bodyS",css:v.text,children:l("features.bookings.bookOneAppointment.rescheduleOrCancelYourAppointment")})]}),m(Oe,{css:v.actions,children:[i(N,{variant:"primary",css:v.button,onClick:()=>d(p(A.myAppointments)),children:l("pages.appointments.myAppointments")}),i(N,{onClick:()=>d(p(A.home)),css:v.button,children:l("features.bookings.scheduleError.back")})]})]})]})}const $e=()=>{var H,U,Q;const t=X(),l=xe(),{t:d,i18n:p}=V(),[x]=Ve(),u=ee(),C=x.get("defaultCommunity"),T=p.language,[B,f]=n.useState(),r=Be(),b=Pe({}),e=!b.isLoading&&((H=b.data)==null?void 0:H.code)===be&&!!((Q=(U=b.data)==null?void 0:U.data)!=null&&Q[0]);n.useEffect(()=>{if(!P(r.data))return;if(!r.data.data.length){u(t(A.thankForRegistering));return}},[r.data]);const D=_(Z),[k,F]=n.useState(),a=n.useMemo(()=>{var o,y;let s=[];if(P(r.data)){const I=(y=((o=r.data)==null?void 0:o.data).find(S=>S.CommunityName===k))==null?void 0:y.salesCenters;s=(I==null?void 0:I.map(S=>({community:S.Community,isDefault:S.DefaultLocation==="Y",label:T==="en"?S.CenterNameEn:S.CenterNameAr,value:S.LocationId})))??[]}return s},[r.data,k]),h=n.useMemo(()=>{let s=[];return P(r.data)&&(s=r.data.data.map(o=>({label:T==="en"?`${o.CommunityName} - ${o.CommunityCityEN??""}`:`${o.CommunityAR} - ${o.CommunityCityAR??""}`,value:o.CommunityName}))),s},[r.data]),g=n.useRef({}),[te,j]=n.useState(!1),[O,q]=n.useState(void 0),ie=M(async s=>{var y;if(!s.community||e){q(void 0),f(void 0);return}if(F(s.community.value),!s.salesCenter||g.current.salesCenter===s.salesCenter)return;j(!0),g.current=s;const o=await D.getBookingSlots({locationId:s.salesCenter.value});g.current===s&&(P(o)&&(f(void 0),q(o.data)),(ke(o)||!((y=o.data)!=null&&y.length))&&f(d("features.bookings.scheduleDialog.unavailable.title")),j(!1),l(ye(s.community.value)))}),se=M(async s=>{if(!s.community||!s.salesCenter)return;const o=O==null?void 0:O.find(W=>K.fromISO(W.startTime).toISO()===K.fromJSDate(s.selectedStartTime).toISO());if(!o)return;f(void 0);const y=await D.bookAppointment({communityOfInterestName:s.community.value,endTime:o.endTime,locationId:s.salesCenter.value,salesCenterCommunityName:s.salesCenter.community??"",startTime:o.startTime});P(y)?(l(Ce(o.startTime,y.data.transactionId)),u(t(A.myAppointments))):f(d("features.bookings.scheduleDialog.error.description"))});return m(ae,{children:[i(Ee,{defaultCommunity:C??void 0,allCommunities:h,allSalesCenters:a,onChange:ie,slots:O,fetchingSlots:te,onSubmit:se,fetchingComsAndCenters:r.isLoading,error:B}),i(Re,{open:e})]})},Le=re($e,{redirectTo:A.signUp}),Ut={profilingCollector:{skip:!0}};function Qt(){return i(oe,{children:()=>i(Le,{})})}export{Qt as default,Ut as handle};
