import path from 'node:path';
import os from 'node:os';
import { exists, readJson, readText } from '../fsx.js';
import { CODEX_HOOK_EVENTS, codexHookEventName } from '../codex-compat/codex-hook-events.js';
import { codexCommandHookCurrentHash, codexHookStateKey } from './codex-hook-hash.js';
export async function readCodexHookTrustEntries(root, opts = {}) {
    const candidates = [
        { source_path: path.join(root, '.codex', 'hooks.json'), source_kind: 'project', state_path: path.join(root, '.codex', 'config.toml') },
        { source_path: path.join(os.homedir(), '.codex', 'hooks.json'), source_kind: 'user', state_path: path.join(os.homedir(), '.codex', 'config.toml') }
    ];
    const entries = [];
    for (const candidate of candidates) {
        if (!(await exists(candidate.source_path)))
            continue;
        const hooks = await readJson(candidate.source_path, {});
        const stateText = await readText(candidate.state_path, '');
        const trustedHashes = parseTrustedHashes(String(stateText || ''));
        entries.push(...entriesFromHooksFile(candidate.source_path, candidate.source_kind, hooks, trustedHashes, opts.managed === true));
    }
    return entries;
}
export function entriesFromHooksFile(sourcePath, sourceKind, hooksFile, trustedHashes = {}, managed = false) {
    const entries = [];
    const hooksRoot = hooksFile?.hooks && typeof hooksFile.hooks === 'object' && !Array.isArray(hooksFile.hooks) ? hooksFile.hooks : {};
    for (const event of CODEX_HOOK_EVENTS) {
        const groups = Array.isArray(hooksRoot[event]) ? hooksRoot[event] : [];
        groups.forEach((group, groupIndex) => {
            const handlers = Array.isArray(group?.hooks) ? group.hooks : [];
            handlers.forEach((handler, handlerIndex) => {
                if (!handler || typeof handler !== 'object' || handler.type !== 'command')
                    return;
                const matcher = typeof group.matcher === 'string' ? group.matcher : null;
                const command = String(handler.command || '');
                const timeout = Math.max(1, Number(handler.timeout || 600));
                const currentHash = codexCommandHookCurrentHash({
                    event,
                    matcher,
                    command,
                    timeout,
                    async: handler.async === true,
                    statusMessage: typeof handler.statusMessage === 'string' ? handler.statusMessage : null,
                    commandWindows: typeof handler.commandWindows === 'string' ? handler.commandWindows : typeof handler.command_windows === 'string' ? handler.command_windows : null
                });
                const key = codexHookStateKey(sourcePath, event, groupIndex, handlerIndex);
                const trustedHash = trustedHashes[key] || null;
                const trustStatus = managed ? 'Managed' : trustedHash === currentHash ? 'Trusted' : trustedHash ? 'Modified' : 'Untrusted';
                entries.push({
                    source_path: sourcePath,
                    source_kind: sourceKind,
                    event,
                    group_index: groupIndex,
                    handler_index: handlerIndex,
                    matcher,
                    command,
                    timeout,
                    async: handler.async === true,
                    statusMessage: typeof handler.statusMessage === 'string' ? handler.statusMessage : null,
                    key,
                    current_hash: currentHash,
                    trusted_hash: trustedHash,
                    trust_status: trustStatus,
                    repair_action: trustStatus === 'Managed' || trustStatus === 'Trusted' ? null : `sks hooks trust-fix --json --key ${JSON.stringify(key)}`,
                    warnings: trustStatus === 'Managed' || trustStatus === 'Trusted' ? [] : [`${trustStatus} hook: ${key}`]
                });
            });
        });
    }
    return entries;
}
export function parseTrustedHashes(tomlText) {
    const hashes = {};
    const lines = String(tomlText || '').split(/\r?\n/);
    let key = null;
    for (const line of lines) {
        const header = line.match(/^\s*\[hooks\.state\."((?:\\"|[^"])*)"\]\s*$/);
        if (header?.[1]) {
            key = header[1].replace(/\\"/g, '"').replace(/\\\\/g, '\\');
            continue;
        }
        const hash = line.match(/^\s*trusted_hash\s*=\s*"([^"]+)"\s*$/);
        if (key && hash?.[1])
            hashes[key] = hash[1];
    }
    return hashes;
}
export function normalizeCodexHookEvent(value) {
    return codexHookEventName(value);
}
//# sourceMappingURL=codex-hook-trust-state.js.map