document.addEventListener("click",a=>{if(!a.target.closest(".add-new-notifal_template"))return;a.preventDefault();const e=document.querySelector(".notifal-modal-backdrop");e&&(e.style.display="flex"),e?.querySelector(".notifal-modal-close")?.addEventListener("click",()=>{e.style.display="none"})});document.addEventListener("click",a=>{const t=a.target.closest("#notifal-import-trigger");if(!t)return;a.preventDefault();const e=document.getElementById("notifal-import-file"),o=document.getElementById("notifal-import-form");!e||!o||(e.click(),e.addEventListener("change",()=>{if(!e.files.length)return;if(!confirm(`⚠️ ${NotifalTemplateStrings.import.import_template_alert}`)){e.value="";return}const n=new FormData(o);n.set("notifal_import_file",e.files[0]);const c=t.dataset.loadingText,l=t.dataset.originalText;t.textContent=c,t.classList.add("loading","notifal-disable"),t.setAttribute("disabled","disabled"),fetch(NotifalTemplatesAjax.ajax_url,{method:"POST",body:n}).then(i=>i.json()).then(i=>{t.textContent=l,t.classList.remove("loading","notifal-disable"),t.removeAttribute("disabled");const r=i?.data||{},d=r.success||0,m=d>0?`${r.title} ${NotifalTemplateStrings.import.import_done}`:NotifalTemplateStrings.import.import_failed,f=d>0?"success":"error",s=new URL(NotifalTemplateStrings.templates_page_url);s.searchParams.set("notifal_message",m),s.searchParams.set("notifal_message_type",f),window.location.href=s.toString()}).catch(()=>{NotifalUtils.showMessage(NotifalTemplateStrings.import_failed,"error"),t.textContent=l,t.classList.remove("loading","notifal-disable"),t.removeAttribute("disabled")})}))});
