// Maple Code Blocks - Simple Block Registration
console.log('Maple Code Blocks: Starting block registration');

(function(wp) {
    // Check if wp.blocks exists
    if (!wp || !wp.blocks) {
        console.error('Maple Code Blocks: wp.blocks not available');
        return;
    }
    
    const { registerBlockType } = wp.blocks;
    const { TextControl, PanelBody, SelectControl } = wp.components;
    const { InspectorControls } = wp.blockEditor || wp.editor || {};
    const { Fragment, createElement: el } = wp.element;
    
    console.log('Maple Code Blocks: Dependencies loaded, registering block...');
    
    // Register the main Maple Code Block
    const blockRegistered = registerBlockType('maple-code-blocks/code-block', {
        title: 'Maple Code Block',
        description: 'Display code from GitHub, GitLab, Bitbucket or Codeberg',
        category: 'widgets',
        icon: 'editor-code',
        keywords: ['maple', 'code', 'github', 'gitlab', 'bitbucket'],
        attributes: {
            repository: {
                type: 'string',
                default: ''
            },
            theme: {
                type: 'string',
                default: 'dark'
            },
            height: {
                type: 'string',
                default: '600px'
            }
        },
        
        edit: function(props) {
            const { attributes, setAttributes } = props;
            const { repository, theme, height } = attributes;
            
            return el(Fragment, {},
                el(InspectorControls, {},
                    el(PanelBody, { title: 'Repository Settings', initialOpen: true },
                        el(TextControl, {
                            label: 'Repository',
                            value: repository,
                            onChange: function(value) { setAttributes({ repository: value }) },
                            placeholder: 'e.g., facebook/react or gitlab:gnome/gimp',
                            help: 'Format: [platform:]owner/repo. Platforms: github (default), gitlab, bitbucket, codeberg. Examples: facebook/react, gitlab:gitlab-org/gitlab, bitbucket:atlassian/python-bitbucket, codeberg:forgejo/forgejo'
                        }),
                        el(SelectControl, {
                            label: 'Theme',
                            value: theme,
                            options: [
                                { label: 'Dark', value: 'dark' },
                                { label: 'Light', value: 'light' },
                                { label: 'Monokai', value: 'monokai' },
                                { label: 'Solarized', value: 'solarized' }
                            ],
                            onChange: function(value) { setAttributes({ theme: value }) }
                        }),
                        el(TextControl, {
                            label: 'Height',
                            value: height,
                            onChange: function(value) { setAttributes({ height: value }) },
                            placeholder: '600px'
                        })
                    )
                ),
                el('div', { 
                    className: 'maple-code-block-editor',
                    style: { 
                        padding: '20px', 
                        backgroundColor: theme === 'dark' ? '#1e1e1e' : '#fff',
                        color: theme === 'dark' ? '#fff' : '#000',
                        border: '1px solid #ddd',
                        borderRadius: '4px'
                    }
                },
                    el('h3', { style: { marginTop: 0 } }, 'Maple Code Block'),
                    repository ? 
                        el('p', {}, 'Repository: ', el('strong', {}, repository)) :
                        el('p', { style: { color: '#999' } }, 'Enter a repository in the block settings'),
                    el('p', {}, 'Theme: ', el('strong', {}, theme)),
                    el('p', {}, 'Height: ', el('strong', {}, height))
                )
            );
        },
        
        save: function() {
            // Rendered server-side
            return null;
        }
    });
    
    if (blockRegistered) {
        console.log('Maple Code Blocks: Block registered successfully!', blockRegistered);
    } else {
        console.error('Maple Code Blocks: Block registration failed');
    }
    
})(window.wp);

// Also try registering with global wp if window.wp fails
if (typeof wp !== 'undefined' && wp.blocks && !wp.blocks.getBlockType('maple-code-blocks/code-block')) {
    console.log('Maple Code Blocks: Attempting registration with global wp');
    wp.blocks.registerBlockType('maple-code-blocks/code-block', {
        title: 'Maple Code Block',
        description: 'Display code from repositories',
        category: 'widgets',
        icon: 'editor-code',
        edit: function() { return wp.element.createElement('div', {}, 'Maple Code Block'); },
        save: function() { return null; }
    });
}
