import{a as L}from"./chunk-DFR323EV.js";import{a as P,b as p}from"./chunk-OU45VCVM.js";import{a as M}from"./chunk-ULT7T2WM.js";import{b as c}from"./chunk-JC2BH436.js";var m=c(M());var b=c(P()),w=c(L()),d=class{constructor(t){this.dialogRect=null;this.dialogOffsetX=0;this.dialogOffsetY=0;this.handlePointerDown=t=>{let e=window.visualViewport,i=this.dialogRect,o=this.dialogHeaderElement,s,n,l=(r,D)=>{this.dialogOffsetX=Math.max(r-s,-i.left),this.dialogOffsetY=Math.max(D-n,-i.top),this.dialogOffsetX=Math.min(this.dialogOffsetX,e.width-i.right),this.dialogOffsetY=Math.min(this.dialogOffsetY,e.height-i.bottom),this.element.style.transform=`translate(${this.dialogOffsetX}px, ${this.dialogOffsetY}px)`},a=r=>{l(r.clientX,r.clientY)},h=r=>{r.preventDefault(),l(r.touches[0].clientX,r.touches[0].clientY)},y=r=>{o.releasePointerCapture(r.pointerId),o.removeEventListener("pointermove",a)},O=r=>{o.removeEventListener("touchmove",h)};t instanceof PointerEvent?(s=t.clientX-this.dialogOffsetX,n=t.clientY-this.dialogOffsetY,o.setPointerCapture(t.pointerId),o.addEventListener("pointermove",a),o.addEventListener("pointerup",y,{once:!0})):(s=t.touches[0].clientX-this.dialogOffsetX,n=t.touches[0].clientY-this.dialogOffsetY,o.addEventListener("touchmove",h),o.addEventListener("touchend",O,{once:!0}))};if(this.element=t,this.formElement=this.element.querySelector('form[method="dialog"]'),!this.formElement)throw new Error(`${this} requires child <form method="dialog">`);this.dialogHeaderElement=this.element.querySelector(".dialog-header"),this.isModal=this.element.hasAttribute("df-modal"),this.induceOpen=this.evalInducer("df-induce-open",(...e)=>this.openDialog(...e)),this.induceClose=this.evalInducer("df-induce-close",(...e)=>this.closeDialog(...e))}evalInducer(t,e){var o;let i=(o=this.element)==null?void 0:o.getAttribute(t);if(!(0,m.default)(i))return()=>{};try{let s=new Function(`return ${p(i,{startRule:"InduceExpression"})}`);return(...n)=>{s.call(this)&&e(...n)}}catch(s){throw new Error(`Error while parsing <dialog ${t}="${i}">: ${s}.`)}}openDialog(...t){let e=window.visualViewport;this.element.open||!e||(this.isModal?this.element.showModal():(this.element.show(),this.dialogHeaderElement&&!this.dialogRect&&(this.dialogRect=this.element.getBoundingClientRect(),this.dialogOffsetY=Math.max((e.height-this.dialogRect.height)/2,0),this.element.style.transform=`translate(${this.dialogOffsetX}px, ${this.dialogOffsetY}px)`,this.dialogHeaderElement.addEventListener("pointerdown",this.handlePointerDown),this.dialogHeaderElement.addEventListener("touchstart",this.handlePointerDown))),this.element.addEventListener("close",()=>this.closeDialog(),{once:!0}))}closeDialog(...t){this.element.close(t[1])}getDataValue(t){return null}isButtonActive(t,e){return!1}isOpen(){return this.element.open}updateOperability(...t){this.induceOpen(...t),this.induceClose(...t)}},u=class extends d{constructor(e){super(e);this.registerInducer=e=>{e instanceof CustomEvent&&(this.form=e.detail.form,this.form.formset.registerInducer(this,this.updateOperability))};let i=e.querySelector('form[method="dialog"]');if(!i)throw new Error(`${e} requires child <form method="dialog">`);i.addEventListener("django-formset-connected",this.registerInducer,{once:!0})}openDialog(...e){if(!this.element.open){if(!this.form)throw new Error(`${this}.form has never been registered`);this.form.setPristine(),this.form.untouch(),e[0]&&(0,w.default)(e[1])&&e[1].call(e[0],this.form.path),super.openDialog(...e)}}closeDialog(...e){if(!(0,m.default)(e[1]))return;let i=this.form;switch(e[1]){case"apply":i.isValid()&&this.element.close("apply");break;case"close":this.element.blur(),this.element.close("close");break;case"reset":i.resetToInitial();break;case"clear":i.resetToInitial(),this.element.blur(),this.element.close("clear");break;default:break}}getDataValue(e){return this.form.getDataValue(e)}isButtonActive(e,i){let o=e[0]!==""?e:(()=>{let n=[...this.form.path],l=e.filter(h=>h!==""),a=e.length-l.length;return n.splice(n.length-a+1),n.push(...l),n})(),s=this.form.formset.buttons.find(n=>(0,b.default)(n.path,o));return i==="active"&&s===this.form.formset.currentActiveButton}},g=Symbol("FormDialog"),v,E=class extends(v=HTMLDialogElement,g,v){constructor(){super(),this[g]=new u(this)}};export{d as a,E as b};
