"use strict";
/**
 * Deterministic feature discovery — finds opt-in Amplitude features that
 * apply to the current project. Used by both the interactive TUI (where
 * the store auto-enables everything discovered via
 * `autoEnableInlineAddons`) and non-interactive modes (CI, agent) which
 * call `autoEnableOptInFeatures` here to do the same thing.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.discoverFeatures = discoverFeatures;
exports.autoEnableOptInFeatures = autoEnableOptInFeatures;
const fs_1 = require("fs");
const path_1 = require("path");
const wizard_session_1 = require("./wizard-session");
const feature_flags_1 = require("./feature-flags");
const analytics_1 = require("../utils/analytics");
const STRIPE_PACKAGES = ['stripe', '@stripe/stripe-js'];
const LLM_PACKAGES = [
    'openai',
    '@anthropic-ai/sdk',
    'ai',
    '@ai-sdk/openai',
    'langchain',
    '@langchain/openai',
    '@langchain/langgraph',
    '@google/generative-ai',
    '@google/genai',
    '@instructor-ai/instructor',
    '@mastra/core',
    'portkey-ai',
];
const BROWSER_REPLAY_INTEGRATIONS = new Set([
    'nextjs',
    'vue',
    'react-router',
    'javascript_web',
]);
function discoverFeatures(opts) {
    const features = [];
    try {
        const pkg = JSON.parse((0, fs_1.readFileSync)((0, path_1.join)(opts.installDir, 'package.json'), 'utf-8'));
        const depNames = Object.keys({
            ...pkg.dependencies,
            ...pkg.devDependencies,
        });
        if (depNames.some((d) => STRIPE_PACKAGES.includes(d))) {
            features.push(wizard_session_1.DiscoveredFeature.Stripe);
        }
        if ((0, feature_flags_1.isFlagEnabled)(feature_flags_1.FLAG_LLM_ANALYTICS) &&
            depNames.some((d) => LLM_PACKAGES.includes(d))) {
            features.push(wizard_session_1.DiscoveredFeature.LLM);
        }
    }
    catch {
        // No package.json or parse error — skip dep-based discovery
    }
    if (opts.integration && BROWSER_REPLAY_INTEGRATIONS.has(opts.integration)) {
        features.push(wizard_session_1.DiscoveredFeature.SessionReplay);
        features.push(wizard_session_1.DiscoveredFeature.Engagement);
    }
    return features;
}
/**
 * Map a DiscoveredFeature to its corresponding AdditionalFeature, or null
 * if the discovery is informational only (e.g. Stripe).
 */
function discoveredToAdditional(feature) {
    if (feature === wizard_session_1.DiscoveredFeature.SessionReplay)
        return wizard_session_1.AdditionalFeature.SessionReplay;
    if (feature === wizard_session_1.DiscoveredFeature.LLM)
        return wizard_session_1.AdditionalFeature.LLM;
    if (feature === wizard_session_1.DiscoveredFeature.Engagement)
        return wizard_session_1.AdditionalFeature.Engagement;
    return null;
}
/** Enable a single additional feature on the session (idempotent). */
function enableAdditionalFeature(session, additional, source) {
    if (!session.additionalFeatureQueue.includes(additional)) {
        session.additionalFeatureQueue = [
            ...session.additionalFeatureQueue,
            additional,
        ];
    }
    if (additional === wizard_session_1.AdditionalFeature.LLM)
        session.llmOptIn = true;
    if (additional === wizard_session_1.AdditionalFeature.SessionReplay)
        session.sessionReplayOptIn = true;
    if (additional === wizard_session_1.AdditionalFeature.Engagement)
        session.engagementOptIn = true;
    analytics_1.analytics.wizardCapture('feature enabled', {
        feature: additional,
        source,
    });
}
/**
 * Auto-enable every opt-in additional feature discovered in the project.
 * Mirrors the TUI's "all defaults on" behavior for CI / agent runs.
 * Mutates the session in place and fires per-feature telemetry.
 */
function autoEnableOptInFeatures(session, source) {
    const discovered = discoverFeatures({
        installDir: session.installDir,
        integration: session.integration,
    });
    session.discoveredFeatures = [...discovered];
    for (const feature of discovered) {
        const additional = discoveredToAdditional(feature);
        if (!additional)
            continue;
        enableAdditionalFeature(session, additional, source);
    }
    session.optInFeaturesComplete = true;
}
