/* lead-api.jsx — client helper for POSTing form submissions to /api/lead.
   Uses window.__LEAD_ENDPOINT if set (e.g. absolute URL for subpages),
   otherwise falls back to the relative /api/lead path. */

window.submitLead = async function submitLead(payload) {
  const endpoint = window.__LEAD_ENDPOINT || "/api/lead";
  try {
    const res = await fetch(endpoint, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(payload || {}),
    });
    if (!res.ok) throw new Error(`HTTP ${res.status}`);
    return await res.json();
  } catch (err) {
    console.warn("[lead] submission failed:", err.message);
    return { ok: false, error: err.message };
  }
};
