// engine/ai-integration/agents-md/updater.ts — AGENTS.md section updater (SPEC-269)
import { writeFileSync, readFileSync, existsSync } from 'node:fs';
import { join } from 'node:path';
const PLANU_SECTION_START = '<!-- planu-sdd:start -->';
const PLANU_SECTION_END = '<!-- planu-sdd:end -->';
/** Returns true if AGENTS.md exists in the given project directory. */
export function hasAgentsMd(projectPath) {
    return existsSync(join(projectPath, 'AGENTS.md'));
}
/**
 * Updates the planu-sdd section in an existing AGENTS.md file without overwriting user content.
 * If the file does not exist, it creates it with the new content.
 * If the file exists but has no planu-sdd markers, the section is appended.
 * If the file has existing markers, only the marked section is replaced.
 */
export function updateAgentsMdSection(filePath, newContent) {
    if (!existsSync(filePath)) {
        writeFileSync(filePath, newContent + '\n', 'utf8');
        return;
    }
    const existing = readFileSync(filePath, 'utf8');
    const startIdx = existing.indexOf(PLANU_SECTION_START);
    const endIdx = existing.indexOf(PLANU_SECTION_END);
    if (startIdx === -1 || endIdx === -1) {
        const separator = existing.endsWith('\n') ? '' : '\n';
        writeFileSync(filePath, existing + separator + newContent + '\n', 'utf8');
        return;
    }
    const before = existing.slice(0, startIdx);
    const after = existing.slice(endIdx + PLANU_SECTION_END.length);
    writeFileSync(filePath, before + newContent + after, 'utf8');
}
//# sourceMappingURL=updater.js.map