const schema = require('./data/eds-c-footer.schema.json');
const exampleJson = require('./data/eds-c-footer.example.json');
const fs = require('node:fs');
const path = require('node:path');
const handlebars = require('handlebars');

const {findTemplates} = require('../../development/tests/templateLoader.js');
const { validateSchema } = require('../../development/tests/validate-schema');
const dataAttributesSchema = require('../eds-c-data-attributes/data/eds-c-data-attributes.schema.json');
findTemplates(['eds-c-footer', 'eds-c-data-attributes']).load(handlebars);

const componentTemplate = '{{> eds-c-footer}}';
const componentHbs = handlebars.compile(componentTemplate);
const renderedComponent = componentHbs(exampleJson);

describe('EDS component footer', () => {
    test('docs file exists', () => {
        const exists = fs.existsSync(path.resolve(__dirname, 'docs.md'));
        expect(exists).toBe(true);
    });
});

describe('footer Schema Validation and Accessibility Test', () => {

    const { configureAxe, toHaveNoViolations } = require('jest-axe');
    expect.extend(toHaveNoViolations);
    
    const axe = configureAxe({
        rules: {
            region: { enabled: false }
        }
    });

    test('Example data matches schema', () => {
        const valid = validateSchema(schema, exampleJson, [
            { name: 'eds-c-data-attributes', schema: dataAttributesSchema }
        ]);
        expect(valid).toBeTruthy();
    });
    
    test('Container should have correct attributes', () => {
        document.body.innerHTML = renderedComponent;
        const container = document.querySelector('.eds-c-footer');
        expect(container).not.toBeNull();
        expect(container.getAttribute('data-test')).toBe('footer-container-yes-test');
    });

    test('Should display user debug info', () => {
        document.body.innerHTML = renderedComponent;
        const userDebugInfo = document.querySelector('.eds-c-footer__user');
        expect(userDebugInfo).not.toBeNull();
        expect(userDebugInfo.textContent.replace(/\s+/g, ' ').trim()).toBe('Logged in as Harold Finch - 192.168.1.1 John Reese (123) - Sameen Shaw (456) - Lionel Fusco (789)');
    });

    test('Should display legal info', () => {
        document.body.innerHTML = renderedComponent;
        const userDebugInfo = document.querySelector('.eds-c-footer__legal');
        expect(userDebugInfo).not.toBeNull();
        expect(userDebugInfo.textContent.replace(/\s+/g, ' ').trim()).toBe('© 2023 Springer Nature');
    });

    test('Should display image', () => {
        document.body.innerHTML = renderedComponent;
        const linkElement = document.querySelector('.eds-c-footer__link');
        const imageElement = document.querySelector('.eds-c-footer__link img');
        expect(linkElement).not.toBeNull();
        expect(imageElement).not.toBeNull();
        expect(linkElement.getAttribute('href')).toBe('https://www.nature.com/siteindex');
        expect(imageElement.getAttribute('src')).toBe('../img/logo-springernature_white.svg');
        expect(imageElement.getAttribute('alt')).toBe('Springer Nature');
    });

    test('Should display navigation', () => {
        document.body.innerHTML = renderedComponent;
        const navigationElement = document.querySelector('.eds-c-footer__links');
        const listItems = navigationElement.querySelectorAll('.eds-c-footer__item');
        const listItemsButtons = navigationElement.querySelectorAll('button');
        const listItemsLinks = navigationElement.querySelectorAll('a');
        expect(navigationElement).not.toBeNull();
        expect(listItems).not.toBeNull();
        expect([...listItems].length).toBe(7);
        expect([...listItemsButtons].length).toBe(3);
        expect([...listItemsLinks].length).toBe(4);
    });

    test('Should display button', () => {
        document.body.innerHTML = renderedComponent;
        const buttonElement = document.querySelector('.eds-c-footer__links .eds-c-footer__item button');
        expect(buttonElement).not.toBeNull();
        expect(buttonElement.textContent.replace(/\s+/g, ' ').trim()).toBe('Your privacy choices/Manage cookies');
        expect(buttonElement.getAttribute('data-cc-action')).toBe('preferences');
        expect(buttonElement.getAttribute('data-test')).toBe('yes-test');
        expect(buttonElement.getAttribute('data-track-context')).toBe('Your privacy choices/Manage cookies');
        expect(buttonElement.getAttribute('data-track-content-type')).toBe('footer');
        expect(buttonElement.getAttribute('data-track-label')).toBe('footer');
    });

    test('Should display link', () => {
        document.body.innerHTML = renderedComponent;
        const linkElement = document.querySelector('.eds-c-footer__links .eds-c-footer__item a');
        expect(linkElement).not.toBeNull();
        expect(linkElement.textContent.replace(/\s+/g, ' ').trim()).toBe('Accessibility statement');
        expect(linkElement.getAttribute('href')).toBe('https://www.springernature.com/gp/info/accessibility');
        expect(linkElement.getAttribute('data-test')).toBe('yes-test');
        expect(linkElement.getAttribute('data-track-context')).toBe('Accessibility statement');
        expect(linkElement.getAttribute('data-track-content-type')).toBe('footer');
        expect(linkElement.getAttribute('data-track-label')).toBe('footer');
    });

    test('Should display product navigation', () => {
        document.body.innerHTML = renderedComponent;
        const navigationElement = document.querySelector('.eds-c-footer__container');
        const navigationElements = navigationElement.querySelectorAll('.eds-c-footer__group');
        expect(navigationElement).not.toBeNull();
        expect(navigationElements).not.toBeNull();
        expect([...navigationElements].length).toBe(6);
    });

    test('Should display product link', () => {
        document.body.innerHTML = renderedComponent;
        const linkElement = document.querySelector('.eds-c-footer__group a');
        const linkList = document.querySelectorAll('.eds-c-footer__group a');
        expect(linkElement).not.toBeNull();
        expect([...linkList].length).toBe(25);
        expect(linkElement.textContent.replace(/\s+/g, ' ').trim()).toBe('Journals A-Z');
        expect(linkElement.getAttribute('href')).toBe('https://www.nature.com/siteindex');
        expect(linkElement.getAttribute('data-test')).toBe('yes-test');
        expect(linkElement.getAttribute('data-track-context')).toBe('journals a-z');
        expect(linkElement.getAttribute('data-track-content-type')).toBe('footer');
        expect(linkElement.getAttribute('data-track-label')).toBe('footer');
    });

    test('Template passes basic HTML accessibility', async () => {
        expect(await axe(renderedComponent)).toHaveNoViolations();
    });
});
