import path from 'node:path';
import { readJson, readText, writeTextAtomic } from '../fsx.js';
import { CODEX_HOOK_EVENTS } from '../codex-compat/codex-hook-events.js';
import { codexCommandHookCurrentHash, codexHookStateKey } from './codex-hook-hash.js';
export async function writeTrustedHashStateForHooksFile(root, hooksPath = path.join(root, '.codex', 'hooks.json'), statePath = path.join(root, '.codex', 'config.toml'), opts = {}) {
    if (opts.allowSksHashFallback !== true) {
        return {
            schema: 'sks.codex-hook-state-writer.v2',
            ok: false,
            hooks_path: hooksPath,
            state_path: statePath,
            updated: 0,
            blocks: [],
            blocked: true,
            blocker: 'official_codex_hook_hash_unavailable',
            policy: 'use_sks_hooks_install_managed_instead_of_writing_sks_only_trusted_hashes',
            repair_action: 'sks hooks install --managed --json',
            reason: opts.reason || 'SKS refuses to write trusted_hash values from its own canonicalJson hash unless official Codex hash parity is proven.'
        };
    }
    const hooksFile = await readJson(hooksPath, {});
    const blocks = trustedHashBlocksForHooksFile(hooksPath, hooksFile);
    const next = upsertTrustBlocks(await readText(statePath, ''), blocks);
    await writeTextAtomic(statePath, next);
    return {
        schema: 'sks.codex-hook-state-writer.v1',
        ok: true,
        hooks_path: hooksPath,
        state_path: statePath,
        updated: blocks.length,
        blocks
    };
}
export function trustedHashBlocksForHooksFile(hooksPath, hooksFile) {
    const blocks = [];
    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 key = codexHookStateKey(hooksPath, event, groupIndex, handlerIndex);
                const trustedHash = codexCommandHookCurrentHash({
                    event,
                    matcher: typeof group.matcher === 'string' ? group.matcher : null,
                    command: String(handler.command || ''),
                    timeout: Number(handler.timeout || 600),
                    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
                });
                blocks.push({
                    key,
                    trusted_hash: trustedHash,
                    block: `[hooks.state."${tomlQuotedKey(key)}"]\ntrusted_hash = "${trustedHash}"`
                });
            });
        });
    }
    return blocks;
}
export function upsertTrustBlocks(existing, blocks) {
    let next = String(existing || '').trimEnd();
    for (const block of blocks) {
        next = upsertTomlTable(next, `hooks.state."${tomlQuotedKey(block.key)}"`, block.block);
    }
    return `${next.trim()}\n`;
}
function upsertTomlTable(text, table, block) {
    let lines = String(text || '').trimEnd().split('\n');
    if (lines.length === 1 && lines[0] === '')
        lines = [];
    const header = `[${table}]`;
    const start = lines.findIndex((line) => line.trim() === header);
    const blockLines = String(block || '').trim().split('\n');
    if (start === -1)
        return [...lines, ...(lines.length ? [''] : []), ...blockLines].join('\n').replace(/\n{3,}/g, '\n\n');
    let end = lines.length;
    for (let i = start + 1; i < lines.length; i += 1) {
        if (/^\s*\[.+\]\s*$/.test(lines[i] || '')) {
            end = i;
            break;
        }
    }
    lines.splice(start, end - start, ...blockLines);
    return lines.join('\n').replace(/\n{3,}/g, '\n\n');
}
function tomlQuotedKey(value) {
    return value.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
}
//# sourceMappingURL=codex-hook-state-writer.js.map