"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FLASK_AGENT_CONFIG = void 0;
const framework_config_1 = require("../../lib/framework-config");
const package_manager_detection_1 = require("../../lib/package-manager-detection");
const constants_1 = require("../../lib/constants");
const brand_colors_1 = require("../../lib/brand-colors");
const fast_glob_1 = __importDefault(require("fast-glob"));
const fs = __importStar(require("node:fs"));
const path = __importStar(require("node:path"));
const utils_1 = require("./utils");
exports.FLASK_AGENT_CONFIG = {
    metadata: {
        name: 'Flask',
        glyph: '🧪',
        glyphColor: brand_colors_1.BrandColors.gray10,
        integration: constants_1.Integration.flask,
        beta: true,
        docsUrl: 'https://amplitude.com/docs/sdks/analytics/python',
        unsupportedVersionDocsUrl: 'https://amplitude.com/docs/sdks/analytics/python',
        gatherContext: async (options) => {
            const projectType = await (0, utils_1.getFlaskProjectType)(options);
            const appFile = await (0, utils_1.findFlaskAppFile)(options);
            return { projectType, appFile };
        },
    },
    detection: {
        packageName: 'flask',
        packageDisplayName: 'Flask',
        usesPackageJson: false,
        getVersion: () => undefined,
        getVersionBucket: utils_1.getFlaskVersionBucket,
        minimumVersion: '2.0.0',
        getInstalledVersion: (options) => (0, utils_1.getFlaskVersion)(options),
        detect: async (options) => {
            const { installDir } = options;
            const requirementsFiles = await (0, fast_glob_1.default)([
                '**/requirements*.txt',
                '**/pyproject.toml',
                '**/setup.py',
                '**/Pipfile',
            ], {
                cwd: installDir,
                ignore: utils_1.IGNORE_PATTERNS,
            });
            for (const reqFile of requirementsFiles) {
                try {
                    const content = fs.readFileSync(path.join(installDir, reqFile), 'utf-8');
                    if (/^flask([<>=~!]|$|\s)/im.test(content) ||
                        /["']flask["']/i.test(content)) {
                        return true;
                    }
                }
                catch {
                    continue;
                }
            }
            const pyFiles = await (0, fast_glob_1.default)(['**/app.py', '**/wsgi.py', '**/application.py', '**/__init__.py'], {
                cwd: installDir,
                ignore: utils_1.IGNORE_PATTERNS,
            });
            for (const pyFile of pyFiles) {
                try {
                    const content = fs.readFileSync(path.join(installDir, pyFile), 'utf-8');
                    if (content.includes('from flask import') ||
                        content.includes('import flask') ||
                        /Flask\s*\(/.test(content)) {
                        return true;
                    }
                }
                catch {
                    continue;
                }
            }
            return false;
        },
        detectPackageManager: package_manager_detection_1.detectPythonPackageManagers,
    },
    environment: {
        uploadToHosting: false,
        getEnvVars: (apiKey, _host) => ({
            AMPLITUDE_API_KEY: apiKey,
        }),
    },
    analytics: {
        getTags: (context) => ({
            projectType: context.projectType || 'unknown',
        }),
    },
    prompts: {
        packageInstallation: framework_config_1.PYTHON_PACKAGE_INSTALLATION,
        projectTypeDetection: 'This is a Python/Flask project. Look for requirements.txt, pyproject.toml, setup.py, Pipfile, or app.py/wsgi.py to confirm.',
        getAdditionalContextLines: (context) => {
            const projectTypeName = context.projectType
                ? (0, utils_1.getFlaskProjectTypeName)(context.projectType)
                : 'unknown';
            // Map project type to framework ID for MCP docs resource
            const frameworkIdMap = {
                [utils_1.FlaskProjectType.STANDARD]: 'flask',
                [utils_1.FlaskProjectType.RESTFUL]: 'flask',
                [utils_1.FlaskProjectType.RESTX]: 'flask',
                [utils_1.FlaskProjectType.SMOREST]: 'flask',
                [utils_1.FlaskProjectType.BLUEPRINT]: 'flask',
            };
            const frameworkId = context.projectType
                ? frameworkIdMap[context.projectType]
                : 'flask';
            const lines = [
                `Project type: ${projectTypeName}`,
                `Framework docs ID: ${frameworkId} (use amplitude://docs/frameworks/${frameworkId} for documentation)`,
            ];
            if (context.appFile) {
                lines.push(`App file: ${context.appFile}`);
            }
            return lines;
        },
    },
    ui: {
        successMessage: 'Amplitude integration complete',
        estimatedDurationMinutes: 5,
        getOutroChanges: (context) => {
            const projectTypeName = context.projectType
                ? (0, utils_1.getFlaskProjectTypeName)(context.projectType)
                : 'Flask';
            return [
                `Analyzed your ${projectTypeName} project structure`,
                `Installed the Amplitude Python package`,
                `Configured Amplitude in your Flask application`,
                `Added Amplitude initialization with automatic event tracking`,
            ];
        },
        getOutroNextSteps: () => [
            'Start your Flask development server to see Amplitude in action',
            'Visit your Amplitude dashboard to see incoming events',
            'Use amplitude.identify() to associate events with users',
        ],
    },
};
