/**
 * Capability probe for the optional `agent-reviews` posting flow (see design.md
 * → Error Handling → Capability probe).
 *
 * The provide-remote skills prefer `agent-reviews` for tooling symmetry IF it
 * exposes a "post / submit a full PR review" flow. The probe runs a
 * non-mutating `agent-reviews --help` once, parses the help text for such a
 * flag, and caches the result for the run. `gh api` is always the safe
 * fallback — the probe never fails the skill.
 *
 * Empirical current-state finding (probed 2026-05-29, `agent-reviews@1.0.2`):
 * the CLI exposes list/detail/watch commands plus `--reply <id>` for replying
 * to existing comments, but NO command or flag that posts a full PR review. So
 * the probe returns `not-supported` today and the skill posts via `gh api`.
 * The probe is forward-compatible: when `agent-reviews` gains a posting flow,
 * this parser recognizes it without a code change to the skill.
 */
/**
 * Candidate flag tokens that, if present in the help text, indicate a
 * full-review posting flow. Reply-to-comment flags (`--reply`) are deliberately
 * excluded — replying to an existing comment is not posting a review.
 *
 * The intent-specific tokens (`--post-review`, `--submit-review`) come first;
 * the generic `--review`/`--post` are kept as broad fallbacks. The generic
 * tokens carry a small forward-compat false-positive risk: a future
 * `agent-reviews` could add `--review` with an unrelated meaning (e.g.
 * "show a review"), which the probe would report as `supported`. That risk is
 * accepted here because `gh api` remains the safe posting path even when the
 * probe mis-detects an `agent-reviews` posting flow — a false positive degrades
 * to the same fallback as `unknown`, it never drops or corrupts a review.
 */
const POSTING_FLAG_CANDIDATES = [
    '--post-review',
    '--submit-review',
    '--review',
    '--post',
];
/** Extract the first posting flag present in the help text, or `undefined`. */
function findPostingFlag(helpText) {
    for (const flag of POSTING_FLAG_CANDIDATES) {
        // Word-boundary-ish match: the flag followed by whitespace, `=`, or EOL, so
        // `--post` does not spuriously match `--post-review` (and vice versa) and a
        // substring of an unrelated token never matches.
        const pattern = new RegExp(`(?:^|\\s)${escapeRegExp(flag)}(?=$|[\\s=])`, 'm');
        if (pattern.test(helpText)) {
            return flag;
        }
    }
    return undefined;
}
function escapeRegExp(value) {
    return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
/**
 * Probe whether `agent-reviews` supports posting a full PR review.
 *
 * @param probe injectable `agent-reviews --help` runner.
 * @param cache optional per-run cache; when provided, the probe runs at most
 *   once and subsequent calls return the cached result.
 */
export async function probeAgentReviewsPosting(probe, cache) {
    if (cache?.result !== undefined) {
        return cache.result;
    }
    let outcome;
    try {
        outcome = await probe();
    }
    catch {
        // A rejecting probe is treated the same as an errored one: unknown.
        outcome = { ok: false, stdout: '' };
    }
    let result;
    if (!outcome.ok) {
        result = { posting: 'unknown' };
    }
    else {
        const flag = findPostingFlag(outcome.stdout);
        result = flag
            ? { posting: 'supported', flag }
            : { posting: 'not-supported' };
    }
    if (cache) {
        cache.result = result;
    }
    return result;
}
