const registry = new Map();
export function registerToolPlugin(plugin) {
    if (!plugin?.id?.trim()) {
        throw new Error('Tool plugin id cannot be blank.');
    }
    registry.set(plugin.id, normalizePlugin(plugin));
}
export function unregisterToolPlugin(id) {
    registry.delete(id);
}
export function listRegisteredToolPlugins() {
    return Array.from(registry.values());
}
export async function instantiateToolPlugins(target, context, options = {}) {
    const modules = [];
    for (const plugin of registry.values()) {
        if (!supportsTarget(plugin, target)) {
            continue;
        }
        if (options.filter && !options.filter(plugin)) {
            continue;
        }
        const result = await plugin.create(context);
        if (!result) {
            continue;
        }
        if (Array.isArray(result)) {
            for (const entry of result) {
                if (entry) {
                    modules.push(entry);
                }
            }
        }
        else {
            modules.push(result);
        }
    }
    return modules;
}
function normalizePlugin(plugin) {
    if (!plugin.targets?.length) {
        return {
            ...plugin,
            targets: ['universal'],
        };
    }
    return {
        ...plugin,
        targets: Array.from(new Set(plugin.targets)),
    };
}
function supportsTarget(plugin, target) {
    if (plugin.targets.includes('universal')) {
        return true;
    }
    return plugin.targets.includes(target);
}
//# sourceMappingURL=registry.js.map