"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("@testing-library/react");
const ExperienceBuilderIntegration_1 = require("../ExperienceBuilderIntegration");
// Mock Experience Builder - handle both named and default exports
jest.mock('@drupal/experience-builder', () => ({
    __esModule: true,
    default: {
        registerComponent: jest.fn(),
    },
    registerComponent: jest.fn(),
}));
// Mock accessibility system
jest.mock('../../components/accessibility/AccessibilitySystem', () => ({
    useAccessibility: () => ({
        analyzeComponent: jest.fn().mockResolvedValue({
            score: 100,
            issues: [],
            wcagCriteria: ['1.1.1', '1.4.3', '2.1.1', '2.4.4', '4.1.2'],
        }),
    }),
}));
describe('Experience Builder Integration', () => {
    beforeEach(() => {
        jest.clearAllMocks();
    });
    it('registers components with Experience Builder', async () => {
        const { registerComponent } = await Promise.resolve().then(() => __importStar(require('@drupal/experience-builder')));
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ExperienceBuilderIntegration_1.ExperienceBuilderProvider, { children: (0, jsx_runtime_1.jsx)("div", { children: "Test App" }) }));
        // Wait for component registration
        await (0, react_1.act)(async () => {
            await new Promise(resolve => setTimeout(resolve, 0));
        });
        // Check if components were registered
        expect(registerComponent).toHaveBeenCalledTimes(4);
        expect(registerComponent).toHaveBeenCalledWith('bfui-button', expect.any(Object));
        expect(registerComponent).toHaveBeenCalledWith('bfui-input', expect.any(Object));
        expect(registerComponent).toHaveBeenCalledWith('bfui-select', expect.any(Object));
        expect(registerComponent).toHaveBeenCalledWith('bfui-checkbox', expect.any(Object));
    });
    it('includes accessibility metadata in component definitions', async () => {
        const { registerComponent } = await Promise.resolve().then(() => __importStar(require('@drupal/experience-builder')));
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ExperienceBuilderIntegration_1.ExperienceBuilderProvider, { children: (0, jsx_runtime_1.jsx)("div", { children: "Test App" }) }));
        // Wait for component registration
        await (0, react_1.act)(async () => {
            await new Promise(resolve => setTimeout(resolve, 0));
        });
        // Check if accessibility metadata was included
        const calls = registerComponent.mock.calls;
        calls.forEach(([name, definition]) => {
            expect(definition.metadata).toBeDefined();
            expect(definition.metadata.accessibility).toBeDefined();
            expect(definition.metadata.accessibility.score).toBe(100);
            expect(definition.metadata.accessibility.issues).toEqual([]);
            expect(definition.metadata.accessibility.wcagCriteria).toEqual([
                '1.1.1',
                '1.4.3',
                '2.1.1',
                '2.4.4',
                '4.1.2',
            ]);
        });
    });
    it('provides component definitions for direct use', () => {
        const definitions = (0, ExperienceBuilderIntegration_1.getComponentDefinitions)();
        // Check if all components are available
        expect(definitions['bfui-button']).toBeDefined();
        expect(definitions['bfui-input']).toBeDefined();
        expect(definitions['bfui-select']).toBeDefined();
        expect(definitions['bfui-checkbox']).toBeDefined();
        // Check component structure
        Object.values(definitions).forEach(definition => {
            expect(definition.name).toBeDefined();
            expect(definition.label).toBeDefined();
            expect(definition.category).toBe('BFUI Components');
            expect(definition.fields).toBeDefined();
            expect(definition.preview).toBeDefined();
        });
    });
    it('exports individual components for direct use', () => {
        expect(ExperienceBuilderIntegration_1.ExperienceBuilderComponents.Button).toBeDefined();
        expect(ExperienceBuilderIntegration_1.ExperienceBuilderComponents.Input).toBeDefined();
        expect(ExperienceBuilderIntegration_1.ExperienceBuilderComponents.Select).toBeDefined();
        expect(ExperienceBuilderIntegration_1.ExperienceBuilderComponents.Checkbox).toBeDefined();
    });
    it('handles registration errors gracefully', async () => {
        const { registerComponent } = await Promise.resolve().then(() => __importStar(require('@drupal/experience-builder')));
        registerComponent.mockRejectedValueOnce(new Error('Registration failed'));
        const consoleError = jest.spyOn(console, 'error').mockImplementation(() => { });
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ExperienceBuilderIntegration_1.ExperienceBuilderProvider, { children: (0, jsx_runtime_1.jsx)("div", { children: "Test App" }) }));
        // Wait for component registration
        await (0, react_1.act)(async () => {
            await new Promise(resolve => setTimeout(resolve, 0));
        });
        expect(consoleError).toHaveBeenCalledWith('Failed to register components with Experience Builder:', expect.any(Error));
        consoleError.mockRestore();
    });
    describe('Component Fields', () => {
        it('defines required fields for Button component', () => {
            const buttonDef = ExperienceBuilderIntegration_1.ExperienceBuilderComponents.Button;
            expect(buttonDef.fields.label).toBeDefined();
            expect(buttonDef.fields.label.required).toBe(true);
            expect(buttonDef.fields.variant).toBeDefined();
            expect(buttonDef.fields.size).toBeDefined();
            expect(buttonDef.fields.icon).toBeDefined();
            expect(buttonDef.fields.iconPosition).toBeDefined();
            expect(buttonDef.fields.isLoading).toBeDefined();
            expect(buttonDef.fields.isDisabled).toBeDefined();
            expect(buttonDef.fields.onClick).toBeDefined();
        });
        it('defines required fields for Input component', () => {
            const inputDef = ExperienceBuilderIntegration_1.ExperienceBuilderComponents.Input;
            expect(inputDef.fields.label).toBeDefined();
            expect(inputDef.fields.label.required).toBe(true);
            expect(inputDef.fields.type).toBeDefined();
            expect(inputDef.fields.placeholder).toBeDefined();
            expect(inputDef.fields.helperText).toBeDefined();
            expect(inputDef.fields.error).toBeDefined();
            expect(inputDef.fields.isRequired).toBeDefined();
            expect(inputDef.fields.isDisabled).toBeDefined();
            expect(inputDef.fields.maxLength).toBeDefined();
            expect(inputDef.fields.showCounter).toBeDefined();
            expect(inputDef.fields.onChange).toBeDefined();
            expect(inputDef.fields.onBlur).toBeDefined();
        });
        it('defines required fields for Select component', () => {
            const selectDef = ExperienceBuilderIntegration_1.ExperienceBuilderComponents.Select;
            expect(selectDef.fields.label).toBeDefined();
            expect(selectDef.fields.label.required).toBe(true);
            expect(selectDef.fields.options).toBeDefined();
            expect(selectDef.fields.options.required).toBe(true);
            expect(selectDef.fields.placeholder).toBeDefined();
            expect(selectDef.fields.helperText).toBeDefined();
            expect(selectDef.fields.error).toBeDefined();
            expect(selectDef.fields.isRequired).toBeDefined();
            expect(selectDef.fields.isDisabled).toBeDefined();
            expect(selectDef.fields.isSearchable).toBeDefined();
            expect(selectDef.fields.onChange).toBeDefined();
        });
        it('defines required fields for Checkbox component', () => {
            const checkboxDef = ExperienceBuilderIntegration_1.ExperienceBuilderComponents.Checkbox;
            expect(checkboxDef.fields.label).toBeDefined();
            expect(checkboxDef.fields.label.required).toBe(true);
            expect(checkboxDef.fields.helperText).toBeDefined();
            expect(checkboxDef.fields.error).toBeDefined();
            expect(checkboxDef.fields.isRequired).toBeDefined();
            expect(checkboxDef.fields.isDisabled).toBeDefined();
            expect(checkboxDef.fields.isIndeterminate).toBeDefined();
            expect(checkboxDef.fields.onChange).toBeDefined();
        });
        describe('Radio component fields', () => {
            it('defines required fields for Radio component', () => {
                const radioFields = (0, ExperienceBuilderIntegration_1.getComponentDefinitions)().radio.fields;
                expect(radioFields.label.required).toBe(true);
                expect(radioFields.name.required).toBe(true);
                expect(radioFields.options.required).toBe(true);
                expect(radioFields.options.items.properties.label.required).toBe(true);
                expect(radioFields.options.items.properties.value.required).toBe(true);
            });
            it('defines optional fields for Radio component', () => {
                const radioFields = (0, ExperienceBuilderIntegration_1.getComponentDefinitions)().radio.fields;
                expect(radioFields.options.items.properties.description.required).toBeUndefined();
                expect(radioFields.options.items.properties.isDisabled.required).toBeUndefined();
                expect(radioFields.value.required).toBeUndefined();
                expect(radioFields.defaultValue.required).toBeUndefined();
                expect(radioFields.helperText.required).toBeUndefined();
                expect(radioFields.error.required).toBeUndefined();
                expect(radioFields.isRequired.required).toBeUndefined();
                expect(radioFields.isDisabled.required).toBeUndefined();
                expect(radioFields.isHorizontal.required).toBeUndefined();
                expect(radioFields.showDescriptions.required).toBeUndefined();
                expect(radioFields.className.required).toBeUndefined();
                expect(radioFields.optionsClassName.required).toBeUndefined();
                expect(radioFields.optionClassName.required).toBeUndefined();
                expect(radioFields.ariaLabel.required).toBeUndefined();
                expect(radioFields.ariaDescribedby.required).toBeUndefined();
                expect(radioFields.showAccessibilityIssues.required).toBeUndefined();
            });
        });
        describe('Tabs component fields', () => {
            it('defines required fields for Tabs component', () => {
                const tabsFields = (0, ExperienceBuilderIntegration_1.getComponentDefinitions)().tabs.fields;
                expect(tabsFields.id.required).toBe(true);
                expect(tabsFields.items.required).toBe(true);
                expect(tabsFields.items.items.properties.id.required).toBe(true);
                expect(tabsFields.items.items.properties.label.required).toBe(true);
                expect(tabsFields.items.items.properties.content.required).toBe(true);
            });
            it('defines optional fields for Tabs component', () => {
                const tabsFields = (0, ExperienceBuilderIntegration_1.getComponentDefinitions)().tabs.fields;
                expect(tabsFields.items.items.properties.description.required).toBeUndefined();
                expect(tabsFields.items.items.properties.isDisabled.required).toBeUndefined();
                expect(tabsFields.items.items.properties.icon.required).toBeUndefined();
                expect(tabsFields.selectedTabId.required).toBeUndefined();
                expect(tabsFields.defaultSelectedTabId.required).toBeUndefined();
                expect(tabsFields.isDisabled.required).toBeUndefined();
                expect(tabsFields.isLoading.required).toBeUndefined();
                expect(tabsFields.className.required).toBeUndefined();
                expect(tabsFields.tabListClassName.required).toBeUndefined();
                expect(tabsFields.tabClassName.required).toBeUndefined();
                expect(tabsFields.tabPanelClassName.required).toBeUndefined();
                expect(tabsFields.showDescriptions.required).toBeUndefined();
                expect(tabsFields.showIcons.required).toBeUndefined();
                expect(tabsFields.isVertical.required).toBeUndefined();
                expect(tabsFields.isCompact.required).toBeUndefined();
                expect(tabsFields.isFilled.required).toBeUndefined();
                expect(tabsFields.isOutlined.required).toBeUndefined();
                expect(tabsFields.isPill.required).toBeUndefined();
                expect(tabsFields.isAnimated.required).toBeUndefined();
                expect(tabsFields.ariaLabel.required).toBeUndefined();
                expect(tabsFields.ariaDescribedby.required).toBeUndefined();
                expect(tabsFields.tabPanelAriaLabel.required).toBeUndefined();
                expect(tabsFields.tabPanelAriaDescribedby.required).toBeUndefined();
                expect(tabsFields.showAccessibilityIssues.required).toBeUndefined();
            });
        });
    });
    describe('Component Previews', () => {
        it('renders Button preview with all props', () => {
            const buttonDef = ExperienceBuilderIntegration_1.ExperienceBuilderComponents.Button;
            const fields = {
                label: 'Test Button',
                variant: 'primary',
                size: 'md',
                icon: 'plus',
                iconPosition: 'start',
                isLoading: false,
                isDisabled: false,
                onClick: jest.fn(),
            };
            const { container } = (0, react_1.render)(buttonDef.preview({ fields }));
            expect(container).toMatchSnapshot();
        });
        it('renders Input preview with all props', () => {
            const inputDef = ExperienceBuilderIntegration_1.ExperienceBuilderComponents.Input;
            const fields = {
                label: 'Test Input',
                type: 'text',
                placeholder: 'Enter text',
                helperText: 'Helper text',
                error: '',
                isRequired: false,
                isDisabled: false,
                maxLength: 100,
                showCounter: true,
                onChange: jest.fn(),
                onBlur: jest.fn(),
            };
            const { container } = (0, react_1.render)(inputDef.preview({ fields }));
            expect(container).toMatchSnapshot();
        });
        it('renders Select preview with all props', () => {
            const selectDef = ExperienceBuilderIntegration_1.ExperienceBuilderComponents.Select;
            const fields = {
                label: 'Test Select',
                options: [
                    { label: 'Option 1', value: '1' },
                    { label: 'Option 2', value: '2', group: 'Group 1' },
                    { label: 'Option 3', value: '3', description: 'Description' },
                ],
                placeholder: 'Select an option',
                helperText: 'Helper text',
                error: '',
                isRequired: false,
                isDisabled: false,
                isSearchable: true,
                onChange: jest.fn(),
            };
            const { container } = (0, react_1.render)(selectDef.preview({ fields }));
            expect(container).toMatchSnapshot();
        });
        it('renders Checkbox preview with all props', () => {
            const checkboxDef = ExperienceBuilderIntegration_1.ExperienceBuilderComponents.Checkbox;
            const fields = {
                label: 'Test Checkbox',
                helperText: 'Helper text',
                error: '',
                isRequired: false,
                isDisabled: false,
                isIndeterminate: false,
                onChange: jest.fn(),
            };
            const { container } = (0, react_1.render)(checkboxDef.preview({ fields }));
            expect(container).toMatchSnapshot();
        });
        describe('Radio component preview', () => {
            it('renders Radio component with default options', () => {
                const { container } = (0, react_1.render)((0, ExperienceBuilderIntegration_1.getComponentDefinitions)().radio.preview({}));
                const radioGroup = container.querySelector('[role="radiogroup"]');
                expect(radioGroup).toBeInTheDocument();
                expect(radioGroup).toHaveAttribute('aria-label', 'Radio options');
                const options = container.querySelectorAll('input[type="radio"]');
                expect(options).toHaveLength(3);
                expect(options[0]).toHaveAttribute('value', '1');
                expect(options[1]).toHaveAttribute('value', '2');
                expect(options[2]).toHaveAttribute('value', '3');
            });
            it('renders Radio component with custom options', () => {
                const customOptions = [
                    { label: 'Custom 1', value: 'a' },
                    { label: 'Custom 2', value: 'b', description: 'Custom description' },
                ];
                const { container } = (0, react_1.render)((0, ExperienceBuilderIntegration_1.getComponentDefinitions)().radio.preview({ options: customOptions }));
                const options = container.querySelectorAll('input[type="radio"]');
                expect(options).toHaveLength(2);
                expect(options[0]).toHaveAttribute('value', 'a');
                expect(options[1]).toHaveAttribute('value', 'b');
                const descriptions = container.querySelectorAll('[data-description]');
                expect(descriptions).toHaveLength(1);
                expect(descriptions[0]).toHaveTextContent('Custom description');
            });
        });
        describe('Tabs component preview', () => {
            it('renders Tabs component with default items', () => {
                const { container } = (0, react_1.render)((0, ExperienceBuilderIntegration_1.getComponentDefinitions)().tabs.preview({}));
                const tabList = container.querySelector('[role="navigation"]');
                expect(tabList).toBeInTheDocument();
                const tabs = container.querySelectorAll('button[data-selected]');
                expect(tabs).toHaveLength(3);
                expect(tabs[0]).toHaveTextContent('Tab 1');
                expect(tabs[1]).toHaveTextContent('Tab 2');
                expect(tabs[2]).toHaveTextContent('Tab 3');
                expect(tabs[2]).toHaveAttribute('data-disabled', 'true');
                const panels = container.querySelectorAll('[data-panel]');
                expect(panels).toHaveLength(3);
                expect(panels[0]).toHaveTextContent('Content for tab 1');
                expect(panels[1]).toHaveTextContent('Content for tab 2');
                expect(panels[2]).toHaveTextContent('Content for tab 3');
            });
            it('renders Tabs component with custom items', () => {
                const customItems = [
                    {
                        id: 'custom1',
                        label: 'Custom Tab 1',
                        content: 'Custom content 1',
                        description: 'Custom description 1',
                    },
                    {
                        id: 'custom2',
                        label: 'Custom Tab 2',
                        content: 'Custom content 2',
                        isDisabled: true,
                    },
                ];
                const { container } = (0, react_1.render)((0, ExperienceBuilderIntegration_1.getComponentDefinitions)().tabs.preview({ items: customItems }));
                const tabs = container.querySelectorAll('button[data-selected]');
                expect(tabs).toHaveLength(2);
                expect(tabs[0]).toHaveTextContent('Custom Tab 1');
                expect(tabs[1]).toHaveTextContent('Custom Tab 2');
                expect(tabs[1]).toHaveAttribute('data-disabled', 'true');
                const panels = container.querySelectorAll('[data-panel]');
                expect(panels).toHaveLength(2);
                expect(panels[0]).toHaveTextContent('Custom content 1');
                expect(panels[1]).toHaveTextContent('Custom content 2');
                const descriptions = container.querySelectorAll('[data-description]');
                expect(descriptions).toHaveLength(1);
                expect(descriptions[0]).toHaveTextContent('Custom description 1');
            });
        });
    });
    describe('component exports', () => {
        it('exports all components for direct use', () => {
            expect(ExperienceBuilderIntegration_1.ExperienceBuilderComponents.Button).toBeDefined();
            expect(ExperienceBuilderIntegration_1.ExperienceBuilderComponents.Input).toBeDefined();
            expect(ExperienceBuilderIntegration_1.ExperienceBuilderComponents.Select).toBeDefined();
            expect(ExperienceBuilderIntegration_1.ExperienceBuilderComponents.Checkbox).toBeDefined();
            expect(ExperienceBuilderIntegration_1.ExperienceBuilderComponents.Radio).toBeDefined();
            expect(ExperienceBuilderIntegration_1.ExperienceBuilderComponents.Tabs).toBeDefined();
        });
    });
});
//# sourceMappingURL=ExperienceBuilderIntegration.test.js.map