!function(){"use strict";document.addEventListener("DOMContentLoaded",function(){const e=document.querySelectorAll(".mailerpress-my-lists-form");e.length&&e.forEach(function(e){!function(e){const s=e.querySelector(".mailerpress-my-lists-submit"),t=e.querySelector(".mailerpress-my-lists-message"),n=e.dataset.successMessage||"Your subscription preferences have been updated successfully.",r=e.dataset.errorMessage||"An error occurred while updating your preferences. Please try again.",a=e.dataset.loadingText||"Saving...";if(!s||!t)return void console.error("MailerPress: Required form elements not found");const i=s.textContent.trim(),l=t.className||"mailerpress-my-lists-message",o=e.querySelector('select[name="status"]'),c=function(){if(!o)return;const s="unsubscribed"===o.value;e.querySelectorAll('input[name="lists[]"]').forEach(function(e){s&&(e.checked=!1)})};o&&(o.addEventListener("change",c),e.querySelectorAll('input[name="lists[]"]').forEach(function(e){e.addEventListener("change",function(){e.checked&&"unsubscribed"===o.value&&(o.value="subscribed")})}),c());const d=e.querySelector(".mailerpress-show-more-lists");if(d){const s=e.querySelectorAll(".mailerpress-list-hidden");d.addEventListener("click",function(e){e.preventDefault(),s.forEach(function(e){e.classList.remove("mailerpress-list-hidden")}),d.style.display="none"})}e.addEventListener("submit",function(c){if(c.preventDefault(),!e.checkValidity())return void e.reportValidity();s.disabled=!0,s.classList.add("mailerpress-my-lists-loading"),s.textContent=a,t.style.display="none",t.textContent="",t.className=l;const d="1"===e.dataset.isLoggedIn,u=new FormData(e),m=[],f=e.querySelectorAll('input[name="lists[]"]:checked'),y=d&&o?o.value:"";"unsubscribed"!==y&&f.forEach(function(e){m.push(parseInt(e.value,10))});const p={first_name:u.get("first_name")||"",last_name:u.get("last_name")||"",lists:m,website:u.get("website")||""};if(d&&o&&(p.status=y||"subscribed"),!d){const e=u.get("email");if(!e)return t.textContent="Email address is required.",t.classList.add("mailerpress-my-lists-message--error"),t.style.display="block",s.disabled=!1,s.classList.remove("mailerpress-my-lists-loading"),void(s.textContent=i);p.email=e}const h="undefined"!=typeof mailerpressMyLists&&mailerpressMyLists.apiUrl?mailerpressMyLists.apiUrl:"/wp-json/mailerpress/v1/my-lists",b=d?h+"/update":h+"/subscribe",g="undefined"!=typeof mailerpressMyLists&&mailerpressMyLists.nonce?mailerpressMyLists.nonce:"";fetch(b,{method:"POST",headers:{"Content-Type":"application/json","X-WP-Nonce":g},body:JSON.stringify(p)}).then(function(e){return e.json().then(function(s){if(!e.ok){const e=s.message||r;throw new Error(e)}return s})}).then(function(s){if(!s.success)throw new Error(s.message||r);if(t.textContent=s.message||n,t.classList.add("mailerpress-my-lists-message--success"),t.style.display="block",s.data&&s.data.contact){const t=e.querySelector('input[name="first_name"]'),n=e.querySelector('input[name="last_name"]');t&&s.data.contact.first_name&&(t.value=s.data.contact.first_name),n&&s.data.contact.last_name&&(n.value=s.data.contact.last_name)}t.scrollIntoView({behavior:"smooth",block:"nearest"})}).catch(function(e){t.textContent=e.message||r,t.classList.add("mailerpress-my-lists-message--error"),t.style.display="block",t.scrollIntoView({behavior:"smooth",block:"nearest"}),console.error("MailerPress My Lists Error:",e)}).finally(function(){s.disabled=!1,s.classList.remove("mailerpress-my-lists-loading"),s.textContent=i})})}(e)})})}();