/**
 * Session Context Injection
 *
 * Reads protocol injection content for a given protocol type. Extracted from
 * engine-compat.ts during the memory domain cutover (T5241) to separate
 * session concerns from brain.db cognitive memory.
 *
 * @task T5241
 * @epic T5149
 */
import { existsSync, readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { ExitCode } from '@cleocode/contracts';
import { CleoError } from '../errors.js';
import { getProjectRoot } from '../paths.js';
/**
 * Resolve the project root, defaulting to getProjectRoot() if not specified.
 */
function resolveRoot(projectRoot) {
    return projectRoot || getProjectRoot();
}
/**
 * Read protocol injection content for a given protocol type.
 * Core logic for session.context.inject.
 */
export function injectContext(protocolType, params, projectRoot) {
    if (!protocolType) {
        throw new CleoError(ExitCode.INVALID_INPUT, 'protocolType is required');
    }
    const root = resolveRoot(projectRoot);
    const protocolLocations = [
        resolve(root, 'protocols', `${protocolType}.md`),
        resolve(root, 'skills', '_shared', `${protocolType}.md`),
        resolve(root, 'agents', 'cleo-subagent', 'protocols', `${protocolType}.md`),
    ];
    let protocolContent = null;
    let protocolPath = null;
    for (const loc of protocolLocations) {
        if (existsSync(loc)) {
            try {
                protocolContent = readFileSync(loc, 'utf-8');
                protocolPath = loc.replace(root + '/', '');
                break;
            }
            catch { }
        }
    }
    if (!protocolContent) {
        throw new CleoError(ExitCode.NOT_FOUND, `Protocol '${protocolType}' not found in src/protocols/, skills/_shared/, or agents/cleo-subagent/protocols/`);
    }
    return {
        protocolType,
        content: protocolContent,
        path: protocolPath,
        contentLength: protocolContent.length,
        estimatedTokens: Math.ceil(protocolContent.length / 4),
        taskId: params?.taskId || null,
        variant: params?.variant || null,
    };
}
//# sourceMappingURL=context-inject.js.map