import { t as canonicalizeBase64 } from "./base64-D03BM6T6.js";
//#region src/media/inline-image-data-url.ts
const INLINE_IMAGE_DATA_URL_PREFIX = "data:";
const IMAGE_SIGNATURES = [
	{
		mime: "image/png",
		matches: (buffer) => buffer.length >= 8 && buffer[0] === 137 && buffer[1] === 80 && buffer[2] === 78 && buffer[3] === 71 && buffer[4] === 13 && buffer[5] === 10 && buffer[6] === 26 && buffer[7] === 10
	},
	{
		mime: "image/jpeg",
		matches: (buffer) => buffer.length >= 3 && buffer[0] === 255 && buffer[1] === 216 && buffer[2] === 255
	},
	{
		mime: "image/webp",
		matches: (buffer) => buffer.length >= 12 && buffer.subarray(0, 4).toString("ascii") === "RIFF" && buffer.subarray(8, 12).toString("ascii") === "WEBP"
	},
	{
		mime: "image/gif",
		matches: (buffer) => buffer.length >= 6 && (buffer.subarray(0, 6).toString("ascii") === "GIF87a" || buffer.subarray(0, 6).toString("ascii") === "GIF89a")
	}
];
function startsWithDataUrl(value) {
	return value.slice(0, 5).toLowerCase() === INLINE_IMAGE_DATA_URL_PREFIX;
}
function sniffInlineImageMime(buffer) {
	return IMAGE_SIGNATURES.find((signature) => signature.matches(buffer))?.mime;
}
function parseInlineImageDataUrl(value) {
	if (!startsWithDataUrl(value)) return {
		metadata: [],
		payload: value
	};
	const commaIndex = value.indexOf(",");
	if (commaIndex < 0) return;
	return {
		metadata: value.slice(5, commaIndex).split(";").map((part) => part.trim()),
		payload: value.slice(commaIndex + 1)
	};
}
function metadataAllowsImageBase64(metadata) {
	const [mimeType, ...options] = metadata;
	return mimeType !== void 0 && mimeType.toLowerCase().startsWith("image/") && options.some((part) => part.toLowerCase() === "base64");
}
function sanitizeInlineImageDataUrl(imageUrl) {
	const parsed = parseInlineImageDataUrl(imageUrl);
	if (!parsed) return;
	if (parsed.metadata.length === 0) return imageUrl;
	if (!metadataAllowsImageBase64(parsed.metadata)) return;
	const canonicalPayload = canonicalizeBase64(parsed.payload);
	if (!canonicalPayload) return;
	const sniffedMimeType = sniffInlineImageMime(Buffer.from(canonicalPayload, "base64"));
	if (!sniffedMimeType) return;
	return `data:${sniffedMimeType};base64,${canonicalPayload}`;
}
//#endregion
export { sanitizeInlineImageDataUrl as n, sniffInlineImageMime as r, INLINE_IMAGE_DATA_URL_PREFIX as t };
