/* esm.sh - @sentry-internal/replay-canvas@9.17.0 */
import{defineIntegration as $}from"/@sentry/core@9.17.0/es2022/core.mjs";var X=Object.defineProperty,Y=(t,e,n)=>e in t?X(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,k=(t,e,n)=>Y(t,typeof e!="symbol"?e+"":e,n),R=class{constructor(){k(this,"idNodeMap",new Map),k(this,"nodeMetaMap",new WeakMap)}getId(e){return e?this.getMeta(e)?.id??-1:-1}getNode(e){return this.idNodeMap.get(e)||null}getIds(){return Array.from(this.idNodeMap.keys())}getMeta(e){return this.nodeMetaMap.get(e)||null}removeNodeFromMap(e){let n=this.getId(e);this.idNodeMap.delete(n),e.childNodes&&e.childNodes.forEach(r=>this.removeNodeFromMap(r))}has(e){return this.idNodeMap.has(e)}hasNode(e){return this.nodeMetaMap.has(e)}add(e,n){let r=n.id;this.idNodeMap.set(r,e),this.nodeMetaMap.set(e,n)}replace(e,n){let r=this.getNode(e);if(r){let s=this.nodeMetaMap.get(r);s&&this.nodeMetaMap.set(n,s)}this.idNodeMap.set(e,n)}reset(){this.idNodeMap=new Map,this.nodeMetaMap=new WeakMap}};function Z(){return new R}function J(t,e){for(let n=t.classList.length;n--;){let r=t.classList[n];if(e.test(r))return!0}return!1}function O(t,e,n=1/0,r=0){return!t||t.nodeType!==t.ELEMENT_NODE||r>n?-1:e(t)?r:O(t.parentNode,e,n,r+1)}function D(t,e){return n=>{let r=n;if(r===null)return!1;try{if(t){if(typeof t=="string"){if(r.matches(`.${t}`))return!0}else if(J(r,t))return!0}return!!(e&&r.matches(e))}catch{return!1}}}var b=`Please stop import mirror directly. Instead of that,\r
now you can use replayer.getMirror() to access the mirror instance of a replayer,\r
or you can use record.mirror to access the mirror instance during recording.`,P={map:{},getId(){return console.error(b),-1},getNode(){return console.error(b),null},removeNodeFromMap(){console.error(b)},has(){return console.error(b),!1},reset(){console.error(b)}};typeof window<"u"&&window.Proxy&&window.Reflect&&(P=new Proxy(P,{get(t,e,n){return e==="map"&&console.error(b),Reflect.get(t,e,n)}}));function x(t,e,n,r,s=window){let a=s.Object.getOwnPropertyDescriptor(t,e);return s.Object.defineProperty(t,e,r?n:{set(o){j(()=>{n.set.call(this,o)},0),a&&a.set&&a.set.call(this,o)}}),()=>x(t,e,a||{},!0)}function E(t,e,n){try{if(!(e in t))return()=>{};let r=t[e],s=n(r);return typeof s=="function"&&(s.prototype=s.prototype||{},Object.defineProperties(s,{__rrweb_original__:{enumerable:!1,value:r}})),t[e]=s,()=>{t[e]=r}}catch{return()=>{}}}Date.now().toString();function K(t){if(!t)return null;try{return t.nodeType===t.ELEMENT_NODE?t:t.parentElement}catch{return null}}function I(t,e,n,r,s){if(!t)return!1;let a=K(t);if(!a)return!1;let o=D(e,n);if(!s){let h=r&&a.matches(r);return o(a)&&!h}let i=O(a,o),c=-1;return i<0?!1:(r&&(c=O(a,D(null,r))),i>-1&&c<0?!0:i<c)}var F={};function z(t){let e=F[t];if(e)return e;let n=window.document,r=window[t];if(n&&typeof n.createElement=="function")try{let s=n.createElement("iframe");s.hidden=!0,n.head.appendChild(s);let a=s.contentWindow;a&&a[t]&&(r=a[t]),n.head.removeChild(s)}catch{}return F[t]=r.bind(window)}function w(...t){return z("requestAnimationFrame")(...t)}function j(...t){return z("setTimeout")(...t)}var C=(t=>(t[t["2D"]=0]="2D",t[t.WebGL=1]="WebGL",t[t.WebGL2=2]="WebGL2",t))(C||{}),S;function tt(t){S=t}var W=t=>S?(...n)=>{try{return t(...n)}catch(r){if(S&&S(r)===!0)return()=>{};throw r}}:t,M="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",et=typeof Uint8Array>"u"?[]:new Uint8Array(256);for(v=0;v<M.length;v++)et[M.charCodeAt(v)]=v;var v,nt=function(t){var e=new Uint8Array(t),n,r=e.length,s="";for(n=0;n<r;n+=3)s+=M[e[n]>>2],s+=M[(e[n]&3)<<4|e[n+1]>>4],s+=M[(e[n+1]&15)<<2|e[n+2]>>6],s+=M[e[n+2]&63];return r%3===2?s=s.substring(0,s.length-1)+"=":r%3===1&&(s=s.substring(0,s.length-2)+"=="),s},G=new Map;function rt(t,e){let n=G.get(t);return n||(n=new Map,G.set(t,n)),n.has(e)||n.set(e,[]),n.get(e)}var q=(t,e,n)=>{if(!t||!(Q(t,e)||typeof t=="object"))return;let r=t.constructor.name,s=rt(n,r),a=s.indexOf(t);return a===-1&&(a=s.length,s.push(t)),a};function A(t,e,n){if(t instanceof Array)return t.map(r=>A(r,e,n));if(t===null)return t;if(t instanceof Float32Array||t instanceof Float64Array||t instanceof Int32Array||t instanceof Uint32Array||t instanceof Uint8Array||t instanceof Uint16Array||t instanceof Int16Array||t instanceof Int8Array||t instanceof Uint8ClampedArray)return{rr_type:t.constructor.name,args:[Object.values(t)]};if(t instanceof ArrayBuffer){let r=t.constructor.name,s=nt(t);return{rr_type:r,base64:s}}else{if(t instanceof DataView)return{rr_type:t.constructor.name,args:[A(t.buffer,e,n),t.byteOffset,t.byteLength]};if(t instanceof HTMLImageElement){let r=t.constructor.name,{src:s}=t;return{rr_type:r,src:s}}else if(t instanceof HTMLCanvasElement){let r="HTMLImageElement",s=t.toDataURL();return{rr_type:r,src:s}}else{if(t instanceof ImageData)return{rr_type:t.constructor.name,args:[A(t.data,e,n),t.width,t.height]};if(Q(t,e)||typeof t=="object"){let r=t.constructor.name,s=q(t,e,n);return{rr_type:r,index:s}}}}return t}var V=(t,e,n)=>t.map(r=>A(r,e,n)),Q=(t,e)=>!!["WebGLActiveInfo","WebGLBuffer","WebGLFramebuffer","WebGLProgram","WebGLRenderbuffer","WebGLShader","WebGLShaderPrecisionFormat","WebGLTexture","WebGLUniformLocation","WebGLVertexArrayObject","WebGLVertexArrayObjectOES"].filter(s=>typeof e[s]=="function").find(s=>t instanceof e[s]);function st(t,e,n,r,s){let a=[],o=Object.getOwnPropertyNames(e.CanvasRenderingContext2D.prototype);for(let i of o)try{if(typeof e.CanvasRenderingContext2D.prototype[i]!="function")continue;let c=E(e.CanvasRenderingContext2D.prototype,i,function(h){return function(...f){return I(this.canvas,n,r,s,!0)||j(()=>{let d=V(f,e,this);t(this.canvas,{type:C["2D"],property:i,args:d})},0),h.apply(this,f)}});a.push(c)}catch{let c=x(e.CanvasRenderingContext2D.prototype,i,{set(h){t(this.canvas,{type:C["2D"],property:i,args:[h],setter:!0})}});a.push(c)}return()=>{a.forEach(i=>i())}}function at(t){return t==="experimental-webgl"?"webgl":t}function T(t,e,n,r,s){let a=[];try{let o=E(t.HTMLCanvasElement.prototype,"getContext",function(i){return function(c,...h){if(!I(this,e,n,r,!0)){let f=at(c);if("__context"in this||(this.__context=f),s&&["webgl","webgl2"].includes(f))if(h[0]&&typeof h[0]=="object"){let d=h[0];d.preserveDrawingBuffer||(d.preserveDrawingBuffer=!0)}else h.splice(0,1,{preserveDrawingBuffer:!0})}return i.apply(this,[c,...h])}});a.push(o)}catch{console.error("failed to patch HTMLCanvasElement.prototype.getContext")}return()=>{a.forEach(o=>o())}}function U(t,e,n,r,s,a,o,i){let c=[],h=Object.getOwnPropertyNames(t);for(let f of h)if(!["isContextLost","canvas","drawingBufferWidth","drawingBufferHeight"].includes(f))try{if(typeof t[f]!="function")continue;let d=E(t,f,function(l){return function(...m){let u=l.apply(this,m);if(q(u,i,this),"tagName"in this.canvas&&!I(this.canvas,r,s,a,!0)){let g=V(m,i,this),p={type:e,property:f,args:g};n(this.canvas,p)}return u}});c.push(d)}catch{let d=x(t,f,{set(l){n(this.canvas,{type:e,property:f,args:[l],setter:!0})}});c.push(d)}return c}function ot(t,e,n,r,s,a){let o=[];return o.push(...U(e.WebGLRenderingContext.prototype,C.WebGL,t,n,r,s,a,e)),typeof e.WebGL2RenderingContext<"u"&&o.push(...U(e.WebGL2RenderingContext.prototype,C.WebGL2,t,n,r,s,a,e)),()=>{o.forEach(i=>i())}}var it='for(var e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",t="undefined"==typeof Uint8Array?[]:new Uint8Array(256),a=0;a<64;a++)t[e.charCodeAt(a)]=a;var n=function(t){var a,n=new Uint8Array(t),r=n.length,s="";for(a=0;a<r;a+=3)s+=e[n[a]>>2],s+=e[(3&n[a])<<4|n[a+1]>>4],s+=e[(15&n[a+1])<<2|n[a+2]>>6],s+=e[63&n[a+2]];return r%3==2?s=s.substring(0,s.length-1)+"=":r%3==1&&(s=s.substring(0,s.length-2)+"=="),s};const r=new Map,s=new Map;const i=self;i.onmessage=async function(e){if(!("OffscreenCanvas"in globalThis))return i.postMessage({id:e.data.id});{const{id:t,bitmap:a,width:o,height:f,maxCanvasSize:c,dataURLOptions:g}=e.data,u=async function(e,t,a){const r=e+"-"+t;if("OffscreenCanvas"in globalThis){if(s.has(r))return s.get(r);const i=new OffscreenCanvas(e,t);i.getContext("2d");const o=await i.convertToBlob(a),f=await o.arrayBuffer(),c=n(f);return s.set(r,c),c}return""}(o,f,g),[h,d]=function(e,t,a){if(!a)return[e,t];const[n,r]=a;if(e<=n&&t<=r)return[e,t];let s=e,i=t;return s>n&&(i=Math.floor(n*t/e),s=n),i>r&&(s=Math.floor(r*e/t),i=r),[s,i]}(o,f,c),l=new OffscreenCanvas(h,d),w=l.getContext("bitmaprenderer"),p=h===o&&d===f?a:await createImageBitmap(a,{resizeWidth:h,resizeHeight:d,resizeQuality:"low"});w.transferFromImageBitmap(p),a.close();const y=await l.convertToBlob(g),v=y.type,b=await y.arrayBuffer(),m=n(b);if(p.close(),!r.has(t)&&await u===m)return r.set(t,m),i.postMessage({id:t});if(r.get(t)===m)return i.postMessage({id:t});i.postMessage({id:t,type:v,base64:m,width:o,height:f}),r.set(t,m)}};';function ct(){let t=new Blob([it]);return URL.createObjectURL(t)}var N=class{constructor(e){this.pendingCanvasMutations=new Map,this.rafStamps={latestId:0,invokeId:null},this.shadowDoms=new Set,this.windowsSet=new WeakSet,this.windows=[],this.restoreHandlers=[],this.frozen=!1,this.locked=!1,this.snapshotInProgressMap=new Map,this.worker=null,this.lastSnapshotTime=0,this.processMutation=(d,l)=>{(this.rafStamps.invokeId&&this.rafStamps.latestId!==this.rafStamps.invokeId||!this.rafStamps.invokeId)&&(this.rafStamps.invokeId=this.rafStamps.latestId),this.pendingCanvasMutations.has(d)||this.pendingCanvasMutations.set(d,[]),this.pendingCanvasMutations.get(d).push(l)};let{sampling:n="all",win:r,blockClass:s,blockSelector:a,unblockSelector:o,maxCanvasSize:i,recordCanvas:c,dataURLOptions:h,errorHandler:f}=e;this.mutationCb=e.mutationCb,this.mirror=e.mirror,this.options=e,f&&tt(f),(c&&typeof n=="number"||e.enableManualSnapshot)&&(this.worker=this.initFPSWorker()),this.addWindow(r),!e.enableManualSnapshot&&W(()=>{c&&n==="all"&&(this.startRAFTimestamping(),this.startPendingCanvasMutationFlusher()),c&&typeof n=="number"&&this.initCanvasFPSObserver(n,s,a,o,i,{dataURLOptions:h})})()}reset(){this.pendingCanvasMutations.clear(),this.restoreHandlers.forEach(e=>{try{e()}catch{}}),this.restoreHandlers=[],this.windowsSet=new WeakSet,this.windows=[],this.shadowDoms=new Set,this.worker?.terminate(),this.worker=null,this.snapshotInProgressMap=new Map}freeze(){this.frozen=!0}unfreeze(){this.frozen=!1}lock(){this.locked=!0}unlock(){this.locked=!1}addWindow(e){let{sampling:n="all",blockClass:r,blockSelector:s,unblockSelector:a,recordCanvas:o,enableManualSnapshot:i}=this.options;if(!this.windowsSet.has(e)){if(i){this.windowsSet.add(e),this.windows.push(new WeakRef(e));return}W(()=>{if(o&&n==="all"&&this.initCanvasMutationObserver(e,r,s,a),o&&typeof n=="number"){let c=T(e,r,s,a,!0);this.restoreHandlers.push(()=>{c()})}})(),this.windowsSet.add(e),this.windows.push(new WeakRef(e))}}addShadowRoot(e){this.shadowDoms.add(new WeakRef(e))}resetShadowRoots(){this.shadowDoms=new Set}initFPSWorker(){let e=new Worker(ct());return e.onmessage=n=>{let r=n.data,{id:s}=r;if(this.snapshotInProgressMap.set(s,!1),!("base64"in r))return;let{base64:a,type:o,width:i,height:c}=r;this.mutationCb({id:s,type:C["2D"],commands:[{property:"clearRect",args:[0,0,i,c]},{property:"drawImage",args:[{rr_type:"ImageBitmap",args:[{rr_type:"Blob",data:[{rr_type:"ArrayBuffer",base64:a}],type:o}]},0,0,i,c]}]})},e}initCanvasFPSObserver(e,n,r,s,a,o){let i=this.takeSnapshot(!1,e,n,r,s,a,o.dataURLOptions);this.restoreHandlers.push(()=>{cancelAnimationFrame(i)})}initCanvasMutationObserver(e,n,r,s){let a=T(e,n,r,s,!1),o=st(this.processMutation.bind(this),e,n,r,s),i=ot(this.processMutation.bind(this),e,n,r,s,this.mirror);this.restoreHandlers.push(()=>{a(),o(),i()})}snapshot(e){let{options:n}=this,r=this.takeSnapshot(!0,n.sampling==="all"?2:n.sampling||2,n.blockClass,n.blockSelector,n.unblockSelector,n.maxCanvasSize,n.dataURLOptions,e);this.restoreHandlers.push(()=>{cancelAnimationFrame(r)})}takeSnapshot(e,n,r,s,a,o,i,c){let h=1e3/n,f,d=m=>{if(m)return[m];let u=[],g=p=>{p.querySelectorAll("canvas").forEach(y=>{I(y,r,s,a,!0)||u.push(y)})};for(let p of this.windows){let y=p.deref(),_;try{_=y&&y.document}catch{}_&&g(_)}for(let p of this.shadowDoms){let y=p.deref();y&&g(y)}return u},l=m=>{if(this.windows.length){if(this.lastSnapshotTime&&m-this.lastSnapshotTime<h){f=w(l);return}this.lastSnapshotTime=m,d(c).forEach(u=>{if(!this.mirror.hasNode(u))return;let g=this.mirror.getId(u);if(!this.snapshotInProgressMap.get(g)&&!(!u.width||!u.height)){if(this.snapshotInProgressMap.set(g,!0),!e&&["webgl","webgl2"].includes(u.__context)){let p=u.getContext(u.__context);p?.getContextAttributes()?.preserveDrawingBuffer===!1&&p.clear(p.COLOR_BUFFER_BIT)}createImageBitmap(u).then(p=>{this.worker?.postMessage({id:g,bitmap:p,width:u.width,height:u.height,dataURLOptions:i,maxCanvasSize:o},[p])}).catch(p=>{W(()=>{throw p})()})}}),e||(f=w(l))}};return f=w(l),f}startPendingCanvasMutationFlusher(){w(()=>this.flushPendingCanvasMutations())}startRAFTimestamping(){let e=n=>{this.rafStamps.latestId=n,w(e)};w(e)}flushPendingCanvasMutations(){this.pendingCanvasMutations.forEach((e,n)=>{let r=this.mirror.getId(n);this.flushPendingCanvasMutationFor(n,r)}),w(()=>this.flushPendingCanvasMutations())}flushPendingCanvasMutationFor(e,n){if(this.frozen||this.locked)return;let r=this.pendingCanvasMutations.get(e);if(!r||n===-1)return;let s=r.map(o=>{let{type:i,...c}=o;return c}),{type:a}=r[0];this.mutationCb({id:n,type:a,commands:s}),this.pendingCanvasMutations.delete(e)}};try{if(Array.from([1],t=>t*2)[0]!==2){let t=document.createElement("iframe");document.body.appendChild(t),Array.from=t.contentWindow?.Array.from||Array.from,document.body.removeChild(t)}}catch(t){console.debug("Unable to override Array.from",t)}Z();var H;(function(t){t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped"})(H||(H={}));var B={low:{sampling:{canvas:1},dataURLOptions:{type:"image/webp",quality:.25}},medium:{sampling:{canvas:2},dataURLOptions:{type:"image/webp",quality:.4}},high:{sampling:{canvas:4},dataURLOptions:{type:"image/webp",quality:.5}}},ft="ReplayCanvas",L=1280,ht=((t={})=>{let[e,n]=t.maxCanvasSize||[],r={quality:t.quality||"medium",enableManualSnapshot:t.enableManualSnapshot,maxCanvasSize:[e?Math.min(e,L):L,n?Math.min(n,L):L]},s,a=new Promise(o=>s=o);return{name:ft,getOptions(){let{quality:o,enableManualSnapshot:i,maxCanvasSize:c}=r;return{enableManualSnapshot:i,recordCanvas:!0,getCanvasManager:h=>{let f=new N({...h,enableManualSnapshot:i,maxCanvasSize:c,errorHandler:d=>{try{typeof d=="object"&&(d.__rrweb__=!0)}catch{}}});return s(f),f},...B[o]||B.medium}},async snapshot(o){(await a).snapshot(o)}}}),ut=$(ht);export{ut as replayCanvasIntegration};
//# sourceMappingURL=replay-canvas.mjs.map