import { type ChildProcess, type SpawnOptions } from "node:child_process";
import type { ErrorReason } from "@wirecat/cli-base/standards";
import type { Command } from "commander";
import { type AppIdentity } from "../ports/app.js";
export type { AppIdentity };
/** What `startCli` hands the CLI's own program: the process, wired once. */
export interface EntryContext {
    /** `process.argv` without the runtime and the script. */
    args: string[];
    env: NodeJS.ProcessEnv;
    /** Whether stdout is a terminal. */
    tty: boolean;
    /** Aborted on SIGINT, SIGTERM and a closed or failing stdout. */
    signal: AbortSignal;
    /** The first-run questions, before the command acts: call it from a `preAction` hook with the action command. */
    atStart: (command: Command) => Promise<void>;
    /** A yes/no question on stderr; false without asking when there is no terminal, under CI, `--json`, `--jsonl` or `--no-input`. */
    ask: (question: string, command?: Command) => Promise<boolean>;
    /**
     * Opens the URL in the system browser, or in `browser` when given; never waits for the browser to close. Does nothing
     * where nobody can use a browser (WC-CMD-15): under CI, `--json`, `--jsonl`, `--no-input`, or with no terminal.
     * The caller prints the URL either way.
     */
    openBrowser: (url: string, browser?: string, command?: Command) => Promise<void>;
    /** True once a reader closed the output (`| head`): nothing more reaches the terminal. */
    outputClosed: () => boolean;
    /** The exit code of an answer that is not a failure, a partial one; under `commands --serve`, of this request alone. */
    setExitCode: (code: number) => void;
}
/** The parts of the process `startCli` touches, replaced in tests. */
export interface EntryIo {
    argv: readonly string[];
    env: NodeJS.ProcessEnv;
    platform: NodeJS.Platform;
    stdin: NodeJS.ReadableStream & {
        isTTY?: boolean;
    };
    stdout: NodeJS.WritableStream & {
        isTTY?: boolean;
    };
    stderr: NodeJS.WritableStream & {
        isTTY?: boolean;
    };
    signals: Pick<NodeJS.Process, "on" | "off">;
    setExitCode: (code: number) => void;
    spawn: (command: string, args: readonly string[], options: SpawnOptions) => ChildProcess;
}
export interface CliEntry {
    app: AppIdentity;
    /** Builds and runs the CLI's program; a returned number is the exit code, nothing keeps the one already set. */
    run: (entry: EntryContext) => Promise<number | undefined>;
    /** Says a failure the program threw, once, and gives its exit code. */
    fail: (error: unknown, entry: EntryContext) => number;
    /** Whether a crash may print the human form; stdout being a terminal when left out. */
    interactive?: (args: readonly string[], tty: boolean) => boolean;
    reasons?: readonly ErrorReason[];
    /** Swap a system SQLite that cannot hold the store before anything runs (`ensureSqlite`). */
    sqlite?: boolean;
    io?: Partial<EntryIo>;
}
/**
 * The entry point of a CLI that builds its own commander program: crash catch, cancellation, a quiet end on a closed
 * pipe, the first-run questions, the consent prompt and the browser. It sets `process.exitCode` and never exits, so
 * output still flushes.
 */
export declare const startCli: (entry: CliEntry) => Promise<void>;
/** The first-run questions, before the command acts. A dry run writes nothing, and these write the shared config. */
export declare const firstRunAt: (entry: CliEntry, io: EntryIo, headless: (command: Command) => boolean) => (command: Command) => Promise<void>;
/** Opens `url` in the system browser, or in `browser` when given; resolves once the browser has started. */
export declare const openBrowser: (url: string, browser?: string, io?: Pick<EntryIo, "platform" | "spawn">) => Promise<void>;
