"use strict";
/**
 * Remediation runner — PR-5a-1
 *
 * Reads pack.remediations[], routes each entry to the correct handler
 * by kind, and returns RemediationResult[] for dry-run preview or live apply.
 *
 * Handler dispatch:
 *   config-patch   → handlers/config-patch.ts (applyConfigPatch / writeConfigPatch)
 *   doc-insert     → handlers/doc-insert.ts   (applyDocInsert / writeDocInsert)
 *   wrapper-import → handlers/wrapper-import.ts (applyWrapperImport / writeWrapperImport)
 *   codemod        → handlers/codemod.ts STUB  (returns 'skipped' — real impl in PR-5a-2)
 *
 * @connexum/ai-governance PR-5a-1
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.runRemediations = runRemediations;
const config_patch_1 = require("./handlers/config-patch");
const doc_insert_1 = require("./handlers/doc-insert");
const wrapper_import_1 = require("./handlers/wrapper-import");
const codemod_1 = require("./handlers/codemod");
/**
 * Execute a list of remediations.
 *
 * In dry-run mode, all handlers return diffs without writing.
 * In apply mode, handlers write to disk and return the applied diffs.
 *
 * PR-5a-2: now async to support the codemod handler (vm sandbox is async).
 */
async function runRemediations(options) {
    const { agentDir, packId, packVersion, remediations, mode } = options;
    const results = [];
    for (const remediation of remediations) {
        const result = await dispatchRemediation({
            agentDir,
            packId,
            packVersion,
            remediation,
            mode,
        });
        results.push(result);
    }
    return results;
}
async function dispatchRemediation(opts) {
    const { agentDir, packId, packVersion, remediation, mode } = opts;
    const payload = remediation.payload;
    try {
        switch (payload.kind) {
            case 'config-patch':
                return handleConfigPatch(agentDir, remediation, payload, packId, packVersion, mode);
            case 'doc-insert':
                return handleDocInsert(agentDir, remediation, payload, packId, packVersion, mode);
            case 'wrapper-import':
                return handleWrapperImport(agentDir, remediation, payload, packId, packVersion, mode);
            case 'codemod':
                // PR-5a-2: real async codemod execution via vm sandbox
                return await (0, codemod_1.applyCodemod)(agentDir, remediation, payload, packId, packVersion);
            default: {
                const exhaustive = payload;
                return {
                    remediation,
                    packId,
                    packVersion,
                    status: 'error',
                    filesAffected: [],
                    diffs: [],
                    error: `Unknown remediation kind: ${exhaustive.kind}`,
                };
            }
        }
    }
    catch (err) {
        return {
            remediation,
            packId,
            packVersion,
            status: 'error',
            filesAffected: [],
            diffs: [],
            error: err instanceof Error ? err.message : String(err),
        };
    }
}
// ---------------------------------------------------------------------------
// Per-kind handlers
// ---------------------------------------------------------------------------
function handleConfigPatch(agentDir, remediation, payload, packId, packVersion, mode) {
    const fn = mode === 'apply' ? config_patch_1.writeConfigPatch : config_patch_1.applyConfigPatch;
    const r = fn(agentDir, payload);
    return {
        remediation,
        packId,
        packVersion,
        status: r.alreadyApplied ? 'skipped' : mode === 'apply' ? 'applied' : 'dry-run',
        filesAffected: r.alreadyApplied ? [] : [r.diff.filePath],
        diffs: r.alreadyApplied ? [] : [r.diff],
    };
}
function handleDocInsert(agentDir, remediation, payload, packId, packVersion, mode) {
    const fn = mode === 'apply' ? doc_insert_1.writeDocInsert : doc_insert_1.applyDocInsert;
    const r = fn(agentDir, payload);
    return {
        remediation,
        packId,
        packVersion,
        status: r.alreadyApplied ? 'skipped' : mode === 'apply' ? 'applied' : 'dry-run',
        filesAffected: r.alreadyApplied ? [] : [r.diff.filePath],
        diffs: r.alreadyApplied ? [] : [r.diff],
    };
}
function handleWrapperImport(agentDir, remediation, payload, packId, packVersion, mode) {
    const fn = mode === 'apply' ? wrapper_import_1.writeWrapperImport : wrapper_import_1.applyWrapperImport;
    const r = fn(agentDir, payload);
    return {
        remediation,
        packId,
        packVersion,
        status: r.alreadyApplied ? 'skipped' : mode === 'apply' ? 'applied' : 'dry-run',
        filesAffected: r.alreadyApplied ? [] : [r.diff.filePath],
        diffs: r.alreadyApplied ? [] : [r.diff],
    };
}
//# sourceMappingURL=run-remediations.js.map