(function(){'use strict';const config=window.ZeroSpamDavidWalsh||{};const INPUT_NAME='zerospam_david_walsh_key';const DATA_ATTR='data-zerospam-davidwalsh';const REFRESH_THRESHOLD=43200;let currentKey=config.key||'';let keyRefreshAttempted=false;function initForm(form){if(form.getAttribute(DATA_ATTR)==='protected'){return;}
if(!currentKey){return;}
form.setAttribute(DATA_ATTR,'protected');let input=form.querySelector(`input[name="${INPUT_NAME}"]`);if(input){input.value=currentKey;}else{input=document.createElement('input');input.type='hidden';input.name=INPUT_NAME;input.value=currentKey;form.appendChild(input);}}
function initAllForms(){if(!config.selectors){return;}
try{const forms=document.querySelectorAll(config.selectors);forms.forEach(initForm);}catch(e){if(console&&console.warn){console.warn('Zero Spam: Invalid selector in davidwalsh.js',e);}}}
async function maybeRefreshKey(){if(keyRefreshAttempted){return;}
if(!config.restUrl||!config.generated){return;}
const now=Math.floor(Date.now()/1000);const keyAge=now-config.generated;if(keyAge<REFRESH_THRESHOLD){return;}
keyRefreshAttempted=true;try{const response=await fetch(config.restUrl,{method:'GET',});if(!response.ok){return;}
const data=await response.json();if(data&&data.key){currentKey=data.key;config.generated=data.generated;updateExistingForms();}}catch(e){if(console&&console.warn){console.warn('Zero Spam: Failed to refresh David Walsh key',e);}}}
function updateExistingForms(){const protectedForms=document.querySelectorAll(`form[${DATA_ATTR}="protected"]`);protectedForms.forEach(function(form){const input=form.querySelector(`input[name="${INPUT_NAME}"]`);if(input){input.value=currentKey;}});}
function setupMutationObserver(){if(typeof MutationObserver==='undefined'){return;}
const observer=new MutationObserver(function(mutations){let shouldInit=false;mutations.forEach(function(mutation){if(mutation.addedNodes.length>0){shouldInit=true;}});if(shouldInit){clearTimeout(observer.timeout);observer.timeout=setTimeout(initAllForms,100);}});observer.observe(document.body,{childList:true,subtree:true,});}
function init(){if(typeof config.key==='undefined'){return;}
initAllForms();setupMutationObserver();maybeRefreshKey();}
if(document.readyState==='loading'){document.addEventListener('DOMContentLoaded',init);}else{init();}})();