"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerPlugin = registerPlugin;
exports.getPlugin = getPlugin;
exports.getAllPlugins = getAllPlugins;
exports.getAllPluginBlocks = getAllPluginBlocks;
exports.unregisterPlugin = unregisterPlugin;
exports.createPlugin = createPlugin;
exports.createBlock = createBlock;
exports.createActionBlock = createActionBlock;
exports.createValueBlock = createValueBlock;
exports.createAssertionBlock = createAssertionBlock;
const blocks_1 = require("./blocks");
// Plugin registry
const pluginRegistry = new Map();
// Register a plugin
function registerPlugin(plugin) {
    if (pluginRegistry.has(plugin.name)) {
        console.warn(`Plugin "${plugin.name}" is already registered. Overwriting...`);
    }
    pluginRegistry.set(plugin.name, plugin);
    // Register the plugin's blocks
    if (plugin.blocks && plugin.blocks.length > 0) {
        (0, blocks_1.registerBlocks)(plugin.blocks);
    }
    console.log(`Plugin registered: ${plugin.name} v${plugin.version}`);
}
// Get a registered plugin
function getPlugin(name) {
    return pluginRegistry.get(name);
}
// Get all registered plugins
function getAllPlugins() {
    return Array.from(pluginRegistry.values());
}
// Get all plugin blocks
function getAllPluginBlocks() {
    const blocks = [];
    for (const plugin of pluginRegistry.values()) {
        blocks.push(...plugin.blocks);
    }
    return blocks;
}
// Unregister a plugin
function unregisterPlugin(name) {
    return pluginRegistry.delete(name);
}
// Create a plugin helper
function createPlugin(config) {
    return {
        name: config.name,
        version: config.version,
        description: config.description,
        blocks: config.blocks || [],
        hooks: config.hooks,
    };
}
// Helper to create a custom block easily
function createBlock(config) {
    return {
        type: config.type,
        category: config.category,
        color: config.color || '#607D8B',
        tooltip: config.tooltip,
        helpUrl: config.helpUrl,
        inputs: config.inputs,
        output: config.output,
        previousStatement: config.previousStatement,
        nextStatement: config.nextStatement,
        execute: config.execute,
    };
}
/**
 * Create an action block - executes an action, can be chained with other blocks
 * Has previous/next statements, no output value
 */
function createActionBlock(config) {
    return createBlock({
        ...config,
        previousStatement: true,
        nextStatement: true,
    });
}
/**
 * Create a value block - returns a value that can be used as input to other blocks
 * Has output, no previous/next statements
 */
function createValueBlock(config) {
    return createBlock({
        ...config,
        output: { type: config.outputType },
        previousStatement: false,
        nextStatement: false,
    });
}
/**
 * Create an assertion block - validates a condition
 * Throws an error if assertion fails
 */
function createAssertionBlock(config) {
    return createBlock({
        type: config.type,
        category: config.category || 'Assertions',
        color: config.color || '#E91E63',
        tooltip: config.tooltip,
        inputs: config.inputs,
        previousStatement: true,
        nextStatement: true,
        execute: async (params, context) => {
            const result = await config.assert(params, context);
            if (!result.pass) {
                throw new Error(result.message || `Assertion failed: ${config.type}`);
            }
            context.logger.info(`✓ ${config.tooltip || config.type}`);
            return result;
        },
    });
}
