import * as fs from 'node:fs';
import * as path from 'node:path';
import { buildRunPaths } from '../../../core/workflow/run/run-paths.js';
import { buildTaskInstruction } from '../../../infra/task/index.js';
import { copyTaskAttachmentsToRunContext } from '../attachments.js';
import { readTaskSpecFile } from '../taskSpecFile.js';
function getTaskSpecPath(projectCwd, taskDir) {
    return path.join(projectCwd, taskDir, 'order.md');
}
function removeEmptyDirectory(directory) {
    if (fs.existsSync(directory) && fs.readdirSync(directory).length === 0) {
        fs.rmdirSync(directory);
    }
}
export function stageTaskSpecForExecution(projectCwd, execCwd, taskDir, reportDirName) {
    const sourceTaskDir = path.join(projectCwd, taskDir);
    const sourceOrderPath = getTaskSpecPath(projectCwd, taskDir);
    const orderContent = readTaskSpecFile(sourceOrderPath);
    const runPaths = buildRunPaths(execCwd, reportDirName);
    try {
        fs.mkdirSync(runPaths.contextTaskAbs, { recursive: true });
        fs.writeFileSync(runPaths.contextTaskOrderAbs, orderContent, 'utf-8');
        copyTaskAttachmentsToRunContext(sourceTaskDir, runPaths.contextTaskAbs);
    }
    catch (error) {
        fs.rmSync(runPaths.contextTaskAbs, { recursive: true, force: true });
        throw error;
    }
    return {
        taskPrompt: buildTaskInstruction(runPaths.contextTaskRel, runPaths.contextTaskOrderRel),
        orderContent,
        contextTaskDir: runPaths.contextTaskAbs,
        contextDir: runPaths.contextAbs,
        runRootDir: runPaths.runRootAbs,
    };
}
export function cleanupStagedTaskSpec(stagedSpec) {
    fs.rmSync(stagedSpec.contextTaskDir, { recursive: true, force: true });
    removeEmptyDirectory(stagedSpec.contextDir);
    removeEmptyDirectory(stagedSpec.runRootDir);
}
//# sourceMappingURL=taskSpecContext.js.map