import { nowIso } from './naming.js';
export class TaskExceedService {
    store;
    constructor(store) {
        this.store = store;
    }
    exceedTask(taskName, options) {
        this.store.update((current) => {
            const index = current.tasks.findIndex((task) => task.name === taskName && task.status === 'running');
            if (index === -1) {
                throw new Error(`Task not found: ${taskName} (running)`);
            }
            const target = current.tasks[index];
            const updated = {
                ...target,
                status: 'exceeded',
                completed_at: nowIso(),
                owner_pid: null,
                failure: undefined,
                start_step: options.currentStep,
                exceeded_max_steps: options.newMaxSteps,
                exceeded_current_iteration: options.currentIteration,
                resume_point: options.resumePoint,
                ...(options.worktreePath ? { worktree_path: options.worktreePath } : {}),
                ...(options.branch ? { branch: options.branch } : {}),
            };
            const tasks = [...current.tasks];
            tasks[index] = updated;
            return { tasks };
        });
    }
    requeueExceededTask(taskName) {
        this.store.update((current) => {
            const index = current.tasks.findIndex((task) => task.name === taskName && task.status === 'exceeded');
            if (index === -1) {
                throw new Error(`Task not found: ${taskName} (exceeded)`);
            }
            const target = current.tasks[index];
            const updated = {
                ...target,
                status: 'pending',
                started_at: null,
                completed_at: null,
                owner_pid: null,
                failure: undefined,
            };
            const tasks = [...current.tasks];
            tasks[index] = updated;
            return { tasks };
        });
    }
}
//# sourceMappingURL=taskExceedService.js.map