import { runCascade } from '../../engine/cascade-hooks/runner.js';
/**
 * SPEC-585: Autopush on done if configured.
 * Returns the push result for inclusion in autopilotSummary (or null when disabled/skipped).
 */
export async function maybeSafePushOnDone(projectPath, newStatus) {
    if (newStatus !== 'done') {
        return null;
    }
    try {
        const { readFile } = await import('node:fs/promises');
        const { join } = await import('node:path');
        const { readPlanuConfig } = await import('../../engine/planu-config-writer.js');
        const configPath = join(projectPath, 'planu', 'config.json');
        const config = await readPlanuConfig(configPath, readFile);
        if (config?.autopush !== true) {
            return null;
        }
        const { safeAutopush } = await import('../../engine/session-safeguard/autopush.js');
        const branches = config.autopushBranches ?? ['feat/*', 'fix/*', 'chore/*'];
        const result = await safeAutopush(projectPath, branches);
        if (result.pushed) {
            return {
                label: 'autopush',
                message: `Pushed ${String(result.commitsPushed)} commit(s) to ${result.ref}`,
            };
        }
        return { label: 'autopush', message: `Not pushed: ${result.reason} — ${result.message}` };
    }
    catch {
        return null;
    }
}
/** Read the disabled hook list from planu/conventions.json (best-effort — returns empty set on any error). */
async function readDisabledByConfig(projectPath) {
    if (!projectPath) {
        return new Set();
    }
    try {
        const { readFile } = await import('node:fs/promises');
        const { join } = await import('node:path');
        const raw = await readFile(join(projectPath, 'planu', 'conventions.json'), 'utf-8');
        const parsed = JSON.parse(raw);
        const disabled = parsed.cascadeHooks?.disabled;
        return Array.isArray(disabled) ? new Set(disabled) : new Set();
    }
    catch {
        return new Set();
    }
}
/** Read the disabled hook list from PLANU_DISABLE_HOOKS env var (comma-separated). */
function readDisabledByEnv() {
    // eslint-disable-next-line @typescript-eslint/dot-notation
    const raw = process.env['PLANU_DISABLE_HOOKS'] ?? '';
    if (!raw.trim()) {
        return new Set();
    }
    return new Set(raw
        .split(',')
        .map((s) => s.trim())
        .filter(Boolean));
}
/** Fire-and-forget side effects after a successful status transition. */
export function fireAndForgetSideEffects(params) {
    const { knowledge, allSpecs, specId, newStatus, validateScore, projectId, spec, currentStatus } = params;
    const projectPath = knowledge?.projectPath;
    const ctx = {
        specId,
        spec,
        newStatus,
        currentStatus,
        projectId,
        projectPath,
        validateScore,
        allSpecs,
    };
    // Launch cascade — fire-and-forget, never blocks the response
    void (async () => {
        const [disabledByConfig, disabledByEnv] = await Promise.all([
            readDisabledByConfig(projectPath),
            Promise.resolve(readDisabledByEnv()),
        ]);
        await runCascade(ctx, { disabledByConfig, disabledByEnv });
    })().catch(() => {
        /* best-effort — cascade errors never surface to the caller */
    });
}
/**
 * SPEC-772: Run cascade and await fast hooks (fast: true) within a 2s window.
 * Returns fast hook results for inclusion in the update_status response humanSummary.
 * Slow hooks continue fire-and-forget after this function returns.
 */
export async function runCascadeForResponse(params) {
    const { knowledge, allSpecs, specId, newStatus, validateScore, projectId, spec, currentStatus } = params;
    const projectPath = knowledge?.projectPath;
    const ctx = {
        specId,
        spec,
        newStatus,
        currentStatus,
        projectId,
        projectPath,
        validateScore,
        allSpecs,
    };
    try {
        const [disabledByConfig, disabledByEnv] = await Promise.all([
            readDisabledByConfig(projectPath),
            Promise.resolve(readDisabledByEnv()),
        ]);
        return await runCascade(ctx, { disabledByConfig, disabledByEnv });
    }
    catch {
        return null;
    }
}
//# sourceMappingURL=side-effects.js.map