import{g as B,r as _}from"./api-DZ3cXm1m.js";(function(){var T;const c=window.billingRuleFormConfig||{},f=((T=c.routes)==null?void 0:T.checkOverlap)??null,p=c.ruleId??null,g=c.translations??{},t=document.getElementById("is_fallback"),v=document.getElementById("conditionsSection");function h(){v&&(v.style.display=t!=null&&t.checked?"none":"")}t&&(t.addEventListener("change",()=>{h(),o()}),h());const s=document.getElementById("overlapWarning");function y(e){if(!s)return;if(!e||e.length===0){s.innerHTML="";return}const i=g.overlap_covered_by||'"{name}" ({module}) already covers this criteria — check priority order.',n=e.map(k=>`<li>${i.replace("{name}",k.rule_name??"").replace("{module}",k.module_name??"—")}</li>`).join(""),r=g.overlap_warning||"Olası kural çakışması";s.innerHTML=`<div class="alert alert-warning py-2 mt-3"><i class="fas fa-exclamation-triangle me-2"></i><strong>${r}:</strong><ul class="mb-0 mt-1">${n}</ul></div>`}function w(){return Array.from(document.querySelectorAll('input[name="events[]"]:checked')).map(e=>e.value)}function O(){const e=document.getElementById("conditionsJson");if(!e)return{};try{return JSON.parse(e.value)}catch{return{}}}async function E(e,i){const n=await fetch(f,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify(i)});if(n.status===401){const r=await _();if(r)return E(r,i);throw new Error("Unauthorized — token refresh failed")}if(!n.ok)throw new Error(`HTTP ${n.status}`);return n.json()}async function l(){if(!f)return;if(t!=null&&t.checked){y([]);return}const e=w(),i=O(),n={events:e,conditions:i};p!==null&&(n.ignore_rule_id=p);try{const r=await E(B(),n);y(r.overlaps||[])}catch(r){console.error("[billing-rules/form] overlap check failed:",r)}}let u=null,d=!1;function o(){d=!1,clearTimeout(u),u=setTimeout(l,600)}document.querySelectorAll('input[name="events[]"]').forEach(e=>{e.addEventListener("change",o)});const a=document.getElementById("ruleBuilderRoot");a&&(a.addEventListener("click",o),a.addEventListener("change",o),a.addEventListener("input",o));const m=document.getElementById("ruleForm");m&&m.addEventListener("submit",async e=>{d||(e.preventDefault(),d=!0,clearTimeout(u),await l(),m.submit())}),setTimeout(l,900)})();
