import*as t from"@wordpress/interactivity";var e={d:(t,s)=>{for(var r in s)e.o(s,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:s[r]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)};const s=(r={getContext:()=>t.getContext,getElement:()=>t.getElement,store:()=>t.store},n={},e.d(n,r),n);var r,n;function o(t){if("object"==typeof GatherPress)return t.split(".").reduce((t,e)=>t?.[e],GatherPress)}function a(t,e){var s;t.posts=null!==(s=t.posts)&&void 0!==s?s:[],e&&!t.posts[e]&&(t.posts[e]={eventResponses:{attending:0,waitingList:0,notAttending:0},currentUser:{status:"no_status",guests:0,anonymous:0},rsvpSelection:"attending"})}const i=(()=>{let t=null,e=null;const s=async function(){return t||e||(e=fetch(o("urls.eventApiUrl")+"/nonce",{method:"GET",credentials:"same-origin"}).then(t=>t.json()).then(s=>(t=s.nonce,e=null,s.nonce)).catch(()=>(e=null,null)),e)};return s.clearCache=()=>{t=null,e=null},s})(),{state:l}=(0,s.store)("gatherpress",{state:{posts:{},rsvpForm:{isSubmitting:!1}},actions:{async handleRsvpFormSubmit(t){t.preventDefault();const e=(0,s.getElement)().ref,r=(0,s.getContext)(),n=r?.postId||0;if(l.rsvpForm.isSubmitting)return;const p=e.querySelector(".gatherpress-submit-button"),c="gatherpress--is-loading";l.rsvpForm.isSubmitting=!0,p&&p.classList.add(c);const u=new FormData(e),g={comment_post_ID:n,author:u.get("author"),email:u.get("email"),gatherpress_rsvp_form_guests:u.get("gatherpress_rsvp_form_guests")||"0"};g.gatherpress_rsvp_anonymous=u.get("gatherpress_rsvp_anonymous")?"1":"0",g.gatherpress_event_updates_opt_in=u.get("gatherpress_event_updates_opt_in")?"1":"0";for(const[t,e]of u.entries())["comment_post_ID","author","email","gatherpress_rsvp_form_guests","gatherpress_rsvp_anonymous","gatherpress_event_updates_opt_in"].includes(t)||(g[t]=e);const m=async(t=!1)=>{const s=await i();if(!s)return l.rsvpForm.isSubmitting=!1,void e.submit();const r=await fetch(o("urls.eventApiUrl")+"/rsvp-form",{method:"POST",headers:{"Content-Type":"application/json","X-WP-Nonce":s},body:JSON.stringify(g)});return 403!==r.status||t?r.json():(i.clearCache(),m(!0))};try{const t=await m();if(t?.success){const s=e.querySelectorAll("[data-gatherpress-rsvp-form-visibility]"),r="past"===e.dataset.gatherpressEventState;s.forEach(t=>{const e=t.dataset.gatherpressRsvpFormVisibility;let s={};try{s=JSON.parse(e)}catch(t){console.warn("Failed to parse RSVP form visibility JSON:",t),s={}}const{onSuccess:n,whenPast:o}=s;let a=null;r&&o?a="show"===o:n&&(a="show"===n),!0===a?(t.style.removeProperty("display"),t.setAttribute("aria-hidden","false"),t.setAttribute("aria-live","polite"),t.setAttribute("role","status")):!1===a&&(t.style.display="none",t.setAttribute("aria-hidden","true"))}),t.responses&&(a(l,n),l.posts[n]&&(l.posts[n].eventResponses={attending:t.responses?.attending?.count||0,waitingList:t.responses?.waiting_list?.count||0,notAttending:t.responses?.not_attending?.count||0}))}else alert(t?.message||"Sorry, there was an issue processing your RSVP. Please try again.")}catch(t){return console.warn("Ajax RSVP submission failed, falling back to regular form submission:",t),l.rsvpForm.isSubmitting=!1,void e.submit()}l.rsvpForm.isSubmitting=!1,p&&p.classList.remove(c)}},callbacks:{initRsvpForm(){const t=(0,s.getElement)().ref,e=(0,s.getContext)();a(l,e?.postId||0),l.rsvpForm.isSubmitting=!1;const r="true"===new URLSearchParams(window.location.search).get("gatherpress_rsvp_success"),n="past"===t.dataset.gatherpressEventState;t.querySelectorAll("[data-gatherpress-rsvp-form-visibility]").forEach(t=>{const e=t.dataset.gatherpressRsvpFormVisibility;let s={};try{s=JSON.parse(e)}catch(t){console.warn("Failed to parse RSVP form visibility JSON:",t),s={}}const{onSuccess:o,whenPast:a}=s;let i=null;n&&a?i="show"===a:n||!a||o?o&&(i=r?"show"===o:"show"!==o):i="show"!==a,!0===i?(t.style.removeProperty("display"),t.setAttribute("aria-hidden","false"),r&&(t.setAttribute("aria-live","polite"),t.setAttribute("role","status"))):!1===i&&(t.style.display="none",t.setAttribute("aria-hidden","true"))})}}});