/**
 * VS Code extension installation utilities
 */
import { execSync, spawn } from 'child_process';
import { existsSync } from 'fs';
import { dirname, join } from 'path';
import { platform } from 'process';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const isWindows = platform === 'win32';
/**
 * Get the path to the bundled VSIX file
 */
export function getVsixPath() {
    // In development: assets folder is at project root
    // In production (npm install): assets folder is in package root
    const possiblePaths = [
        join(__dirname, '../../assets/nanocoder-vscode.vsix'), // development
        join(__dirname, '../../../assets/nanocoder-vscode.vsix'), // npm installed
    ];
    for (const path of possiblePaths) {
        if (existsSync(path)) {
            return path;
        }
    }
    throw new Error('VS Code extension VSIX not found in package');
}
/**
 * Check if the VS Code CLI is available
 */
export function isVSCodeCliAvailable() {
    try {
        execSync('code --version', {
            stdio: 'ignore',
            ...(isWindows && { shell: 'cmd.exe' }),
        });
        return true;
    }
    catch {
        return false;
    }
}
/**
 * Check if the nanocoder VS Code extension is installed
 */
export function isExtensionInstalled() {
    try {
        const output = execSync('code --list-extensions', {
            encoding: 'utf-8',
            stdio: ['pipe', 'pipe', 'ignore'],
            ...(isWindows && { shell: 'cmd.exe' }),
        });
        return output.toLowerCase().includes('nanocollective.nanocoder-vscode');
    }
    catch {
        return false;
    }
}
/**
 * Install the VS Code extension from the bundled VSIX
 * Returns a promise that resolves when installation is complete
 */
export async function installExtension() {
    if (!isVSCodeCliAvailable()) {
        return {
            success: false,
            message: 'VS Code CLI not found. Please install the "code" command:\n' +
                '  1. Open VS Code\n' +
                '  2. Press Cmd+Shift+P (Mac) or Ctrl+Shift+P (Windows/Linux)\n' +
                '  3. Type "Shell Command: Install \'code\' command in PATH"',
        };
    }
    try {
        const vsixPath = getVsixPath();
        return new Promise(resolve => {
            const child = spawn('code', ['--install-extension', vsixPath], {
                stdio: ['ignore', 'pipe', 'pipe'],
                ...(isWindows && { shell: 'cmd.exe' }), // Required on Windows to find code.cmd
            });
            let stdout = '';
            let stderr = '';
            child.stdout?.on('data', (data) => {
                stdout += data.toString();
            });
            child.stderr?.on('data', (data) => {
                stderr += data.toString();
            });
            child.on('close', code => {
                if (code === 0) {
                    resolve({
                        success: true,
                        message: 'VS Code extension installed successfully! Please reload VS Code to activate it.',
                    });
                }
                else {
                    resolve({
                        success: false,
                        message: `Failed to install extension: ${stderr || stdout}`,
                    });
                }
            });
            child.on('error', error => {
                resolve({
                    success: false,
                    message: `Failed to install extension: ${error.message}`,
                });
            });
        });
    }
    catch (error) {
        return {
            success: false,
            message: `Failed to install extension: ${error instanceof Error ? error.message : String(error)}`,
        };
    }
}
//# sourceMappingURL=extension-installer.js.map