/******/ (function() { // webpackBootstrap
/*!****************************************************************!*\
  !*** ./assets/src/js/global/map-scripts/geolocation-widget.js ***!
  \****************************************************************/
(function ($) {
  window.addEventListener('load', function () {
    /*
              get current location
          */
    if ('google' === adbdp_geolocation.select_listing_map) {
      (function () {
        var x = document.querySelector('.widget-location-name');
        var get_lat = document.querySelector('#cityLat');
        var get_lng = document.querySelector('#cityLng');

        // Helper function to format address by removing plus code and using address components
        function formatAddress(result) {
          if (!result || !result.address_components) {
            return '';
          }

          // Check if first element contains plus code (has '+' character)
          var components = result.address_components;
          if (components.length > 0 && components[0].long_name && components[0].long_name.includes('+')) {
            components = components.slice(1);
          }

          // Join long_names with commas
          return components.map(function (c) {
            return c.long_name;
          }).join(', ');
        }
        function getLocation() {
          if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition, showError);
          } else {
            x.value = 'Geolocation is not supported by this browser.';
          }
        }
        function showPosition(position) {
          lat = position.coords.latitude;
          lon = position.coords.longitude;
          displayLocation(lat, lon);
          get_lat.value = lat;
          get_lng.value = lon;
        }
        function showError(error) {
          switch (error.code) {
            case error.PERMISSION_DENIED:
              x.value = 'User denied the request for Geolocation.';
              break;
            case error.POSITION_UNAVAILABLE:
              x.value = 'Location information is unavailable.';
              break;
            case error.TIMEOUT:
              x.value = 'The request to get user location timed out.';
              break;
            case error.UNKNOWN_ERROR:
              x.value = 'An unknown error occurred.';
              break;
          }
        }
        function displayLocation(latitude, longitude) {
          var geocoder;
          geocoder = new google.maps.Geocoder();
          var latlng = new google.maps.LatLng(latitude, longitude);
          geocoder.geocode({
            latLng: latlng
          }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
              if (results[0]) {
                var add = formatAddress(results[0]);
                var value = add.split(',');
                count = value.length;
                country = value[count - 1];
                state = value[count - 2];
                city = value[count - 3];
                x.value = city;
              } else {
                x.value = 'address not found';
              }
            } else {
              x.value = 'Geocoder failed due to: ' + status;
            }
          });
        }
        var get_loc_btn = document.querySelector('.atbd_get_loc_wid');
        get_loc_btn.addEventListener('click', function () {
          getLocation();
        });
        if (directorist.i18n_text.select_listing_map === 'google') {
          function initialize() {
            var input = document.getElementById('address_widget');
            var options = directorist.countryRestriction ? {
              types: ['geocode'],
              componentRestrictions: {
                country: directorist.restricted_countries
              }
            } : '';
            var autocomplete = new google.maps.places.Autocomplete(input, options);
            google.maps.event.addListener(autocomplete, 'place_changed', function () {
              var place = autocomplete.getPlace();
              document.getElementById('cityLat').value = place.geometry.location.lat();
              document.getElementById('cityLng').value = place.geometry.location.lng();
            });
          }
          google.maps.event.addDomListener(window, 'load', initialize);
        }
      })();
    } else if (directorist.i18n_text.select_listing_map === 'openstreet') {
      $('#address_widget').on('keyup', function (event) {
        event.preventDefault();
        var search = $('#address_widget').val();
        $('#address_widget_result').css({
          display: 'block'
        });
        if (search === '') {
          $('#address_widget_result').css({
            display: 'none'
          });
        }
        var res = '';
        $.ajax({
          url: "https://nominatim.openstreetmap.org/?q=%27+".concat(search, "+%27&format=json"),
          type: 'GET',
          data: {},
          success: function success(data) {
            //console.log(data);
            for (var i = 0; i < data.length; i++) {
              res += '<li><a href="#" data-lat=' + data[i].lat + ' data-lon=' + data[i].lon + '>' + data[i].display_name + '</a></li>';
            }
            $('#address_widget_result').html('<ul>' + res + '</ul>');
          }
        });
      });
      $('body').on('click', '#address_widget_result ul li a', function (event) {
        event.preventDefault();
        var text = $(this).text(),
          lat = $(this).data('lat'),
          lon = $(this).data('lon');
        $('#cityLat').val(lat);
        $('#cityLng').val(lon);
        $('#address_widget').val(text);
        $('#address_widget_result').hide();
      });
      function displayLocation(position) {
        var lat = position.coords.latitude;
        var lng = position.coords.longitude;
        $.ajax({
          url: "https://nominatim.openstreetmap.org/reverse?format=json&lon=".concat(lng, "&lat=").concat(lat),
          type: 'GET',
          data: {},
          success: function success(data) {
            $('#address_widget').val(data.display_name);
            $('#cityLat').val(lat);
            $('#cityLng').val(lng);
          }
        });
      }
      $('.atbd_get_loc_wid').on('click', function () {
        navigator.geolocation.getCurrentPosition(displayLocation);
      });
    }
    if ($('#address_widget').val() === '') {
      $('#address_widget_result').css({
        display: 'none'
      });
    }
  });
})(jQuery);
/******/ })()
;
//# sourceMappingURL=public-geolocation-widget.js.map