import { resolve } from 'node:path';
import { pathToFileURL } from 'node:url';
import { statSync } from 'node:fs';
import { logger } from '../../utils/logger.js';
import { summarizeError } from '../../utils/error-display.js';
/**
 * TypeScript hook runner.
 * Dynamically imports the module at hook.path and calls its default export with the event.
 */
export async function run(hook, event, projectRoot) {
    const path = hook.path;
    if (!path) {
        return { ok: false, error: 'ts hook missing "path" field' };
    }
    // Validate path is within the project directory to prevent arbitrary module loading
    const resolvedPath = resolve(projectRoot, path);
    if (!resolvedPath.startsWith(projectRoot + '/')) {
        return { ok: false, error: `ts hook path '${path}' is outside the project directory` };
    }
    try {
        let moduleUrl = pathToFileURL(resolvedPath).href;
        try {
            const { mtimeMs } = statSync(resolvedPath);
            moduleUrl += `?mtime=${mtimeMs}`;
        }
        catch {
            // Ignore stat failures and fall back to the bare file URL so import can surface the real error.
        }
        const mod = await import(moduleUrl);
        const handler = mod.default;
        if (typeof handler !== 'function') {
            return { ok: false, error: `ts hook at ${path} does not export a default function` };
        }
        const result = await handler(event);
        return result;
    }
    catch (err) {
        const message = summarizeError(err);
        logger.error('ts hook error', { path, error: message });
        return { ok: false, error: message };
    }
}
