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