"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.selectPostCompletionUserComments = selectPostCompletionUserComments;
const managed_comments_1 = require("../comments/managed-comments");
const comment_command_1 = require("../workflows/comment-command");
const SYSTEM_COMMENT_AUTHORS = new Set([
    "alpaca",
    "github-actions",
    "gitlab-bot",
    "gitea",
    "system",
]);
function selectPostCompletionUserComments(input) {
    const completedAtMs = Date.parse(input.completedAt);
    if (Number.isNaN(completedAtMs)) {
        return [];
    }
    return input.comments.filter((comment) => {
        const createdAtMs = Date.parse(comment.createdAt);
        return (!Number.isNaN(createdAtMs) &&
            createdAtMs > completedAtMs &&
            isPostCompletionUserComment(comment));
    });
}
function isPostCompletionUserComment(comment) {
    return (comment.body.trim().length > 0 &&
        !isBotOrSystemComment(comment) &&
        !(0, managed_comments_1.isManagedComment)(comment) &&
        !isWorkflowCommandOnlyComment(comment.body));
}
function isBotOrSystemComment(comment) {
    const normalized = comment.author.trim().toLocaleLowerCase();
    return (comment.isBot === true ||
        comment.isSystem === true ||
        normalized.length === 0 ||
        SYSTEM_COMMENT_AUTHORS.has(normalized) ||
        normalized.endsWith("[bot]"));
}
function isWorkflowCommandOnlyComment(body) {
    const lines = body.split(/\r?\n/u);
    const firstNonEmptyLineIndex = lines.findIndex((line) => line.trim().length > 0);
    if (firstNonEmptyLineIndex === -1) {
        return false;
    }
    const firstLine = lines[firstNonEmptyLineIndex];
    if (firstLine === undefined ||
        (0, comment_command_1.parseWorkflowCommandFirstLine)(firstLine).kind === "none") {
        return false;
    }
    return lines
        .slice(firstNonEmptyLineIndex + 1)
        .every((line) => line.trim().length === 0);
}
