import { s as e, t } from "./rafScheduler-CYaktF5t.js";
import { t as n } from "./TopicQuickPicker-CM1HfziA.js";
import { a as r, i, n as a, r as o, s, t as c } from "./imageTypes-hMsGduBk.js";
import { a as l, t as u } from "./time-BFgy7ftw.js";
import { useEffect as d, useRef as f, useState as p } from "react";
import { jsx as m, jsxs as h } from "react/jsx-runtime";
//#region src/features/panels/Image/image-core/h264.ts
function g(e) {
	return _(e) ? "key" : "delta";
}
function _(e) {
	for (let t of v(e)) if (t === 5 || t === 7 || t === 8) return !0;
	return !1;
}
function v(e) {
	let t = [], n = 0;
	for (; n < e.byteLength - 3;) {
		let r = y(e, n);
		if (r < 0) break;
		let i = r + (e[r + 2] === 1 ? 3 : 4);
		i < e.byteLength && t.push(e[i] & 31), n = i + 1;
	}
	return t.length > 0 ? t : e.byteLength > 0 ? [e[0] & 31] : [];
}
function y(e, t) {
	for (let n = t; n < e.byteLength - 3; n += 1) if (!(e[n] !== 0 || e[n + 1] !== 0) && (e[n + 2] === 1 || e[n + 2] === 0 && e[n + 3] === 1)) return n;
	return -1;
}
//#endregion
//#region src/features/panels/Image/image-core/messageFrameAdapter.ts
function b(e) {
	let t = e.message;
	if (o(t)) {
		let n = s(t.data);
		return n ? {
			frame: {
				kind: "compressed",
				receiveTime: e.receiveTime,
				format: a(t),
				data: n.data
			},
			transfer: n.transfer
		} : null;
	}
	if (r(t)) {
		let n = s(t.data);
		return n ? {
			frame: {
				kind: "raw",
				receiveTime: e.receiveTime,
				encoding: t.encoding,
				width: t.width,
				height: t.height,
				step: t.step,
				isBigEndian: t.is_bigendian,
				data: n.data
			},
			transfer: n.transfer
		} : null;
	}
	return null;
}
function x(e) {
	return i(e.message);
}
function S(e) {
	let t = e.message;
	return !o(t) || !i(t) ? null : t.data;
}
//#endregion
//#region src/features/panels/Image/image-core/h264SeekRepair.ts
var C = [
	2e3,
	5e3,
	1e4,
	3e4
];
function w(e) {
	for (let t = e.length - 1; t >= 0; --t) {
		let n = e[t];
		if (!n) continue;
		let r = S(n);
		if (r && g(r) === "key") return t;
	}
	return -1;
}
function T(e, t) {
	let n = l(t), r = e.filter((e) => x(e) && l(e.receiveTime) <= n).sort((e, t) => {
		let n = l(e.receiveTime) - l(t.receiveTime);
		return n < 0n ? -1 : +(n > 0n);
	}), i = w(r);
	return i < 0 ? [] : r.slice(i).slice(-180);
}
async function E(e, t, n, r) {
	if (!e.getMessagesInTimeRange) return !1;
	for (let i of C) {
		let a = u(r, -i), o = T((await e.getMessagesInTimeRange({
			start: a,
			end: r,
			topics: [n]
		})).filter((e) => e.topic === n), r);
		if (o.length !== 0) {
			t.postMessage({ type: "reset" });
			for (let e of o) {
				let n = b(e);
				n && t.postMessage({
					type: "frame",
					frame: n.frame
				}, n.transfer);
			}
			return !0;
		}
	}
	return !1;
}
//#endregion
//#region src/features/panels/Image/image-core/ImageRender.worker.ts?worker&inline
var D = "const e={colorMode:`colormap`,flatColor:`#ffffff`,gradient:[`#000000`,`#ffffff`],colorMap:`turbo`,explicitAlpha:1};function t(e,t,n){return Math.max(t,Math.min(n,e))}const n={r:0,g:0,b:0,a:0},r={r:0,g:0,b:0,a:0};function i(e,t){let n=t.trim(),r=n.match(/^#([0-9a-f]{3}|[0-9a-f]{6})$/i);if(r){let t=r[1];return t.length===3&&(t=t[0]+t[0]+t[1]+t[1]+t[2]+t[2]),e.r=parseInt(t.slice(0,2),16)/255,e.g=parseInt(t.slice(2,4),16)/255,e.b=parseInt(t.slice(4,6),16)/255,e.a=1,e}let i=n.match(/^rgba?\\(\\s*([0-9.]+)\\s*,\\s*([0-9.]+)\\s*,\\s*([0-9.]+)(?:\\s*,\\s*([0-9.]+))?\\s*\\)$/i);return i?(e.r=Number(i[1])/255,e.g=Number(i[2])/255,e.b=Number(i[3])/255,e.a=i[4]==null?1:Number(i[4]),e):(e.r=e.g=e.b=e.a=1,e)}function a(e,n,r,i){let a=t(i,0,1);e.r=n.r+(r.r-n.r)*a,e.g=n.g+(r.g-n.g)*a,e.b=n.b+(r.b-n.b)*a,e.a=n.a+(r.a-n.a)*a}function o(e,n){let r=(1-t(n,0,1))*5+1,i=Math.floor(r),a=r%1;i%2<1&&(a=1-a);let o=1-a;i<=1?(e.r=o,e.g=0,e.b=1):i===2?(e.r=0,e.g=o,e.b=1):i===3?(e.r=0,e.g=1,e.b=o):i===4?(e.r=o,e.g=1,e.b=0):(e.r=1,e.g=o,e.b=0),e.a=1}const s=[.13572138,4.6153926,-42.66032258,132.13108234],c=[.09140261,2.19418839,4.84296658,-14.18503333],l=[.1066733,12.64194608,-60.58204836,110.36276771],u=[-152.94239396,59.28637943],d=[4.27729857,2.82956604],f=[-89.90310912,27.34824973];function p(e,n){let r=t(n,0,1)*.99+.01,i=r*r,a=i*r,o=1*s[0]+r*s[1]+i*s[2]+a*s[3],p=1*c[0]+r*c[1]+i*c[2]+a*c[3],m=1*l[0]+r*l[1]+i*l[2]+a*l[3],h=i,g=a,_=h*u[0]+g*u[1],v=h*d[0]+g*d[1],y=h*f[0]+g*f[1];e.r=t(o+_,0,1),e.g=t(p+v,0,1),e.b=t(m+y,0,1),e.a=1}const m=65535;let h;function g(e,t){if(!h){h=new Float32Array(m*3);let e={r:0,g:0,b:0,a:0};for(let t=0;t<m;t++){p(e,t/(m-1));let n=t*3;h[n+0]=e.r,h[n+1]=e.g,h[n+2]=e.b}}let n=Math.trunc(t*(m-1))*3;e.r=h[n+0],e.g=h[n+1],e.b=h[n+2],e.a=1}function _(e,t){let n=t>>>0;e.a=((n&4278190080)>>>24)/255,e.r=((n&16711680)>>>16)/255,e.g=((n&65280)>>>8)/255,e.b=((n&255)>>>0)/255}function v(e,t,s){let c=e.colorMode;if(c===`rgba-fields`)throw Error(`rgba-fields color mode is not supported for scalar depth images`);switch(c){case`flat`:{let t=i(n,e.flatColor);return e=>{e.r=t.r,e.g=t.g,e.b=t.b,e.a=t.a}}case`gradient`:{let o=Math.max(s-t,2**-52),c=i(n,e.gradient[0]),l=i(r,e.gradient[1]);return(e,n)=>{a(e,c,l,Math.max(0,Math.min((n-t)/o,1)))}}case`colormap`:{let n=Math.max(s-t,2**-52);return e.colorMap===`turbo`?(r,i)=>{g(r,Math.max(0,Math.min((i-t)/n,1))),r.a=e.explicitAlpha}:(r,i)=>{o(r,Math.max(0,Math.min((i-t)/n,1))),r.a=e.explicitAlpha}}case`rgb`:return(t,n)=>{_(t,n),t.a=e.explicitAlpha};case`rgba`:return(e,t)=>{_(e,t)};default:throw Error(`Unsupported color mode: ${String(c)}`)}}function y(e,t,n,r,i,a){a[i]=e+Math.trunc(1403*r/1e3),a[i+1]=e-Math.trunc(344*t/1e3)-Math.trunc(714*r/1e3),a[i+2]=e+Math.trunc(1770*t/1e3),a[i+3]=255,a[i+4]=n+Math.trunc(1403*r/1e3),a[i+5]=n-Math.trunc(344*t/1e3)-Math.trunc(714*r/1e3),a[i+6]=n+Math.trunc(1770*t/1e3),a[i+7]=255}function b(e,t,n,r,i){if(r<t*2)throw Error(`UYVY image row step (${r}) must be at least 2*width (${t*2})`);let a=0;for(let o=0;o<n;o++){let n=o*r;for(let r=0;r<t;r+=2){let t=n+r*2,o=e[t]-128,s=e[t+1],c=e[t+2]-128,l=e[t+3];y(s,o,l,c,a,i),a+=8}}}function ee(e,t,n,r,i){if(r<t*2)throw Error(`YUYV image row step (${r}) must be at least 2*width (${t*2})`);let a=0;for(let o=0;o<n;o++){let n=o*r;for(let r=0;r<t;r+=2){let t=n+r*2,o=e[t],s=e[t+1]-128,c=e[t+2];y(o,s,c,e[t+3]-128,a,i),a+=8}}}function te(e,t,n,r,i){if(r<t*3)throw Error(`RGB8 image row step (${r}) must be at least 3*width (${t*3})`);let a=0;for(let o=0;o<n;o++){let n=o*r;for(let r=0;r<t;r++){let t=n+r*3;i[a++]=e[t],i[a++]=e[t+1],i[a++]=e[t+2],i[a++]=255}}}function ne(e,t,n,r,i){if(r<t*4)throw Error(`RGBA8 image row step (${r}) must be at least 4*width (${t*4})`);let a=0;for(let o=0;o<n;o++){let n=o*r;for(let r=0;r<t;r++){let t=n+r*4;i[a++]=e[t],i[a++]=e[t+1],i[a++]=e[t+2],i[a++]=e[t+3]}}}function x(e,t,n,r,i){if(r<t*4)throw Error(`BGRA8 image row step (${r}) must be at least 4*width (${t*4})`);let a=0;for(let o=0;o<n;o++){let n=o*r;for(let r=0;r<t;r++){let t=n+r*4;i[a++]=e[t+2],i[a++]=e[t+1],i[a++]=e[t],i[a++]=e[t+3]}}}function S(e,t,n,r,i){if(r<t*3)throw Error(`BGR8 image row step (${r}) must be at least 3*width (${t*3})`);let a=0;for(let o=0;o<n;o++){let n=o*r;for(let r=0;r<t;r++){let t=n+r*3;i[a++]=e[t+2],i[a++]=e[t+1],i[a++]=e[t],i[a++]=255}}}function C(t,n,r,i,a,o,s){if(i<n*4)throw Error(`Float image row step (${i}) must be at least 4*width (${n*4})`);let c=s.minValue??0,l=s.maxValue??1,u;try{u=v(s,c,l)}catch{u=v({...e,colorMode:`gradient`},c,l)}let d={r:0,g:0,b:0,a:0},f=new DataView(t.buffer,t.byteOffset,t.byteLength),p=0;for(let e=0;e<r;e++){let t=e*i;for(let e=0;e<n;e++){let n=f.getFloat32(t+e*4,!a);u(d,n),o[p++]=Math.round(w(d.r)*255),o[p++]=Math.round(w(d.g)*255),o[p++]=Math.round(w(d.b)*255),o[p++]=Math.round(w(d.a)*255)}}}function w(e){return Math.max(0,Math.min(1,e))}function T(e,t,n,r,i){if(r<t)throw Error(`Mono8 image row step (${r}) must be at least width (${t})`);let a=0;for(let o=0;o<n;o++){let n=o*r;for(let r=0;r<t;r++){let t=e[n+r];i[a++]=t,i[a++]=t,i[a++]=t,i[a++]=255}}}function E(t,n,r,i,a,o,s){if(i<n*2)throw Error(`Mono16 image row step (${i}) must be at least 2*width (${n*2})`);let c=s.minValue??0,l=s.maxValue??65535,u;try{u=v(s,c,l)}catch{u=v({...e,colorMode:`gradient`},c,l)}let d={r:0,g:0,b:0,a:0},f=new DataView(t.buffer,t.byteOffset,t.byteLength),p=0;for(let e=0;e<r;e++){let t=e*i;for(let e=0;e<n;e++){let n=f.getUint16(t+e*2,!a);u(d,n),o[p++]=Math.round(w(d.r)*255),o[p++]=Math.round(w(d.g)*255),o[p++]=Math.round(w(d.b)*255),o[p++]=Math.round(w(d.a)*255)}}}function D(e,t,n,r){return Function(`data`,`width`,`height`,`step`,`output`,`\n      if (step < width) {\n        throw new Error(\\`Bayer image row step (\\${step}) must be at least width (\\${width})\\`);\n      }\n      for (let i = 0; i < height / 2; i++) {\n        let inIdx = i * 2 * step;\n        let outTopIdx = i * 2 * width * 4;\n        let outBottomIdx = (i * 2 + 1) * width * 4;\n        for (let j = 0; j < width / 2; j++) {\n          const tl = data[inIdx++];\n          const tr = data[inIdx++];\n          const bl = data[inIdx + step - 2];\n          const br = data[inIdx + step - 1];\n\n          const ${e} = tl;\n          const ${t} = tr;\n          const ${n} = bl;\n          const ${r} = br;\n\n          output[outTopIdx++] = r;\n          output[outTopIdx++] = g0;\n          output[outTopIdx++] = b;\n          output[outTopIdx++] = 255;\n          output[outTopIdx++] = r;\n          output[outTopIdx++] = g0;\n          output[outTopIdx++] = b;\n          output[outTopIdx++] = 255;\n\n          output[outBottomIdx++] = r;\n          output[outBottomIdx++] = g1;\n          output[outBottomIdx++] = b;\n          output[outBottomIdx++] = 255;\n          output[outBottomIdx++] = r;\n          output[outBottomIdx++] = g1;\n          output[outBottomIdx++] = b;\n          output[outBottomIdx++] = 255;\n        }\n      }\n    `)}const re=D(`r`,`g0`,`g1`,`b`),ie=D(`b`,`g0`,`g1`,`r`),ae=D(`g0`,`b`,`r`,`g1`),oe=D(`g0`,`r`,`b`,`g1`);function O(e){return e.trim().toLowerCase()}function k(t,n,r){let i=t.width,a=t.height,o=t.step??se(t),s=t.is_bigendian??!1,c=O(t.encoding),l=t.data,u={...e,...r};switch(c){case`rgb8`:te(l,i,a,o,n);return;case`rgba8`:ne(l,i,a,o,n);return;case`bgra8`:x(l,i,a,o,n);return;case`bgr8`:case`8uc3`:S(l,i,a,o,n);return;case`mono8`:case`8uc1`:T(l,i,a,o,n);return;case`mono16`:case`16uc1`:E(l,i,a,o,s,n,u);return;case`32fc1`:C(l,i,a,o,s,n,u);return;case`uyvy`:case`yuv422`:b(l,i,a,o,n);return;case`yuyv`:case`yuv422_yuy2`:ee(l,i,a,o,n);return;case`bayer_rggb8`:re(l,i,a,o,n);return;case`bayer_bggr8`:ie(l,i,a,o,n);return;case`bayer_gbrg8`:ae(l,i,a,o,n);return;case`bayer_grbg8`:oe(l,i,a,o,n);return;default:throw Error(`Unsupported image encoding: ${t.encoding}`)}}function se(e){switch(O(e.encoding)){case`rgb8`:case`bgr8`:case`8uc3`:return e.width*3;case`rgba8`:case`bgra8`:case`32fc1`:return e.width*4;case`uyvy`:case`yuyv`:case`yuv422`:case`yuv422_yuy2`:return e.width*2;case`mono16`:case`16uc1`:return e.width*2;case`mono8`:case`8uc1`:case`bayer_rggb8`:case`bayer_bggr8`:case`bayer_gbrg8`:case`bayer_grbg8`:return e.width;default:return e.width*4}}function A(e){return j(e)?`key`:`delta`}function j(e){for(let t of M(e))if(t===5||t===7||t===8)return!0;return!1}function M(e){let t=[],n=0;for(;n<e.byteLength-3;){let r=N(e,n);if(r<0)break;let i=r+(e[r+2]===1?3:4);i<e.byteLength&&t.push(e[i]&31),n=i+1}return t.length>0?t:e.byteLength>0?[e[0]&31]:[]}function N(e,t){for(let n=t;n<e.byteLength-3;n+=1)if(!(e[n]!==0||e[n+1]!==0)&&(e[n+2]===1||e[n+2]===0&&e[n+3]===1))return n;return-1}function P(e,t,n,r){let i=!1,a=null;return new Promise((o,s)=>{a=setTimeout(()=>{i=!0,a=null,s(Error(n))},t),e.then(e=>{if(a!=null&&(clearTimeout(a),a=null),i){r?.(e);return}o(e)},e=>{a!=null&&(clearTimeout(a),a=null),i||s(e instanceof Error?e:Error(String(e)))})})}const F=/\\b(jpeg|jpg|png|webp|gif|avif|bmp|h264)\\b/i;function I(e){let t=e.trim().toLowerCase();if(t===`h264`||t.includes(`h264`))return`h264`;let n=e.match(F);if(!n||!n[1])return null;let r=n[1].toLowerCase();return r===`jpg`||r===`jpeg`?`jpeg`:r===`png`||r===`webp`||r===`gif`||r===`avif`||r===`bmp`?r:r===`h264`?`h264`:null}function L(e){let t=I(e);if(t===`jpeg`)return`image/jpeg`;if(t===`png`||t===`webp`||t===`gif`||t===`avif`||t===`bmp`)return`image/${t}`;let n=e.split(`;`)[0]?.trim().split(/\\s+/)[0]?.toLowerCase();return n&&n.startsWith(`image/`)?n:`image/jpeg`}const R={backgroundColor:`#000000`,flipHorizontal:!1,flipVertical:!1,rotationDeg:0,smoothing:!0,fitMode:`contain`};function z(e){return(e%360+360)%360}function B(e,t,n){let r=z(n)*Math.PI/180,i=Math.abs(Math.cos(r)),a=Math.abs(Math.sin(r));return{w:e*i+t*a,h:e*a+t*i}}const V={cssWidth:0,cssHeight:0,devicePixelRatio:1},H=`avc1.4D4020`,U=5e3;var W=class{#e=null;#t=[];#n=0n;#r=Promise.resolve(void 0);#i=null;#a=null;#o=null;dispose(){this.#u(),this.#e&&this.#e.state!==`closed`&&this.#e.close(),this.#e=null,this.#t=[],this.#n=0n,this.#r=Promise.resolve()}decodeFrame(e,t){if(typeof VideoDecoder>`u`)return Promise.reject(Error(`WebCodecs VideoDecoder is not supported`));let n=async()=>{await this.#s();let n=this.#e,r=A(e);r===`key`&&(this.#t.push({timeKey:t,data:K(e)}),this.#t.length>10&&this.#t.shift());let i=null;if(r===`delta`&&t<this.#n){for(let{timeKey:e,data:r}of this.#t)if(e<t)try{let e=await this.#c(n,r,`key`);i?.close(),i=e}catch{}}this.#n=t;let a=await this.#c(n,e,r);return i?.close(),a},r=this.#r.then(()=>n());return this.#r=r.then(()=>void 0,()=>void 0),r}async#s(){if(!(this.#e&&this.#e.state!==`closed`)){if(!(await VideoDecoder.isConfigSupported({codec:H})).supported)throw Error(`H.264 codec ${H} is not supported`);this.#e=new VideoDecoder({output:e=>{this.#d(e)},error:e=>{this.#f(Error(String(e)))}}),this.#e.configure({codec:H})}}async#c(e,t,n){let r=this.#l();try{e.decode(new EncodedVideoChunk({type:n,timestamp:0,data:t}))}catch(e){throw this.#u(),e instanceof Error?e:Error(String(e))}return await r}#l(){return new Promise((e,t)=>{this.#i=e,this.#a=t,this.#o=setTimeout(()=>{if(this.#a){let e=this.#a;this.#u(),e(Error(`H.264 decode timed out`))}},U)})}#u(){this.#o!=null&&(clearTimeout(this.#o),this.#o=null),this.#i=null,this.#a=null}#d(e){if(this.#o!=null&&(clearTimeout(this.#o),this.#o=null),!this.#i){e.close();return}let t=this.#i;this.#i=null,this.#a=null,t(e)}#f(e){if(this.#o!=null&&(clearTimeout(this.#o),this.#o=null),!this.#a)return;let t=this.#a;this.#i=null,this.#a=null,t(e)}},G=class{#e=null;#t=null;#n=new OffscreenCanvas(1,1);#r=this.#n.getContext(`2d`,{alpha:!1});#i={...R};#a={...V};#o={};#s=null;#c=[];#l=!1;#u=new W;#d=`idle`;#f=!1;#p=null;#m=null;#h=new Map;#g=null;constructor(){if(!this.#r)throw Error(`Buffer canvas context is unavailable in worker`)}handle(e){switch(e.type){case`init`:if(this.#e=e.canvas,this.#t=e.canvas.getContext(`2d`,{alpha:!1,desynchronized:!0}),!this.#t)throw Error(`Canvas 2D context is unavailable in worker`);this.#M(),this.#N(),this.#O({phase:`idle`});return;case`viewport`:this.#a=e.viewport,this.#M(),this.#C();return;case`renderOptions`:this.#i=e.options,this.#C();return;case`rawDecodeOptions`:this.#o=e.options,this.#S();return;case`frame`:if(this.#f)return;this.#_(e.frame),this.#l||this.#b();return;case`reset`:this.#s=null,this.#c=[],this.#f=!1,this.#D(),this.#T(),this.#g=null,this.#N(),this.#O({phase:`idle`});return;case`dispose`:this.#s=null,this.#c=[],this.#f=!1,this.#u.dispose(),this.#D(),this.#T(),this.#g=null,self.close();return}}#_(e){if(!J(e)){this.#s=e;return}this.#c.push(e),this.#v()}#v(){if(this.#c.length<=180)return;let e=ue(this.#c);e>0&&(this.#c=this.#c.slice(e)),this.#c.length>180&&(this.#c=this.#c.slice(-180))}#y(){let e=this.#c.shift();if(e)return e;let t=this.#s;return this.#s=null,t}async#b(){if(!this.#l){this.#l=!0;try{let e;for(;e=this.#y();)if(await this.#x(e),this.#f){this.#s=null,this.#c=[];break}}finally{this.#l=!1}}}async#x(e){this.#O({phase:`decoding`,receiveTime:e.receiveTime});try{if(e.kind===`compressed`){let t=I(e.format),n=q(e.data);if(n.byteLength===0)throw Error(`Compressed image payload is empty: ${e.format}`);let r=de(e.receiveTime);if(t===`h264`){let t=await this.#u.decodeFrame(n,r);try{let n=t.displayWidth||t.codedWidth,r=t.displayHeight||t.codedHeight;this.#j(t,n,r),this.#O({phase:`ready`,width:n,height:r,encoding:e.format,receiveTime:e.receiveTime}),this.#T(),this.#g=null;try{let i=await createImageBitmap(t);this.#w(i,n,r,e.format,e.receiveTime)}catch{}}finally{t.close()}return}let i=await P(this.#E(n,e.format),U,`Compressed image decode timed out: ${e.format}`,Y),a=i;try{let t=`displayWidth`in i?i.displayWidth:i.width,n=`displayHeight`in i?i.displayHeight:i.height,r=Q(i)?i:await P(createImageBitmap(i),U,`Compressed image bitmap creation timed out: ${e.format}`,Z);Q(i)&&(a=null),X(a),a=null,this.#w(r,t,n,e.format,e.receiveTime),this.#A(r,t,n),this.#O({phase:`ready`,width:t,height:n,encoding:e.format,receiveTime:e.receiveTime})}catch(e){throw X(a),e}return}let t=q(e.data),n=e.width*e.height*4,r=this.#p;(!r||r.length!==n)&&(r=new Uint8ClampedArray(n),this.#p=r),(!this.#m||this.#m.width!==e.width||this.#m.height!==e.height)&&(this.#m=new ImageData(r,e.width,e.height));let i=e.step??e.width*ce(e.encoding),a=e.isBigEndian??!1;k({encoding:e.encoding,width:e.width,height:e.height,step:i,is_bigendian:a,data:t},r,this.#o),this.#T(),this.#g={kind:`raw`,width:e.width,height:e.height,encoding:e.encoding,step:i,isBigEndian:a,data:t,receiveTime:e.receiveTime},this.#k(e.width,e.height),this.#O({phase:`ready`,width:e.width,height:e.height,encoding:e.encoding,receiveTime:e.receiveTime})}catch(e){this.#f=!0,this.#O({phase:`error`,message:e instanceof Error?e.message:String(e)})}}#S(){let e=this.#g;if(!e||e.kind!==`raw`)return;let t=e.width*e.height*4,n=this.#p;(!n||n.length!==t)&&(n=new Uint8ClampedArray(t),this.#p=n),(!this.#m||this.#m.width!==e.width||this.#m.height!==e.height)&&(this.#m=new ImageData(n,e.width,e.height));try{k({encoding:e.encoding,width:e.width,height:e.height,step:e.step,is_bigendian:e.isBigEndian,data:e.data},n,this.#o),this.#k(e.width,e.height),this.#O({phase:`ready`,width:e.width,height:e.height,encoding:e.encoding,receiveTime:e.receiveTime})}catch{}}#C(){let e=this.#g;if(!e){this.#N();return}e.kind===`raw`?(this.#k(e.width,e.height),this.#O({phase:`ready`,width:e.width,height:e.height,encoding:e.encoding,receiveTime:e.receiveTime})):(this.#A(e.bitmap,e.width,e.height),this.#O({phase:`ready`,width:e.width,height:e.height,encoding:e.encoding,receiveTime:e.receiveTime}))}#w(e,t,n,r,i){this.#T(),this.#g={kind:`bitmap`,width:t,height:n,encoding:r,bitmap:e,receiveTime:i}}#T(){this.#g?.kind===`bitmap`&&this.#g.bitmap.close()}async#E(e,t){let n=L(t);if(typeof ImageDecoder<`u`){let t=this.#h.get(n);if(t===void 0&&(t=await ImageDecoder.isTypeSupported(n),this.#h.set(n,t)),t){let t=new ImageDecoder({type:n,data:e});try{let{image:e}=await t.decode({frameIndex:0});return e}finally{t.close()}}}return createImageBitmap(new Blob([e],{type:n}))}#D(){this.#h.clear(),this.#p=null,this.#m=null}#O(e){if(e.phase===`decoding`&&this.#d!==`idle`&&this.#d!==`error`)return;this.#d=e.phase;let t={type:`status`,status:e};$.postMessage(t)}#k(e,t){le(this.#n,e,t),this.#r.putImageData(this.#m,0,0),this.#j(this.#n,e,t)}#A(e,t,n){this.#j(e,t,n)}#j(e,t,n){this.#M();let r=this.#t,i=this.#e;if(!r||!i)return;let a=this.#a.cssWidth||i.width/Math.max(1,this.#a.devicePixelRatio)||1,o=this.#a.cssHeight||i.height/Math.max(1,this.#a.devicePixelRatio)||1,s=z(this.#i.rotationDeg),{w:c,h:l}=B(t,n,s),u=this.#i.fitMode===`contain`?Math.min(a/c,o/l):Math.max(a/c,o/l),d=Math.max(1,t*u),f=Math.max(1,n*u);r.save(),r.setTransform(this.#a.devicePixelRatio,0,0,this.#a.devicePixelRatio,0,0),r.clearRect(0,0,a,o),r.fillStyle=this.#i.backgroundColor,r.fillRect(0,0,a,o),r.imageSmoothingEnabled=this.#i.smoothing,r.imageSmoothingQuality=this.#i.smoothing?`high`:`low`,r.translate(a/2,o/2),r.rotate(s*Math.PI/180),r.scale(this.#i.flipHorizontal?-1:1,this.#i.flipVertical?-1:1),r.drawImage(e,-d/2,-f/2,d,f),r.restore()}#M(){if(!this.#e)return;let e=Math.max(1,Math.round(Math.max(0,this.#a.cssWidth)*Math.max(1,this.#a.devicePixelRatio))),t=Math.max(1,Math.round(Math.max(0,this.#a.cssHeight)*Math.max(1,this.#a.devicePixelRatio)));this.#e.width!==e&&(this.#e.width=e),this.#e.height!==t&&(this.#e.height=t)}#N(){!this.#t||!this.#e||(this.#t.save(),this.#t.setTransform(1,0,0,1,0,0),this.#t.clearRect(0,0,this.#e.width,this.#e.height),this.#t.fillStyle=this.#i.backgroundColor,this.#t.fillRect(0,0,this.#e.width,this.#e.height),this.#t.restore())}};function ce(e){switch(e.trim().toLowerCase()){case`rgb8`:case`bgr8`:case`8uc3`:return 3;case`rgba8`:case`bgra8`:case`32fc1`:return 4;case`mono16`:case`16uc1`:case`uyvy`:case`yuyv`:case`yuv422`:case`yuv422_yuy2`:return 2;default:return 1}}function le(e,t,n){e.width!==t&&(e.width=t),e.height!==n&&(e.height=n)}function K(e){let t=new Uint8Array(new ArrayBuffer(e.byteLength));return t.set(e),t}function q(e){return e.buffer instanceof ArrayBuffer&&e.byteOffset===0&&e.byteLength===e.buffer.byteLength?e:K(e)}function J(e){return e.kind===`compressed`&&I(e.format)===`h264`}function ue(e){for(let t=e.length-1;t>=0;--t){let n=e[t];if(!(!n||!J(n))&&A(n.data)===`key`)return t}return-1}function de(e){return BigInt(e.sec)*1000000000n+BigInt(e.nsec)}function Y(e){e.close()}function X(e){e&&Y(e)}function Z(e){e.close()}function Q(e){return typeof ImageBitmap<`u`&&e instanceof ImageBitmap}const fe=new G,$=self;$.onmessage=e=>{fe.handle(e.data)};", O = typeof self < "u" && self.Blob && new Blob(["URL.revokeObjectURL(import.meta.url);", D], { type: "text/javascript;charset=utf-8" });
function k(e) {
	let t;
	try {
		if (t = O && (self.URL || self.webkitURL).createObjectURL(O), !t) throw "";
		let n = new Worker(t, {
			type: "module",
			name: e?.name
		});
		return n.addEventListener("error", () => {
			(self.URL || self.webkitURL).revokeObjectURL(t);
		}), n;
	} catch {
		return new Worker("data:text/javascript;charset=utf-8," + encodeURIComponent(D), {
			type: "module",
			name: e?.name
		});
	}
}
//#endregion
//#region src/features/panels/Image/Component.tsx
function A(e) {
	return {
		colorMode: e.colorMode,
		flatColor: e.flatColor,
		gradient: e.gradient,
		colorMap: e.colorMap,
		explicitAlpha: e.explicitAlpha,
		minValue: e.minValue,
		maxValue: e.maxValue
	};
}
var j = (r) => {
	let { formatMessage: i } = e(), { player: a, panelId: o, setConfig: s, topic: u, backgroundColor: g, showStatusText: _, fitMode: v, flipHorizontal: y, flipVertical: b, rotation: S, smoothing: C, colorMode: w, colorMap: T, gradient: D, flatColor: O, explicitAlpha: j, minValue: F, maxValue: I } = r, L = f(null), R = f(null), z = f(null), B = f(null), V = f(null), H = f(null), U = f({ phase: "idle" }), W = f(!1), [G, K] = p({ phase: "idle" }), q = `${o}:image-main`, J = `${o}:image-main-h264`;
	d(() => {
		let e = L.current, n = R.current;
		if (!e || !n) return;
		if (typeof e.transferControlToOffscreen != "function") {
			let e = {
				phase: "error",
				message: i({ id: "panels.image.error.offscreenUnsupported" })
			};
			U.current = e, K(e);
			return;
		}
		B.current != null && (window.clearTimeout(B.current), B.current = null), z.current && V.current && V.current !== e && (z.current.postMessage({ type: "dispose" }), z.current.terminate(), z.current = null, V.current = null);
		let r = z.current;
		if (!r) {
			r = new k(), z.current = r;
			let t = e.transferControlToOffscreen();
			V.current = e, r.postMessage({
				type: "init",
				canvas: t
			}, [t]);
		}
		r.onmessage = (e) => {
			let t = e.data;
			if (t.type !== "status") return;
			let n = t.status;
			N(U.current, n) || (U.current = n, K(n));
		};
		let a = -1, o = -1, s = -1, c = null, l = () => {
			let e = n.getBoundingClientRect(), t = window.devicePixelRatio || 1, i = e.width, c = e.height;
			i === a && c === o && t === s || (a = i, o = c, s = t, r.postMessage({
				type: "viewport",
				viewport: {
					cssWidth: i,
					cssHeight: c,
					devicePixelRatio: t
				}
			}));
		}, u = () => {
			c?.(), c = t(l);
		};
		l();
		let d = new ResizeObserver(u);
		return d.observe(n), window.addEventListener("resize", u), () => {
			c?.(), c = null, window.removeEventListener("resize", u), d.disconnect(), B.current = window.setTimeout(() => {
				let e = z.current;
				e && (e.postMessage({ type: "dispose" }), e.terminate(), z.current = null, V.current = null, U.current = { phase: "idle" }, K({ phase: "idle" }), B.current = null);
			}, 0);
		};
	}, [i]), d(() => {
		if (!u) return;
		let e = z.current;
		if (e) return W.current = !1, e.postMessage({ type: "reset" }), a.registerHighFrequencyConsumer(q, {
			topic: u,
			lane: "video",
			mode: "latest",
			onLatestMessage: (t) => {
				if (x(t)) {
					if (W.current) return;
					W.current = !0, a.registerHighFrequencyConsumer(J, {
						topic: u,
						lane: "video",
						mode: "all",
						onMessageBatch: (t) => {
							for (let n of t) x(n) && P(e, n);
						}
					});
				}
				P(e, t);
			},
			onMessageBatch: (t) => {
				if (W.current) return;
				let n = t.at(-1);
				n && P(e, n);
			}
		}), () => {
			a.unregisterHighFrequencyConsumer(q), a.unregisterHighFrequencyConsumer(J), e.postMessage({ type: "reset" });
		};
	}, [
		a,
		q,
		J,
		u
	]), d(() => a.subscribeCurrentTime((e) => {
		let t = l(e), n = H.current;
		if (n != null && t + 5000000n < n) {
			let t = z.current;
			t && u && W.current ? (t.postMessage({ type: "reset" }), E(a, t, u, e)) : z.current?.postMessage({ type: "reset" });
		}
		H.current = t;
	}), [a, u]), d(() => {
		let e = z.current;
		e && e.postMessage({
			type: "rawDecodeOptions",
			options: A({
				colorMode: w,
				colorMap: T,
				gradient: D,
				flatColor: O,
				explicitAlpha: j,
				minValue: F,
				maxValue: I
			})
		});
	}, [
		w,
		T,
		D,
		O,
		j,
		F,
		I
	]), d(() => {
		let e = z.current;
		if (!e) return;
		let t = {
			backgroundColor: g,
			flipHorizontal: y,
			flipVertical: b,
			rotationDeg: S,
			smoothing: C,
			fitMode: v
		};
		e.postMessage({
			type: "renderOptions",
			options: t
		});
	}, [
		g,
		y,
		b,
		S,
		C,
		v
	]);
	let Y = M(G);
	return /* @__PURE__ */ h("div", {
		className: "flex flex-col h-full overflow-hidden relative",
		style: { background: g },
		"data-testid": "image-panel",
		children: [/* @__PURE__ */ m("div", {
			className: "flex shrink-0 items-center gap-2 border-b border-zinc-800 bg-zinc-950",
			children: /* @__PURE__ */ m(n, {
				value: u,
				onChange: (e) => s((t) => ({
					...t,
					topic: e
				})),
				typeIncludes: [...c],
				placeholder: i({ id: "panels.framework.topicPicker.imagePlaceholder" }),
				className: "min-w-0 flex-1",
				triggerClassName: "border-zinc-700 bg-zinc-950 text-zinc-100 hover:bg-zinc-900 hover:text-zinc-50"
			})
		}), /* @__PURE__ */ h("div", {
			ref: R,
			className: "flex-1 relative min-h-0 min-w-0 flex items-center justify-center",
			children: [
				/* @__PURE__ */ m("canvas", {
					ref: L,
					className: "w-full h-full block",
					"data-testid": "image-panel-canvas"
				}),
				_ && Y && /* @__PURE__ */ m("div", {
					className: "absolute inset-0 flex items-center justify-center pointer-events-none text-white/40 italic text-xs",
					children: Y
				}),
				_ && G.phase === "ready" && G.width && G.height && /* @__PURE__ */ h("div", {
					className: "absolute bottom-0 left-0 right-0 px-2 py-1 text-white/30 text-[10px] font-mono truncate pointer-events-none",
					"data-testid": "image-panel-status",
					children: [
						G.width,
						"x",
						G.height,
						" ",
						G.encoding ?? ""
					]
				})
			]
		})]
	});
};
function M(e) {
	return e.phase === "idle" ? "Waiting for image data" : e.phase === "error" ? e.message ?? "Image decode failed" : e.phase === "decoding" && !e.width && !e.height ? "Decoding latest frame..." : null;
}
function N(e, t) {
	return e.phase === t.phase && e.width === t.width && e.height === t.height && e.encoding === t.encoding && e.message === t.message;
}
function P(e, t) {
	let n = b(t);
	n && e.postMessage({
		type: "frame",
		frame: n.frame
	}, n.transfer);
}
//#endregion
export { j as ImagePanel };
