"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.previewRouter = previewRouter;
exports.onThemeSaved = onThemeSaved;
const express_1 = require("express");
const previewService_1 = require("../services/previewService");
function previewRouter(db, storageDir) {
    const router = (0, express_1.Router)();
    // POST /api/v1/creatives/:id/preview/start
    router.post("/creatives/:id/preview/start", async (req, res) => {
        try {
            const creativeId = req.params.id;
            const creative = db
                .prepare("SELECT * FROM creatives WHERE id = ?")
                .get(creativeId);
            if (!creative) {
                res.status(404).json({ error: { message: "Creative not found" } });
                return;
            }
            if (creative.status === "draft") {
                res.status(400).json({
                    error: { message: "请先上传素材后再预览" },
                });
                return;
            }
            const theme = req.body?.theme || JSON.parse(creative.theme || "{}");
            const result = await (0, previewService_1.startPreview)(creativeId, theme, storageDir);
            res.json({ data: result });
        }
        catch (err) {
            res.status(500).json({ error: { message: err.message } });
        }
    });
    // POST /api/v1/creatives/:id/preview/stop
    router.post("/creatives/:id/preview/stop", (_req, res) => {
        (0, previewService_1.stopPreview)();
        res.json({ data: { stopped: true } });
    });
    return router;
}
/**
 * 当 Theme 保存时，若预览正在运行则更新配置
 */
function onThemeSaved(creativeId, theme, storageDir) {
    (0, previewService_1.updatePreviewConfig)(creativeId, theme, storageDir);
}
//# sourceMappingURL=preview.js.map