// Core Aladin functionality - extracted from main aladin.js
export class AladinCore {
    constructor() {
        this.extensions = new Map();
        this.hooks = new Map();
    }

    // Extension registration system
    registerExtension(name, extensionFactory) {
        if (this.extensions.has(name)) {
            console.warn(`Extension '${name}' is already registered`);
            return false;
        }
        
        this.extensions.set(name, extensionFactory);
        return true;
    }

    // Load and initialize extension
    async loadExtension(name, aladinInstance) {
        if (!this.extensions.has(name)) {
            console.error(`Extension '${name}' not found`);
            return false;
        }

        try {
            const extensionFactory = this.extensions.get(name);
            const extension = extensionFactory(aladinInstance);
            
            // Merge extension methods into aladin instance
            Object.assign(aladinInstance, extension);
            
            // Call extension initialization if available
            if (extension.initialize && typeof extension.initialize === 'function') {
                await extension.initialize();
            }
            
            return true;
        } catch (error) {
            console.error(`Error loading extension '${name}':`, error);
            return false;
        }
    }

    // Hook system for extension points
    addHook(hookName, callback) {
        if (!this.hooks.has(hookName)) {
            this.hooks.set(hookName, []);
        }
        this.hooks.get(hookName).push(callback);
    }

    async executeHook(hookName, ...args) {
        if (!this.hooks.has(hookName)) return;
        
        const callbacks = this.hooks.get(hookName);
        for (const callback of callbacks) {
            try {
                await callback(...args);
            } catch (error) {
                console.error(`Error executing hook '${hookName}':`, error);
            }
        }
    }
}

// Global extension registry
window.AladinExtensions = new AladinCore();

// Utility function for smooth scrolling to elements
window.scrollToElement = function(elementId, offset = 0) {
    const element = document.getElementById(elementId);
    if (element) {
        const elementPosition = element.getBoundingClientRect().top + window.pageYOffset;
        const offsetPosition = elementPosition - offset;
        
        window.scrollTo({
            top: offsetPosition,
            behavior: 'smooth'
        });
    }
};
