/**
 * Labels aggregation core module.
 * @task T4783
 */
import { ExitCode } from '@cleocode/contracts';
import { CleoError } from '../errors.js';
import { getAccessor } from '../store/data-accessor.js';
/** Get all labels with counts and task IDs per label. */
export async function getLabels(cwd, accessor) {
    const dataAccessor = accessor ?? (await getAccessor(cwd));
    const result = await dataAccessor.queryTasks({});
    if (!result) {
        throw new CleoError(ExitCode.CONFIG_ERROR, 'No task data found');
    }
    const tasks = result.tasks ?? [];
    const labelMap = {};
    let taggedCount = 0;
    for (const t of tasks) {
        const taskLabels = t.labels ?? [];
        if (taskLabels.length > 0)
            taggedCount++;
        for (const label of taskLabels) {
            if (!labelMap[label])
                labelMap[label] = [];
            labelMap[label].push(t.id);
        }
    }
    const labels = Object.entries(labelMap)
        .map(([label, taskIds]) => ({ label, count: taskIds.length, tasks: taskIds }))
        .sort((a, b) => b.count - a.count);
    return {
        labels,
        totalLabels: labels.length,
        totalTagged: taggedCount,
        totalUntagged: tasks.length - taggedCount,
    };
}
//# sourceMappingURL=labels.js.map