document.addEventListener("alpine:init",()=>{Alpine.data("addressAutocomplete",t=>({query:"",results:[],open:!1,loading:!1,selecting:!1,debounceTimer:null,searchUrl:t,handleInput(e){if(!this.selecting){if(this.query=e,clearTimeout(this.debounceTimer),this.query.length<3){this.results=[],this.open=!1;return}this.debounceTimer=setTimeout(()=>this.search(),400)}},async search(){this.loading=!0;try{const e=await fetch(this.searchUrl+"?q="+encodeURIComponent(this.query));this.results=await e.json(),this.open=this.results.length>0}catch{this.results=[],this.open=!1}finally{this.loading=!1}},select(e){this.selecting=!0,this.fillField(this.$refs.streetWrapper,e.street),this.fillField(this.$refs.cityWrapper,e.city),this.fillField(this.$refs.postalWrapper,e.postal),this.query=e.street,this.open=!1,this.results=[],clearTimeout(this.debounceTimer),this.selecting=!1},fillField(e,s){const i=e.querySelector("input");i&&(i.value=s,i.dispatchEvent(new Event("input",{bubbles:!0})),i.dispatchEvent(new Event("change",{bubbles:!0})))}}))});document.addEventListener("livewire:navigated",()=>{window.Alpine&&window.Alpine.initTree(document.body)});document.addEventListener("livewire:init",()=>{Livewire.hook("request",({fail:t})=>{t&&t.status===419&&alert("Your session has expired — this usually happens when the page has been open for a long time. Please refresh and try again.")})});
