"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateAdConfig = generateAdConfig;
exports.createAssetsSymlink = createAssetsSymlink;
exports.cleanupBuildArtifacts = cleanupBuildArtifacts;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const storageService_1 = require("./storageService");
const TEMPLATE_DIR = path_1.default.resolve(__dirname, "../../../../templates/coloring");
/**
 * 扫描用户素材目录，生成 _ad_config_.ts 内容。
 *
 * 关键逻辑：
 * - 必填素材 (config/page/map) → 必定 import
 * - optional 素材 (special) → 存在才 import，不存在则不生成对应字段
 * - 模板自有素材 → import 指向 templates/coloring/assets/
 */
function generateAdConfig(input) {
    const assetsDir = path_1.default.join(input.storageDir, "creatives", input.creativeId, "assets");
    const files = (0, storageService_1.scanAssetFiles)(assetsDir);
    const lines = [];
    // == 用户素材 ==
    lines.push("// ==== 用户上传素材 ====");
    lines.push(`import configRaw from "/assets/user/config.json?raw";`);
    lines.push(`import pageUrl from "/assets/user/page.png?url";`);
    lines.push(`import mapUrl from "/assets/user/map.png?url";`);
    let hasSpecial = false;
    if (files.special) {
        hasSpecial = true;
        lines.push(`import specialUrl from "/assets/user/${files.special}?url";`);
    }
    // == 模板自有素材 ==
    lines.push("");
    lines.push("// ==== 模板自有素材 ====");
    lines.push(`import numberFontUrl from "/assets/fonts/numbers_roboto_500.png?url";`);
    lines.push(`import fingerUrl from "/assets/img/finger.png?url";`);
    lines.push(`import logoUrl from "/assets/img/logo.png?url";`);
    lines.push(`import logoTxtUrl from "/assets/img/logo-txt.png?url";`);
    lines.push(`import coloringPagesUrl from "/assets/img/coloring-pages.png?url";`);
    lines.push(`import slogonUrl from "/assets/img/slogon.png?url";`);
    // == adAssets 导出 ==
    lines.push("");
    lines.push("export const adAssets = {");
    lines.push("  configRaw,");
    lines.push("  pageUrl,");
    lines.push("  mapUrl,");
    if (hasSpecial) {
        lines.push("  specialUrl,");
    }
    lines.push("  numberFontUrl,");
    lines.push("  fingerUrl,");
    lines.push("  logoUrl,");
    lines.push("  logoTxtUrl,");
    lines.push("  coloringPagesUrl,");
    lines.push("  slogonUrl,");
    lines.push("};");
    // == adTheme 导出 ==
    lines.push("");
    lines.push("export const adTheme = {");
    lines.push(`  bgGradient: ${JSON.stringify(input.theme.bgGradient || "linear-gradient(160deg, #fff9f2 0%, #ffeedd 100%)")},`);
    lines.push(`  ctaGradient: ${JSON.stringify(input.theme.ctaGradient || "linear-gradient(135deg, #ff5f1f 0%, #ffb300 100%)")},`);
    lines.push(`  ctaText: ${JSON.stringify(input.theme.ctaText || "PLAY NOW")},`);
    lines.push(`  frameColor: ${JSON.stringify(input.theme.frameColor || "#edce9b")},`);
    lines.push(`  frameLineWidth: ${JSON.stringify(Number(input.theme.frameLineWidth) || 16)},`);
    lines.push("};");
    return lines.join("\n") + "\n";
}
/**
 * 创建 symlink：templates/coloring/assets/user/ → storage/creatives/<id>/assets/
 */
function createAssetsSymlink(creativeId, storageDir) {
    const symlinkPath = path_1.default.join(TEMPLATE_DIR, "assets", "user");
    const targetPath = path_1.default.join(storageDir, "creatives", creativeId, "assets");
    // 清理旧的 symlink（用 try-lstat 而非 existsSync，因为悬空 symlink 的 existsSync 返回 false）
    try {
        const stat = fs_1.default.lstatSync(symlinkPath);
        if (stat.isSymbolicLink()) {
            fs_1.default.unlinkSync(symlinkPath);
        }
        else {
            fs_1.default.rmSync(symlinkPath, { recursive: true, force: true });
        }
    }
    catch {
        // 路径不存在，无需清理
    }
    fs_1.default.symlinkSync(targetPath, symlinkPath, "dir");
    console.log(`[config] Symlink created: ${symlinkPath} → ${targetPath}`);
}
/**
 * 清理构建产生的临时文件
 */
function cleanupBuildArtifacts() {
    const adConfigPath = path_1.default.join(TEMPLATE_DIR, "src", "filler", "_ad_config_.ts");
    if (fs_1.default.existsSync(adConfigPath)) {
        fs_1.default.unlinkSync(adConfigPath);
    }
    // symlink 保留，下次构建复用
}
//# sourceMappingURL=configGenerator.js.map