"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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ShellCompletionInstaller = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const config_manager_1 = require("./config-manager");
/**
 * Shell Completion Installer
 * Auto-configures shell completion for bash, zsh, fish, PowerShell
 */
class ShellCompletionInstaller {
    constructor() {
        this.homeDir = os.homedir();
        this.ccsDir = (0, config_manager_1.getCcsDir)();
        this.completionDir = path.join(this.ccsDir, 'completions');
        this.scriptsDir = path.join(__dirname, '../../scripts/completion');
    }
    /**
     * Detect current shell
     */
    detectShell() {
        const shell = process.env.SHELL || '';
        if (shell.includes('bash'))
            return 'bash';
        if (shell.includes('zsh'))
            return 'zsh';
        if (shell.includes('fish'))
            return 'fish';
        if (process.platform === 'win32')
            return 'powershell';
        return null;
    }
    /**
     * Ensure completion files are in ~/.ccs/completions/
     */
    ensureCompletionFiles() {
        if (!fs.existsSync(this.completionDir)) {
            fs.mkdirSync(this.completionDir, { recursive: true });
        }
        // Copy completion scripts
        const files = ['ccs.bash', 'ccs.zsh', 'ccs.fish', 'ccs.ps1'];
        files.forEach((file) => {
            const src = path.join(this.scriptsDir, file);
            const dest = path.join(this.completionDir, file);
            if (fs.existsSync(src)) {
                fs.copyFileSync(src, dest);
            }
        });
    }
    /**
     * Safely create directory, checking for file conflicts
     */
    ensureDirectory(dirPath) {
        if (fs.existsSync(dirPath)) {
            const stat = fs.statSync(dirPath);
            if (!stat.isDirectory()) {
                throw new Error(`Cannot create directory: ${dirPath} exists but is a file.\n` +
                    `Please remove or rename this file and try again.`);
            }
            // Directory exists, nothing to do
            return;
        }
        // Check parent directories recursively
        const parentDir = path.dirname(dirPath);
        if (parentDir !== dirPath) {
            this.ensureDirectory(parentDir);
        }
        // Create the directory
        fs.mkdirSync(dirPath);
    }
    /**
     * Install bash completion
     */
    installBash(force = false) {
        const rcFile = path.join(this.homeDir, '.bashrc');
        const completionPath = path.join(this.completionDir, 'ccs.bash');
        if (!fs.existsSync(completionPath)) {
            throw new Error('Completion file not found. Please reinstall CCS.');
        }
        const marker = '# CCS shell completion';
        const sourceCmd = `source "${completionPath}"`;
        const block = `\n${marker}\n${sourceCmd}\n`;
        // Check if already installed
        if (fs.existsSync(rcFile)) {
            const content = fs.readFileSync(rcFile, 'utf8');
            if (content.includes(marker)) {
                if (!force) {
                    return { success: true, alreadyInstalled: true };
                }
                // Force: completion files already updated by ensureCompletionFiles()
                return {
                    success: true,
                    message: `Updated completion files in ${this.completionDir}`,
                    reload: 'source ~/.bashrc',
                };
            }
        }
        // Append to .bashrc
        fs.appendFileSync(rcFile, block);
        return {
            success: true,
            message: `Added to ${rcFile}`,
            reload: 'source ~/.bashrc',
        };
    }
    /**
     * Install zsh completion
     */
    installZsh(force = false) {
        const rcFile = path.join(this.homeDir, '.zshrc');
        const completionPath = path.join(this.completionDir, 'ccs.zsh');
        const zshCompDir = path.join(this.homeDir, '.zsh', 'completion');
        if (!fs.existsSync(completionPath)) {
            throw new Error('Completion file not found. Please reinstall CCS.');
        }
        // Create zsh completion directory (with file conflict checking)
        this.ensureDirectory(zshCompDir);
        // Copy to zsh completion directory (always update for force)
        const destFile = path.join(zshCompDir, '_ccs');
        fs.copyFileSync(completionPath, destFile);
        const marker = '# CCS shell completion';
        const setupCmds = ['fpath=(~/.zsh/completion $fpath)', 'autoload -Uz compinit && compinit'];
        const block = `\n${marker}\n${setupCmds.join('\n')}\n`;
        // Check if already installed
        if (fs.existsSync(rcFile)) {
            const content = fs.readFileSync(rcFile, 'utf8');
            if (content.includes(marker)) {
                if (!force) {
                    return { success: true, alreadyInstalled: true };
                }
                // Force: files already updated above
                return {
                    success: true,
                    message: `Updated ${destFile}`,
                    reload: 'source ~/.zshrc',
                };
            }
        }
        // Append to .zshrc
        fs.appendFileSync(rcFile, block);
        return {
            success: true,
            message: `Added to ${rcFile}`,
            reload: 'source ~/.zshrc',
        };
    }
    /**
     * Install fish completion
     */
    installFish(force = false) {
        const completionPath = path.join(this.completionDir, 'ccs.fish');
        const fishCompDir = path.join(this.homeDir, '.config', 'fish', 'completions');
        if (!fs.existsSync(completionPath)) {
            throw new Error('Completion file not found. Please reinstall CCS.');
        }
        // Create fish completion directory (with file conflict checking)
        this.ensureDirectory(fishCompDir);
        // Copy to fish completion directory (fish auto-loads from here)
        const destFile = path.join(fishCompDir, 'ccs.fish');
        // Check if already installed
        if (fs.existsSync(destFile) && !force) {
            return { success: true, alreadyInstalled: true };
        }
        fs.copyFileSync(completionPath, destFile);
        return {
            success: true,
            message: `Installed to ${destFile}`,
            reload: 'Fish auto-loads completions (no reload needed)',
        };
    }
    /**
     * Install PowerShell completion
     */
    installPowerShell(force = false) {
        const profilePath = process.env.PROFILE ||
            path.join(this.homeDir, 'Documents', 'PowerShell', 'Microsoft.PowerShell_profile.ps1');
        const completionPath = path.join(this.completionDir, 'ccs.ps1');
        if (!fs.existsSync(completionPath)) {
            throw new Error('Completion file not found. Please reinstall CCS.');
        }
        const marker = '# CCS shell completion';
        const sourceCmd = `. "${completionPath.replace(/\\/g, '\\\\')}"`;
        const block = `\n${marker}\n${sourceCmd}\n`;
        // Create profile directory if needed (with file conflict checking)
        const profileDir = path.dirname(profilePath);
        this.ensureDirectory(profileDir);
        // Check if already installed
        if (fs.existsSync(profilePath)) {
            const content = fs.readFileSync(profilePath, 'utf8');
            if (content.includes(marker)) {
                if (!force) {
                    return { success: true, alreadyInstalled: true };
                }
                // Force: completion files already updated by ensureCompletionFiles()
                return {
                    success: true,
                    message: `Updated completion files in ${this.completionDir}`,
                    reload: '. $PROFILE',
                };
            }
        }
        // Append to PowerShell profile
        fs.appendFileSync(profilePath, block);
        return {
            success: true,
            message: `Added to ${profilePath}`,
            reload: '. $PROFILE',
        };
    }
    /**
     * Install for detected or specified shell
     */
    install(shell = null, options = {}) {
        const targetShell = shell || this.detectShell();
        const { force = false } = options;
        if (!targetShell) {
            throw new Error('Could not detect shell. Please specify: --bash, --zsh, --fish, or --powershell');
        }
        // Ensure completion files exist (always copy latest)
        this.ensureCompletionFiles();
        // Install for target shell
        switch (targetShell) {
            case 'bash':
                return this.installBash(force);
            case 'zsh':
                return this.installZsh(force);
            case 'fish':
                return this.installFish(force);
            case 'powershell':
                return this.installPowerShell(force);
            default:
                throw new Error(`Unsupported shell: ${targetShell}`);
        }
    }
}
exports.ShellCompletionInstaller = ShellCompletionInstaller;
//# sourceMappingURL=shell-completion.js.map