/**
 * Spawn readiness validation.
 * @task T4784
 */
import { getAccessor } from '../store/data-accessor.js';
/** Validate spawn readiness for a task. */
export async function validateSpawnReadiness(taskId, cwd, accessor) {
    const acc = accessor ?? (await getAccessor(cwd));
    const task = await acc.loadSingleTask(taskId);
    if (!task) {
        return {
            taskId,
            title: '',
            ready: false,
            issues: [{ code: 'V_NOT_FOUND', message: `Task ${taskId} not found`, severity: 'error' }],
        };
    }
    const issues = [];
    if (task.status === 'done') {
        issues.push({
            code: 'V_ALREADY_DONE',
            message: 'Task is already completed',
            severity: 'error',
        });
    }
    if (task.status === 'cancelled') {
        issues.push({ code: 'V_CANCELLED', message: 'Task is cancelled', severity: 'error' });
    }
    if (task.depends) {
        const depTasks = await acc.loadTasks(task.depends);
        const depMap = new Map(depTasks.map((t) => [t.id, t]));
        for (const dep of task.depends) {
            const depTask = depMap.get(dep);
            if (!depTask) {
                issues.push({
                    code: 'V_MISSING_DEP',
                    message: `Dependency ${dep} not found`,
                    severity: 'error',
                });
            }
            else if (depTask.status !== 'done') {
                issues.push({
                    code: 'V_UNMET_DEP',
                    message: `Dependency ${dep} (${depTask.title}) is not complete (status: ${depTask.status})`,
                    severity: 'error',
                });
            }
        }
    }
    if (!task.title) {
        issues.push({ code: 'V_MISSING_TITLE', message: 'Task title is missing', severity: 'error' });
    }
    if (!task.description) {
        issues.push({
            code: 'V_MISSING_DESC',
            message: 'Task description is missing',
            severity: 'error',
        });
    }
    return {
        taskId,
        title: task.title,
        ready: issues.filter((i) => i.severity === 'error').length === 0,
        issues,
    };
}
//# sourceMappingURL=validate-spawn.js.map