"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VimPlugin = void 0;
const glob_1 = require("glob");
const utils_1 = require("../utils");
const PluginBase_1 = require("./PluginBase");
class VimPlugin extends PluginBase_1.PluginBase {
    static meta = {
        key: "vim",
        name: "Vim Plugin",
        requires: [],
    };
    meta = VimPlugin.meta;
    constructor(context) {
        super(context);
    }
    async embed(userPrompt) {
        return [];
    }
    async getVimFiles() {
        const vimFiles = (0, glob_1.globSync)("./**/*.swp", { dot: true });
        return vimFiles;
    }
    async getSourcePath(vimPath, dotFile = false) {
        const pathParts = vimPath.split("/");
        const fileName = pathParts[pathParts.length - 1];
        const cleanedFileName = fileName.slice(1, fileName.length - 4);
        const sourcePath = pathParts.slice(0, pathParts.length - 1).join("/");
        const finalPath = sourcePath + "/" + cleanedFileName;
        const dotFilePath = sourcePath + "/." + cleanedFileName;
        const finalFileExists = await (0, utils_1.fileExists)(finalPath);
        if (finalFileExists) {
            return finalPath;
        }
        const dotFileExists = await (0, utils_1.fileExists)(dotFilePath);
        if (dotFileExists) {
            return dotFilePath;
        }
        return finalPath;
    }
    async getFileContents(swapFile) {
        const filePath = await this.getSourcePath(swapFile);
        const exists = await (0, utils_1.fileExists)(filePath);
        if (!exists) {
            return { filePath, content: "FILE DOES NOT EXIST" };
        }
        const stat = await (0, utils_1.fileStat)(filePath);
        if (stat.isDirectory()) {
            return { filePath, content: "DIRECTORY" };
        }
        if (stat.size > 32000) {
            this.log(`VIM PLUGIN: File ${filePath} is too large with size ${stat.size}`, "error");
            return { filePath, content: "FILE TOO LARGE" };
        }
        this.log(`VIM PLUGIN: Reading file ${filePath}`);
        const content = await (0, utils_1.readFile)(filePath, "utf8");
        return { filePath, content };
    }
    async call() {
        const vimFiles = await this.getVimFiles();
        const fileContents = vimFiles.map((f) => {
            return {
                sourceFile: f,
            };
        });
        if (fileContents.length === 0) {
            return "VIM PLUGIN: No files open in vim";
        }
        return ("VIM PLUGIN: The following files are open in vim: " +
            JSON.stringify(fileContents));
    }
}
exports.VimPlugin = VimPlugin;
//# sourceMappingURL=vim.js.map