/* Scoped to: marathon-registration-form */
(function() {
  'use strict';
  var componentRoots = document.querySelectorAll('[data-component-slug="marathon-registration-form"]');
  componentRoots.forEach(function(componentRoot) {
    // Helper to query within component, with fallback to document for sibling elements
    var $ = function(sel) { 
      return componentRoot.querySelector(sel) || document.querySelector('[data-component-slug="marathon-registration-form"] ~ ' + sel) || document.querySelector(sel);
    };
    var $$ = function(sel) { 
      var results = componentRoot.querySelectorAll(sel);
      return results.length ? results : document.querySelectorAll(sel);
    };
    // Original component JS runs in this scope
    (function () { setTimeout(function () { var CONFIG = { apiEndpoint: "{{form_api_endpoint}}", successMessage: "{{success_message}}" || "Thank you for registering! We'll be in touch soon.", errorMessage: "{{error_message}}" || "Something went wrong. Please try again.", redirectUrl: "{{redirect_url}}", redirectDelay: parseInt("{{redirect_delay}}") || 2000, }; var forms = document.querySelectorAll( "form[data-registration-form]:not([data-initialized])" ); forms.forEach(function (form) { form.setAttribute("data-initialized", "true"); var container = form.closest("section") || form.parentElement; var statusEl = container ? container.querySelector("#form-status") : null; form.setAttribute("onsubmit", "return false;"); form.addEventListener("submit", function (e) { e.preventDefault(); e.stopPropagation(); var formData = new FormData(form); var data = {}; formData.forEach(function (value, key) { data[key] = value; }); var submitBtn = form.querySelector('button[type="submit"]'); var originalText = submitBtn ? submitBtn.textContent : ""; if (submitBtn) { submitBtn.textContent = "Submitting..."; submitBtn.disabled = true; } if (CONFIG.apiEndpoint && CONFIG.apiEndpoint.indexOf("http") === 0) { fetch(CONFIG.apiEndpoint, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data), }) .then(function (response) { if (!response.ok) throw new Error("Network error"); return response.json(); }) .then(function () { showStatus(CONFIG.successMessage, "success"); form.reset(); if ( CONFIG.redirectUrl && (CONFIG.redirectUrl.indexOf("http") === 0 || CONFIG.redirectUrl.indexOf("/") === 0) ) { setTimeout(function () { window.location.href = CONFIG.redirectUrl; }, CONFIG.redirectDelay); } }) .catch(function () { showStatus(CONFIG.errorMessage, "error"); }) .finally(function () { if (submitBtn) { submitBtn.textContent = originalText; submitBtn.disabled = false; } }); } else { setTimeout(function () { showStatus(CONFIG.successMessage, "success"); form.reset(); if (submitBtn) { submitBtn.textContent = originalText; submitBtn.disabled = false; } }, 1000); } return false; }); function showStatus(message, type) { if (!statusEl) return; statusEl.textContent = message; statusEl.className = "py-3 px-4 rounded-lg text-center font-medium " + (type === "success" ? "bg-green-100 text-green-800" : "bg-red-100 text-red-800"); statusEl.classList.remove("hidden"); setTimeout(function () { statusEl.classList.add("hidden"); }, 5000); } }); }, 0); })();
  });
})();