import { mkdir, rm, writeFile } from "node:fs/promises";
import path from "node:path";
const SYSTEM_SKILLS_CACHE_ROOT = ".superblocks/system-skills";
function validateRelativePath(relativePath) {
    const normalized = path.normalize(relativePath);
    if (path.isAbsolute(normalized) || normalized.startsWith("..")) {
        throw new Error(`Invalid system skill path: ${relativePath}`);
    }
    return normalized;
}
async function writeSkillFiles(destinationDir, skills) {
    for (const [relativePath, content] of Object.entries(skills)) {
        const validatedPath = validateRelativePath(relativePath);
        const absolutePath = path.join(destinationDir, validatedPath);
        await mkdir(path.dirname(absolutePath), { recursive: true });
        await writeFile(absolutePath, content, "utf-8");
    }
}
async function rewriteSkillDirectory(rootDir, skills) {
    await rm(rootDir, { recursive: true, force: true });
    await mkdir(rootDir, { recursive: true });
    await writeSkillFiles(rootDir, skills);
}
export async function ensureMaterializedSystemSkills(appRootDirPath, skills, logger) {
    const rootDir = path.join(appRootDirPath, SYSTEM_SKILLS_CACHE_ROOT);
    await rewriteSkillDirectory(rootDir, skills);
    logger.info("[Skills] Materialized system skills to filesystem", {
        fileCount: Object.keys(skills).length,
        rootDir: SYSTEM_SKILLS_CACHE_ROOT,
    });
    return { systemSkillsDir: rootDir };
}
//# sourceMappingURL=materialized.js.map