const searchSchema = require('./data/eds-c-hero-search.schema.json');
const searchExample = require('./data/eds-c-hero-search.example.json');
const { createVariantTests } = require('./eds-c-hero.test');

describe('Hero Search', () => {
    const tests = createVariantTests('Search', searchSchema, searchExample, 'eds-c-hero-search');

    tests.schemaValidation();
    tests.accessibilityTest();

    tests.renderTest('.eds-c-hero__search', (example) => example.heroSearch);
    tests.renderTest('.eds-c-form', (example) => example.heroSearch);
    tests.renderTest('.eds-c-hero__title');
    tests.renderTest('.eds-c-hero__metadata-list', (example) => example.metadata);

    tests.customTest('renders search form with proper attributes', (rendered, example) => {
        if (example.heroSearch) {
            const searchRegion = document.querySelector('search');
            expect(searchRegion).toBeTruthy();
            expect(searchRegion.getAttribute('aria-label')).toBe(example.heroSearch.heroSearchAriaLabel);
            
            const searchForm = document.querySelector('.eds-c-form');
            expect(searchForm).toBeTruthy();
            
            if (example.heroSearch.url) {
                expect(searchForm.getAttribute('action')).toBe(example.heroSearch.url);
            }
        }
    });

    tests.customTest('renders search input when provided', (rendered, example) => {
        if (example.heroSearch && example.heroSearch.searchInput) {
            const searchInput = document.querySelector('input[type="search"]');
            expect(searchInput).toBeTruthy();
            
            if (example.heroSearch.searchInput.placeholder) {
                expect(searchInput.getAttribute('placeholder')).toBe(example.heroSearch.searchInput.placeholder);
            }
        }
    });

    tests.customTest('renders search button when provided', (rendered, example) => {
        if (example.heroSearch && example.heroSearch.searchBtn) {
            const searchButton = document.querySelector('.eds-c-form .eds-c-button');
            expect(searchButton).toBeTruthy();
            
            if (example.heroSearch.searchBtn.text) {
                expect(searchButton.textContent).toContain(example.heroSearch.searchBtn.text);
            }
        }
    });

    tests.customTest('renders advanced search link when provided', (rendered, example) => {
        if (example.heroSearch && example.heroSearch.advancedSearch) {
            const advancedLink = document.querySelector('.eds-c-form a');
            expect(advancedLink).toBeTruthy();
            expect(advancedLink.getAttribute('href')).toBe(example.heroSearch.advancedSearch.url);
        }
    });
});