/**
 * Shared utilities for hook handlers.
 *
 * @module core/hooks/handlers/utils
 */
import * as fs from 'fs';
import * as path from 'path';
import { HookLogger } from '../hook-logger.js';
/**
 * Walk up from `startDir` to find the nearest directory containing `.specweave/config.json`.
 * Returns the project root path, or null if not found.
 */
export function findProjectRoot(startDir) {
    let current = startDir ?? process.cwd();
    const root = path.parse(current).root;
    let limit = 0;
    while (current !== root && limit < 100) {
        if (fs.existsSync(path.join(current, '.specweave', 'config.json'))) {
            return current;
        }
        current = path.dirname(current);
        limit++;
    }
    return null;
}
/**
 * Build a HookContext from a project root path.
 */
export function createContext(projectRoot) {
    return {
        projectRoot,
        stateDir: path.join(projectRoot, '.specweave', 'state'),
        logsDir: path.join(projectRoot, '.specweave', 'logs'),
        configPath: path.join(projectRoot, '.specweave', 'config.json'),
        timestamp: new Date().toISOString(),
    };
}
/**
 * Parse a raw stdin string as JSON. Returns empty object on failure.
 */
export function parseStdinJson(raw) {
    if (!raw || !raw.trim())
        return {};
    try {
        return JSON.parse(raw);
    }
    catch {
        return {};
    }
}
/**
 * Append a log entry to `.specweave/logs/hooks.log` (plaintext, backwards compat)
 * and to `.specweave/logs/hooks/{handler}.log` (structured JSONL via HookLogger).
 * Never throws — logging failures are silently ignored.
 */
export function logHook(context, handler, message) {
    try {
        // Plaintext log (backwards compat)
        fs.mkdirSync(context.logsDir, { recursive: true });
        const entry = `[${context.timestamp}] ${handler}: ${message}\n`;
        fs.appendFileSync(path.join(context.logsDir, 'hooks.log'), entry);
        // Structured JSONL dual-write
        const hooksLogDir = path.join(context.logsDir, 'hooks');
        const logger = new HookLogger(hooksLogDir);
        const isError = message.startsWith('[ERROR]');
        const isBlock = message.includes('block') || message.includes('GUARD');
        logger.log({
            hookName: handler,
            status: isError ? 'error' : isBlock ? 'warning' : 'success',
            error: isError || isBlock ? message : undefined,
        }).catch(() => { });
    }
    catch {
        // Never throw from logging
    }
}
//# sourceMappingURL=utils.js.map