"use strict";
/**
 * Repo visibility detection — probes `gh repo view --json visibility`
 * to learn whether the current repo is public, private, or internal
 * (GitHub Enterprise's middle tier).
 *
 * # Why this module exists
 *
 * Baseline mode resolution (see `./modes.ts`) needs the answer to
 * "is this a public repo?" to pick the right default posture.
 * Public repos default to `ref-based` (no committed baseline,
 * zero disclosure); private repos default to `committed-full`.
 * Without a reliable visibility probe the picker can't be safe-
 * by-default.
 *
 * # Failure semantics
 *
 * Every failure path returns `'unknown'` rather than throwing.
 * Callers treat unknown the same way they treat private — the
 * safe default. Concrete failure modes:
 *
 *   - `gh` binary missing
 *   - `gh auth` not configured
 *   - Repo has no GitHub remote
 *   - Repo is on a non-GitHub host (GitLab, self-hosted)
 *   - Network failure / API throttling
 *   - Repo deleted or made inaccessible to the calling user
 *
 * None of these warrant a surprise switch to sanitized mode — a
 * customer's private repo shouldn't suddenly start writing
 * stripped baselines because `gh auth` lapsed.
 *
 * # Caching
 *
 * The probe is slow (~500ms cold). Results are cached per-process
 * by absolute cwd. Tests clear the cache via `clearVisibilityCache`.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.detectRepoVisibility = detectRepoVisibility;
exports.clearVisibilityCache = clearVisibilityCache;
const child_process_1 = require("child_process");
const path = __importStar(require("path"));
const VISIBILITY_CACHE = new Map();
/**
 * Detect the visibility of the repo rooted at `cwd`. Returns
 * `'unknown'` on every failure path — never throws. Cached per
 * absolute cwd for the lifetime of the process.
 *
 * Production callers always use this through `resolveBaselineMode`;
 * direct invocations should be rare. The single-entry contract keeps
 * the `gh` probe count predictable + makes mocking trivial in tests.
 */
function detectRepoVisibility(cwd) {
    const cacheKey = path.resolve(cwd);
    const cached = VISIBILITY_CACHE.get(cacheKey);
    if (cached !== undefined)
        return cached;
    const resolved = detectRepoVisibilityUncached(cwd);
    VISIBILITY_CACHE.set(cacheKey, resolved);
    return resolved;
}
function detectRepoVisibilityUncached(cwd) {
    try {
        const out = (0, child_process_1.execSync)('gh repo view --json visibility', {
            cwd,
            stdio: ['ignore', 'pipe', 'pipe'],
            encoding: 'utf-8',
            timeout: 5000,
        });
        const parsed = JSON.parse(out);
        const raw = typeof parsed.visibility === 'string' ? parsed.visibility.toLowerCase() : '';
        if (raw === 'public' || raw === 'private' || raw === 'internal')
            return raw;
        return 'unknown';
    }
    catch {
        return 'unknown';
    }
}
/**
 * Test seam: clear the per-process visibility cache. Production
 * callers never use this — the cache lives for the entire CLI
 * invocation and dies with the process.
 */
function clearVisibilityCache() {
    VISIBILITY_CACHE.clear();
}
//# sourceMappingURL=visibility.js.map