//#region src/magic-bytes.ts
const TEXT_DECODER = new TextDecoder("latin1");
function bytesToAscii(bytes, start, length) {
	return TEXT_DECODER.decode(bytes.subarray(start, start + length));
}
function startsWithAscii(bytes, start, literal) {
	if (bytes.length < start + literal.length) return false;
	for (let i = 0; i < literal.length; i++) if (bytes[start + i] !== literal.charCodeAt(i)) return false;
	return true;
}
function isDangerousTextHeader(header) {
	const sample = TEXT_DECODER.decode(header.subarray(0, Math.min(header.length, 256))).trimStart().toLowerCase();
	if (sample.startsWith("<svg")) return true;
	if (sample.startsWith("<html")) return true;
	if (sample.startsWith("<!doctype html")) return true;
	if (sample.startsWith("<!doctype svg")) return true;
	if (sample.startsWith("<?xml")) return true;
	if (sample.includes("<script")) return true;
	return false;
}
function isJpeg(header) {
	return header.length >= 3 && header[0] === 255 && header[1] === 216 && header[2] === 255;
}
function isPng(header) {
	return header.length >= 8 && header[0] === 137 && header[1] === 80 && header[2] === 78 && header[3] === 71 && header[4] === 13 && header[5] === 10 && header[6] === 26 && header[7] === 10;
}
function isGif(header) {
	if (header.length < 6) return false;
	const tag = bytesToAscii(header, 0, 6);
	return tag === "GIF87a" || tag === "GIF89a";
}
function isWebp(header) {
	return header.length >= 12 && bytesToAscii(header, 0, 4) === "RIFF" && bytesToAscii(header, 8, 4) === "WEBP";
}
/** Strict AVIF brand check: major or compatible brand must be `avif` or `avis`. Bare `ftyp` alone is rejected. */
function isStrictAvif(header) {
	if (header.length < 12 || !startsWithAscii(header, 4, "ftyp")) return false;
	const brands = [bytesToAscii(header, 8, 4)];
	for (let offset = 16; offset + 4 <= header.length; offset += 4) brands.push(bytesToAscii(header, offset, 4));
	return brands.some((brand) => brand === "avif" || brand === "avis");
}
/**
* Detect image MIME type from the first bytes of a file.
* Header-only, zero-dep. Rejects SVG/HTML/script/XML and loose bare-`ftyp` AVIF.
*/
function detectMimeFromMagicBytes(header) {
	if (header.length === 0) return null;
	if (isDangerousTextHeader(header)) return null;
	if (isJpeg(header)) return "image/jpeg";
	if (isPng(header)) return "image/png";
	if (isGif(header)) return "image/gif";
	if (isWebp(header)) return "image/webp";
	if (isStrictAvif(header)) return "image/avif";
	return null;
}
//#endregion
export { detectMimeFromMagicBytes };
