"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveLinkedIssueIdFromPullRequest = resolveLinkedIssueIdFromPullRequest;
exports.resolveLinkedIssueIdFromPullRequestText = resolveLinkedIssueIdFromPullRequestText;
const ISSUE_REF_PATTERN = /(?:\[\s*)?#([A-Za-z0-9][A-Za-z0-9._-]*)(?:\s*\]\([^)]+\))?/u;
const BODY_REFS_PATTERN = new RegExp(String.raw `\bRefs?\b\s*:?\s*${ISSUE_REF_PATTERN.source}`, "iu");
const RELATED_ISSUE_PATTERN = new RegExp(String.raw `\bRelated\s+issue\b[\s*:_-]*${ISSUE_REF_PATTERN.source}`, "iu");
function resolveLinkedIssueIdFromPullRequest(issue, comments = []) {
    const bodyIssueId = resolveLinkedIssueIdFromPullRequestText(issue.body);
    if (bodyIssueId !== null) {
        return bodyIssueId;
    }
    for (const comment of comments) {
        const commentIssueId = resolveLinkedIssueIdFromPullRequestText(comment.body);
        if (commentIssueId !== null) {
            return commentIssueId;
        }
    }
    return null;
}
function resolveLinkedIssueIdFromPullRequestText(text) {
    const refsMatch = BODY_REFS_PATTERN.exec(text);
    if (refsMatch?.[1]) {
        return refsMatch[1];
    }
    const relatedIssueMatch = RELATED_ISSUE_PATTERN.exec(text);
    return relatedIssueMatch?.[1] ?? null;
}
