import{A as ht,B as ut}from"./schema-B1NKxfZc.js";let X=null;function wt(){return window.spine?Promise.resolve(window.spine):X||(X=new Promise((e,n)=>{const s=document.createElement("script");s.src=`/vendor/spine-webgl-3.6.js?v=${ut}`,s.onload=()=>window.spine?e(window.spine):n(new Error("spine-webgl-3.6.js loaded but window.spine is missing")),s.onerror=()=>n(new Error("failed to load /vendor/spine-webgl-3.6.js")),document.head.appendChild(s)}),X)}function G(e){let n=!1;const s=()=>{n||(n=!0,e())},u=requestAnimationFrame(s);return setTimeout(s,100),u}function gt(e){const n=e.webgl.WebGLBlendModeConverter;if(n.__l15ScreenFixed)return;const s=n.getSourceGLBlendMode.bind(n),u=n.getDestGLBlendMode.bind(n);n.getSourceGLBlendMode=(o,f=!1)=>s(o===e.BlendMode.Screen?e.BlendMode.Additive:o,f),n.getDestGLBlendMode=o=>u(o===e.BlendMode.Screen?e.BlendMode.Additive:o),n.__l15ScreenFixed=!0}function Mt(e,n,s,u,o,f){const t=new Float32Array(48);u.forEach((x,v)=>{s.setBlendMode(v===0?n.ONE:n.SRC_ALPHA,v===0?n.ZERO:n.ONE_MINUS_SRC_ALPHA);const g=x.getImage(),M=g.width/g.height,a=o/f,w=M>a?a/M:1,b=M>a?1:M/a,E=(1-w)/2,I=(1-b)/2;[[0,1,E,I],[1,1,E+w,I],[1,0,E+w,I+b],[0,0,E,I+b]].forEach(([d,h,S,k],L)=>t.set([d,h,1,1,1,1,S,k,0,0,0,0],L*12)),s.draw(x,t,[0,1,2,2,3,0])})}function bt(e,n){const s=new Map,u=o=>o.data.attachmentName??o.getAttachment()?.name??"";for(const o of n.slots){const f=u(o);f&&o.data.blendMode!==e.BlendMode.Normal&&s.set(f,o.data.blendMode)}for(const o of n.slots){const f=s.get(u(o));f!==void 0&&o.data.blendMode===e.BlendMode.Normal&&(o.data.blendMode=f)}}function dt(e,n,s,u,o="body"){if(o==="all"){n.getBounds(s,u,[]);return}let f=1/0,t=1/0,x=-1/0,v=-1/0;const g=[];for(const M of n.drawOrder){if(M.data.blendMode!==e.BlendMode.Normal||/^(fx|fire|glow|particle|smoke|wenli)/i.test(String(M.data.name)))continue;const a=M.getAttachment();let w=null;if(a instanceof e.RegionAttachment?(w=e.Utils.setArraySize(g,8,0),a.computeWorldVertices(M.bone,w,0,2)):a instanceof e.MeshAttachment&&(w=e.Utils.setArraySize(g,a.worldVerticesLength,0),a.computeWorldVertices(M,0,a.worldVerticesLength,w,0,2)),!!w)for(let b=0;b<w.length;b+=2)f=Math.min(f,w[b]),x=Math.max(x,w[b]),t=Math.min(t,w[b+1]),v=Math.max(v,w[b+1])}if(!isFinite(f)){n.getBounds(s,u,[]);return}s.set(f,t),u.set(x-f,v-t)}const J={minWidth:.6,maxBottom:.15,maxTop:.85,minAspect:1.2};function yt(e,n,s,u,o,f=8){const t=[],x=i=>{const d=i.getAttachment();let h=null;if(d instanceof e.RegionAttachment?(h=e.Utils.setArraySize(t,8,0),d.computeWorldVertices(i.bone,h,0,2)):d instanceof e.MeshAttachment&&(h=e.Utils.setArraySize(t,d.worldVerticesLength,0),d.computeWorldVertices(i,0,d.worldVerticesLength,h,0,2)),!h)return null;let S=1/0,k=-1/0,L=1/0,D=-1/0;for(let T=0;T<h.length;T+=2)S=Math.min(S,h[T]),k=Math.max(k,h[T]),L=Math.min(L,h[T+1]),D=Math.max(D,h[T+1]);return{x0:S,x1:k,y0:L,y1:D}},v=new Set,g=new Set,M=()=>{for(const i of n.slots){if(i.data.blendMode===e.BlendMode.Normal)continue;const d=x(i);if(!d)continue;g.add(i);const h=d.x1-d.x0,S=d.y1-d.y0,k=h/o.w>=J.minWidth,L=(d.y0-o.y)/o.h<=J.maxBottom,D=(d.y1-o.y)/o.h<=J.maxTop&&(S<=0||h/S>=J.minAspect);k&&L&&D&&v.add(i)}};M();for(const i of u){const d=new e.AnimationState(new e.AnimationStateData(s));try{d.setAnimation(0,i,!0)}catch{continue}const h=d.tracks?.[0]?.animation?.duration??0;if(!(h<=0))for(let S=0;S<=f;S++)S>0&&d.update(h/f),d.apply(n),n.updateWorldTransform(),M()}n.setToSetupPose(),n.updateWorldTransform();const a=[...v],w=g.size-a.length;if(!a.length)return{bones:[],slots:[],rejected:w};const b=new Set;for(const i of n.slots)i.data.blendMode===e.BlendMode.Normal&&b.add(i.bone);const E=i=>b.has(i)||(i.children??[]).some(d=>E(d));return{bones:[...new Set(a.map(i=>i.bone))].filter(i=>!E(i)),slots:a,rejected:w}}const xt=.12;function At(e){const n=e[0];if(e.length===1)return n;const s=Math.min(...e.map(t=>t.cx-t.refW/2)),u=Math.max(...e.map(t=>t.cx+t.refW/2)),o=Math.min(...e.map(t=>t.restY)),f=Math.max(...e.map(t=>t.restY+t.refH));return{cx:(s+u)/2,refW:u-s,refH:f-o,bottomY:Math.min(...e.map(t=>t.bottomY)),restY:o}}function St(e,n,s,u,o=24,f="body"){const t=new e.Vector2,x=new e.Vector2;dt(e,n,t,x,f);const v=t.y,g={cx:t.x+x.x/2,refW:x.x,refH:x.y,bottomY:t.y,restY:v};for(const M of u){const a=new e.AnimationState(new e.AnimationStateData(s));try{a.setAnimation(0,M,!0)}catch{continue}const w=a.tracks?.[0]?.animation?.duration??0;if(w<=0)continue;const b=w/o;for(let E=0;E<=o;E++)E>0&&a.update(b),a.apply(n),n.updateWorldTransform(),dt(e,n,t,x,f),g.bottomY=Math.min(g.bottomY,t.y)}return g.bottomY=Math.max(g.bottomY,v-g.refH*xt),n.setToSetupPose(),n.updateWorldTransform(),g}function vt(e,n,s,u,o,f,t={}){const x=t.fill??1,v=t.bottomMargin??.012,g=t.introAnimation,M=g&&(t.introInFit??!0)?[g,o]:[o],a={...t.tuning},w=()=>a.smokeDrop??t.smokeDrop??0,b=()=>a.smokeOpacity??t.smokeOpacity??1,E=t.widthSlack??1.5,I=t.backdrop??[];let i=!1,d=null,h=0,S=null,k=!1,L={bones:[],slots:[],rejected:0},D=[],T=null,H=null,tt={slots:0,drawn:0},et=null;const m=document.createElement("canvas");m.style.cssText="position:absolute;inset:0;width:100%;height:100%;",e.appendChild(m);const j=c=>{i||(m.remove(),f(c instanceof Error?c:new Error(String(c))))};return wt().then(c=>{if(!i)try{const _=Math.min(window.devicePixelRatio||1,2),nt=()=>{m.width=Math.max(1,Math.round(e.clientWidth*_)),m.height=Math.max(1,Math.round(e.clientHeight*_))};nt();const A=m.getContext("webgl",{alpha:!0})??m.getContext("experimental-webgl",{alpha:!0});if(!A)throw new Error("WebGL unavailable");A.viewport(0,0,m.width,m.height),gt(c);const C=c.webgl.Shader.newTwoColoredTextured(A),V=new c.webgl.PolygonBatcher(A),ot=new c.webgl.Matrix4,rt=new c.webgl.Matrix4,at=()=>{if(!t.region)return{x:0,y:0,w:m.width,h:m.height};const l=t.region(),p=Math.max(1,Math.round(l.h*_)),P=Math.max(1,Math.round(l.w*_));return{x:Math.round(l.x*_),y:m.height-Math.round(l.y*_)-p,w:P,h:p}},st=new c.webgl.SkeletonRenderer(A),it=t.layers?.length?t.layers:[{prefix:s,name:u}],z=it.map(l=>{const p=new c.webgl.AssetManager(A,`${n}/${ht.spineDir(l.prefix,l.name)}/`);return p.loadText(`${l.name}.json`),p.loadTextureAtlas(`${l.name}.atlas`),p}),$=new c.webgl.AssetManager(A,"");d=()=>{for(const l of z)try{l.dispose()}catch{}try{$.dispose()}catch{}try{A.getExtension("WEBGL_lose_context")?.loseContext()}catch{}};for(const l of I)$.loadTexture(l);let O=[],q={bones:[],slots:[],rejected:0},Z=0;const ct=()=>{if(!i)try{if(z.some(r=>!r.isLoadingComplete())||!$.isLoadingComplete()){h=G(ct);return}for(const r of z)if(r.hasErrors())throw new Error(`spine asset load failed: ${JSON.stringify(r.getErrors())}`);if($.hasErrors())throw new Error(`showcase backdrop failed: ${JSON.stringify($.getErrors())}`);const l=I.map(r=>$.get(r)),p=[];O=it.map((r,B)=>{const W=z[B].get(`${r.name}.atlas`),Y=new c.SkeletonJson(new c.AtlasAttachmentLoader(W)).readSkeletonData(z[B].get(`${r.name}.json`)),R=new c.Skeleton(Y);R.setToSetupPose(),bt(c,R),R.updateWorldTransform(),p.push(St(c,R,Y,M,24,t.boundsMode??"body")),B===0&&(q=L=yt(c,R,Y,M,{y:p[0].restY,w:p[0].refW,h:p[0].refH}));const y=new c.AnimationState(new c.AnimationStateData(Y)),ft=new Set((Y.animations??[]).map(mt=>String(mt.name)));return{skeleton:R,state:y,names:ft}}),H=At(p),S=K,k=!0,K();const P=r=>{let B=!1;if(g&&r.names.has(g))try{r.state.setAnimation(0,g,!1),r.names.has(o)&&r.state.addAnimation(0,o,!0,0),B=!0}catch{B=!1}if(B)return;const W=r.names.has(o)?o:[...r.names][0];W&&r.state.setAnimation(0,W,!0)};for(const r of O)P(r);D=[...new Set(O.flatMap(r=>[...r.names]))],et=(r,B)=>{for(const W of O){const N=W.names.has(r)?r:[...W.names][0];N&&W.state.setAnimation(0,N,B)}};let U=Date.now()/1e3;const F=()=>{if(i)return;const r=Date.now()/1e3,B=r-U;U=r,A.viewport(0,0,m.width,m.height);const W=t.clearColor;W?A.clearColor(W[0],W[1],W[2],1):A.clearColor(0,0,0,0),A.clear(A.COLOR_BUFFER_BIT);const N=(H?.refH??0)*w(),Y=b();for(const y of O)y.state.update(B),y.skeleton.setToSetupPose(),y.state.apply(y.skeleton);if(N)for(const y of q.bones)y.y-=N;for(const y of O)y.skeleton.updateWorldTransform();if(Y!==1)for(const y of q.slots)y.color.a=Math.min(1,y.color.a*Y);C.bind(),C.setUniformi(c.webgl.Shader.SAMPLER,0),l.length&&(rt.ortho2d(0,0,1,1),C.setUniform4x4f(c.webgl.Shader.MVP_MATRIX,rt.values),V.begin(C),Mt(c,A,V,l,m.width,m.height),V.end());const R=at();A.viewport(R.x,R.y,R.w,R.h),C.setUniform4x4f(c.webgl.Shader.MVP_MATRIX,ot.values),V.begin(C),st.premultipliedAlpha=!1;for(const y of O)st.draw(V,y.skeleton);if(V.end(),C.unbind(),Z++,Z===1&&t.onReady)try{t.onReady()}catch(y){console.warn("[spine] onReady threw",y)}h=G(F)};h=G(F)}catch(l){j(l)}},K=()=>{if(!H)return;const l=H,p=at(),P=p.w/p.h,U=t.referenceAspect??P,F=(t.unitHeight??Math.max(l.refH,l.refW/U)/Math.max(.05,x))/(a.scale??1);let r=a.scale!==void 0?F:Math.max(F,l.refW/E/P);t.cover&&(r=Math.min(r,l.refH,l.refW/P));const B=r*P;T={x:l.cx-B/2-r*(a.offsetX??0),y:l.bottomY-r*(v+(a.offsetY??0)),w:B,h:r},ot.ortho2d(T.x,T.y,T.w,T.h)};h=G(ct),setTimeout(()=>{i||Z>0||j(new Error(`spine showcase drew no frames within 10s (${u}) — falling back to the static portrait`))},1e4);const Q=()=>{const l=`${m.width}x${m.height}`;nt(),`${m.width}x${m.height}`!==l&&(A.viewport(0,0,m.width,m.height),H&&K())},lt=new ResizeObserver(Q);lt.observe(e),window.addEventListener("resize",Q),m.__cleanup=()=>{lt.disconnect(),window.removeEventListener("resize",Q)}}catch(_){j(_)}},j),{info(){return k?{hazeSlots:L.slots.length,hazeMoved:L.bones.length,hazeRejected:L.rejected,animations:D,fit:H,view:T,slots:tt.slots,drawnSlots:tt.drawn}:null},play(c,_=!0){try{et?.(c,_)}catch{}},setTuning(c){Object.assign(a,c),S?.()},destroy(){i=!0,cancelAnimationFrame(h),m.__cleanup?.();try{d?.()}catch{}d=null,m.remove()}}}export{vt as m};
