"use strict";varU=Object.create;vary=Object.defineProperty;varE=Object.getOwnPropertyDescriptor;varV=Object.getOwnPropertyNames;varM=Object.getPrototypeOf,v=Object.prototype.hasOwnProperty;varR=(e,t)=>{for(varrint)y(e,r,{get:t[r],enumerable:!0})},A=(e,t,r,n)=>{if(t&&typeoft=="object"||typeoft=="function")for(letsofV(t))!v.call(e,s)&&s!==r&&y(e,s,{get:()=>t[s],enumerable:!(n=E(t,s))||n.enumerable});returne};varS=(e,t,r)=>(r=e!=null?U(M(e)):{},A(t||!e||!e.__esModule?y(r,"default",{value:e,enumerable:!0}):r,e)),T=e=>A(y({},"__esModule",{value:!0}),e);varz={};R(z,{compositePaintedImage:()=>Z});module.exports=T(z);varC=require("buffer"),k=S(require("react-native-fs")),x=S(require("upng-js")),F=require("./paintShaderRuntime"),I=require("./exportUtils");functionq(e,t,r,n,s){constm=n*s,c=newUint8Array(m*4),d=newMap;for(const[a,g]ofr)d.set(a+1,g);for(leta=0;a<m;a++){constg=t[a],l=g>0?d.get(g):void0,i=a*4;if(l)c[i]=l.r,c[i+1]=l.g,c[i+2]=l.b,c[i+3]=255;else{constf=a*3;c[i]=e[f+2],c[i+1]=e[f+1],c[i+2]=e[f],c[i+3]=255}}consth=x.default.encode([c.buffer],n,s,0);returnnewUint8Array(h)}asyncfunctionZ(e){const{originBuffer:t,cols:r,rows:n,pickBuffer:s,paintedRegions:m,destDir:c,exportPngBase64:d,exportPngBytes:h,shaderTextures:a,renderWidth:g,renderHeight:l}=e;if(m.size===0)thrownewError("No painted regions, cannot save");if(s.length!==r*n){consto="pickMap size does not match image";throwconsole.error("[VIZ-SAVE] composite will throw:",o,{pickLen:s.length,expected:r*n,cols:r,rows:n}),newError(o)}if(t.length!==r*n*3){consto="Original buffer size does not match image";throwconsole.error("[VIZ-SAVE] composite will throw:",o,{originLen:t.length,expected:r*n*3,cols:r,rows:n}),newError(o)}leti,f,B=!1,P=!1;if(d&&d.length>0)f=d,B=!0;elseif(h&&h.length>0)i=h,B=!0;elseif(a&&g&&l)try{consto=await(0,F.renderPaintedImageOffscreen)({originImage:a.originImage,paintColorMap:a.paintColorMap,lowFreqImage:a.lowFreqImage,highFreqImage:a.highFreqImage,width:g,height:l,showOrigin:!1});if(o){letw="";try{constp=o.encodeToBase64;typeofp=="function"&&(w=p.call(o)||"")}catch(p){console.warn("[VIZ-SAVE] offscreen encodeToBase64 failed:",p)}w&&w.length>0&&(i=newUint8Array(C.Buffer.from(w,"base64")),P=!0);try{constp=o.dispose;typeofp=="function"&&p.call(o)}catch{}}}catch(o){console.warn("[VIZ-SAVE] rich shader offscreen for export failed (will fallback):",o)}if(!f&&!i)try{i=q(t,s,m,r,n)}catch{thrownewError("CPU recolor PNG decoding failed")}constb=c??k.default.CachesDirectoryPath,u=`${b}/painted_${Date.now()}.png`;try{f?await(0,I.writePngBase64ToFile)(u,f):await(0,I.writePngBytesToFile)(u,i)}catch(o){throwconsole.error("[VIZ-SAVE] composite writeFile threw:",o,{filePath:u,dir:b}),o}return{filePath:u,width:r,height:n,paintedCount:m.size}}