/**
 * Pure JSON serialiser for `olam plans list --json`.
 *
 * Kept separate from the command action so the wire contract is
 * unit-testable without a live cloud endpoint (mirrors the pattern
 * used by `repos-list-json.ts`, `auth-list-json.ts`, and
 * `servicesStatusJson` in services.ts).
 *
 * WHY this exists: agent/script drivers (Rico, flywheel chain, CI pipelines)
 * need to enumerate cloud plan state — plan IDs, statuses, modes — before
 * deciding whether to spawn a new plan or pick up an existing one. The human
 * table is colorized and column-width-sensitive, so scraping it is brittle.
 * This serialiser gives drivers a stable, color-free array via
 * `olam plans list --json | jq '.plans[] | select(.status=="active")'`.
 *
 * Secret redaction: the PlanSummary shape carries two URL fields
 * (`host_cp_url`, `auth_recovery_callback_url`) that MAY embed auth
 * tokens in the URL userinfo component. Both are redacted before emission
 * using the canonical `:***@` substitution that matches
 * `@olam/core/src/skill-sources/store.ts`'s `redactUrl`.
 * The `bearer` (showcase password) used for HTTP auth never enters the
 * PlanSummary shape itself and is therefore never serialised.
 */
/**
 * Redact the password component of a URL's userinfo (`:password@` → `:***@`).
 * Matches the same pattern used by `@olam/core/src/skill-sources/store.ts`'s
 * `redactUrl`. Inlined here so the serialiser has no cross-package import that
 * would require built packages to resolve in test environments.
 */
function redactUrlCredentials(url) {
    return url.replace(/:[^@/\s]+@/g, ':***@');
}
/**
 * Serialise a PlanSummary array to the stable `--json` wire shape.
 * Returns a JSON string (no trailing newline — caller adds it).
 *
 * Redacts any URL credentials in `host_cp_url` and
 * `auth_recovery_callback_url` using the canonical `redactUrl` helper
 * (`:***@` substitution), matching the pattern used by `repos-list-json.ts`
 * and `skills source list --json`.
 */
export function renderPlansListJson(plans) {
    const rows = plans.map((p) => ({
        plan_id: p.plan_id,
        operator_id: p.operator_id,
        status: p.status,
        mode: p.mode,
        created_at: p.created_at,
        last_active_at: p.last_active_at ?? null,
        pinned_until: p.pinned_until ?? null,
        host_cp_url: p.host_cp_url != null ? redactUrlCredentials(p.host_cp_url) : null,
        auth_recovery_callback_url: p.auth_recovery_callback_url != null ? redactUrlCredentials(p.auth_recovery_callback_url) : null,
        has_recovery_callback: p.has_recovery_callback,
    }));
    const payload = {
        count: rows.length,
        plans: rows,
    };
    return JSON.stringify(payload);
}
//# sourceMappingURL=plans-list-json.js.map