"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var ImageEncodingService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImageEncodingService = void 0;
const common_1 = require("@nestjs/common");
let ImageEncodingService = ImageEncodingService_1 = class ImageEncodingService {
    constructor() {
        this.logger = new common_1.Logger(ImageEncodingService_1.name);
    }
    async imageUrlToBase64(url, mimeTypeOverride, maxSizeBytes = 5 * 1024 * 1024) {
        this.logger.debug(`Fetching image from URL: ${url}`);
        const response = await fetch(url);
        if (!response.ok) {
            throw new Error(`Failed to fetch image (${response.status} ${response.statusText})`);
        }
        const contentType = mimeTypeOverride ||
            response.headers.get('content-type') ||
            'image/jpeg';
        const arrayBuffer = await response.arrayBuffer();
        const buffer = Buffer.from(arrayBuffer);
        if (buffer.length > maxSizeBytes) {
            throw new Error(`Image size ${buffer.length} bytes exceeds limit of ${maxSizeBytes} bytes`);
        }
        return {
            base64: buffer.toString('base64'),
            mediaType: contentType,
        };
    }
    async imageUrlToDataUrl(url, mimeTypeOverride, maxSizeBytes) {
        const { base64, mediaType } = await this.imageUrlToBase64(url, mimeTypeOverride, maxSizeBytes);
        return `data:${mediaType};base64,${base64}`;
    }
};
exports.ImageEncodingService = ImageEncodingService;
exports.ImageEncodingService = ImageEncodingService = ImageEncodingService_1 = __decorate([
    (0, common_1.Injectable)()
], ImageEncodingService);
//# sourceMappingURL=image-encoding.helper.js.map