import multer from 'multer';
import { CHAT_SESSION_IMAGE_UPLOAD_LIMITS, ChatSessionUploadError, isChatSessionUploadError, } from '../../../../server/services/control-plane/chat-session-image-uploads.js';
export class ChatSessionUploadsRestController {
    imageUploads;
    uploadImagesMiddleware;
    constructor(imageUploads) {
        this.imageUploads = imageUploads;
        this.uploadImagesMiddleware = this.createUploadImagesMiddleware();
    }
    uploadImages = (request, response) => {
        response.json(this.imageUploads.completeUploads(request));
    };
    handleUploadError = (error, _request, response, _next) => {
        this.sendUploadError(response, error);
    };
    createUploadImagesMiddleware() {
        const upload = multer({
            storage: multer.diskStorage({
                destination: (request, _file, callback) => {
                    void this.imageUploads.resolveUploadDirectory(request)
                        .then((directory) => callback(null, directory))
                        .catch((error) => callback(error instanceof Error ? error : new Error(String(error)), ''));
                },
                filename: (_request, file, callback) => {
                    callback(null, this.imageUploads.createStoredFilename(file));
                },
            }),
            limits: {
                fileSize: CHAT_SESSION_IMAGE_UPLOAD_LIMITS.maxFileSizeBytes,
                files: CHAT_SESSION_IMAGE_UPLOAD_LIMITS.maxFiles,
            },
            fileFilter: (_request, file, callback) => {
                if (!this.imageUploads.acceptsImageFile(file)) {
                    callback(new ChatSessionUploadError(400, 'Upload supports .png, .jpg, .jpeg, .gif, and .webp images only.'));
                    return;
                }
                callback(null, true);
            },
        });
        return upload.fields([
            { name: 'images', maxCount: CHAT_SESSION_IMAGE_UPLOAD_LIMITS.maxFiles },
            { name: 'files', maxCount: CHAT_SESSION_IMAGE_UPLOAD_LIMITS.maxFiles },
            { name: 'file', maxCount: CHAT_SESSION_IMAGE_UPLOAD_LIMITS.maxFiles },
        ]);
    }
    sendUploadError(response, error) {
        if (isChatSessionUploadError(error)) {
            response.status(error.statusCode).json({ error: error.message });
            return;
        }
        if (error instanceof multer.MulterError) {
            response.status(400).json({ error: this.formatMulterError(error) });
            return;
        }
        const message = error instanceof Error ? error.message : String(error);
        response.status(500).json({ error: message || 'Image upload failed.' });
    }
    formatMulterError(error) {
        const errorMessages = {
            LIMIT_FILE_SIZE: `Each image must be ${this.formatBytes(CHAT_SESSION_IMAGE_UPLOAD_LIMITS.maxFileSizeBytes)} or smaller.`,
            LIMIT_FILE_COUNT: `Upload at most ${CHAT_SESSION_IMAGE_UPLOAD_LIMITS.maxFiles} images at a time.`,
            LIMIT_UNEXPECTED_FILE: 'Unexpected image upload field.',
        };
        return errorMessages[error.code] ?? error.message;
    }
    formatBytes(bytes) {
        return `${Math.floor(bytes / (1024 * 1024))} MB`;
    }
}
//# sourceMappingURL=chat-session-uploads.js.map