"use strict";var h=Object.defineProperty;var q=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var A=Object.prototype.hasOwnProperty;var F=(e,r)=>{for(var n in r)h(e,n,{get:r[n],enumerable:!0})},T=(e,r,n,o)=>{if(r&&typeof r=="object"||typeof r=="function")for(let u of I(r))!A.call(e,u)&&u!==n&&h(e,u,{get:()=>r[u],enumerable:!(o=q(r,u))||o.enumerable});return e};var L=e=>T(h({},"__esModule",{value:!0}),e);var G={};F(G,{bgrColorEquals:()=>U,buildZoomPanMatrix:()=>E,canvasToNormalized:()=>j,clampPanOffset:()=>N,getContainRect:()=>C,getRegionHitPolygons:()=>S,lookupRegionFromPickMap:()=>D,pickKickNearStrip:()=>$,pickKickRegionFromMask:()=>K,pointHitsRegion:()=>B,pointInPolygon:()=>M,pointInPolygonWithPadding:()=>p,pointStrictlyHitsRegion:()=>w,prepareWorkScaledBgrBuffer:()=>Z,rectsEqual:()=>_,releaseOriginSkImage:()=>V,releasePaintResourceLayers:()=>O,resolveRegionHit:()=>Y,screenToCanvasCoords:()=>X,timeLog:()=>z});module.exports=L(G);var P=require("@shopify/react-native-skia"),d=require("./maskSegmentation"),g=require("./maskSegmentRuntime"),R=require("./pngImage");function U(e,r){return e.b===r.b&&e.g===r.g&&e.r===r.r}function _(e,r){return e.x===r.x&&e.y===r.y&&e.w===r.w&&e.h===r.h}function C(e,r,n,o){const u=n/o,m=e/r;if(u>m){const s=e,l=e/u;return{x:0,y:(r-l)/2,w:s,h:l}}const i=r,t=r*u;return{x:(e-t)/2,y:0,w:t,h:i}}function j(e,r,n,o,u,m){const i=C(n,o,u,m);return e<i.x||e>i.x+i.w||r<i.y||r>i.y+i.h?null:{x:(e-i.x)/i.w,y:(r-i.y)/i.h}}function E(e,r,n,o,u){const m=o/2,i=u/2,t=P.Skia.Matrix();return t.translate(e,r),t.translate(m,i),t.scale(n,n),t.translate(-m,-i),t}function N(e,r,n,o,u){if(!u||r<=1||n<=0||o<=0)return{x:0,y:0};const m=n/2,i=o/2,t=u,s=m+r*(t.x-m),l=m+r*(t.x+t.w-m),a=i+r*(t.y-i),f=i+r*(t.y+t.h-i);let c=e.x,b=e.y;return l-s>n?c=Math.max(n-l,Math.min(-s,c)):c=0,f-a>o?b=Math.max(o-f,Math.min(-a,b)):b=0,{x:c,y:b}}function X(e,r,n,o,u,m){return u<=1?{x:e,y:r}:{x:(e-m.x-n/2)/u+n/2,y:(r-m.y-o/2)/u+o/2}}function M(e,r,n){let o=!1;for(let u=0,m=n.length-1;u<n.length;m=u++){const i=n[u].x,t=n[u].y,s=n[m].x,l=n[m].y;t>r!=l>r&&e<(s-i)*(r-t)/(l-t)+i&&(o=!o)}return o}function p(e,r,n,o){if(n.length<3)return!1;let u=n[0].x,m=n[0].x,i=n[0].y,t=n[0].y;for(const s of n)u=Math.min(u,s.x),m=Math.max(m,s.x),i=Math.min(i,s.y),t=Math.max(t,s.y);return e>=u-o&&e<=m+o&&r>=i-o&&r<=t+o&&(t-i<o*2.5||m-u<o*2.5)?!0:M(e,r,n)}function S(e){return e.hitPolygons&&e.hitPolygons.length>0?e.hitPolygons:e.polygons}function B(e,r,n,o){const u=(0,g.getMaskSegmentRuntimeConfig)().interaction,m=o?.thinPadding??u.thinStripPadding,i=n.thinStrip?m:u.regionPadding;return S(n).some(t=>t.length>=3&&p(e,r,t,i))}function w(e,r,n){return S(n).some(o=>o.length>=3&&M(e,r,o))}function Y(e,r,n){const o=[];for(const t of e){const s=t.thinStrip?.005:0,l=t.bbox;r<l.x-s||r>l.x+l.w+s||n<l.y-s||n>l.y+l.h+s||B(r,n,t)&&o.push(t)}if(o.length===0)return null;if(o.length===1)return o[0].id;const u=o.filter(t=>!t.thinStrip&&w(r,n,t));if(u.length>0)return u.sort((t,s)=>t.area-s.area),u[0].id;const m=o.filter(t=>t.thinStrip&&w(r,n,t));if(m.length>0)return m.sort((t,s)=>t.area-s.area),m[0].id;const i=o.filter(t=>!t.thinStrip);return i.length>0?(i.sort((t,s)=>t.area-s.area),i[0].id):(o.sort((t,s)=>t.area-s.area),o[0].id)}function K(e,r,n,o,u,m=!1){const i=Math.floor(e*n.cols),t=Math.floor(r*n.rows);if(i<0||t<0||i>=n.cols||t>=n.rows)return null;if(m)return u?u[t*n.cols+i]?o:null:(0,d.isBaseboardMaskPixel)(n.buffer,n.cols,n.rows,i,t)?o:null;const s=(0,g.getMaskSegmentRuntimeConfig)().interaction,l=Math.max(s.kickMaskPickRadiusPx,Math.floor(n.cols*.022)),a=l*l;for(let f=-l;f<=l;f++)for(let c=-l;c<=l;c++){if(c*c+f*f>a)continue;const b=i+c,x=t+f;if(!(b<0||x<0||b>=n.cols||x>=n.rows)){if(u){if(u[x*n.cols+b])return o;continue}if((0,d.isBaseboardMaskPixel)(n.buffer,n.cols,n.rows,b,x))return o}}return null}function $(e,r,n){const o=n.hitPolygons??n.polygons,u=(0,g.getMaskSegmentRuntimeConfig)().interaction.thinStripPadding+.004;return o.some(m=>m.length>=3&&p(e,r,m,u))}function D(e,r,n,o=(0,g.getMaskSegmentRuntimeConfig)().interaction.pickMapSearchRadiusPx){const u=Math.min(n.cols-1,Math.max(0,Math.floor(e*n.cols))),m=Math.min(n.rows-1,Math.max(0,Math.floor(r*n.rows))),i=(a,f)=>n.buffer[f*n.cols+a],t=i(u,m);if(t>0)return t-1;if(o<=0)return null;const s=Math.max(4,o),l=s*s;for(let a=-s;a<=s;a++)for(let f=-s;f<=s;f++){if(f*f+a*a>l)continue;const c=u+f,b=m+a;if(c<0||b<0||c>=n.cols||b>=n.rows)continue;const x=i(c,b);if(x>0)return x-1}return null}function O(e){e&&(e.lowFreqImage.dispose(),e.highFreqImage.dispose())}function V(e){e&&e.dispose()}async function Z(e,r,n,o){if(o>=1)return{buffer:e,cols:r,rows:n};const u=Math.floor(r*o),m=Math.floor(n*o);return{buffer:(0,R.resizeBgrBuffer)(e,r,n,u,m),cols:u,rows:m}}let y=0;function z(e){if(!__DEV__)return;const r=performance.now(),n=y?r-y:0;console.log(`[\u23F1 ${e}] ${n.toFixed(2)} ms`),y=r}
