(()=>{"use strict";const e=window.wp.element,t=window.wp.i18n,a=window.wp.components,{applyFilters:o}=wp.hooks,n=n=>{let{title:r,message:l,confirmText:c,cancelText:s,onConfirm:i,onCancel:m,isDestructive:d,modalContext:p}=n;const[u,w]=(0,e.useState)(!1);return(0,e.createElement)(a.Modal,{title:r||(0,t.__)("Confirm Action","memberpress"),onRequestClose:m,shouldCloseOnClickOutside:!0,shouldCloseOnEscKey:!0,focusOnMount:!0,size:"medium",className:"mepr-modal"},(0,e.createElement)(a.__experimentalVStack,{spacing:"5"},(0,e.createElement)("div",{className:"mepr-modal-message"},(0,e.createElement)("p",null,l)),o(`meprModal.${p}.before-actions`,null),(0,e.createElement)(a.__experimentalHStack,{className:"mepr-modal-actions",alignment:"right"},(0,e.createElement)(a.Button,{variant:"secondary",onClick:m,disabled:u},s||(0,t.__)("Cancel","memberpress")),(0,e.createElement)(a.Button,{variant:"primary",isDestructive:d,disabled:u,onClick:async()=>{w(!0);try{await i()}catch(e){console.error("Modal confirm error:",e)}finally{w(!1)}}},u?(0,e.createElement)(a.Spinner,null):c||(0,t.__)("Confirm","memberpress")))))},{applyFilters:r,doAction:l}=wp.hooks;let c=null;window.MeprModal=t=>{let{title:a,message:o,confirmText:s,cancelText:i,isDestructive:m=!1,ajaxData:d,onConfirm:p,onSuccess:u,onError:w,modalContext:C}=t;const f=(()=>{let t=document.getElementById("mepr-modal-root");return t||(t=document.createElement("div"),t.id="mepr-modal-root",document.body.appendChild(t)),c||(c=(0,e.createRoot)(t)),c})(),x=()=>{f.render(null)};C=C||d.action,a=r(`meprModal.${C}.title`,a),o=r(`meprModal.${C}.message`,o),f.render((0,e.createElement)(n,{title:a,message:o,confirmText:s,cancelText:i,isDestructive:m,onConfirm:async()=>{if(p){try{await p()}catch(e){console.error("Modal confirm callback error:",e)}x()}else if(d)try{d=r(`MeprModal.${C}.ajaxData`,d);const e=new FormData;Object.keys(d).forEach((t=>{e.append(t,d[t])}));const t=await fetch(window.ajaxurl||ajaxurl,{method:"POST",body:e}),a=(e=>e.replace(/^\s+|\s+$/g,""))(await t.text());return"true"===a||t.ok?(l(`meprModal.${C}.success`,{ajaxData:d,response:a}),u&&await u(a),void x()):(w&&w(a),void x())}catch(e){throw new Error(e.message)}},onCancel:()=>{x()},modalContext:C}))}})();