"use strict";
// src/cli/InstallWizardV2.ts
// Phase-based install wizard — entry point invoked by `openlife init`.
//
// Walks ALL_PHASES sequentially, skipping phases whose `when` predicate is
// false. Every phase ends with an explicit `io.pause()` so the operator
// controls pacing with Enter.
//
// Backward compat: when the operator aborts before any product is selected,
// or invokes `openlife init --no-interactive` (handled in src/index.ts),
// the original InstallWizard flow remains available.
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReadlineWizardIO = exports.CannedIO = exports.InstallWizardV2 = void 0;
exports.buildEmptyContext = buildEmptyContext;
const Phases_1 = require("./install/Phases");
const WizardIO_1 = require("./install/WizardIO");
/**
 * The new install wizard. Returns a WizardResultV2 — `ok: true` if all phases
 * completed without abort, plus the fully-mutated context so callers can
 * inspect what happened.
 *
 * Note: the wizard itself does NOT call InstallFlow — the per-product confirm
 * phases (16-core-confirm, 28-agent-confirm) do that inline so each install
 * happens after its own preview and the user has a chance to abort mid-flow.
 */
class InstallWizardV2 {
    ownsIO;
    io;
    context;
    constructor(opts = {}) {
        const root = opts.root || process.cwd();
        if (opts.io) {
            this.io = opts.io;
            this.ownsIO = false;
        }
        else {
            this.io = new WizardIO_1.ReadlineWizardIO();
            this.ownsIO = true;
        }
        this.context = buildEmptyContext(root, opts.initialProducts);
    }
    async run() {
        try {
            for (const phase of Phases_1.ALL_PHASES) {
                if (this.context.aborted)
                    break;
                if (phase.when && !phase.when(this.context))
                    continue;
                try {
                    await phase.run(this.context, this.io);
                }
                catch (err) {
                    const msg = err instanceof Error ? err.message : String(err);
                    this.io.err(`Phase "${phase.id}" failed: ${msg}`);
                    return {
                        ok: false,
                        context: this.context,
                        reason: 'unexpected_error',
                        detail: `phase=${phase.id} err=${msg}`,
                    };
                }
            }
            if (this.context.aborted) {
                return {
                    ok: false,
                    context: this.context,
                    reason: 'user_aborted',
                    detail: this.context.abortReason,
                };
            }
            // Final summary card.
            this.io.divider();
            this.io.print('🚀 OpenLife install wizard complete.');
            if (this.context.warnings.length > 0) {
                this.io.print('');
                this.io.print(`Warnings (${this.context.warnings.length}):`);
                for (const w of this.context.warnings)
                    this.io.print(`  - ${w}`);
            }
            this.io.print('');
            return { ok: true, context: this.context };
        }
        finally {
            if (this.ownsIO) {
                try {
                    this.io.close();
                }
                catch { /* ignore */ }
            }
        }
    }
}
exports.InstallWizardV2 = InstallWizardV2;
function buildEmptyContext(root, initialProducts) {
    return {
        root,
        installMode: 'full',
        products: initialProducts ?? [],
        coreHosts: [],
        coreProviders: [],
        coreApiKeys: {},
        agentAuthDecisions: [],
        agentOAuthResults: [],
        agentApiKeys: {},
        warnings: [],
    };
}
// Re-export so consumers can do `import { ReadlineWizardIO, CannedIO } from '...'`
var WizardIO_2 = require("./install/WizardIO");
Object.defineProperty(exports, "CannedIO", { enumerable: true, get: function () { return WizardIO_2.CannedIO; } });
Object.defineProperty(exports, "ReadlineWizardIO", { enumerable: true, get: function () { return WizardIO_2.ReadlineWizardIO; } });
