/**
 * Task injection core module.
 *
 * Selects and formats tasks for injection into external systems.
 *
 * ARCHITECTURE NOTE: Instruction injection is a CAAMP domain responsibility.
 * Once @cleocode/caamp is available as a dependency, the injection formatting
 * and template resolution should delegate to CAAMP's injection provider.
 * CLEO's role here is task selection and filtering (what to inject),
 * while CAAMP handles the injection format (how to inject).
 *
 * @task T4539
 * @epic T4454
 */
import { getAccessor } from '../store/data-accessor.js';
/**
 * Select tasks eligible for injection based on filters.
 * This is CLEO-specific task selection logic (stays in CLEO).
 * @task T4539
 */
function selectTasksForInjection(allTasks, opts) {
    const maxTasks = opts.maxTasks ?? 8;
    let tasks = allTasks.filter((t) => t.status !== 'done');
    // Filter by focused task
    if (opts.focusedOnly && opts.focusedTaskId) {
        tasks = tasks.filter((t) => t.id === opts.focusedTaskId);
    }
    // Filter by phase
    const phase = opts.phase ?? opts.currentPhase ?? undefined;
    if (phase) {
        const phaseTasks = tasks.filter((t) => t.phase === phase);
        if (phaseTasks.length > 0)
            tasks = phaseTasks;
    }
    // Sort: active first, then by priority
    const priorityOrder = { critical: 0, high: 1, medium: 2, low: 3 };
    tasks.sort((a, b) => {
        if (a.status === 'active' && b.status !== 'active')
            return -1;
        if (b.status === 'active' && a.status !== 'active')
            return 1;
        return ((priorityOrder[a.priority ?? 'medium'] ?? 2) - (priorityOrder[b.priority ?? 'medium'] ?? 2));
    });
    return tasks.slice(0, maxTasks);
}
/**
 * Format tasks for injection.
 * Format: [T###] [!]? [BLOCKED]? <title>
 * @task T4539
 */
function formatForInjection(tasks) {
    return tasks.map((t) => {
        let prefix = `[${t.id}]`;
        if (t.priority === 'critical' || t.priority === 'high')
            prefix += ' [!]';
        if (t.status === 'blocked')
            prefix += ' [BLOCKED]';
        return { id: t.id, text: `${prefix} ${t.title}`, status: t.status };
    });
}
/** Inject tasks for external consumption. */
export async function injectTasks(opts, accessor) {
    const acc = accessor ?? (await getAccessor(opts.cwd));
    const { tasks: allTasks } = await acc.queryTasks({});
    const projectMeta = await acc.getMetaValue('project');
    const focusMeta = await acc.getMetaValue('focus_state');
    const selectedTasks = selectTasksForInjection(allTasks, {
        ...opts,
        focusedTaskId: focusMeta?.currentTask ?? null,
        currentPhase: projectMeta?.currentPhase ?? null,
    });
    const formatted = formatForInjection(selectedTasks);
    const phase = opts.phase ?? projectMeta?.currentPhase ?? null;
    return {
        tasks: formatted,
        count: formatted.length,
        phase,
        focusedOnly: opts.focusedOnly ?? false,
        dryRun: opts.dryRun ?? false,
    };
}
//# sourceMappingURL=index.js.map