/**
 * ATP Compiler Plugin API
 *
 * Extensible plugin system for custom transformations and detections
 */
/**
 * Plugin registry for managing plugins
 */
export class PluginRegistry {
    detectors = [];
    transformers = [];
    optimizers = [];
    validators = [];
    /**
     * Register a plugin
     */
    register(plugin) {
        // Check for name conflicts
        if (this.findPlugin(plugin.name)) {
            throw new Error(`Plugin "${plugin.name}" is already registered. ` +
                `Please use a unique name or unregister the existing plugin first.`);
        }
        // Type guards to determine plugin type
        if (this.isDetectionPlugin(plugin)) {
            this.detectors.push(plugin);
            this.detectors.sort((a, b) => (b.priority || 50) - (a.priority || 50));
        }
        if (this.isTransformationPlugin(plugin)) {
            this.transformers.push(plugin);
            this.transformers.sort((a, b) => (b.priority || 50) - (a.priority || 50));
        }
        if (this.isOptimizerPlugin(plugin)) {
            this.optimizers.push(plugin);
            this.optimizers.sort((a, b) => (b.priority || 50) - (a.priority || 50));
        }
        if (this.isValidatorPlugin(plugin)) {
            this.validators.push(plugin);
            this.validators.sort((a, b) => (b.priority || 50) - (a.priority || 50));
        }
    }
    /**
     * Find a plugin by name
     */
    findPlugin(name) {
        const allPlugins = [
            ...this.detectors,
            ...this.transformers,
            ...this.optimizers,
            ...this.validators,
        ];
        return allPlugins.find((p) => p.name === name);
    }
    /**
     * Unregister a plugin by name
     */
    unregister(name) {
        let removed = false;
        this.detectors = this.detectors.filter((p) => {
            if (p.name === name) {
                removed = true;
                return false;
            }
            return true;
        });
        this.transformers = this.transformers.filter((p) => {
            if (p.name === name) {
                removed = true;
                return false;
            }
            return true;
        });
        this.optimizers = this.optimizers.filter((p) => {
            if (p.name === name) {
                removed = true;
                return false;
            }
            return true;
        });
        this.validators = this.validators.filter((p) => {
            if (p.name === name) {
                removed = true;
                return false;
            }
            return true;
        });
        return removed;
    }
    /**
     * Get all detection plugins
     */
    getDetectors() {
        return this.detectors;
    }
    /**
     * Get all transformation plugins
     */
    getTransformers() {
        return this.transformers;
    }
    /**
     * Get all optimizer plugins
     */
    getOptimizers() {
        return this.optimizers;
    }
    /**
     * Get all validator plugins
     */
    getValidators() {
        return this.validators;
    }
    /**
     * Initialize all plugins
     */
    async initializeAll(config) {
        const allPlugins = [
            ...this.detectors,
            ...this.transformers,
            ...this.optimizers,
            ...this.validators,
        ];
        for (const plugin of allPlugins) {
            if (plugin.initialize) {
                await plugin.initialize(config);
            }
        }
    }
    /**
     * Dispose all plugins
     */
    async disposeAll() {
        const allPlugins = [
            ...this.detectors,
            ...this.transformers,
            ...this.optimizers,
            ...this.validators,
        ];
        for (const plugin of allPlugins) {
            if (plugin.dispose) {
                await plugin.dispose();
            }
        }
    }
    /**
     * Type guards
     */
    isDetectionPlugin(plugin) {
        return 'detect' in plugin && 'patterns' in plugin;
    }
    isTransformationPlugin(plugin) {
        return 'getVisitor' in plugin && 'getMetadata' in plugin && 'reset' in plugin;
    }
    isOptimizerPlugin(plugin) {
        return 'optimize' in plugin;
    }
    isValidatorPlugin(plugin) {
        return 'validate' in plugin;
    }
}
/**
 * Utility function to create a simple transformation plugin
 */
export function createTransformPlugin(config) {
    let transformCount = 0;
    return {
        name: config.name,
        version: config.version,
        priority: config.priority || 50,
        getVisitor() {
            return config.visitor;
        },
        getMetadata() {
            return config.getMetadata ? config.getMetadata() : { loopCount: transformCount };
        },
        reset() {
            transformCount = 0;
        },
    };
}
//# sourceMappingURL=plugin-api.js.map