var TF_Address_Lookup=function(){function e(){this.controller=null,this.searchResults=null,this.initEvents()}var t=e.prototype;return t.initEvents=function(){var t=this;document.addEventListener("click",function(e){this.onSelectAutocompleteItem(e),this.onHideAutocompleteResults(e)}.bind(this)),document.addEventListener("focusout",function(e){this.onFocusOut(e)}.bind(this)),document.addEventListener("keyup",this.debounce(function(e){t.onAddressAutocomplete(e)},500))},t.onFocusOut=function(e){var e=e.target.closest(".tf-address-lookup-field-address");e&&""===e.value&&(this.controller&&this.controller.abort(),e.previousElementSibling.value="",e=e.nextElementSibling,this.hideLoading(e))},t.debounce=function(n,i){var o;return function(){var e=this,t=arguments;clearTimeout(o),o=setTimeout(function(){return n.apply(e,t)},i)}},t.onAddressAutocomplete=function(e){var t,n,i,o,e=e.target.closest('.tf-address-lookup-field-address[data-autocomplete="true"]');e&&(this.controller&&this.controller.abort(),this.controller=new AbortController,t=this.controller.signal,n=e.nextElementSibling.nextElementSibling,i=e.nextElementSibling,o=this,null==(e=e.value.split(" ").map(function(e){return encodeURIComponent(e)}).join("+").trim())||""===e?(this.hideResults(n),this.hideLoading(i)):(this.showLoading(i),fetch("https://nominatim.openstreetmap.org/search?q="+e+"&addressdetails=1&format=json",{signal:t}).then(function(e){return e.json()}).then(function(e){o.hideLoading(i),o.showResults(e,n)}).catch(function(e){"AbortError"!==e.name&&o.hideLoading(i)})))},t.showResults=function(e,n){n.innerHTML="",e.length?((this.searchResults=e).forEach(function(e){var t='<div class="tf-address-lookup-field-autocomplete-item" data-coordinates="{coordinates}">{label}</div>';t=(t=t.replace("{coordinates}",e.lat+","+e.lon)).replace("{label}",e.display_name),n.innerHTML+=t}),n.classList.add("is-visible")):this.hideResults(n)},t.hideResults=function(e){e.classList.remove("is-visible"),this.searchResults=null},t.showLoading=function(e){e.classList.add("is-visible")},t.hideLoading=function(e){e.classList.remove("is-visible")},t.onSelectAutocompleteItem=function(e){var t,n,e=e.target.closest(".tf-address-lookup-field-autocomplete-item");e&&(e.parentElement.previousElementSibling.previousElementSibling.value=e.innerHTML,e.parentElement.previousElementSibling.previousElementSibling.previousElementSibling.value=e.dataset.coordinates,n=(t=e.dataset.coordinates.split(","))[0].trim(),t=t[1].trim(),n=new CustomEvent("onTFAddressLookupSelectedItem",{detail:{item:e,lat:n,lng:t,address:e.innerHTML}}),document.dispatchEvent(n),this.hideAutocompleteResult(e.parentElement))},t.onHideAutocompleteResults=function(e){var t=this;e.target.closest(".tf-address-lookup-field-autocomplete-results")||document.querySelectorAll(".tf-address-lookup-field-autocomplete-results.is-visible").forEach(function(e){t.hideAutocompleteResult(e)})},t.hideAutocompleteResult=function(e){e.classList.remove("is-visible"),e.innerHTML=""},e}();document.addEventListener("DOMContentLoaded",function(){new TF_Address_Lookup});

