const CANCEL_PATTERN = /aborted|cancel|annul/i;
const TIMEOUT_PATTERN = /timeout/i;
function mapStatus(jobStatus, abortSignalAborted) {
    if (jobStatus.status === 'completed' && !abortSignalAborted) {
        return 'success';
    }
    const error = jobStatus.error ?? '';
    if (abortSignalAborted || CANCEL_PATTERN.test(error)) {
        return 'killed';
    }
    if (TIMEOUT_PATTERN.test(error)) {
        return 'timeout';
    }
    return 'failed';
}
function toIso(date, fallback) {
    if (date) {
        return date.toISOString();
    }
    return fallback.toISOString();
}
function buildRecord(input) {
    const { jobStatus, abortSignalAborted, now } = input;
    const completedAtDate = jobStatus.completedAt ?? now();
    const startedAtDate = jobStatus.startedAt ?? completedAtDate;
    const durationMs = Math.max(0, completedAtDate.getTime() - startedAtDate.getTime());
    const status = mapStatus(jobStatus, abortSignalAborted);
    const exitReason = jobStatus.error && jobStatus.error.length > 0 ? jobStatus.error : null;
    return {
        jobId: jobStatus.job.id,
        platform: jobStatus.job.platform,
        projectPath: jobStatus.job.projectPath,
        mergeRequestId: jobStatus.job.mrNumber,
        jobType: jobStatus.job.jobType ?? 'review',
        startedAt: toIso(jobStatus.startedAt, completedAtDate),
        completedAt: toIso(jobStatus.completedAt, now()),
        durationMs,
        status,
        exitReason,
    };
}
export class PersistJobRecordUseCase {
    deps;
    constructor(deps) {
        this.deps = deps;
    }
    async execute(input) {
        const record = buildRecord(input);
        try {
            await this.deps.jobHistoryGateway.appendRecord(record);
        }
        catch (error) {
            const reason = error instanceof Error ? error.message : String(error);
            this.deps.logger.warn({ jobId: record.jobId, error: reason }, `Échec persistance job ${record.jobId} : ${reason}`);
        }
    }
}
//# sourceMappingURL=persistJobRecord.usecase.js.map