import { n as MLModel, r as MLTask, t as inBrowserMLService } from "./in-browser-ml-service-18Xw5br1.js";
import { contributionRegistry, createLogger, registerAll, rootContext, taskService, toastError } from "@eclipse-docks/core";
import { signal } from "@eclipse-docks/core/externals/lit";
//#region src/media-viewer-toolbar-contribution.ts
var logger = createLogger("InBrowserML");
var MEDIA_VIEWER_TOOLBAR_TARGET = "toolbar:media-viewer";
var detectionRunningSignal = signal(false);
var segmentationRunningSignal = signal(false);
function isMlRunning() {
	return detectionRunningSignal.get() || segmentationRunningSignal.get();
}
function isMediaViewerWithImage(part) {
	if (!part || typeof part.getMediaUrl !== "function") return false;
	return part.getIsImage?.() === true;
}
var CATEGORY_PALETTE = [
	"#0d6efd",
	"#dc3545",
	"#198754",
	"#fd7e14",
	"#6f42c1",
	"#20c997",
	"#e83e8c",
	"#ffc107",
	"#0dcaf0",
	"#6c757d"
];
function categoryToColor(category, palette) {
	let n = 0;
	for (let i = 0; i < category.length; i++) n = n * 31 + category.charCodeAt(i) >>> 0;
	return palette[n % palette.length];
}
function detectionResultsToOverlays(results, imageWidth, imageHeight) {
	const w = imageWidth || 1;
	const h = imageHeight || 1;
	return results.map((r) => ({
		type: "bbox",
		left: r.box.xmin / w,
		top: r.box.ymin / h,
		width: (r.box.xmax - r.box.xmin) / w,
		height: (r.box.ymax - r.box.ymin) / h,
		label: `${r.label} ${(r.score * 100).toFixed(0)}%`,
		color: categoryToColor(r.label, CATEGORY_PALETTE)
	}));
}
registerAll({
	command: {
		id: "detect_objects_in_current_image",
		name: "Detect objects",
		description: "Runs object detection on the image currently open in the media viewer.",
		parameters: [],
		output: []
	},
	handler: { execute: async (context) => {
		const part = context.activeEditor;
		if (!isMediaViewerWithImage(part)) return { error: "No image open in the media viewer." };
		const url = part.getMediaUrl();
		if (!url) return { error: "No image URL available." };
		try {
			detectionRunningSignal.set(true);
			await taskService.runAsync("Detecting objects", async () => {
				const results = await (await inBrowserMLService.getPipeline(MLTask.OBJECT_DETECTION, MLModel.OBJECT_DETECTION_YOLOV9))(url);
				const { width, height } = part.getImageDimensions();
				part.setOverlays(detectionResultsToOverlays(results, width, height));
			});
			return { ok: true };
		} catch (err) {
			const msg = err instanceof Error ? err.message : String(err);
			logger.error(`Object detection failed: ${msg}`);
			toastError(msg);
			return { error: msg };
		} finally {
			detectionRunningSignal.set(false);
		}
	} }
});
registerAll({
	command: {
		id: "segment_image_in_current_image",
		name: "Segment",
		description: "Runs image segmentation on the image currently open in the media viewer.",
		parameters: [],
		output: []
	},
	handler: { execute: async (context) => {
		const part = context.activeEditor;
		if (!isMediaViewerWithImage(part)) return { error: "No image open in the media viewer." };
		const url = part.getMediaUrl();
		if (!url) return { error: "No image URL available." };
		try {
			segmentationRunningSignal.set(true);
			await taskService.runAsync("Segmenting image", async () => {
				const results = await (await inBrowserMLService.getPipeline(MLTask.IMAGE_SEGMENTATION, MLModel.IMAGE_SEGMENTATION))(url, { taskType: "image-segmentation" });
				part.setOverlays(results);
			});
			return { ok: true };
		} catch (err) {
			const msg = err instanceof Error ? err.message : String(err);
			logger.error(`Segmentation failed: ${msg}`);
			toastError(msg);
			return { error: msg };
		} finally {
			segmentationRunningSignal.set(false);
		}
	} }
});
contributionRegistry.registerContribution(MEDIA_VIEWER_TOOLBAR_TARGET, {
	label: "Detect objects",
	icon: "magnifying-glass",
	command: "detect_objects_in_current_image",
	disabled: () => isMlRunning()
});
contributionRegistry.registerContribution(MEDIA_VIEWER_TOOLBAR_TARGET, {
	label: "Segment",
	icon: "scissors",
	command: "segment_image_in_current_image",
	disabled: () => isMlRunning()
});
//#endregion
//#region src/in-browser-ml-extension.ts
var in_browser_ml_extension_default = ({}) => {
	rootContext.put("inBrowserMLService", inBrowserMLService);
};
//#endregion
export { in_browser_ml_extension_default as default };

//# sourceMappingURL=in-browser-ml-extension-B_JuNITB.js.map