(function(){"use strict";var it=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof{}<"u"?{}:typeof self<"u"?self:{},G={},rt={get exports(){return G},set exports(r){G=r}};(function(r,i){(function(c,f){r.exports=f()})(it,function(){var c=1e3,f=6e4,y=36e5,w="millisecond",$="second",S="minute",_="hour",v="day",O="week",M="month",A="quarter",h="year",Y="date",et="Invalid Date",ht=/^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/,mt=/\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g,pt={name:"en",weekdays:"Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"),months:"January_February_March_April_May_June_July_August_September_October_November_December".split("_"),ordinal:function(s){var n=["th","st","nd","rd"],t=s%100;return"["+s+(n[(t-20)%10]||n[t]||n[0])+"]"}},V=function(s,n,t){var o=String(s);return!o||o.length>=n?s:""+Array(n+1-o.length).join(t)+s},$t={s:V,z:function(s){var n=-s.utcOffset(),t=Math.abs(n),o=Math.floor(t/60),e=t%60;return(n<=0?"+":"-")+V(o,2,"0")+":"+V(e,2,"0")},m:function s(n,t){if(n.date()1)return s(a[0])}else{var d=n.name;q[d]=n,e=d}return!o&&e&&(I=e),e||!o&&I},g=function(s,n){if(Z(s))return s.clone();var t=typeof n=="object"?n:{};return t.date=s,t.args=arguments,new U(t)},l=$t;l.l=P,l.i=Z,l.w=function(s,n){return g(s,{locale:n.$L,utc:n.$u,x:n.$x,$offset:n.$offset})};var U=function(){function s(t){this.$L=P(t.locale,null,!0),this.parse(t)}var n=s.prototype;return n.parse=function(t){this.$d=function(o){var e=o.date,u=o.utc;if(e===null)return new Date(NaN);if(l.u(e))return new Date;if(e instanceof Date)return new Date(e);if(typeof e=="string"&&!/Z$/i.test(e)){var a=e.match(ht);if(a){var d=a[2]-1||0,p=(a[7]||"0").substring(0,3);return u?new Date(Date.UTC(a[1],d,a[3]||1,a[4]||0,a[5]||0,a[6]||0,p)):new Date(a[1],d,a[3]||1,a[4]||0,a[5]||0,a[6]||0,p)}}return new Date(e)}(t),this.$x=t.x||{},this.init()},n.init=function(){var t=this.$d;this.$y=t.getFullYear(),this.$M=t.getMonth(),this.$D=t.getDate(),this.$W=t.getDay(),this.$H=t.getHours(),this.$m=t.getMinutes(),this.$s=t.getSeconds(),this.$ms=t.getMilliseconds()},n.$utils=function(){return l},n.isValid=function(){return this.$d.toString()!==et},n.isSame=function(t,o){var e=g(t);return this.startOf(o)<=e&&e<=this.endOf(o)},n.isAfter=function(t,o){return g(t)new Promise(f=>{const y=new MutationObserver(()=>{!!r.querySelector(i)==c&&(y.disconnect(),f(void 0))});y.observe(r,{childList:!0,subtree:!0}),!!r.querySelector(i)==c&&f(void 0)}),K=r=>new Promise(function(i,c){setTimeout(()=>c("Timeout"),r)}),X=(r,i)=>Promise.race([Q(r,i,!0),K(1e4)]),ut=(r,i)=>Promise.race([Q(r,i,!1),K(1e4)]),H=async(r,i,c)=>{const f=await(await fetch(i)).blob(),y=new File([f],c),w=new DataTransfer;w.items.add(y),r.querySelector("button[aria-label='Clear']")?.click(),await ut(r,"button[aria-label='Clear']");const $=r.querySelector("input[type='file']");$.value="",$.files=w.files,$.dispatchEvent(new Event("change",{bubbles:!0,composed:!0})),await X(r,"button[aria-label='Clear']")},L=(r,i)=>{r.querySelectorAll("button")[i].click()},ct=r=>{const i=r.querySelector(":scope > .label-wrap");i&&(i.classList.contains("open")||i.click())},dt=r=>{gradioApp().querySelector("#openpose3d_iframe").contentWindow.postMessage(r,"*")},z={},J={},lt=()=>{window.addEventListener("message",r=>{const{data:i}=r;if(i&&i.cmd&&i.cmd=="openpose-3d"&&i.method){const c=i.method;console.log("Method",c,r),i.type=="return"?z[c]?.(i.payload):i.type=="event"&&(console.log(J),J[c]?.(i.payload))}})},ft=r=>{Object.assign(J,r)};function W(r,...i){return new Promise((c,f)=>{const y=setTimeout(()=>{delete z[r],f({method:r,status:"Timeout"})},1e3),w=$=>{clearTimeout(y),c($)};z[r]=w,dt({cmd:"openpose-3d",method:r,type:"call",payload:i})})}const tt=()=>{const r=gradioApp().querySelector("#tab_threedopenpose");return r&&r.style.display!="none"};let R=!1,j=!1;onUiLoaded(async()=>{console.log("sd-webui-3d-open-pose-editor: onUiLoaded");const r=async(i,c,f,y,w,$,S,_,v)=>{let O=i.querySelector("#controlnet");if(O)ct(O);else{for(const h of i.querySelectorAll(".cursor-pointer > span"))h.textContent?.includes("ControlNet")&&(h.textContent?.includes("M2M")||(O=h.parentElement?.parentElement));if(!O){console.error("ControlNet element not found");return}}await X(O,'div[data-testid="image"]');const M=O.querySelectorAll('div[data-testid="image"]'),A=O.querySelector(".tab-nav");if(c&&f!=""&&f!="-"){const h=Number(f);A&&L(A,h),await H(M[h],c,"pose.png")}if(y&&w!=""&&w!="-"){const h=Number(w);A&&L(A,h),await H(M[h],y,"depth.png")}if($&&S!=""&&S!="-"){const h=Number(S);A&&L(A,h),await H(M[h],$,"normal.png")}if(_&&v!=""&&v!="-"){const h=Number(v);A&&L(A,h),await H(M[h],_,"canny.png")}};window.openpose3d={sendTxt2img:async(i,c,f,y,w,$,S,_)=>{const v=gradioApp().querySelector("#txt2img_script_container");switch_to_txt2img(),await r(v,i,c,f,y,w,$,S,_)},sendImg2img:async(i,c,f,y,w,$,S,_)=>{const v=gradioApp().querySelector("#img2img_script_container");switch_to_img2img(),await r(v,i,c,f,y,w,$,S,_)},downloadImage:(i,c)=>{if(!i)return;const f=c+"_"+ot()+".png";at(i,f)}},lt(),ft({MakeImages:async i=>{for(const[f,y]of Object.entries(i)){const w=gradioApp().querySelector(`#openpose3d_${f}_image`);await H(w,y,f+".png")}const c=gradioApp().querySelector("#openpose3d_main");L(c,1)}});for(let i=0;i<30;++i)try{await W("GetAppVersion"),R=!0;break}catch(c){if(c.status!="Timeout")throw c}if(!R){console.error("sd-webui-3d-open-pose-editor: Timeout");return}tt()||await W("Pause")}),onUiUpdate(async()=>{R&&(tt()?j&&(j=!1,await W("Resume")):j||(j=!0,await W("Pause")))})})();