"use strict";
/**
 * Native target execution — short-circuit dispatch for passthrough flag commands,
 * and top-level profile dispatcher (Phase E switch).
 *
 * Extracted from src/ccs.ts (lines 291-295, 394-426).
 * Handles direct execution of native Codex passthrough commands (--help, resume, etc.)
 * before the main profile dispatch loop runs.
 *
 * dispatchProfile() collapses the 6-branch switch in main() to a single call.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.dispatchProfile = exports.execNativeCodexCommand = void 0;
const ui_1 = require("../utils/ui");
const targets_1 = require("../targets");
const cli_argument_parser_1 = require("./cli-argument-parser");
const cliproxy_flow_1 = require("./flows/cliproxy-flow");
const copilot_flow_1 = require("./flows/copilot-flow");
const cursor_flow_1 = require("./flows/cursor-flow");
const settings_flow_1 = require("./flows/settings-flow");
const account_flow_1 = require("./flows/account-flow");
const default_flow_1 = require("./flows/default-flow");
// ========== Native Codex Command Executor ==========
function execNativeCodexCommand(args) {
    const adapter = (0, targets_1.getTarget)('codex');
    if (!adapter) {
        console.error((0, ui_1.fail)('Target adapter not found for "codex"'));
        process.exit(1);
    }
    const binaryInfo = adapter.detectBinary();
    if (!binaryInfo) {
        console.error((0, ui_1.fail)('Codex CLI not found.'));
        console.error((0, ui_1.info)('Install a recent @openai/codex build, then retry.'));
        process.exit(1);
    }
    const targetArgs = (0, cli_argument_parser_1.getNativeCodexPassthroughArgs)(args);
    if (!targetArgs) {
        console.error((0, ui_1.fail)('Native Codex passthrough args could not be resolved.'));
        process.exit(1);
    }
    const creds = {
        profile: 'default',
        baseUrl: '',
        apiKey: '',
    };
    const builtArgs = adapter.buildArgs('default', targetArgs, {
        creds,
        profileType: 'default',
        binaryInfo,
    });
    const targetEnv = adapter.buildEnv(creds, 'default');
    adapter.exec(builtArgs, targetEnv, { binaryInfo });
}
exports.execNativeCodexCommand = execNativeCodexCommand;
// ========== Profile Dispatcher ==========
/**
 * Dispatch to the correct per-profile-type flow.
 *
 * Collapses the 6-branch if/else-if switch that previously lived in main() into a
 * single call site. The headless -p delegation short-circuit is handled in main()
 * before this function is called.
 */
async function dispatchProfile(ctx) {
    const { profileInfo } = ctx;
    switch (profileInfo.type) {
        case 'cliproxy':
            return (0, cliproxy_flow_1.runCliproxyFlow)(ctx);
        case 'copilot':
            return (0, copilot_flow_1.runCopilotFlow)(ctx);
        case 'cursor':
            return (0, cursor_flow_1.runCursorFlow)(ctx);
        case 'settings':
            return (0, settings_flow_1.runSettingsFlow)(ctx);
        case 'account':
            return (0, account_flow_1.runAccountFlow)(ctx);
        default:
            return (0, default_flow_1.runDefaultFlow)(ctx);
    }
}
exports.dispatchProfile = dispatchProfile;
//# sourceMappingURL=target-executor.js.map