// engine/github/spec-linker.ts — Link specs to PRs and issues automatically.
const SPEC_PATTERN = /SPEC-(\d+)/gi;
/**
 * Extract spec IDs from a string (title, body, branch name).
 */
export function extractSpecIds(text) {
    const matches = text.matchAll(SPEC_PATTERN);
    const ids = new Set();
    for (const match of matches) {
        const num = match[1];
        if (num) {
            ids.add(`SPEC-${num}`);
        }
    }
    return [...ids];
}
/**
 * Find PRs linked to a given spec ID by checking branch names and titles.
 */
export async function findLinkedPRs(client, specId) {
    const allPRs = await client.getPRStatus();
    const specNum = specId.replace(/^SPEC-/i, '');
    const pattern = new RegExp(`SPEC-${specNum}`, 'i');
    return allPRs.filter((pr) => {
        if (pattern.test(pr.branch)) {
            return true;
        }
        if (pattern.test(pr.title)) {
            return true;
        }
        return false;
    });
}
/**
 * Given a PR, identify which spec(s) it is implementing.
 */
export function identifySpecsFromPR(pr) {
    const fromBranch = extractSpecIds(pr.branch);
    const fromTitle = extractSpecIds(pr.title);
    const combined = new Set([...fromBranch, ...fromTitle]);
    return [...combined];
}
/**
 * Build a SpecPRLink from a spec ID and associated PRs/issues.
 */
export function buildSpecLink(specId, prs, issueNumbers = []) {
    return {
        specId,
        prNumbers: prs.map((pr) => pr.number),
        issueNumbers,
        lastUpdated: new Date().toISOString(),
    };
}
//# sourceMappingURL=spec-linker.js.map