"use strict";
/**
 * `vyuh-dxkit setup-prebuild` — configures GitHub Codespaces prebuilds
 * for the repo, so fresh Codespaces start from a prebuilt image
 * instead of re-running the full devcontainer build.
 *
 * Math: dxkit's polyglot devcontainer takes ~7 min cold-start (post per-
 * stack feature work in Sprint 1). Prebuilds drop this to ~30s by
 * maintaining the built image in GitHub's image cache. Storage cost
 * ~$3-5/month per region; for a 20-dev team averaging 5 fresh
 * Codespaces/week, the wall-clock savings dwarf the cost by 2-3 orders
 * of magnitude.
 *
 * Wraps `gh api -X POST repos/{owner}/{repo}/codespaces/prebuilds` with
 * a JSON payload defining branch + region + retention. Edge cases:
 *   - gh CLI missing → preflight fails
 *   - No .devcontainer/ → fail with "run init --with-devcontainer first"
 *   - Org Codespaces disabled → HTTP 403 / 422 with org-admin guidance
 *   - Existing prebuild config for the same branch → skip with notice
 *     (idempotency; avoids accidental clobber of customer-chosen regions)
 */
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.runSetupPrebuild = runSetupPrebuild;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const logger = __importStar(require("./logger"));
const setup_gh_1 = require("./setup-gh");
function checkDevcontainer(cwd) {
    return fs.existsSync(path.join(cwd, '.devcontainer', 'devcontainer.json'));
}
/**
 * List existing prebuild configurations for the repo. Returns an empty
 * array on 404 (Codespaces not enabled for the org) — caller decides
 * whether to surface that as an error or continue.
 */
function listExistingPrebuilds(cwd, owner, repo) {
    const out = (0, setup_gh_1.ghApi)(`repos/${owner}/${repo}/codespaces/prebuilds`, {
        cwd,
        method: 'GET',
    });
    if (!out || !Array.isArray(out.prebuilds_configurations))
        return [];
    return out.prebuilds_configurations.map((p) => ({
        id: p.id,
        ref: p.ref,
        region_count: p.region_count ?? 0,
    }));
}
async function runSetupPrebuild(cwd, opts = {}) {
    logger.header('vyuh-dxkit setup-prebuild');
    if (!(0, setup_gh_1.preflightGhCli)('setup-prebuild')) {
        process.exitCode = 1;
        return;
    }
    // Devcontainer presence — prebuild without a devcontainer is useless
    // (nothing to prebuild). Surface the fix path explicitly.
    if (!checkDevcontainer(cwd)) {
        logger.fail('No .devcontainer/devcontainer.json found. Prebuilds need a devcontainer to build.');
        logger.dim('  → Run `vyuh-dxkit init --with-devcontainer --yes` first to scaffold one.');
        process.exitCode = 1;
        return;
    }
    let ownerRepo;
    try {
        ownerRepo = (0, setup_gh_1.resolveOwnerRepo)(cwd);
    }
    catch (e) {
        if (e instanceof setup_gh_1.GhError) {
            process.exitCode = (0, setup_gh_1.renderGhError)(e, 'Resolve repo');
            return;
        }
        throw e;
    }
    const { owner, repo } = ownerRepo;
    const branch = opts.branch ?? (0, setup_gh_1.resolveDefaultBranch)(cwd);
    // Idempotency: skip if a prebuild already exists for this branch
    // (unless --force). Avoids clobbering customer-chosen regions.
    logger.info(`Checking existing prebuilds for ${owner}/${repo}#${branch}...`);
    let existing;
    try {
        existing = listExistingPrebuilds(cwd, owner, repo);
    }
    catch (e) {
        if (e instanceof setup_gh_1.GhError) {
            // 404 here usually means org Codespaces disabled OR you don't
            // have access to the prebuilds API on this repo.
            if (e.httpStatus === 404) {
                logger.fail('Codespaces prebuilds API returned 404 — either Codespaces is disabled for this org ' +
                    "or the repo doesn't support prebuilds.");
                logger.dim('  → Ask your org admin to enable Codespaces, or configure manually in repo Settings → Codespaces.');
                process.exitCode = 1;
                return;
            }
            process.exitCode = (0, setup_gh_1.renderGhError)(e, 'List existing prebuilds');
            return;
        }
        throw e;
    }
    const branchRef = `refs/heads/${branch}`;
    const existingForBranch = existing.find((p) => p.ref === branchRef || p.ref === branch);
    if (existingForBranch && !opts.force) {
        logger.warn(`Prebuild config already exists for ${branch} (id=${existingForBranch.id}, ` +
            `regions=${existingForBranch.region_count}). Skipping.`);
        logger.dim('  → Pass --force to re-create, or edit in repo Settings → Codespaces.');
        return;
    }
    // Payload — region selection is the trickiest part because GitHub's
    // API expects region IDs rather than friendly names. When the
    // customer doesn't specify, omit the region field and let GitHub
    // pick a sensible default based on the org's Codespaces preferences.
    const payload = {
        ref: branchRef,
        repository_id: undefined,
    };
    if (opts.regions) {
        payload.regions = opts.regions.split(',').map((r) => r.trim());
    }
    // Retention is optional; let GitHub's default apply (typically keep
    // the latest prebuild + retire older ones automatically).
    logger.info(`Creating prebuild for ${branch}` + (opts.regions ? ` (regions: ${opts.regions})` : '') + '...');
    try {
        (0, setup_gh_1.ghApi)(`repos/${owner}/${repo}/codespaces/prebuilds`, {
            cwd,
            method: 'POST',
            inputJson: JSON.stringify(payload),
        });
    }
    catch (e) {
        if (e instanceof setup_gh_1.GhError) {
            // 402 / 422 typically indicate org-billing limits or invalid
            // region IDs. Surface clearly.
            if (e.httpStatus === 402) {
                logger.fail('Codespaces billing limit reached for this org.');
                logger.dim('  → Check repo / org spending limits in GitHub Settings.');
                process.exitCode = 1;
                return;
            }
            process.exitCode = (0, setup_gh_1.renderGhError)(e, 'Create prebuild');
            return;
        }
        throw e;
    }
    console.log(''); // slop-ok
    logger.success(`Prebuild configured for ${owner}/${repo}#${branch}.`);
    logger.dim('  → First prebuild takes ~25 min (one-time); subsequent fresh Codespaces start in ~30s.');
    logger.dim(`  → Verify: https://github.com/${owner}/${repo}/settings/codespaces`);
}
//# sourceMappingURL=setup-prebuild.js.map