"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.REACT_ROUTER_AGENT_CONFIG = void 0;
const package_manager_detection_light_1 = require("../../lib/package-manager-detection-light");
const constants_1 = require("../../lib/constants");
const package_json_1 = require("../../utils/package-json");
const package_json_light_1 = require("../../utils/package-json-light");
const ui_1 = require("../../ui");
const browser_sdk_prompt_1 = require("../_shared/browser-sdk-prompt");
const utils_1 = require("./utils");
const REACT_ROUTER_MINIMUM_VERSION = '6.0.0';
const TANSTACK_MINIMUM_VERSION = '1.0.0';
// Priority matches gatherContext: TanStack Start > TanStack Router > react-router
function getReactRouterVersionCheckInfo(packageJson) {
    const tanstackStartVersion = (0, package_json_1.getPackageVersion)('@tanstack/react-start', packageJson);
    if (tanstackStartVersion) {
        return {
            version: tanstackStartVersion,
            minimumVersion: TANSTACK_MINIMUM_VERSION,
            packageDisplayName: 'TanStack Start',
        };
    }
    const tanstackRouterVersion = (0, package_json_1.getPackageVersion)('@tanstack/react-router', packageJson);
    if (tanstackRouterVersion) {
        return {
            version: tanstackRouterVersion,
            minimumVersion: TANSTACK_MINIMUM_VERSION,
            packageDisplayName: 'TanStack Router',
        };
    }
    const reactRouterVersion = (0, package_json_1.getPackageVersion)('react-router', packageJson);
    if (reactRouterVersion) {
        return {
            version: reactRouterVersion,
            minimumVersion: REACT_ROUTER_MINIMUM_VERSION,
            packageDisplayName: 'React Router',
        };
    }
    return {};
}
exports.REACT_ROUTER_AGENT_CONFIG = {
    metadata: {
        name: 'React Router',
        glyph: '🧭',
        glyphColor: '#CA4245',
        integration: constants_1.Integration.reactRouter,
        targetsBrowser: true,
        docsUrl: 'https://amplitude.com/docs/sdks/analytics/browser/browser-sdk-2',
        unsupportedVersionDocsUrl: 'https://amplitude.com/docs/sdks/analytics/browser/browser-sdk-2',
        gatherContext: async (options) => {
            const packageJson = await (0, package_json_light_1.tryGetPackageJson)(options);
            // TanStack Start / TanStack Router share the same browser SDK setup
            if (packageJson &&
                (0, package_json_1.hasPackageInstalled)('@tanstack/react-start', packageJson)) {
                (0, ui_1.getUI)().setDetectedFramework('TanStack Start');
                return { routerMode: utils_1.ReactRouterMode.V7_FRAMEWORK };
            }
            if (packageJson &&
                (0, package_json_1.hasPackageInstalled)('@tanstack/react-router', packageJson)) {
                (0, ui_1.getUI)().setDetectedFramework('TanStack Router');
                return {};
            }
            // Standard React Router detection
            const routerMode = await (0, utils_1.getReactRouterMode)(options);
            if (routerMode) {
                (0, ui_1.getUI)().setDetectedFramework(`React Router ${(0, utils_1.getReactRouterModeName)(routerMode)}`);
                return { routerMode };
            }
            return {};
        },
        getIntegrationSkillId: (context) => {
            // Mirrors the frameworkIdMap in prompts.getAdditionalContextLines
            switch (context.routerMode) {
                case utils_1.ReactRouterMode.V6:
                    return 'integration-react-react-router-6';
                case utils_1.ReactRouterMode.V7_FRAMEWORK:
                    return 'integration-react-react-router-7-framework';
                case utils_1.ReactRouterMode.V7_DATA:
                    return 'integration-react-react-router-7-data';
                case utils_1.ReactRouterMode.V7_DECLARATIVE:
                    return 'integration-react-react-router-7-declarative';
                default:
                    // TanStack Router (no routerMode) and unresolved cases: leave to the
                    // agent's load_skill_menu fallback.
                    return null;
            }
        },
    },
    detection: {
        packageName: 'react-router',
        packageDisplayName: 'React Router',
        getVersion: (packageJson) => {
            const pkg = packageJson;
            // Priority matches gatherContext: TanStack Start > TanStack Router > react-router
            return ((0, package_json_1.getPackageVersion)('@tanstack/react-start', pkg) ??
                (0, package_json_1.getPackageVersion)('@tanstack/react-router', pkg) ??
                (0, package_json_1.getPackageVersion)('react-router', pkg));
        },
        getVersionBucket: utils_1.getReactRouterVersionBucket,
        minimumVersion: REACT_ROUTER_MINIMUM_VERSION,
        getInstalledVersion: async (options) => {
            const packageJson = await (0, package_json_light_1.tryGetPackageJson)(options);
            if (!packageJson)
                return undefined;
            return getReactRouterVersionCheckInfo(packageJson).version;
        },
        getVersionCheckInfo: async (options) => {
            const packageJson = await (0, package_json_light_1.tryGetPackageJson)(options);
            if (!packageJson)
                return {};
            return getReactRouterVersionCheckInfo(packageJson);
        },
        detect: async (options) => {
            const packageJson = await (0, package_json_light_1.tryGetPackageJson)(options);
            if (!packageJson)
                return false;
            return ((0, package_json_1.hasPackageInstalled)('react-router', packageJson) ||
                (0, package_json_1.hasPackageInstalled)('@tanstack/react-router', packageJson) ||
                (0, package_json_1.hasPackageInstalled)('@tanstack/react-start', packageJson));
        },
        detectPackageManager: package_manager_detection_light_1.detectNodePackageManagersLight,
    },
    environment: {
        uploadToHosting: false,
        getEnvVars: (apiKey, _host) => ({
            REACT_APP_AMPLITUDE_API_KEY: apiKey,
        }),
    },
    analytics: {
        getTags: (context) => ({
            routerMode: context.routerMode || 'unknown',
        }),
    },
    prompts: {
        projectTypeDetection: 'This is a JavaScript/TypeScript project. Look for package.json and lockfiles (package-lock.json, yarn.lock, pnpm-lock.yaml, bun.lockb) to confirm.',
        getAdditionalContextLines: (context) => {
            const routerMode = context.routerMode;
            const modeName = routerMode
                ? (0, utils_1.getReactRouterModeName)(routerMode)
                : 'unknown';
            // Map router mode to framework ID for MCP docs resource
            const frameworkIdMap = {
                [utils_1.ReactRouterMode.V6]: 'react-react-router-6',
                [utils_1.ReactRouterMode.V7_FRAMEWORK]: 'react-react-router-7-framework',
                [utils_1.ReactRouterMode.V7_DATA]: 'react-react-router-7-data',
                [utils_1.ReactRouterMode.V7_DECLARATIVE]: 'react-react-router-7-declarative',
            };
            const frameworkId = routerMode
                ? frameworkIdMap[routerMode]
                : utils_1.ReactRouterMode.V7_FRAMEWORK;
            return [
                `Router mode: ${modeName}`,
                `Framework docs ID: ${frameworkId} (use amplitude://docs/frameworks/${frameworkId} for documentation)`,
                browser_sdk_prompt_1.BROWSER_UNIFIED_SDK_PROMPT_LINE,
                `Initialize from the project's main entry point (typically src/main.tsx, src/index.tsx, or root.tsx for React Router 7 framework mode) before <RouterProvider /> mounts so the SDK is available on the first render.`,
            ];
        },
    },
    ui: {
        successMessage: 'Amplitude integration complete',
        estimatedDurationMinutes: 8,
        getOutroChanges: (context) => {
            const modeName = context.routerMode
                ? (0, utils_1.getReactRouterModeName)(context.routerMode)
                : 'React Router';
            return [
                `Analyzed your React Router project structure (${modeName})`,
                `Created and configured Amplitude initializers`,
                `Integrated Amplitude into your application`,
            ];
        },
        getOutroNextSteps: () => [
            'Start your development server to see Amplitude in action',
            'Visit your Amplitude dashboard to see incoming events',
        ],
    },
};
