/**
 * Phase 1 — CommandEnvelope builder
 * Assembles the canonical envelope from parsed argv, config, and resolved theme.
 */
import { parseArgv, inferInputSource } from "./argv-parser.js";
import { loadProjectConfig, loadUserConfig, resolveConfigValue } from "./config-loader.js";
import { resolveInput } from "./input-resolver.js";
import { validateInput } from "./validator.js";
export async function buildCommandEnvelope(opts) {
    const parsed = parseArgv(opts.argv);
    const cwd = process.cwd();
    const invokedAt = new Date().toISOString();
    const command = parsed.command ?? opts.defaultCommand ?? "run";
    const source = inferInputSource(parsed);
    const projectCfg = loadProjectConfig(cwd);
    const userCfg = loadUserConfig();
    const input = await resolveInput({
        source,
        positionalArgs: parsed.positionalArgs,
        flags: parsed.flags,
        hasStdinPipe: parsed.hasStdinPipe,
        cwd,
    });
    const normalizedInput = {
        ...input,
        rawArgs: parsed.raw,
        metadata: { cwd, invokedAt, isTty: Boolean(process.stdout.isTTY) },
    };
    const validation = validateInput(normalizedInput);
    const outputProfile = {
        format: resolveConfigValue(parsed.flags["output"], process.env.OMK_OUTPUT, projectCfg?.data.outputFormat, userCfg?.data.outputFormat, "json"),
        pretty: resolveConfigValue(Boolean(parsed.flags["pretty"]), process.env.OMK_PRETTY === "1", projectCfg?.data.pretty, userCfg?.data.pretty, false),
        includeMessages: resolveConfigValue(Boolean(parsed.flags["messages"]), process.env.OMK_MESSAGES !== "0", projectCfg?.data.includeMessages, userCfg?.data.includeMessages, true),
        includeTrace: resolveConfigValue(Boolean(parsed.flags["trace"]), process.env.OMK_TRACE === "1", projectCfg?.data.includeTrace, userCfg?.data.includeTrace, false),
        stream: resolveConfigValue(Boolean(parsed.flags["stream"]), process.env.OMK_STREAM === "1", projectCfg?.data.stream, userCfg?.data.stream, false),
        destination: "stdout",
        outputFile: typeof parsed.flags["out"] === "string" ? parsed.flags["out"] : undefined,
    };
    const resolvedTheme = {
        name: resolveConfigValue(parsed.flags["theme"], process.env.OMK_THEME, projectCfg?.data.theme, userCfg?.data.theme, "omk"),
        mode: resolveConfigValue(parsed.flags["mode"], process.env.OMK_THEME_MODE, projectCfg?.data.themeMode, userCfg?.data.themeMode, "auto"),
    };
    const runtime = {
        runId: typeof parsed.flags["run-id"] === "string" ? parsed.flags["run-id"] : undefined,
        workers: typeof parsed.flags["workers"] === "string" ? parsed.flags["workers"] : undefined,
        provider: typeof parsed.flags["provider"] === "string" ? parsed.flags["provider"] : undefined,
        sudo: Boolean(parsed.flags["sudo"]),
    };
    const config = {
        cwd,
        env: process.env,
        projectConfig: projectCfg?.data,
        userConfig: userCfg?.data,
    };
    const envelope = {
        kind: command,
        input: normalizedInput,
        config,
        output: outputProfile,
        theme: resolvedTheme,
        runtime,
    };
    return { envelope, validation };
}
