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

describe('Hero Search Autocomplete', () => {
    const tests = createVariantTests('Search Autocomplete', searchAutocompleteSchema, searchAutocompleteExample, 'eds-c-hero-search-autocomplete');

    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 autocomplete component when provided', (rendered, example) => {
        if (example.heroSearch && example.heroSearch.searchAutocomplete) {
            const autocompleteWrapper = document.querySelector('[data-eds-c-autocomplete]');
            expect(autocompleteWrapper).toBeTruthy();
            
            const autocompleteInput = document.querySelector('input[type="text"]');
            expect(autocompleteInput).toBeTruthy();
            
            if (example.heroSearch.searchAutocomplete.input) {
                expect(autocompleteInput.getAttribute('id')).toBe(example.heroSearch.searchAutocomplete.input.inputId);
                expect(autocompleteInput.getAttribute('name')).toBe(example.heroSearch.searchAutocomplete.input.name);
            }

            const autocompleteDropdown = document.querySelector('.eds-c-autocomplete__dropdown');
            expect(autocompleteDropdown).toBeTruthy();
        }
    });

    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);
        }
    });

    tests.customTest('renders hint text for autocomplete input', (rendered, example) => {
        if (example.heroSearch && example.heroSearch.searchAutocomplete && example.heroSearch.searchAutocomplete.input && example.heroSearch.searchAutocomplete.input.hint) {
            const hintText = document.querySelector('.eds-c-hint span');
            expect(hintText).toBeTruthy();
            expect(hintText.textContent).toContain(example.heroSearch.searchAutocomplete.input.hint.hintText);
        }
    });
});
