(function () {
  function addPressFeedback() {
    var items = document.querySelectorAll('.opcao, .btn-full, .btn-submit, .btn-secondary');
    for (var i = 0; i < items.length; i++) {
      (function (el) {
        el.addEventListener('touchstart', function () { el.classList.add('is-pressed'); }, { passive: true });
        var clear = function () { el.classList.remove('is-pressed'); };
        el.addEventListener('touchend', clear, { passive: true });
        el.addEventListener('touchcancel', clear, { passive: true });
        el.addEventListener('mouseup', clear);
        el.addEventListener('mouseleave', clear);
      })(items[i]);
    }
  }

  function autoGeolocation() {
    var lat = document.getElementById('latitude');
    var lng = document.getElementById('longitude');
    var button = document.getElementById('geoButton');
    if (!lat || !lng || !navigator.geolocation) return;

    var fill = function (pos) {
      lat.value = pos.coords.latitude;
      lng.value = pos.coords.longitude;
    };

    var getLocation = function () {
      navigator.geolocation.getCurrentPosition(fill, function () {}, {
        enableHighAccuracy: true,
        timeout: 10000,
        maximumAge: 0
      });
    };

    getLocation();
    if (button) button.addEventListener('click', getLocation);
  }

  function submitFeedback() {
    var form = document.getElementById('appRegistroForm');
    var button = document.getElementById('submitAppButton');
    var feedback = document.getElementById('submitFeedback');
    if (!form || !button) return;

    form.addEventListener('submit', function () {
      button.classList.add('is-loading');
      button.innerHTML = 'Enviando...';
      if (feedback) feedback.classList.add('is-visible');
    });
  }

  document.addEventListener('DOMContentLoaded', function () {
    addPressFeedback();
    autoGeolocation();
    submitFeedback();
  });
})();
