/**
 * olam completion <shell> — emit shell completion script.
 *
 * Phase B2 of olam-operator-onboarding-parity (plan
 * ~/.claude/plans/olam-operator-onboarding-parity.md).
 *
 * Wraps the runtime-emit generator at `../lib/completion-generator.ts`
 * (Decision 4 — runtime-emit from commander tree, not static fixtures).
 *
 * Usage:
 *   olam completion zsh   # → stdout: #compdef olam + _olam() function
 *   olam completion bash  # → stdout: _olam_completions() + complete -F binding
 *   olam completion fish  # → stderr: "Unsupported shell..."; exit 1
 *
 * Pipe to `eval` in shell rc OR let Phase C's `olam setup` do the
 * idempotent shell-rc append for you:
 *   echo 'eval "$(olam completion zsh)"' >> ~/.zshrc
 */
import { generateCompletion, UnsupportedShellError } from '../lib/completion-generator.js';
/**
 * Register `olam completion <shell>` on the given commander program.
 *
 * Captures `program` in the closure so the action handler can walk the
 * full command tree at invoke time. Using `program.parent ?? program`
 * keeps the implementation working when commands inject their subcommands
 * via a different parent — though in olam's current shape, `program` IS
 * always the root, so the parent fallback is defence-in-depth.
 */
export function registerCompletion(program) {
    program
        .command('completion')
        .description('Emit a POSIX shell completion script for zsh or bash. '
        + 'Pipe to `eval` in your shell rc, or let `olam setup` append it idempotently. '
        + 'Completes top-level commands + one nested level (`olam kg <Tab>`); deeper '
        + 'subcommand trees (`olam world snapshot <Tab>`) fall through without completion.')
        .argument('<shell>', 'zsh | bash')
        .action((shell) => {
        const tree = program.parent ?? program;
        try {
            const output = generateCompletion(tree, shell);
            process.stdout.write(output);
        }
        catch (err) {
            if (err instanceof UnsupportedShellError) {
                process.stderr.write(`${err.message}\n`);
                process.exitCode = 1;
                return;
            }
            throw err;
        }
    });
}
//# sourceMappingURL=completion.js.map