(function(wp) {
    var el = wp.element.createElement;
    var Fragment = wp.element.Fragment;
    var BlockControls = wp.blockEditor.BlockControls;
    var ToolbarDropdownMenu = wp.components.ToolbarDropdownMenu;
    var registerPlugin = wp.plugins.registerPlugin;

    var languages = [
        { label: '普通文本', value: 'plaintext' },
        { label: 'PHP', value: 'php' },
        { label: 'Blade', value: 'blade' },    // 添加 Laravel Blade 支持
        { label: 'JavaScript', value: 'javascript' },
        { label: 'HTML', value: 'html' },
        { label: 'CSS', value: 'css' },
        { label: 'SQL', value: 'sql' },
        { label: 'Bash', value: 'bash' },
        { label: 'PowerShell', value: 'powershell' },
        { label: 'Windows 批处理', value: 'batch' },
        { label: 'C#', value: 'csharp' },
        { label: 'Razor', value: 'cshtml' },    // 添加 Razor 支持
        { label: 'C++', value: 'cpp' },
        { label: 'C', value: 'c' },
        { label: 'Java', value: 'java' },
        { label: 'Kotlin', value: 'kotlin' },    // 添加 Kotlin 支持
        { label: 'Python', value: 'python' },
        { label: 'Delphi', value: 'pascal' },
        { label: 'BASIC', value: 'basic' },
        { label: 'Nginx', value: 'nginx' },
        { label: 'Apache', value: 'apacheconf' },
        { label: 'XML', value: 'xml' },
        { label: 'YAML', value: 'yaml' },
        { label: 'JSON', value: 'json' },
        { label: 'INI', value: 'ini' },
    ];

    wp.hooks.addFilter(
        'blocks.registerBlockType',
        'techcode-prism/code-block-language',
        function(settings, name) {
            if (name !== 'core/code') return settings;

            return Object.assign({}, settings, {
                attributes: Object.assign({}, settings.attributes, {
                    language: {
                        type: 'string',
                        default: 'plaintext'
                    }
                })
            });
        }
    );

    wp.hooks.addFilter(
        'editor.BlockEdit',
        'techcode-prism/with-language-selector',
        function(BlockEdit) {
            return function(props) {
                if (props.name !== 'core/code') {
                    return el(BlockEdit, props);
                }

                // 获取当前语言
                let currentLanguage = 'plaintext';
                const className = props.attributes.className || '';
                const match = className.match(/language-(\w+)/);
                if (match) {
                    currentLanguage = match[1];
                }

                // 同步 language 属性
                if (props.attributes.language !== currentLanguage) {
                    props.setAttributes({ language: currentLanguage });
                }

                return el(
                    Fragment,
                    null,
                    el(BlockEdit, props),
                    el(
                        BlockControls,
                        null,
                        el(ToolbarDropdownMenu, {
                            icon: 'editor-code',
                            label: '选择语言',
                            // 显示当前选中的语言
                            text: languages.find(lang => lang.value === currentLanguage)?.label || '选择语言',
                            popoverProps: {
                                position: 'bottom right'
                            },
                            controls: languages.map(function(lang) {
                                const isSelected = currentLanguage === lang.value;
                                return {
                                    title: lang.label,
                                    // 添加选中图标
                                    icon: isSelected ? 'yes' : '',
                                    // 设置选中状态
                                    isActive: isSelected,
                                    role: 'menuitemradio',
                                    onClick: function() {
                                        props.setAttributes({
                                            language: lang.value,
                                            // 保持原有的 wp-block-code 类名
                                            className: `wp-block-code language-${lang.value}`
                                        });
                                    }
                                };
                            })
                        })
                    )
                );
            };
        }
    );
})(window.wp);