(()=>{"use strict";const e=["flexa_hp","_wp_http_referer"];function t(t){const r={};return t.querySelectorAll("input, textarea, select").forEach(t=>{const n=t.name;if(n&&!e.includes(n)&&!(t instanceof HTMLInputElement&&"file"===t.type)){if(t instanceof HTMLInputElement&&("checkbox"===t.type||"radio"===t.type)){if(!t.checked)return;const e=t.value&&"on"!==t.value?t.value:"Yes";return void("checkbox"===t.type&&void 0!==r[n]?r[n]=`${r[n]}, ${e}`:r[n]=e)}r[n]=t.value}}),r}function r(e,t,r){const n=e.querySelector(".flexa-toast");n&&n.remove();const a=document.createElement("div");a.className=`flexa-toast flexa-subscribe-form__message flexa-subscribe-form__message--${r}`,a.setAttribute("role","status"),a.setAttribute("aria-live","polite"),a.textContent=t,e.appendChild(a),requestAnimationFrame(()=>a.classList.add("flexa-toast--show"));const o=()=>{a.classList.remove("flexa-toast--show"),window.setTimeout(()=>a.remove(),250)},s=window.setTimeout(o,4500);a.addEventListener("click",()=>{window.clearTimeout(s),o()})}function n(e,t){const r=e.closest(".flexa-field");if(!r)return;if(r.classList.add("flexa-field--invalid"),r.querySelector(".flexa-field__error"))return;const n=document.createElement("span");n.className="flexa-field__error",n.textContent=t,r.appendChild(n)}function a(e,t){if(!e)return;e.disabled=t,e.classList.toggle("is-loading",t);let r=e.querySelector(".flexa-subscribe-form__dots");t?r||(r=document.createElement("span"),r.className="flexa-subscribe-form__dots",r.setAttribute("aria-hidden","true"),r.innerHTML="<span>.</span><span>.</span><span>.</span>",e.appendChild(r)):r&&r.remove()}function o(){document.querySelectorAll(".flexa-subscribe-form").forEach(o=>{if(o.dataset.flexaInit)return;o.dataset.flexaInit="1",o.addEventListener("submit",s=>{s.preventDefault(),async function(o){const s=function(e){const t=e.dataset;return{ajax:t.flexaAjax||"",nonce:t.flexaNonce||"",post:t.flexaPost||"",block:t.flexaBlock||"",confirm:t.flexaConfirm||"message",redirect:t.flexaRedirect||"",success:t.flexaSuccess||"Thanks! Your subscription has been received.",error:t.flexaError||"Something went wrong. Please try again.",required:t.flexaRequired||"This field is required.",phone:t.flexaPhone||"Please enter a valid phone number."}}(o);if(!function(t,r){!function(e){e.querySelectorAll(".flexa-field__error").forEach(e=>e.remove()),e.querySelectorAll(".flexa-field--invalid").forEach(e=>e.classList.remove("flexa-field--invalid"))}(t);let a=null;return t.querySelectorAll("input, textarea, select").forEach(t=>{if(!t.name||e.includes(t.name))return;if(t instanceof HTMLInputElement&&("checkbox"===t.type||"radio"===t.type))return;let o="";var s;t instanceof HTMLInputElement&&"tel"===t.type&&""!==t.value.trim()&&(s=t.value,/^[\d+()\-.\s]{6,}$/.test(s)&&s.replace(/\D/g,"").length>=6||(o=r.phone)),o||"function"!=typeof t.checkValidity||t.checkValidity()||(o=t.validationMessage||r.required),o&&(n(t,o),a||(a=t))}),t.querySelectorAll('.flexa-field__options[data-required="1"]').forEach(e=>{const t=e.querySelectorAll('input[type="checkbox"], input[type="radio"]');!Array.from(t).some(e=>e.checked)&&t[0]&&(n(t[0],r.required),a||(a=t[0]))}),!a||(a.focus(),!1)}(o,s))return;const i=o.querySelector('[name="flexa_hp"]');if(i&&i.value)return r(o,s.success,"success"),void o.reset();const l=o.querySelector(".flexa-subscribe-form__submit");a(l,!0);const c={action:"flexa_subscribe",nonce:s.nonce,post_id:s.post,block_id:s.block,flexa_hp:i?i.value:"",form_data:JSON.stringify(t(o))},f=Array.from(o.querySelectorAll('input[type="file"]'));let u,d;if(f.some(e=>e.files&&e.files.length>0)){const e=new FormData;Object.entries(c).forEach(([t,r])=>e.append(t,r)),f.forEach(t=>{t.name&&t.files&&Array.from(t.files).forEach(r=>e.append(t.name,r,r.name))}),u=e}else{const e=new URLSearchParams;Object.entries(c).forEach(([t,r])=>e.set(t,r)),u=e.toString(),d={"Content-Type":"application/x-www-form-urlencoded"}}try{const e=await fetch(s.ajax,{method:"POST",credentials:"same-origin",headers:d,body:u}),t=await e.json();if(e.ok&&t&&t.success){if("url"===s.confirm&&s.redirect)return void(window.location.href=s.redirect);r(o,s.success,"success"),o.reset()}else r(o,s.error,"error")}catch(e){r(o,s.error,"error")}finally{a(l,!1)}}(o)});const s=e=>{const t=e.target,r=t&&t.closest?t.closest(".flexa-field"):null;if(r){r.classList.remove("flexa-field--invalid");const e=r.querySelector(".flexa-field__error");e&&e.remove()}};o.addEventListener("input",s),o.addEventListener("change",s)})}"loading"===document.readyState?document.addEventListener("DOMContentLoaded",o):o()})();