import { fileExists, hasAnyFile } from '../detection-utils.js';
/**
 * Returns the extension platform if the project is a browser or IDE extension.
 * Extensions need file content parsing since framework detection alone is insufficient.
 */
export async function detectExtensionPlatform(projectPath) {
    // VS Code extension: package.json with contributes + engines.vscode
    if (await isVSCodeExtension(projectPath)) {
        return 'vscode';
    }
    // JetBrains plugin: plugin.xml
    if ((await fileExists(projectPath, 'src/main/resources/META-INF/plugin.xml')) ||
        (await hasAnyFile(projectPath, ['**/META-INF/plugin.xml']))) {
        return 'jetbrains';
    }
    // Browser extension: manifest.json with manifest_version field
    const manifestPlatform = await detectBrowserExtension(projectPath);
    if (manifestPlatform !== null) {
        return manifestPlatform;
    }
    return null;
}
/**
 * Get the extension's manifest version (for browser extensions).
 */
export async function getBrowserExtensionManifestVersion(projectPath) {
    try {
        const fsModule = await import('node:fs/promises');
        const pathModule = await import('node:path');
        const content = await fsModule.readFile(pathModule.join(projectPath, 'manifest.json'), 'utf-8');
        const manifest = JSON.parse(content);
        const version = manifest.manifest_version;
        return typeof version === 'number' ? version : null;
    }
    catch {
        return null;
    }
}
async function isVSCodeExtension(projectPath) {
    try {
        const fsModule = await import('node:fs/promises');
        const pathModule = await import('node:path');
        const content = await fsModule.readFile(pathModule.join(projectPath, 'package.json'), 'utf-8');
        const pkg = JSON.parse(content);
        const engines = pkg.engines;
        const hasVscodeEngine = engines !== undefined && 'vscode' in engines;
        const hasContributes = 'contributes' in pkg;
        return hasVscodeEngine && hasContributes;
    }
    catch {
        return false;
    }
}
async function detectBrowserExtension(projectPath) {
    const manifestVersion = await getBrowserExtensionManifestVersion(projectPath);
    if (manifestVersion === null) {
        return null;
    }
    // Check for browser-specific fields to distinguish Firefox from Chrome
    try {
        const fsModule = await import('node:fs/promises');
        const pathModule = await import('node:path');
        const content = await fsModule.readFile(pathModule.join(projectPath, 'manifest.json'), 'utf-8');
        const manifest = JSON.parse(content);
        // Firefox-specific: browser_specific_settings with gecko key
        const bss = manifest.browser_specific_settings;
        if (bss?.gecko !== undefined) {
            return 'firefox';
        }
        return 'chrome';
    }
    catch {
        return null;
    }
}
//# sourceMappingURL=extensions.js.map