"use strict";
// Shared CLI context — values computed once at startup and consumed by every
// command module. Keeping these out of bin.ts lets each command file resolve
// invocation-style strings ("npx @amplitude/wizard" vs "amplitude-wizard")
// and the wizard version without re-deriving the logic per file.
Object.defineProperty(exports, "__esModule", { value: true });
exports.CLI_INVOCATION = exports.WIZARD_VERSION = exports.IS_WIZARD_DEV = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
/**
 * Dev mode toggle — when set (e.g. via `pnpm try` / `pnpm dev`), internal
 * flags like --local-mcp show up in --help. End users never see them.
 */
exports.IS_WIZARD_DEV = process.env.AMPLITUDE_WIZARD_DEV === '1';
exports.WIZARD_VERSION = (() => {
    // npm/pnpm set this when running via package scripts
    if (process.env.npm_package_version)
        return process.env.npm_package_version;
    // Fallback: read package.json relative to this file. After the TS build,
    // this file lives at dist/src/commands/context.js, so package.json sits
    // three levels up. Source-tree (tsx) execution resolves the same path
    // (src/commands/context.ts → repo root → package.json) when '..' is
    // doubled, but to keep both layouts working we walk up until we find one.
    try {
        let dir = (0, path_1.dirname)(__filename);
        for (let i = 0; i < 5; i += 1) {
            const candidate = (0, path_1.resolve)(dir, 'package.json');
            try {
                // Plain validation (no zod): we only need a single string field, and
                // pulling in zod just for `parse({version: z.string()})` adds 30–50 ms
                // of import time on every CLI launch — bin.ts → context.ts is on the
                // synchronous boot path. Keep startup lean by hand-rolling the check.
                const parsed = JSON.parse((0, fs_1.readFileSync)(candidate, 'utf-8'));
                if (parsed !== null &&
                    typeof parsed === 'object' &&
                    typeof parsed.version === 'string' &&
                    // An empty string is technically a string, but the previous
                    // `if (pkg.version)` truthiness check skipped it and walked
                    // up the tree; preserve that behavior so we don't propagate
                    // an empty `WIZARD_VERSION` into Sentry release tags, the
                    // update notifier, or analytics.
                    parsed.version.length > 0) {
                    return parsed.version;
                }
            }
            catch {
                // not at this level — keep walking up
            }
            const parent = (0, path_1.dirname)(dir);
            if (parent === dir)
                break;
            dir = parent;
        }
        return 'unknown';
    }
    catch {
        return 'unknown';
    }
})();
/**
 * How the user invoked this CLI — echoed back in help/error messages so we
 * don't tell `npx @amplitude/wizard` users to run `amplitude-wizard login`
 * (which only works when globally installed).
 *
 * npx stages packages under a cache path containing `/_npx/`. Everything
 * else is treated as a direct bin invocation.
 */
exports.CLI_INVOCATION = (() => {
    const scriptPath = process.argv[1] ?? '';
    if (scriptPath.includes('/_npx/') || scriptPath.includes('\\_npx\\')) {
        return 'npx @amplitude/wizard';
    }
    // npm >= 7 implements `npx` as `npm exec`, which always sets
    // npm_command=exec — even when npx resolves to an already-installed copy
    // (e.g. running `npx @amplitude/wizard` from inside this repo, or from
    // a project that depends on it). argv[1] doesn't contain /_npx/ in that
    // case, so this catches it.
    if (process.env.npm_command === 'exec') {
        return 'npx @amplitude/wizard';
    }
    return 'amplitude-wizard';
})();
