"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 __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var UploadsService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.UploadsService = void 0;
const common_1 = require("@nestjs/common");
const config_1 = require("@nestjs/config");
const promises_1 = require("fs/promises");
const path_1 = require("path");
const crypto = require("crypto");
const ALLOWED_MIME = new Set([
    'image/jpeg',
    'image/png',
    'image/webp',
    'image/gif',
    'application/pdf',
]);
const EXT_BY_MIME = {
    'image/jpeg': '.jpg',
    'image/png': '.png',
    'image/webp': '.webp',
    'image/gif': '.gif',
    'application/pdf': '.pdf',
};
let UploadsService = UploadsService_1 = class UploadsService {
    constructor(config) {
        this.config = config;
        this.logger = new common_1.Logger(UploadsService_1.name);
        this.uploadDir = (0, path_1.join)(process.cwd(), 'uploads', 'receipts');
    }
    async onModuleInit() {
        await (0, promises_1.mkdir)(this.uploadDir, { recursive: true });
        this.logger.log(`Receipt uploads dir: ${this.uploadDir}`);
    }
    async saveReceipt(file) {
        if (!file)
            throw new common_1.BadRequestException('No file uploaded');
        if (!ALLOWED_MIME.has(file.mimetype)) {
            throw new common_1.BadRequestException('Only images (JPEG, PNG, WebP, GIF) or PDF are allowed');
        }
        if (file.size > 5 * 1024 * 1024) {
            throw new common_1.BadRequestException('File must be 5MB or smaller');
        }
        const ext = EXT_BY_MIME[file.mimetype] ?? ((0, path_1.extname)(file.originalname).toLowerCase() || '.bin');
        const filename = `${crypto.randomBytes(16).toString('hex')}${ext}`;
        await (0, promises_1.writeFile)((0, path_1.join)(this.uploadDir, filename), file.buffer);
        const base = this.config.get('app.uploadPublicUrl') ||
            `http://localhost:${this.config.get('app.port', 3001)}`;
        const url = `${base.replace(/\/$/, '')}/uploads/receipts/${filename}`;
        return { url, filename, mimeType: file.mimetype };
    }
};
exports.UploadsService = UploadsService;
exports.UploadsService = UploadsService = UploadsService_1 = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [config_1.ConfigService])
], UploadsService);
//# sourceMappingURL=uploads.service.js.map