"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;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileManager = void 0;
const vscode = __importStar(require("vscode"));
const path = __importStar(require("path"));
class FileManager {
    ignoredDirs = [
        'node_modules', '.git', '.svn', '.hg', 'dist', 'build',
        'out', '.next', '.nuxt', '__pycache__', '.pytest_cache',
        'venv', '.venv', 'env', '.env', 'coverage', '.nyc_output',
        '.vscode', '.idea', 'vendor', 'packages', '.dart_tool'
    ];
    ignoredFiles = [
        '.DS_Store', 'Thumbs.db', '*.pyc', '*.pyo', '*.class',
        '*.exe', '*.dll', '*.so', '*.dylib', 'package-lock.json',
        'yarn.lock', 'pnpm-lock.yaml', '*.map', '*.min.js', '*.min.css'
    ];
    async getWorkspaceFiles() {
        const workspaceFolders = vscode.workspace.workspaceFolders;
        if (!workspaceFolders) {
            return [];
        }
        const files = [];
        for (const folder of workspaceFolders) {
            await this.scanDirectory(folder.uri, folder.uri, files);
        }
        return files;
    }
    async scanDirectory(rootUri, dirUri, files, depth = 0) {
        if (depth > 10)
            return; // Max depth
        try {
            const entries = await vscode.workspace.fs.readDirectory(dirUri);
            for (const [name, type] of entries) {
                if (this.shouldIgnore(name, type === vscode.FileType.Directory)) {
                    continue;
                }
                const entryUri = vscode.Uri.joinPath(dirUri, name);
                const relativePath = path.relative(rootUri.fsPath, entryUri.fsPath);
                if (type === vscode.FileType.Directory) {
                    files.push({
                        path: entryUri.fsPath,
                        relativePath,
                        name,
                        isDirectory: true
                    });
                    await this.scanDirectory(rootUri, entryUri, files, depth + 1);
                }
                else if (type === vscode.FileType.File) {
                    const stat = await vscode.workspace.fs.stat(entryUri);
                    files.push({
                        path: entryUri.fsPath,
                        relativePath,
                        name,
                        isDirectory: false,
                        size: stat.size,
                        language: this.getLanguageFromExtension(name)
                    });
                }
            }
        }
        catch (error) {
            console.error(`Error scanning directory ${dirUri.fsPath}:`, error);
        }
    }
    shouldIgnore(name, isDirectory) {
        if (name.startsWith('.') && name !== '.env.example') {
            return true;
        }
        if (isDirectory) {
            return this.ignoredDirs.includes(name);
        }
        return this.ignoredFiles.some(pattern => {
            if (pattern.startsWith('*')) {
                return name.endsWith(pattern.slice(1));
            }
            return name === pattern;
        });
    }
    getLanguageFromExtension(filename) {
        const ext = path.extname(filename).toLowerCase();
        const languageMap = {
            '.ts': 'typescript',
            '.tsx': 'typescriptreact',
            '.js': 'javascript',
            '.jsx': 'javascriptreact',
            '.py': 'python',
            '.java': 'java',
            '.cpp': 'cpp',
            '.c': 'c',
            '.h': 'c',
            '.hpp': 'cpp',
            '.cs': 'csharp',
            '.go': 'go',
            '.rs': 'rust',
            '.rb': 'ruby',
            '.php': 'php',
            '.swift': 'swift',
            '.kt': 'kotlin',
            '.scala': 'scala',
            '.r': 'r',
            '.sql': 'sql',
            '.html': 'html',
            '.css': 'css',
            '.scss': 'scss',
            '.sass': 'sass',
            '.less': 'less',
            '.json': 'json',
            '.xml': 'xml',
            '.yaml': 'yaml',
            '.yml': 'yaml',
            '.md': 'markdown',
            '.sh': 'shellscript',
            '.bash': 'shellscript',
            '.zsh': 'shellscript',
            '.ps1': 'powershell',
            '.vue': 'vue',
            '.svelte': 'svelte',
            '.dart': 'dart',
            '.lua': 'lua',
            '.ex': 'elixir',
            '.exs': 'elixir',
            '.clj': 'clojure',
            '.hs': 'haskell',
            '.ml': 'ocaml',
            '.fs': 'fsharp',
            '.elm': 'elm',
        };
        return languageMap[ext] || 'plaintext';
    }
    async readFile(filePath) {
        try {
            const uri = vscode.Uri.file(filePath);
            const content = await vscode.workspace.fs.readFile(uri);
            const textContent = Buffer.from(content).toString('utf8');
            return {
                path: filePath,
                content: textContent,
                language: this.getLanguageFromExtension(filePath)
            };
        }
        catch (error) {
            console.error(`Error reading file ${filePath}:`, error);
            return null;
        }
    }
    async writeFile(filePath, content) {
        try {
            const uri = vscode.Uri.file(filePath);
            const buffer = Buffer.from(content, 'utf8');
            await vscode.workspace.fs.writeFile(uri, buffer);
            return true;
        }
        catch (error) {
            console.error(`Error writing file ${filePath}:`, error);
            return false;
        }
    }
    async createFile(filePath, content) {
        return this.writeFile(filePath, content);
    }
    async deleteFile(filePath) {
        try {
            const uri = vscode.Uri.file(filePath);
            await vscode.workspace.fs.delete(uri);
            return true;
        }
        catch (error) {
            console.error(`Error deleting file ${filePath}:`, error);
            return false;
        }
    }
    async fileExists(filePath) {
        try {
            const uri = vscode.Uri.file(filePath);
            await vscode.workspace.fs.stat(uri);
            return true;
        }
        catch {
            return false;
        }
    }
    getOpenEditors() {
        return vscode.window.tabGroups.all
            .flatMap(group => group.tabs)
            .map(tab => {
            if (tab.input instanceof vscode.TabInputText) {
                return tab.input.uri.fsPath;
            }
            return null;
        })
            .filter((p) => p !== null);
    }
    async getActiveFileContent() {
        const activeEditor = vscode.window.activeTextEditor;
        if (!activeEditor) {
            return null;
        }
        return {
            path: activeEditor.document.uri.fsPath,
            content: activeEditor.document.getText(),
            language: activeEditor.document.languageId
        };
    }
}
exports.FileManager = FileManager;
//# sourceMappingURL=FileManager.js.map