/**
 * Hooks detector — names of hook scripts materialized in the version home
 * whose contents match the central source. Mirrors versions.ts:391-421.
 */
import * as fs from 'fs';
import { agentConfigDirName } from '../../agents.js';
import * as path from 'path';
import { capableAgents } from '../../capabilities.js';
import { resolveHookSource } from '../writers/sources.js';
import { lazyAgentMap } from '../writers/lazy-map.js';
function hookSourcesMatch(src, dest) {
    const srcStat = fs.lstatSync(src);
    if (srcStat.isSymbolicLink())
        return false;
    if (srcStat.isDirectory()) {
        if (!fs.existsSync(dest) || !fs.lstatSync(dest).isDirectory())
            return false;
        const entries = fs.readdirSync(src, { withFileTypes: true });
        for (const entry of entries) {
            if (entry.isSymbolicLink())
                continue;
            const srcPath = path.join(src, entry.name);
            const destPath = path.join(dest, entry.name);
            if (!hookSourcesMatch(srcPath, destPath))
                return false;
        }
        return true;
    }
    if (!srcStat.isFile())
        return false;
    if (!fs.existsSync(dest) || !fs.lstatSync(dest).isFile())
        return false;
    return fs.readFileSync(src, 'utf-8') === fs.readFileSync(dest, 'utf-8');
}
function buildHooksDetector(agent) {
    return {
        kind: 'hooks',
        agent,
        list({ versionHome }) {
            const hooksDir = path.join(versionHome, agentConfigDirName(agent), 'hooks');
            if (!fs.existsSync(hooksDir))
                return [];
            const installed = fs.readdirSync(hooksDir).filter(f => !f.startsWith('.'));
            const synced = [];
            for (const hook of installed) {
                const src = resolveHookSource(hook);
                if (!src) {
                    // True orphan — count as accounted for.
                    synced.push(hook);
                    continue;
                }
                try {
                    if (hookSourcesMatch(src, path.join(hooksDir, hook))) {
                        synced.push(hook);
                    }
                }
                catch { /* read failure → not synced */ }
            }
            return synced;
        },
    };
}
export const hooksDetectors = lazyAgentMap(() => {
    const m = {};
    for (const agent of capableAgents('hooks'))
        m[agent] = buildHooksDetector(agent);
    return m;
});
