import { parseHookIngestRequest } from "../core/api-validation.js";
import { loadWorkspaceContextById } from "../state/workspace-state.js";
function canTransitionTaskForHookEvent(summary, event) {
    if (event === "activity") {
        return false;
    }
    if (event === "to_review") {
        return summary.state === "running";
    }
    return (summary.state === "awaiting_review" && (summary.reviewReason === "attention" || summary.reviewReason === "hook"));
}
export function createHooksApi(deps) {
    return {
        ingest: async (input) => {
            try {
                const body = parseHookIngestRequest(input);
                const taskId = body.taskId;
                const workspaceId = body.workspaceId;
                const event = body.event;
                const knownWorkspacePath = deps.getWorkspacePathById(workspaceId);
                const workspaceContext = knownWorkspacePath ? null : await loadWorkspaceContextById(workspaceId);
                const workspacePath = knownWorkspacePath ?? workspaceContext?.repoPath ?? null;
                if (!workspacePath) {
                    return {
                        ok: false,
                        error: `Workspace "${workspaceId}" not found`,
                    };
                }
                const manager = await deps.ensureTerminalManagerForWorkspace(workspaceId, workspacePath);
                const summary = manager.getSummary(taskId);
                if (!summary) {
                    return {
                        ok: false,
                        error: `Task "${taskId}" not found in workspace "${workspaceId}"`,
                    };
                }
                if (!canTransitionTaskForHookEvent(summary, event)) {
                    if (body.metadata) {
                        manager.applyHookActivity(taskId, body.metadata);
                    }
                    return {
                        ok: true,
                    };
                }
                const transitionedSummary = event === "to_review" ? manager.transitionToReview(taskId, "hook") : manager.transitionToRunning(taskId);
                if (!transitionedSummary) {
                    return {
                        ok: false,
                        error: `Task "${taskId}" transition failed`,
                    };
                }
                if (body.metadata) {
                    manager.applyHookActivity(taskId, body.metadata);
                }
                void deps.broadcastRuntimeWorkspaceStateUpdated(workspaceId, workspacePath);
                if (event === "to_review") {
                    deps.broadcastTaskReadyForReview(workspaceId, taskId);
                }
                return { ok: true };
            }
            catch (error) {
                const message = error instanceof Error ? error.message : String(error);
                return { ok: false, error: message };
            }
        },
    };
}
//# sourceMappingURL=hooks-api.js.map