"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.prescriptionUpload = exports.productTypeImageUpload = exports.bannerImageUpload = exports.reviewImageUpload = exports.brandImageUpload = exports.shapeImageUpload = exports.productImageUpload = void 0;
const node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
const multer_1 = __importDefault(require("multer"));
const uploadPaths_js_1 = require("../services/uploadPaths.js");
const productUploadDir = node_path_1.default.join(uploadPaths_js_1.uploadsRoot, "products");
const shapesUploadDir = node_path_1.default.join(uploadPaths_js_1.uploadsRoot, "shapes");
const brandsUploadDir = node_path_1.default.join(uploadPaths_js_1.uploadsRoot, "brands");
const productTypesUploadDir = node_path_1.default.join(uploadPaths_js_1.uploadsRoot, "product-types");
const reviewsUploadDir = node_path_1.default.join(uploadPaths_js_1.uploadsRoot, "reviews");
const prescriptionsUploadDir = node_path_1.default.join(uploadPaths_js_1.uploadsRoot, "prescriptions");
const bannersUploadDir = node_path_1.default.join(uploadPaths_js_1.uploadsRoot, "banners");
node_fs_1.default.mkdirSync(productUploadDir, { recursive: true });
node_fs_1.default.mkdirSync(shapesUploadDir, { recursive: true });
node_fs_1.default.mkdirSync(brandsUploadDir, { recursive: true });
node_fs_1.default.mkdirSync(productTypesUploadDir, { recursive: true });
node_fs_1.default.mkdirSync(reviewsUploadDir, { recursive: true });
node_fs_1.default.mkdirSync(prescriptionsUploadDir, { recursive: true });
node_fs_1.default.mkdirSync(bannersUploadDir, { recursive: true });
const storage = multer_1.default.diskStorage({
    destination: (_req, file, cb) => {
        cb(null, productUploadDir);
    },
    filename: (_req, file, cb) => {
        const extension = node_path_1.default.extname(file.originalname).toLowerCase();
        const safeBaseName = node_path_1.default
            .basename(file.originalname, extension)
            .toLowerCase()
            .replace(/[^a-z0-9]+/g, "-")
            .replace(/^-+|-+$/g, "")
            .slice(0, 80);
        cb(null, `${Date.now()}-${safeBaseName || "product"}${extension}`);
    },
});
const shapesStorage = multer_1.default.diskStorage({
    destination: (_req, file, cb) => {
        cb(null, shapesUploadDir);
    },
    filename: (_req, file, cb) => {
        const extension = node_path_1.default.extname(file.originalname).toLowerCase();
        const safeBaseName = node_path_1.default
            .basename(file.originalname, extension)
            .toLowerCase()
            .replace(/[^a-z0-9]+/g, "-")
            .replace(/^-+|-+$/g, "")
            .slice(0, 80);
        cb(null, `${Date.now()}-${safeBaseName || "shape"}${extension}`);
    },
});
const brandsStorage = multer_1.default.diskStorage({
    destination: (_req, file, cb) => {
        cb(null, brandsUploadDir);
    },
    filename: (_req, file, cb) => {
        const extension = node_path_1.default.extname(file.originalname).toLowerCase();
        const safeBaseName = node_path_1.default
            .basename(file.originalname, extension)
            .toLowerCase()
            .replace(/[^a-z0-9]+/g, "-")
            .replace(/^-+|-+$/g, "")
            .slice(0, 80);
        cb(null, `${Date.now()}-${safeBaseName || "brand"}${extension}`);
    },
});
const productTypesStorage = multer_1.default.diskStorage({
    destination: (_req, file, cb) => {
        cb(null, productTypesUploadDir);
    },
    filename: (_req, file, cb) => {
        const extension = node_path_1.default.extname(file.originalname).toLowerCase();
        const safeBaseName = node_path_1.default
            .basename(file.originalname, extension)
            .toLowerCase()
            .replace(/[^a-z0-9]+/g, "-")
            .replace(/^-+|-+$/g, "")
            .slice(0, 80);
        cb(null, `${Date.now()}-${safeBaseName || "product-type"}${extension}`);
    },
});
exports.productImageUpload = (0, multer_1.default)({
    storage,
    limits: {
        fileSize: 5 * 1024 * 1024,
        files: 8,
    },
    fileFilter: (_req, file, cb) => {
        if (!file.mimetype.startsWith("image/")) {
            cb(new Error("Only image files are allowed."));
            return;
        }
        cb(null, true);
    },
});
exports.shapeImageUpload = (0, multer_1.default)({
    storage: shapesStorage,
    limits: {
        fileSize: 5 * 1024 * 1024,
        files: 4,
    },
    fileFilter: (_req, file, cb) => {
        if (!file.mimetype.startsWith("image/")) {
            cb(new Error("Only image files are allowed."));
            return;
        }
        cb(null, true);
    },
});
exports.brandImageUpload = (0, multer_1.default)({
    storage: brandsStorage,
    limits: {
        fileSize: 5 * 1024 * 1024,
        files: 4,
    },
    fileFilter: (_req, file, cb) => {
        if (!file.mimetype.startsWith("image/")) {
            cb(new Error("Only image files are allowed."));
            return;
        }
        cb(null, true);
    },
});
const reviewsStorage = multer_1.default.diskStorage({
    destination: (_req, file, cb) => {
        cb(null, reviewsUploadDir);
    },
    filename: (_req, file, cb) => {
        const extension = node_path_1.default.extname(file.originalname).toLowerCase();
        const safeBaseName = node_path_1.default
            .basename(file.originalname, extension)
            .toLowerCase()
            .replace(/[^a-z0-9]+/g, "-")
            .replace(/^-+|-+$/g, "")
            .slice(0, 80);
        cb(null, `${Date.now()}-${safeBaseName || "review"}${extension}`);
    },
});
exports.reviewImageUpload = (0, multer_1.default)({
    storage: reviewsStorage,
    limits: {
        fileSize: 5 * 1024 * 1024,
        files: 1,
    },
    fileFilter: (_req, file, cb) => {
        if (!file.mimetype.startsWith("image/")) {
            cb(new Error("Only image files are allowed."));
            return;
        }
        cb(null, true);
    },
});
const bannersStorage = multer_1.default.diskStorage({
    destination: (_req, file, cb) => {
        cb(null, bannersUploadDir);
    },
    filename: (_req, file, cb) => {
        const extension = node_path_1.default.extname(file.originalname).toLowerCase();
        const safeBaseName = node_path_1.default
            .basename(file.originalname, extension)
            .toLowerCase()
            .replace(/[^a-z0-9]+/g, "-")
            .replace(/^-+|-+$/g, "")
            .slice(0, 80);
        cb(null, `${Date.now()}-${safeBaseName || "banner"}${extension}`);
    },
});
exports.bannerImageUpload = (0, multer_1.default)({
    storage: bannersStorage,
    limits: {
        fileSize: 5 * 1024 * 1024,
        files: 1,
    },
    fileFilter: (_req, file, cb) => {
        if (!file.mimetype.startsWith("image/")) {
            cb(new Error("Only image files are allowed."));
            return;
        }
        cb(null, true);
    },
});
exports.productTypeImageUpload = (0, multer_1.default)({
    storage: productTypesStorage,
    limits: {
        fileSize: 5 * 1024 * 1024,
        files: 4,
    },
    fileFilter: (_req, file, cb) => {
        if (!file.mimetype.startsWith("image/")) {
            cb(new Error("Only image files are allowed."));
            return;
        }
        cb(null, true);
    },
});
const prescriptionsStorage = multer_1.default.diskStorage({
    destination: (_req, file, cb) => {
        cb(null, prescriptionsUploadDir);
    },
    filename: (_req, file, cb) => {
        const extension = node_path_1.default.extname(file.originalname).toLowerCase();
        const safeBaseName = node_path_1.default
            .basename(file.originalname, extension)
            .toLowerCase()
            .replace(/[^a-z0-9]+/g, "-")
            .replace(/^-+|-+$/g, "")
            .slice(0, 80);
        cb(null, `${Date.now()}-${safeBaseName || "prescription"}${extension}`);
    },
});
exports.prescriptionUpload = (0, multer_1.default)({
    storage: prescriptionsStorage,
    limits: {
        fileSize: 5 * 1024 * 1024,
        files: 1,
    },
    fileFilter: (_req, file, cb) => {
        const allowed = ["image/", "application/pdf"];
        if (!allowed.some((prefix) => file.mimetype.startsWith(prefix))) {
            cb(new Error("Only image or PDF files are allowed."));
            return;
        }
        cb(null, true);
    },
});
//# sourceMappingURL=uploadMiddleware.js.map