"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isNonInteractiveEnvironment = isNonInteractiveEnvironment;
exports.readEnvironment = readEnvironment;
exports.detectEnvVarPrefix = detectEnvVarPrefix;
const read_env_1 = __importDefault(require("read-env"));
const setup_utils_1 = require("./setup-utils");
const fast_glob_1 = __importDefault(require("fast-glob"));
const constants_1 = require("../lib/constants");
function isNonInteractiveEnvironment() {
    if (constants_1.IS_DEV) {
        return false;
    }
    if (!process.stdout.isTTY || !process.stderr.isTTY) {
        return true;
    }
    return false;
}
function readEnvironment() {
    const result = (0, read_env_1.default)('AMPLITUDE_WIZARD');
    return result;
}
async function detectEnvVarPrefix(options) {
    const packageJson = await (0, setup_utils_1.tryGetPackageJson)(options);
    if (!packageJson)
        return 'VITE_PUBLIC_';
    const deps = { ...packageJson.dependencies, ...packageJson.devDependencies };
    const has = (name) => name in deps;
    const hasAnyFile = async (patterns) => {
        const matches = await (0, fast_glob_1.default)(patterns, {
            cwd: options.installDir,
            absolute: false,
            onlyFiles: true,
            ignore: ['**/node_modules/**'],
        });
        return matches.length > 0;
    };
    // --- Next.js
    if (has('next') || (await hasAnyFile(['**/next.config.{js,ts,mjs,cjs}']))) {
        return 'NEXT_PUBLIC_';
    }
    // --- Create React App
    if (has('react-scripts') ||
        has('create-react-app') ||
        (await hasAnyFile(['**/config-overrides.js']))) {
        return 'REACT_APP_';
    }
    // --- Vite (vanilla, TanStack, Solid, etc.)
    // Note: Vite does not need PUBLIC_ but we use it to follow the docs, to improve the chances of an LLM getting it right.
    if (has('vite') || (await hasAnyFile(['**/vite.config.{js,ts,mjs,cjs}']))) {
        return 'VITE_PUBLIC_';
    }
    // --- SvelteKit
    if (has('@sveltejs/kit') ||
        (await hasAnyFile(['**/svelte.config.{js,ts}']))) {
        return 'PUBLIC_';
    }
    // --- TanStack Start (uses Vite)
    if (has('@tanstack/start') ||
        (await hasAnyFile(['**/tanstack.config.{js,ts}']))) {
        return 'VITE_PUBLIC_';
    }
    // --- SolidStart (uses Vite)
    if (has('solid-start') || (await hasAnyFile(['**/solid.config.{js,ts}']))) {
        return 'VITE_PUBLIC_';
    }
    // --- Astro
    if (has('astro') || (await hasAnyFile(['**/astro.config.{js,ts,mjs}']))) {
        return 'PUBLIC_';
    }
    // We default to Vite if we can't detect a specific framework, since it's the most commonly used.
    return 'VITE_PUBLIC_';
}
