(function(){const a=document.createElement("link").relList;if(a&&a.supports&&a.supports("modulepreload"))return;for(const o of document.querySelectorAll('link[rel="modulepreload"]'))e(o);new MutationObserver(o=>{for(const i of o)if(i.type==="childList")for(const s of i.addedNodes)s.tagName==="LINK"&&s.rel==="modulepreload"&&e(s)}).observe(document,{childList:!0,subtree:!0});function r(o){const i={};return o.integrity&&(i.integrity=o.integrity),o.referrerPolicy&&(i.referrerPolicy=o.referrerPolicy),o.crossOrigin==="use-credentials"?i.credentials="include":o.crossOrigin==="anonymous"?i.credentials="omit":i.credentials="same-origin",i}function e(o){if(o.ep)return;o.ep=!0;const i=r(o);fetch(o.href,i)}})();function S(t){const a=String(t||"").trim().toLowerCase(),r=a.split(".").filter(Boolean);if(a.endsWith(".pages.dev")){if(r.length>=4)return r[1].replace(/-[^-]+$/i,"");if(r.length>=3)return r[0].replace(/-[^-]+$/i,"")}return r[0]||""}const k=1200;let b=null,l;function h(t){if(typeof document>"u")return"";const a=String(t||"").replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),r=document.cookie.match(new RegExp(`(?:^|; )${a}=([^;]*)`));return r?decodeURIComponent(r[1]):""}function E(){const t=h("kombayn_variant_path");return t||window.location.pathname||"/"}function _(t){const a=String(t||"").trim();if(!a)return"";const r=h("_fbc");return r||`fb.1.${Date.now()}.${a}`}function q(){if(l)return l;const t=document.createElement("div");t.className="fixed inset-0 z-[9999] hidden items-center justify-center bg-black/50 px-4",t.innerHTML=`

Подождите

Идет обработка вашей заявки...

`,document.body.appendChild(t);const a=t.querySelector("[data-btn-confirm]"),r=t.querySelector("[data-btn-edit]"),e=t.querySelector("[data-btn-error]");return a.addEventListener("click",()=>{const o=document.getElementById("lead-form");o&&o.reset(),x()}),r.addEventListener("click",()=>{x();const o=document.getElementById("phone-input");o&&(o.classList.add("border-red-600","ring-4","ring-red-600","bg-red-50"),o.focus(),setTimeout(()=>{o.classList.remove("border-red-600","ring-4","ring-red-600","bg-red-50")},4e3))}),e.addEventListener("click",()=>{x()}),l={root:t,spinner:t.querySelector("[data-spinner]"),iconSuccess:t.querySelector("[data-icon-success]"),iconError:t.querySelector("[data-icon-error]"),title:t.querySelector("[data-title]"),text:t.querySelector("[data-text]"),dynamicContent:t.querySelector("[data-dynamic-content]"),actionButtons:t.querySelector("[data-action-buttons]"),btnConfirm:a,btnEdit:r,btnError:e},l}function f({title:t,text:a,state:r="loading",htmlContent:e="",showConfirmBtn:o=!1,showEditBtn:i=!1,showErrorBtn:s=!1}){const n=q();n.title.textContent=t,n.text.textContent=a,n.btnConfirm.textContent="Все вірно",n.btnEdit.textContent="Змінити дані",n.btnError.textContent="Зрозуміло",n.root.classList.remove("hidden"),n.root.classList.add("flex"),n.spinner.classList.add("hidden"),n.iconSuccess.classList.add("hidden"),n.iconSuccess.classList.remove("flex"),n.iconError.classList.add("hidden"),n.iconError.classList.remove("flex"),r==="success"?(n.iconSuccess.classList.remove("hidden"),n.iconSuccess.classList.add("flex")):r==="error"?(n.iconError.classList.remove("hidden"),n.iconError.classList.add("flex")):n.spinner.classList.remove("hidden"),e?(n.dynamicContent.innerHTML=e,n.dynamicContent.classList.remove("hidden")):(n.dynamicContent.classList.add("hidden"),n.dynamicContent.innerHTML=""),o||i||s?(n.actionButtons.classList.remove("hidden"),n.actionButtons.classList.add("flex"),n.btnConfirm.classList.toggle("hidden",!o),n.btnEdit.classList.toggle("hidden",!i),n.btnError.classList.toggle("hidden",!s)):(n.actionButtons.classList.add("hidden"),n.actionButtons.classList.remove("flex"))}function x(){l&&(l.root.classList.add("hidden"),l.root.classList.remove("flex"))}function L(t,a){const r=String(t||"").trim(),e=String(a||"").trim();return!r||!e?Promise.resolve({status:"skipped"}):new Promise(o=>{const i=`https://www.facebook.com/tr?id=${encodeURIComponent(r)}&ev=${encodeURIComponent(e)}&noscript=1`,s=document.createElement("img");let n=!1;const d=m=>{n||(n=!0,window.clearTimeout(p),s.onload=null,s.onerror=null,s.remove(),o({status:m,pixelUrl:i}))},p=window.setTimeout(()=>d("timeout"),k);s.width=1,s.height=1,s.alt="",s.style.position="absolute",s.style.width="1px",s.style.height="1px",s.style.opacity="0",s.style.pointerEvents="none",s.style.left="-9999px",s.style.top="-9999px",s.referrerPolicy="no-referrer-when-downgrade",s.onload=()=>d("loaded"),s.onerror=()=>d("error"),s.src=i,document.body.appendChild(s)})}function P(){return typeof window>"u"||typeof document>"u"?Promise.resolve(!1):typeof window.fbq=="function"?Promise.resolve(!0):b||(b=new Promise(t=>{const a=document.querySelector('script[data-facebook-pixel-sdk="true"]');if(a){a.addEventListener("load",()=>t(typeof window.fbq=="function"),{once:!0}),a.addEventListener("error",()=>t(!1),{once:!0});return}const r=document.createElement("script");if(r.async=!0,r.defer=!0,r.dataset.facebookPixelSdk="true",r.src="https://connect.facebook.net/en_US/fbevents.js",r.onload=()=>t(typeof window.fbq=="function"),r.onerror=()=>t(!1),!window.fbq){const e=function(){e.callMethod?e.callMethod.apply(e,arguments):e.queue.push(arguments)};e.push=e,e.loaded=!0,e.version="2.0",e.queue=[],window.fbq=e,window._fbq=e}document.head.appendChild(r)}),b)}async function C(t,a,r){const e=String(t||"").trim(),o=String(a).trim(),i=String(r||"").trim();if(!e||!o)return{status:"skipped"};if(!await P()||typeof window.fbq!="function")return{...await L(e,o),transport:"img_fallback"};try{return window.fbq("init",e),window.fbq("trackSingle",e,o,{},i?{eventID:i}:{}),await new Promise(n=>window.setTimeout(n,250)),{status:"queued",transport:"fbq",pixelId:e,eventName:o,eventId:i}}catch(n){return{...await L(e,o),transport:"img_fallback",error:n.message}}}document.addEventListener("DOMContentLoaded",()=>{const t=document.getElementById("lead-form");t&&t.addEventListener("submit",async a=>{var m,y,w,v;a.preventDefault();const r=new FormData(t),e=Object.fromEntries(r.entries());if(String(e.phone||"").replace(/\D/g,"").length<10){f({title:"Помилка",text:"Номер телефону має містити щонайменше 10 цифр. Будь ласка, перевірте правильність вводу.",state:"error",showErrorBtn:!0});return}const i=new URLSearchParams(window.location.search);i.has("fbclid")&&(e.fbclid=i.get("fbclid"));const s=h("_fbp"),n=_(e.fbclid),d=t.querySelector('button[type="submit"]');d.disabled=!0;const p=d.innerHTML;d.textContent="Відправка...",f({title:"Зачекайте",text:"Йде обробка вашої заявки..."});try{const u=await fetch("/api/lead",{method:"POST",keepalive:!0,headers:{"Content-Type":"application/json"},body:JSON.stringify({project_id:S(window.location.hostname),client:{name:e.name,phone:e.phone,...e.country?{country:e.country}:{}},fbclid:e.fbclid,fbp:s,fbc:n,utm_source:i.get("utm_source")||"",utm_medium:i.get("utm_medium")||"",utm_campaign:i.get("utm_campaign")||"",utm_term:i.get("utm_term")||"",utm_content:i.get("utm_content")||"",sub1:i.get("sub1")||"",sub2:i.get("sub2")||"",sub3:i.get("sub3")||"",sub4:i.get("sub4")||"",sub5:i.get("sub5")||"",landing_variant:E(),event_source_url:window.location.href})}),c=await u.json().catch(()=>({}));if(!u.ok||!c.success){console.error("Lead API error details:",c);const g=((m=c==null?void 0:c.details)==null?void 0:m.errmsg)||((y=c==null?void 0:c.details)==null?void 0:y.message)||((w=c==null?void 0:c.details)==null?void 0:w.error)||"";throw new Error(g||c.error||"Ошибка сети")}if(((v=c.facebook_client_event)==null?void 0:v.transport)==="pixel"){f({title:"Майже готово",text:"Фіксуємо заявку та завершуємо обробку..."});const g=await C(c.facebook_client_event.pixelId,c.facebook_client_event.eventName||"Lead",c.facebook_client_event.eventId||"");console.info("Facebook browser pixel result:",g)}f({title:"Заявку прийнято!",text:"Будь ласка, перевірте ваш номер телефону. Якщо ви помилилися, ми не зможемо до вас додзвонитися!",state:"success",htmlContent:`
Ваш телефон:
${e.phone}
${e.name?`
Ваше ім'я:
${e.name}
`:""}
`,showConfirmBtn:!0,showEditBtn:!0})}catch(u){console.error(u),f({title:"Помилка відправки",text:u.message||"Сталася непередбачена помилка. Спробуйте ще раз.",state:"error",showErrorBtn:!0})}finally{d.disabled=!1,d.innerHTML=p}})});