/**
 * Task listing with filters.
 * @task T4460
 * @epic T4454
 */
import { taskListItemNext } from '../mvi-helpers.js';
import { paginate } from '../pagination.js';
const TASK_LIST_DEFAULT_LIMIT = 10;
/** Convert a full Task to compact representation with _next directives. */
export function toCompact(task) {
    return {
        id: task.id,
        title: task.title,
        status: task.status,
        priority: task.priority,
        type: task.type,
        parentId: task.parentId,
        _next: taskListItemNext(task.id),
    };
}
/**
 * List tasks with optional filtering and pagination.
 * @task T4460
 */
export async function listTasks(options = {}, cwd, accessor) {
    const dataAccessor = accessor ?? (await (await import('../store/data-accessor.js')).getAccessor(cwd));
    // Build targeted query filters
    const queryFilters = {
        orderBy: 'position',
    };
    if (options.status)
        queryFilters.status = options.status;
    if (options.priority)
        queryFilters.priority = options.priority;
    if (options.type)
        queryFilters.type = options.type;
    if (options.parentId)
        queryFilters.parentId = options.parentId;
    if (options.phase)
        queryFilters.phase = options.phase;
    if (options.label)
        queryFilters.label = options.label;
    const queryResult = await dataAccessor.queryTasks(queryFilters);
    const filtered = queryResult.tasks;
    const filteredCount = queryResult.total;
    // Get total count of all tasks (unfiltered) for the response
    const total = await dataAccessor.countTasks();
    const limit = options.limit === 0
        ? undefined
        : typeof options.limit === 'number' && options.limit > 0
            ? options.limit
            : TASK_LIST_DEFAULT_LIMIT;
    const offset = typeof options.offset === 'number' && options.offset > 0 ? options.offset : undefined;
    const { items: tasks, page } = paginate(filtered, limit, offset);
    const pagination = page.mode === 'offset'
        ? {
            limit: page.limit,
            offset: page.offset,
            hasMore: page.hasMore,
        }
        : undefined;
    // Enrich each task with _next progressive disclosure directives
    const enrichedTasks = tasks.map((t) => ({
        ...t,
        _next: taskListItemNext(t.id),
    }));
    return {
        tasks: enrichedTasks,
        total,
        filtered: filteredCount,
        page,
        pagination,
    };
}
//# sourceMappingURL=list.js.map