{"version":3,"file":"image-process.d.ts","sourceRoot":"","sources":["../../src/utils/image-process.ts"],"names":[],"mappings":"AACA,OAAO,EAAuB,KAAK,kBAAkB,EAAe,MAAM,mBAAmB,CAAC;AAE9F,MAAM,WAAW,mBAAmB;IACnC,wEAAwE;IACxE,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,yEAAyE;IACzE,aAAa,CAAC,EAAE,kBAAkB,CAAC;CACnC;AAED,MAAM,MAAM,kBAAkB,GAC3B;IACA,EAAE,EAAE,IAAI,CAAC;IACT,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,EAAE,CAAC;CACf,GACD;IACA,EAAE,EAAE,KAAK,CAAC;IACV,OAAO,EAAE,MAAM,CAAC;CACf,CAAC;AAmDL,wBAAsB,YAAY,CACjC,KAAK,EAAE,UAAU,EACjB,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,mBAAmB,GAC3B,OAAO,CAAC,kBAAkB,CAAC,CA2C7B","sourcesContent":["import { convertImageBytesToPng } from \"./image-convert.ts\";\nimport { formatDimensionNote, type ImageResizeOptions, resizeImage } from \"./image-resize.ts\";\n\nexport interface ProcessImageOptions {\n\t/** Whether to resize images to inline provider limits. Default: true */\n\tautoResizeImages?: boolean;\n\t/** Optional resize overrides. Uses resizeImage defaults when omitted. */\n\tresizeOptions?: ImageResizeOptions;\n}\n\nexport type ProcessImageResult =\n\t| {\n\t\t\tok: true;\n\t\t\tdata: string;\n\t\t\tmimeType: string;\n\t\t\thints: string[];\n\t  }\n\t| {\n\t\t\tok: false;\n\t\t\tmessage: string;\n\t  };\n\ninterface NormalizedImage {\n\tbytes: Uint8Array;\n\tmimeType: string;\n\tconvertedFrom?: string;\n}\n\nfunction baseMimeType(mimeType: string): string {\n\treturn mimeType.split(\";\")[0]?.trim().toLowerCase() ?? mimeType.toLowerCase();\n}\n\nfunction normalizeSupportedImageMimeType(mimeType: string): string | null {\n\tswitch (baseMimeType(mimeType)) {\n\t\tcase \"image/png\":\n\t\t\treturn \"image/png\";\n\t\tcase \"image/jpeg\":\n\t\tcase \"image/jpg\":\n\t\t\treturn \"image/jpeg\";\n\t\tcase \"image/gif\":\n\t\t\treturn \"image/gif\";\n\t\tcase \"image/webp\":\n\t\t\treturn \"image/webp\";\n\t\tdefault:\n\t\t\treturn null;\n\t}\n}\n\nasync function normalizeImage(bytes: Uint8Array, mimeType: string): Promise<NormalizedImage | null> {\n\tconst normalizedMimeType = normalizeSupportedImageMimeType(mimeType);\n\tif (normalizedMimeType) {\n\t\treturn { bytes, mimeType: normalizedMimeType };\n\t}\n\n\tconst pngBytes = await convertImageBytesToPng(bytes);\n\tif (!pngBytes) {\n\t\treturn null;\n\t}\n\n\treturn {\n\t\tbytes: pngBytes,\n\t\tmimeType: \"image/png\",\n\t\tconvertedFrom: baseMimeType(mimeType),\n\t};\n}\n\nfunction conversionHint(from: string | undefined, to: string): string | undefined {\n\tif (!from || from === to) return undefined;\n\treturn `[Image converted from ${from} to ${to}.]`;\n}\n\nexport async function processImage(\n\tbytes: Uint8Array,\n\tmimeType: string,\n\toptions?: ProcessImageOptions,\n): Promise<ProcessImageResult> {\n\tconst autoResizeImages = options?.autoResizeImages ?? true;\n\tconst normalized = await normalizeImage(bytes, mimeType);\n\tif (!normalized) {\n\t\treturn {\n\t\t\tok: false,\n\t\t\tmessage: \"[Image omitted: could not be converted to a supported inline image format.]\",\n\t\t};\n\t}\n\n\tif (autoResizeImages) {\n\t\tconst resized = await resizeImage(normalized.bytes, normalized.mimeType, options?.resizeOptions);\n\t\tif (!resized) {\n\t\t\treturn {\n\t\t\t\tok: false,\n\t\t\t\tmessage: \"[Image omitted: could not be resized below the inline image size limit.]\",\n\t\t\t};\n\t\t}\n\n\t\tconst hints: string[] = [];\n\t\tconst convertedHint = conversionHint(normalized.convertedFrom, resized.mimeType);\n\t\tif (convertedHint) hints.push(convertedHint);\n\t\tconst dimensionNote = formatDimensionNote(resized);\n\t\tif (dimensionNote) hints.push(dimensionNote);\n\n\t\treturn {\n\t\t\tok: true,\n\t\t\tdata: resized.data,\n\t\t\tmimeType: resized.mimeType,\n\t\t\thints,\n\t\t};\n\t}\n\n\tconst hints: string[] = [];\n\tconst convertedHint = conversionHint(normalized.convertedFrom, normalized.mimeType);\n\tif (convertedHint) hints.push(convertedHint);\n\n\treturn {\n\t\tok: true,\n\t\tdata: Buffer.from(normalized.bytes).toString(\"base64\"),\n\t\tmimeType: normalized.mimeType,\n\t\thints,\n\t};\n}\n"]}