import{g as v,r as w}from"./api-DZ3cXm1m.js";document.addEventListener("DOMContentLoaded",function(){const t=document.getElementById("editPredefinedReplyForm");t&&t.addEventListener("submit",function(n){n.preventDefault(),p()});function p(){const n=t.querySelector('button[type="submit"]')||document.querySelector(`button[type="submit"][form="${t.id}"]`),l=n.innerHTML;n.disabled=!0,n.innerHTML='<i class="fas fa-spinner fa-spin me-2"></i>Processing...';const o=new FormData,a=t.querySelector('input[name="title"]')||t.querySelector('input[name^="translations["][name$="[title]"]'),i=t.querySelector('textarea[name="content"]')||t.querySelector('textarea[name^="translations["][name$="[content]"]'),s=document.getElementById("edit_status"),c=a?a.value:"",h=i?i.value:"",E=s?s.checked:!1;o.append("title",c),o.append("content",h),o.append("status",E?"1":"0");const d=document.getElementById("edit_department_ids");d&&Array.from(d.selectedOptions).map(r=>r.value).forEach(r=>{o.append("department_ids[]",r)});const u=document.getElementById("edit_role_ids");u&&Array.from(u.selectedOptions).map(r=>r.value).forEach(r=>{o.append("role_ids[]",r)}),t.querySelectorAll('input[name^="translations["], textarea[name^="translations["]').forEach(e=>{e.value&&o.append(e.name,e.value)}),o.append("_method","PUT");const m=window.routes&&window.routes.ticketPredefinedReplies&&window.routes.ticketPredefinedReplies.update||t.action,f=e=>({method:"POST",body:o,headers:{Accept:"application/json",Authorization:`Bearer ${e}`,"X-Requested-With":"XMLHttpRequest"}});(async()=>{let e=await fetch(m,f(v()));if(e.status===401){const r=await w();r&&(e=await fetch(m,f(r)))}return e})().then(e=>{if(e.redirected){window.location.href=e.url;return}return e.json()}).then(e=>{e&&e.errors?y(e.errors):e&&e.success===!1?Swal.fire({title:"Error!",text:e.message||"An error occurred",icon:"error"}):e&&e.success===!0&&e.redirect&&(window.location.href=e.redirect)}).catch(e=>{console.error("Error:",e),Swal.fire({title:"Error!",text:"An error occurred while submitting the form",icon:"error"})}).finally(()=>{n.disabled=!1,n.innerHTML=l})}function y(n){t.querySelectorAll(".invalid-feedback").forEach(i=>i.remove()),t.querySelectorAll(".is-invalid").forEach(i=>i.classList.remove("is-invalid")),Object.keys(n).forEach(i=>{const s=t.querySelector(`[name="${i}"]`);if(s){s.classList.add("is-invalid");const c=document.createElement("div");c.className="invalid-feedback",c.textContent=n[i][0],s.parentNode.appendChild(c)}});const a=t.querySelector(".is-invalid");a&&a.focus()}});
