/**
 * Force Stop Workflow Step Use Case
 *
 * Manually flips a stuck `pending` or `running` workflow step to
 * `interrupted`. Used when a daemon restart or lost SSE update left a
 * step displayed as in-progress even though the underlying agent turn
 * finished (or was never started after recovery). Reusing the
 * `interrupted` status keeps the existing "Continue" retry flow in the
 * UI just working.
 *
 * This use case intentionally does NOT kill any agent process — the
 * process lifecycle is owned by `StopAgentRunUseCase`. It only
 * mutates the persisted step row and notifies SSE subscribers so the
 * tracker updates live.
 */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import { injectable, inject } from 'tsyringe';
import { WorkflowStepStatus } from '../../../domain/generated/output.js';
const FORCE_STOP_ERROR_MESSAGE = 'Force-stopped by user';
let ForceStopWorkflowStepUseCase = class ForceStopWorkflowStepUseCase {
    stepRepo;
    session;
    constructor(stepRepo, session) {
        this.stepRepo = stepRepo;
        this.session = session;
    }
    async execute(input) {
        const step = await this.stepRepo.findById(input.stepId);
        if (!step) {
            return { stopped: false, reason: `Workflow step ${input.stepId} not found` };
        }
        const isActive = step.status === WorkflowStepStatus.running || step.status === WorkflowStepStatus.pending;
        if (!isActive) {
            return {
                stopped: false,
                reason: `Workflow step already in terminal state: ${step.status}`,
            };
        }
        await this.stepRepo.updateStatus(step.id, WorkflowStepStatus.interrupted, {
            error: FORCE_STOP_ERROR_MESSAGE,
        });
        const refreshed = await this.stepRepo.findById(step.id);
        if (refreshed) {
            this.session.notifyWorkflowStep(step.featureId, refreshed);
        }
        return { stopped: true, reason: 'Marked as interrupted' };
    }
};
ForceStopWorkflowStepUseCase = __decorate([
    injectable(),
    __param(0, inject('IWorkflowStepRepository')),
    __param(1, inject('IInteractiveSessionService')),
    __metadata("design:paramtypes", [Object, Object])
], ForceStopWorkflowStepUseCase);
export { ForceStopWorkflowStepUseCase };
