import { n as resolvePreferredNodmixTmpDir } from "./tmp-nodmix-dir-C4-0E0hu.js";
import { t as resolveSystemBin } from "./resolve-system-bin-Bag9s6mC.js";
import { RastermillError, RastermillUnavailableError, createRastermill, isRastermillUnavailableError, readImageMetadataFromHeader, readImageProbeFromHeader } from "rastermill";
//#region src/media/image-ops.ts
var ImageProcessorUnavailableError = class extends Error {
	constructor(operation, message, causes = []) {
		super(message ?? `Image processor unavailable for ${operation}`, { cause: causes.find((cause) => cause instanceof Error) });
		this.code = "IMAGE_PROCESSOR_UNAVAILABLE";
		this.name = "ImageProcessorUnavailableError";
		this.operation = operation;
		this.causes = causes;
	}
};
const IMAGE_REDUCE_QUALITY_STEPS = [
	85,
	75,
	65,
	55,
	45,
	35
];
const MAX_IMAGE_INPUT_PIXELS = 25e6;
function createImageProcessor() {
	return createRastermill({
		execution: "auto",
		limits: {
			inputPixels: MAX_IMAGE_INPUT_PIXELS,
			outputPixels: MAX_IMAGE_INPUT_PIXELS
		},
		temp: {
			rootDir: resolvePreferredNodmixTmpDir(),
			prefix: "nodmix-img-"
		},
		commandResolver: (command) => resolveSystemBin(command, { trust: command === "powershell" ? "strict" : "standard" })
	});
}
function isImageProcessorUnavailableError(err) {
	return err instanceof ImageProcessorUnavailableError || isRastermillUnavailableError(err);
}
function buildImageResizeSideGrid(maxSide, sideStart) {
	return [
		sideStart,
		1800,
		1600,
		1400,
		1200,
		1e3,
		800
	].map((value) => Math.min(maxSide, value)).filter((value, idx, arr) => value > 0 && arr.indexOf(value) === idx).toSorted((a, b) => b - a);
}
function readImageMetadataFromHeader$1(buffer) {
	return readImageMetadataFromHeader(buffer);
}
function readImageProbeFromHeader$1(buffer) {
	return readImageProbeFromHeader(buffer);
}
function wrapRastermillUnavailable(operation, error) {
	if (error instanceof RastermillUnavailableError) throw new ImageProcessorUnavailableError(operation, error.message, error.causes);
	throw error;
}
async function getImageMetadata(buffer) {
	const info = await createImageProcessor().probe(buffer);
	return info ? {
		width: info.width,
		height: info.height
	} : null;
}
async function normalizeExifOrientation(buffer) {
	try {
		const rastermill = createImageProcessor();
		const info = await rastermill.probe(buffer);
		if (!info) return (await rastermill.encode(buffer, {
			format: "jpeg",
			autoOrient: true
		})).data;
		if (!info?.orientation || info.orientation === 1) return buffer;
		return (await rastermill.encode(buffer, {
			format: "jpeg",
			autoOrient: true
		})).data;
	} catch (error) {
		if (isImageProcessorUnavailableError(error)) return buffer;
		throw error;
	}
}
async function resizeToJpeg(params) {
	try {
		return (await createImageProcessor().encode(params.buffer, {
			format: "jpeg",
			resize: {
				maxSide: params.maxSide,
				enlarge: params.withoutEnlargement === false
			},
			quality: params.quality
		})).data;
	} catch (error) {
		return wrapRastermillUnavailable("resizeToJpeg", error);
	}
}
async function convertHeicToJpeg(buffer) {
	try {
		return (await createImageProcessor().encode(buffer, { format: "jpeg" })).data;
	} catch (error) {
		return wrapRastermillUnavailable("convertHeicToJpeg", error);
	}
}
async function hasAlphaChannel(buffer) {
	try {
		return (await createImageProcessor().transparency(buffer)).hasAlphaChannel;
	} catch (error) {
		const headerHasAlpha = readImageProbeFromHeader(buffer)?.hasAlpha === true;
		if (isRastermillUnavailableError(error)) return headerHasAlpha;
		if (error instanceof RastermillError && error.code === "RASTERMILL_UNDECODABLE" && readImageProbeFromHeader(buffer)) return headerHasAlpha;
		throw error;
	}
}
async function resizeToPng(params) {
	try {
		return (await createImageProcessor().encode(params.buffer, {
			format: "png",
			resize: {
				maxSide: params.maxSide,
				enlarge: params.withoutEnlargement === false
			},
			...params.compressionLevel === void 0 ? {} : { compressionLevel: params.compressionLevel }
		})).data;
	} catch (error) {
		return wrapRastermillUnavailable("resizeToPng", error);
	}
}
async function optimizeImageToPng(buffer, maxBytes, options) {
	let out;
	try {
		out = await createImageProcessor().encode(buffer, {
			format: "png",
			maxBytes,
			search: options?.sides === void 0 ? {} : { maxSide: options.sides }
		});
	} catch (error) {
		wrapRastermillUnavailable("optimizeImageToPng", error);
	}
	return {
		buffer: out.data,
		optimizedSize: out.bytes,
		resizeSide: out.chosen.maxSide ?? out.width,
		compressionLevel: out.chosen.compressionLevel ?? 6
	};
}
//#endregion
export { convertHeicToJpeg as a, hasAlphaChannel as c, optimizeImageToPng as d, readImageMetadataFromHeader$1 as f, resizeToPng as h, buildImageResizeSideGrid as i, isImageProcessorUnavailableError as l, resizeToJpeg as m, ImageProcessorUnavailableError as n, createImageProcessor as o, readImageProbeFromHeader$1 as p, MAX_IMAGE_INPUT_PIXELS as r, getImageMetadata as s, IMAGE_REDUCE_QUALITY_STEPS as t, normalizeExifOrientation as u };
