import { error, info, success } from '../../../shared/ui/index.js';
import { getErrorMessage } from '../../../shared/utils/index.js';
export function buildTaskResult(params) {
    const { task, runResult, startedAt, completedAt, branch, worktreePath, prUrl } = params;
    const taskSuccess = runResult.success;
    if (!taskSuccess && !runResult.reason) {
        throw new Error('Task failed without reason');
    }
    return {
        task,
        success: taskSuccess,
        response: taskSuccess ? 'Task completed successfully' : runResult.reason,
        executionLog: runResult.lastMessage ? [runResult.lastMessage] : [],
        failureStep: runResult.lastStep,
        failureLastMessage: runResult.lastMessage,
        startedAt,
        completedAt,
        ...(branch ? { branch } : {}),
        ...(worktreePath ? { worktreePath } : {}),
        ...(prUrl ? { prUrl } : {}),
    };
}
export function buildBooleanTaskResult(params) {
    const { task, taskSuccess, startedAt, completedAt, successResponse, failureResponse, branch, worktreePath, } = params;
    return {
        task,
        success: taskSuccess,
        response: taskSuccess ? successResponse : failureResponse,
        executionLog: [],
        startedAt,
        completedAt,
        ...(branch ? { branch } : {}),
        ...(worktreePath ? { worktreePath } : {}),
    };
}
export function persistPrFailedTaskResult(taskRunner, taskResult, prError) {
    taskRunner.prFailTask(taskResult, prError);
    info(`Task "${taskResult.task.name}" completed (PR creation failed)`);
}
export function persistTaskResult(taskRunner, taskResult, options) {
    const emitStatusLog = options?.emitStatusLog !== false;
    if (taskResult.success) {
        taskRunner.completeTask(taskResult);
        if (emitStatusLog) {
            success(`Task "${taskResult.task.name}" completed`);
        }
        return;
    }
    taskRunner.failTask(taskResult);
    if (emitStatusLog) {
        error(`Task "${taskResult.task.name}" failed`);
    }
}
export function persistExceededTaskResult(taskRunner, task, exceeded, context) {
    taskRunner.exceedTask(task.name, {
        currentStep: exceeded.currentStep,
        newMaxSteps: exceeded.newMaxSteps,
        currentIteration: exceeded.currentIteration,
        ...(exceeded.resumePoint ? { resumePoint: exceeded.resumePoint } : {}),
        ...(context?.worktreePath ? { worktreePath: context.worktreePath } : {}),
        ...(context?.branch ? { branch: context.branch } : {}),
    });
    info(`Task "${task.name}" exceeded iteration limit at step "${exceeded.currentStep}"`);
}
export function persistTaskError(taskRunner, task, startedAt, completedAt, err, options) {
    const emitStatusLog = options?.emitStatusLog !== false;
    const responsePrefix = options?.responsePrefix ?? '';
    taskRunner.failTask({
        task,
        success: false,
        response: `${responsePrefix}${getErrorMessage(err)}`,
        executionLog: [],
        startedAt,
        completedAt,
        ...(task.data?.branch ? { branch: task.data.branch } : {}),
        ...(task.worktreePath ? { worktreePath: task.worktreePath } : {}),
    });
    if (emitStatusLog) {
        error(`Task "${task.name}" error: ${getErrorMessage(err)}`);
    }
}
//# sourceMappingURL=taskResultHandler.js.map