function generateActionId() {
    return `action-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
}
function validateAction(action) {
    if (action.type === "THREAD_RESOLVE") {
        if (!action.threadId) {
            return "threadId required for THREAD_RESOLVE";
        }
    }
    else if (action.type === "THREAD_REPLY") {
        if (!action.threadId) {
            return "threadId required for THREAD_REPLY";
        }
        if (!action.message) {
            return "message required for THREAD_REPLY";
        }
    }
    else if (action.type === "POST_COMMENT") {
        if (!action.body) {
            return "body required for POST_COMMENT";
        }
    }
    else if (action.type === "POST_INLINE_COMMENT") {
        if (!action.filePath) {
            return "filePath required for POST_INLINE_COMMENT";
        }
        if (!action.line || action.line <= 0) {
            return "line must be > 0 for POST_INLINE_COMMENT";
        }
        if (!action.body) {
            return "body required for POST_INLINE_COMMENT";
        }
    }
    return null;
}
function toReviewContextAction(action) {
    if (action.type === "THREAD_RESOLVE") {
        return {
            type: "THREAD_RESOLVE",
            threadId: action.threadId,
            message: action.message,
        };
    }
    if (action.type === "THREAD_REPLY") {
        return {
            type: "THREAD_REPLY",
            threadId: action.threadId,
            message: action.message,
        };
    }
    if (action.type === "POST_INLINE_COMMENT") {
        return {
            type: "POST_INLINE_COMMENT",
            filePath: action.filePath,
            line: action.line,
            body: action.body,
        };
    }
    return {
        type: "POST_COMMENT",
        body: action.body,
    };
}
export function addAction(jobId, action, deps) {
    const { jobContextGateway, reviewContextGateway } = deps;
    const validationError = validateAction(action);
    if (validationError) {
        return {
            success: false,
            error: validationError,
        };
    }
    const jobContext = jobContextGateway.get(jobId);
    if (!jobContext) {
        return {
            success: false,
            error: `Job context not found: ${jobId}`,
        };
    }
    const reviewContextAction = toReviewContextAction(action);
    const result = reviewContextGateway.appendAction(jobContext.localPath, jobContext.mergeRequestId, reviewContextAction);
    if (!result.success) {
        return {
            success: false,
            error: "Failed to append action to review context",
        };
    }
    return {
        success: true,
        actionId: generateActionId(),
        actionType: action.type,
    };
}
//# sourceMappingURL=addAction.usecase.js.map