/**
 * Creates a minimal hook context matching what oclif provides to hooks.
 */
export function createHookContext(options = {}) {
    const { logger } = options;
    return {
        debug(...args) {
            logger?.debug(args.map(String).join(' '));
        },
        log(...args) {
            logger?.info(args.map(String).join(' '));
        },
        warn(...args) {
            logger?.warn(args.map(String).join(' '));
        },
        error(...args) {
            logger?.error(args.map(String).join(' '));
        },
        config: options.config ?? {},
    };
}
/**
 * Dynamic import that survives esbuild CJS bundling.
 *
 * esbuild transforms `import()` to `require()` in CJS output, which cannot
 * load ESM plugins. Using `new Function` preserves the native dynamic import.
 */
// eslint-disable-next-line @typescript-eslint/no-implied-eval
const dynamicImport = new Function('specifier', 'return import(specifier)');
/**
 * Dynamically imports a hook file and invokes its default export.
 *
 * @param hookFilePath - Absolute path to the hook JS file
 * @param context - Hook context (`this` inside the hook)
 * @param hookOptions - Options passed as the first argument to the hook function
 * @param logger - Optional logger for warnings on failure
 * @returns The hook function's return value, or `undefined` on error
 */
export async function invokeHook(hookFilePath, context, hookOptions, logger) {
    try {
        const mod = await dynamicImport(hookFilePath);
        const hookFn = (mod.default ?? mod);
        if (typeof hookFn !== 'function') {
            logger?.warn(`Hook file ${hookFilePath} does not export a function`);
            return undefined;
        }
        return await hookFn.call(context, hookOptions);
    }
    catch (err) {
        const message = err instanceof Error ? err.message : String(err);
        logger?.warn(`Failed to invoke hook ${hookFilePath}: ${message}`);
        return undefined;
    }
}
//# sourceMappingURL=loader.js.map