var NRF_Mapaddress_Widget=function(){function e(){this.init()}var t=e.prototype;return t.init=function(){var t=this;this.getInstances().forEach(function(e){t.determineFieldValidityAndToggleValidatorField(e)}),document.addEventListener("onTFMapEditorMarkerMoveEnd",function(e){this.onCoordinatesChange(e)}.bind(this)),document.addEventListener("onTFAddressLookupSelectedItem",function(e){this.onSelectAutocompleteItem(e)}.bind(this)),document.addEventListener("onTFMapEditorClearMap",function(e){this.onClearMap(e)}.bind(this)),document.addEventListener("keyup",this.debounce(function(e){t.onTypeData(e)},500)),document.querySelectorAll("joomla-field-subform").forEach(function(e){e.addEventListener("subform-row-add",function(e){t.render(e)})})},t.getInstances=function(){return document.querySelectorAll(".nrf-widget.tf-mapaddress-editor")},t.debounce=function(d,r){var a;return function(){var e=this,t=arguments;clearTimeout(a),a=setTimeout(function(){return d.apply(e,t)},r)}},t.onTypeData=function(e){e=e.target.closest(".nrf-widget.tf-mapaddress-editor");e&&this.determineFieldValidityAndToggleValidatorField(e)},t.onClearMap=function(e){e=e.detail.map.closest(".nrf-widget.tf-mapaddress-editor");e&&(e.querySelector(".map-markers").value="",this.clearAddressDetails(e),this.determineFieldValidityAndToggleValidatorField(e))},t.onSelectAutocompleteItem=function(e){var t=e.detail.item.closest(".nrf-widget.tf-mapaddress-editor");t&&(t.querySelector(".nrf-widget.openstreetmap.no-map")&&this.getAddressDetails(e.detail.lat,e.detail.lng,t,this.afterCoordinatesChange.bind(this)),this.determineFieldValidityAndToggleValidatorField(t))},t.render=function(e){e=e.target.querySelectorAll(".nrf-widget.tf-mapaddress-editor .nrf-widget.openstreetmap:not(.no-map):not(.done)");e&&e.forEach(function(e){var t=new NR_OSM_Map(e);t.render(),t.renderMarkers(),t.centerMap(),e.classList.add("done")})},t.onCoordinatesChange=function(e){var t=e.detail.map.closest(".nrf-widget.tf-mapaddress-editor");t&&(e.detail.map.classList.remove("clear-is-hidden"),this.afterCoordinatesChange(e.detail.address,t),this.determineFieldValidityAndToggleValidatorField(t))},t.afterCoordinatesChange=function(e,t){this.clearAddressDetails(t),this.updateAddressDetails(t,e)},t.getAddressDetails=function(e,t,d,r){fetch("https://nominatim.openstreetmap.org/reverse?lat="+e+"&lon="+t+"&format=json").then(function(e){return e.json()}).then(function(e){r(e,d)})},t.clearAddressDetails=function(t){t.querySelector(".tf-address-lookup-field-coordinates-value").value="",t.querySelector(".tf-address-lookup-field-address").value="",this.getFields().forEach(function(e){t.querySelector(".tf-mapaddress-field-"+e).value=""})},t.updateAddressDetails=function(e,t){t.error||t.address&&(e.querySelector(".tf-mapaddress-field-latitude").value=t.lat,e.querySelector(".tf-mapaddress-field-longitude").value=t.lon,e.querySelector(".tf-address-lookup-field-address").value=t.display_name,t.address.country&&(e.querySelector(".tf-mapaddress-field-country").value=t.address.country),t.address.country_code&&(e.querySelector(".tf-mapaddress-field-country_code").value=t.address.country_code),t.address.city&&(e.querySelector(".tf-mapaddress-field-city").value=t.address.city),t.address.postcode&&(e.querySelector(".tf-mapaddress-field-postal_code").value=t.address.postcode),t.address.county&&(e.querySelector(".tf-mapaddress-field-county").value=t.address.county),t.address.state&&(e.querySelector(".tf-mapaddress-field-state").value=t.address.state),t.address.municipality&&(e.querySelector(".tf-mapaddress-field-municipality").value=t.address.municipality),t.address.town&&(e.querySelector(".tf-mapaddress-field-town").value=t.address.town),t.address.road&&(e.querySelector(".tf-mapaddress-field-road").value=t.address.road),t.address.house_number)&&(e.querySelector(".tf-mapaddress-field-house_number").value=t.address.house_number)},t.determineFieldValidityAndToggleValidatorField=function(e){var t;this.isRequired(e)&&(t=this.isValid(e)?"hide":"show",e=e.querySelector(':scope > input[type="hidden"]'),"hide"==t?(e.removeAttribute("required"),e.classList.remove("required")):(e.setAttribute("required","required"),e.classList.add("required")))},t.isValid=function(t){var d=!0,e=t.querySelector(".tf-address-lookup-field-address");return e&&""===e.value.trim()&&(d=!1),this.getFields().forEach(function(e){e=t.querySelector(".control-group."+e+".is-visible .tf-mapaddress-field-"+e);e&&""===e.value.trim()&&(d=!1)}),d},t.isRequired=function(e){return e.classList.contains("is-required")},t.getFields=function(){return["latitude","longitude","country","country_code","city","county","postal_code","state","municipality","town","road","house_number"]},e}();document.addEventListener("DOMContentLoaded",function(){new NRF_Mapaddress_Widget});

