//#region src/preview/encoding/mainThreadEncoder.ts
/**
* Main thread canvas encoding (fallback implementation).
*/
const JPEG_QUALITY_HIGH = .92;
const JPEG_QUALITY_MEDIUM = .85;
/**
* Encode a single canvas to a data URL on the main thread (fallback).
* @param canvas - The canvas to encode
* @param canvasScale - Scale factor for encoding (default: 1)
* @returns Encoded result or null if encoding fails
*/
function encodeCanvasOnMainThread(canvas, canvasScale) {
	try {
		if (canvas.width === 0 || canvas.height === 0) return null;
		const preserveAlpha = canvas.dataset.preserveAlpha === "true";
		let dataUrl;
		if (canvasScale < 1) {
			const scaledWidth = Math.floor(canvas.width * canvasScale);
			const scaledHeight = Math.floor(canvas.height * canvasScale);
			const scaledCanvas = document.createElement("canvas");
			scaledCanvas.width = scaledWidth;
			scaledCanvas.height = scaledHeight;
			const scaledCtx = scaledCanvas.getContext("2d");
			if (scaledCtx) {
				scaledCtx.drawImage(canvas, 0, 0, scaledWidth, scaledHeight);
				const quality = canvasScale < .5 ? JPEG_QUALITY_MEDIUM : JPEG_QUALITY_HIGH;
				dataUrl = preserveAlpha ? scaledCanvas.toDataURL("image/png") : scaledCanvas.toDataURL("image/jpeg", quality);
			} else dataUrl = preserveAlpha ? canvas.toDataURL("image/png") : canvas.toDataURL("image/jpeg", JPEG_QUALITY_HIGH);
		} else dataUrl = preserveAlpha ? canvas.toDataURL("image/png") : canvas.toDataURL("image/jpeg", JPEG_QUALITY_HIGH);
		return {
			dataUrl,
			preserveAlpha
		};
	} catch (e) {
		return null;
	}
}

//#endregion
export { encodeCanvasOnMainThread };
//# sourceMappingURL=mainThreadEncoder.js.map