"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.PluginService = void 0;
const vim_1 = require("./vim");
const LinterPlugin_1 = require("./LinterPlugin");
const language_1 = require("./language");
const embedding_1 = require("./embedding");
const url_1 = require("./url");
const GitPlugin_1 = require("./GitPlugin");
const tmux_1 = require("./tmux");
const AgentsMdPlugin_1 = require("./AgentsMdPlugin");
const exec_1 = require("./exec");
const SkillsPlugin_1 = require("./SkillsPlugin");
class PluginService {
    pluginMap = new Map();
    events;
    constructor(context) {
        this.events = context.Events;
        context.Plugins = this;
        this.pluginMap.set("embeddings", new embedding_1.EmbeddingPlugin(context));
        this.pluginMap.set("vim", new vim_1.VimPlugin(context));
        this.pluginMap.set("linter", new LinterPlugin_1.LinterPlugin(context));
        this.pluginMap.set("language", new language_1.LanguagePlugin(context));
        this.pluginMap.set("url", new url_1.UrlPlugin(context));
        this.pluginMap.set("git", new GitPlugin_1.GitPlugin(context));
        this.pluginMap.set("tmux", new tmux_1.TmuxPlugin(context));
        this.pluginMap.set("agents-md", new AgentsMdPlugin_1.AgentsMdPlugin(context));
        this.pluginMap.set("exec", new exec_1.ExecPlugin(context));
        this.pluginMap.set("skills", new SkillsPlugin_1.SkillsPlugin(context));
    }
    async loadPlugin(spec) {
        const { default: PluginCtor } = await Promise.resolve(`${spec}`).then(s => __importStar(require(s)));
        const instance = new PluginCtor(this);
        this.pluginMap.set(instance.meta.key, instance);
        return instance.meta.key;
    }
    disablePlugin(key) {
        const p = this.pluginMap.get(key);
        if (!p)
            return false;
        p.disable();
        return true;
    }
    enablePlugin(key) {
        const p = this.pluginMap.get(key);
        if (!p)
            return false;
        p.enable();
        return true;
    }
    listPlugins() {
        const newPlugins = [...this.pluginMap.keys()];
        return newPlugins;
    }
    isPlugin(name) {
        return this.pluginMap.has(name);
    }
    registerPlugin(name, plugin) {
        this.pluginMap.set(name, plugin);
    }
    getPlugin(name) {
        return this.pluginMap.get(name);
    }
    getPlugins() {
        return Array.from(this.pluginMap.values());
    }
    async callMany(plugins, userInput) {
        if (!plugins || plugins.length === 0) {
            return "";
        }
        const calls = plugins.map(async (p) => {
            return this.callManyForPlugin(p, userInput).catch(() => "");
        });
        const results = await Promise.all(calls);
        return results.filter((result) => result !== "").join("\n\n");
    }
    async call(kind, userInput) {
        const newPlugin = this.pluginMap.get(kind);
        if (!newPlugin) {
            throw new Error(`Plugin ${kind} not found`);
        }
        const enabled = await newPlugin.isEnabled();
        if (!enabled) {
            this.events?.log("PluginService", `Plugin ${kind} is disabled, skipping`);
            return "";
        }
        return newPlugin.call(userInput);
    }
    async callManyForPlugin(kind, userInput) {
        const newPlugin = this.pluginMap.get(kind);
        if (!newPlugin) {
            throw new Error(`Plugin ${kind} not found`);
        }
        const enabled = await newPlugin.isEnabled();
        if (!enabled) {
            this.events?.log("PluginService", `Plugin ${kind} is disabled, skipping`);
            return "";
        }
        return newPlugin.callMany(userInput);
    }
    async embed(kind, userInput) {
        const newPlugin = this.pluginMap.get(kind);
        if (!newPlugin) {
            throw new Error(`Plugin ${kind} not found`);
        }
        const enabled = await newPlugin.isEnabled();
        if (!enabled) {
            this.events?.log("PluginService", `Plugin ${kind} is disabled, skipping`);
            return [];
        }
        return newPlugin.embed ? newPlugin.embed(userInput) : [];
    }
}
exports.PluginService = PluginService;
//# sourceMappingURL=plugins.js.map