import{a as e}from"./rolldown-runtime-COnpUsM8.js";import{At as t,Ht as n,mt as r}from"./vendor-react-N_fu9J_Y.js";import{t as i}from"./matsuya-api-BAyFV8GL.js";import{n as a}from"./NotificationsContextProvider-gq_W_pl_.js";import{s as o}from"./useAntavoMemberInfo-2rKaqvDh.js";import{t as s}from"./Button-CfyTBrFT.js";import"./Button-CpxzSFWT.js";import{t as c}from"./bundle-mjs-CSHEVFDI.js";import{r as l}from"./index-ESYUuDBM.js";var u=e(n()),d=t(),f=(0,u.forwardRef)(function({options:e=[],placeholder:t=``,value:n=``,onChange:i,className:a=``,id:o,name:s},l){let{t:f}=r(),[p,m]=(0,u.useState)(!1),[h,g]=(0,u.useState)(``),[_,v]=(0,u.useState)(n),y=(0,u.useRef)(null),b=(0,u.useRef)(null),x=e.filter(e=>e.label.toLowerCase().startsWith(h.toLowerCase())),S=e.find(e=>e.value===_),C=S?S.label:``,w=e=>{v(e.value),g(``),m(!1),i?.(e.value)};return(0,u.useEffect)(()=>{let e=e=>{b.current&&!b.current.contains(e.target)&&(m(!1),g(``))};return document.addEventListener(`mousedown`,e),()=>document.removeEventListener(`mousedown`,e)},[]),(0,u.useEffect)(()=>{n&&(v(n||``),g(``))},[n]),(0,d.jsxs)(`div`,{className:`relative`,ref:b,children:[(0,d.jsxs)(`div`,{className:c(`w-full border border-gray-300 h-[44px] py-[8px] px-[16px] text-[16px] md:text-[14px] leading-[120%] active:shadow-none focus:shadow-none focus:outline-none focus:border-black/40 rounded-none bg-white cursor-pointer flex items-center justify-between`,a),onClick:()=>{m(!p),p||setTimeout(()=>l?l.current?.focus():y.current?.focus(),0)},children:[(0,d.jsx)(`input`,{ref:l||y,type:`text`,name:s,value:p?h:C,onChange:e=>{g(e.target.value),p||m(!0)},onKeyDown:e=>{e.key===`Escape`&&(m(!1),g(``))},placeholder:t,className:`flex-1 text-[16px] bg-transparent outline-none cursor-pointer focus:border-none focus:shadow-none`,readOnly:!p,id:o}),(0,d.jsx)(`div`,{className:`flex items-center gap-[8px]`,children:(0,d.jsx)(`svg`,{xmlns:`http://www.w3.org/2000/svg`,width:`19`,height:`10`,viewBox:`0 0 19 10`,fill:`none`,className:`text-gray-400 transition-transform ${p?`rotate-180`:``}`,children:(0,d.jsx)(`path`,{d:`M1 1L9.53432 9L17.7273 1`,stroke:`black`})})})]}),p&&(0,d.jsx)(`div`,{className:`absolute top-full left-0 right-0 bg-white border border-gray-300 border-t-0 max-h-[200px] overflow-y-auto z-50`,children:x.length>0?x.map(e=>(0,d.jsx)(`div`,{onClick:()=>w(e),className:`px-[16px] py-[12px] text-[16px] md:text-[14px] leading-[120%] hover:bg-gray-50 cursor-pointer border-b border-gray-100 last:border-b-0`,children:e.label},e.value)):(0,d.jsx)(`div`,{className:`px-[16px] py-[8px] text-[16px] md:text-[14px] leading-[120%] text-gray-500`,children:f(`global.noOptions`,`No options available`)})})]})}),p=()=>{let{t:e}=r(),{addNotification:t}=a(),[n,o]=(0,u.useState)(!1);return{getAddressFromPostalCode:async n=>{o(!0);try{let{data:r,error:a}=await i.GET(`/v1/reference/addresses`,{params:{query:{postalCode:n.replace(/-/g,``)}}});return a||!r?.length?(t({type:`error`,message:e(`checkout.invalidPostalCode`,`No address found for the provided postal code.`)}),[]):r}catch(n){return console.error(`Error fetching address by postal code:`,n),t({type:`error`,message:e(`checkout.fetchPostalCodeFailed`,`Failed to fetch address. Please try again later.`)}),[]}finally{o(!1)}},loading:n}},m=({value:e,onAddressSelected:t,enableSearch:n=!1,register:i,className:a})=>{let{t:c}=r(),{getAddressFromPostalCode:u,loading:f}=p(),m=async()=>{if(e){let n=await u(e);n.length>0&&t(n[0])}};return(0,d.jsxs)(d.Fragment,{children:[(0,d.jsx)(`label`,{className:`block text-[14px] mb-[4px]`,children:c(`global.postalcode`,`Postal code`)}),(0,d.jsxs)(`div`,{className:`grid grid-cols-2 gap-[16px]`,children:[(0,d.jsx)(l,{...i,name:`postcode`,type:`text`,placeholder:`000-0000`,className:a}),n&&(0,d.jsx)(s,{disabled:!e||f,type:`button`,size:`fill`,classes:{buttonModifier:`!w-full !h-auto`},onClick:m,children:f?(0,d.jsx)(o,{className:`mx-auto`}):c(`global.postalCodeSearch`,`Search`)})]})]})},h=({id:e,name:t,checked:n=!1,onChange:r,label:i,disabled:a=!1,className:o=``})=>(0,d.jsxs)(`div`,{className:`flex items-center space-x-3 ${o}`,children:[(0,d.jsxs)(`div`,{className:`relative`,children:[(0,d.jsx)(`input`,{type:`checkbox`,id:`${e}`,name:t,checked:n,disabled:a,className:`sr-only peer`,onChange:r}),(0,d.jsx)(`label`,{htmlFor:`${e}`,className:`flex items-center justify-center w-[20px] h-[20px] border-[1px] cursor-pointer transition-all duration-200
                        ${a?`border-gray-200 cursor-not-allowed bg-gray-100`:`border-gray-300 bg-white hover:border-[#A0CEA8]`}
                        peer-checked:border-[#A0CEA8]`}),(0,d.jsx)(`span`,{className:`pointer-events-none absolute left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 w-[12px] h-[12px] bg-[#A0CEA8] opacity-0 peer-checked:opacity-100 transition-opacity duration-200`})]}),i&&(0,d.jsx)(`label`,{htmlFor:`${e}`,className:`text-[14px] cursor-pointer transition-colors duration-200 ${a?`text-gray-400 cursor-not-allowed`:`text-black hover:text-gray-900`}`,children:i})]});export{m as n,f as r,h as t};