/**
 * olam destroy <world> — Tear down a world and clean up resources.
 */
import ora from 'ora';
import { loadContext } from '../context.js';
import { printError, printSuccess } from '../output.js';
import { findHostCpContainer, callHostCpRegistry } from './host-cp.js';
export function registerDestroy(program) {
    program
        .command('destroy')
        .description('Destroy a world and clean up resources')
        .argument('<world>', 'World ID')
        .option('--skip-crystallize', 'Skip thought crystallization')
        .action(async (worldId, opts) => {
        const { ctx, error } = await loadContext();
        if (!ctx) {
            printError(error?.message ?? 'Olam is not configured. Run `olam init` first.');
            process.exitCode = 1;
            return;
        }
        const world = ctx.worldManager.getWorld(worldId);
        if (!world) {
            printError(`World "${worldId}" not found. Run \`olam list\` to see worlds.`);
            process.exitCode = 1;
            return;
        }
        const spinner = ora(`Destroying ${world.name}...`).start();
        try {
            await ctx.worldManager.destroyWorld(worldId, {
                skipCrystallize: opts.skipCrystallize,
            });
            spinner.succeed(`Destroyed ${world.name} (${worldId})`);
            // Auto-deregister from host CP if it's running. Best-effort —
            // a failed deregister leaves a stale registry entry that
            // resolves to a 502 the next time the SPA tries to fetch the
            // world's data, which is non-fatal but ugly.
            try {
                const hostCp = await findHostCpContainer();
                if (hostCp && hostCp.state === 'running') {
                    await callHostCpRegistry('DELETE', { id: worldId });
                }
            }
            catch {
                // Docker unreachable / host-cp probe failed — silently skip.
            }
        }
        catch (err) {
            spinner.fail('Failed to destroy world');
            printError(err instanceof Error ? err.message : String(err));
            process.exitCode = 1;
        }
    });
}
//# sourceMappingURL=destroy.js.map