const schema = require('./data/eds-c-header-new.schema.json');
const exampleJson = require('./data/eds-c-header-new.example.json');
const fs = require('fs');
const path = require('path');
const partialsDir = __dirname;
const componentsDir = path.join(__dirname, '..');
const filenames = fs.readdirSync(partialsDir);
const headerNewTemplate = fs.readFileSync(path.resolve(__dirname, 'eds-c-header-new.hbs'), 'utf8');
const navigationTemplatePath = path.resolve(componentsDir, 'eds-c-navigation', 'eds-c-navigation.hbs');
const navigationTemplate = fs.readFileSync(navigationTemplatePath, 'utf8');
const accordionTemplatePath = path.resolve(componentsDir, 'eds-c-accordion', 'eds-c-accordion.hbs');
const accordionTemplate = fs.readFileSync(accordionTemplatePath, 'utf8');
const formSchema = require('../eds-c-form/data/eds-c-form.schema.json');
const inputSchema = require('../eds-c-input/data/eds-c-input.schema.json');
const errorSummarySchema = require('../eds-c-error-summary/data/eds-c-error-summary.schema.json');
const errorMessageSchema = require('../eds-c-error-message/data/eds-c-error-message.schema.json');
const labelSchema = require('../eds-c-label/data/eds-c-label.schema.json');
const hintSchema = require('../eds-c-hint/data/eds-c-hint.schema.json');
const buttonSchema = require('../eds-c-button/data/eds-c-button.schema.json');
const navigationSchema = require('../eds-c-navigation/data/eds-c-navigation.schema.json');
const accordionSchema = require('../eds-c-accordion/data/eds-c-accordion.schema.json');

const Ajv = require("ajv");
const ajv = new Ajv();
const handlebars = require('handlebars');

// register the partials that are used inside the component main template
handlebars.registerPartial('eds-c-navigation', navigationTemplate);
handlebars.registerPartial('eds-c-accordion', accordionTemplate);

const templateHeader = handlebars.compile(headerNewTemplate);
const renderedHeader = templateHeader(exampleJson);

describe('EDS Component Header New', () => {

    const element = {};

    beforeEach(() => {
        document.body.innerHTML = renderedHeader;
        element.header = document.querySelector('[data-eds-c-header-new]');

    });

    afterEach(() => {
        document.body.innerHTML = '';
    });

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

describe('Header New Schema Validation and Accessibility Test', () => {    
    filenames.forEach(function (filename) {
        var matches = /^([^.]+).hbs$/.exec(filename);
        if (!matches) {
            return;
        }
        var name = matches[1];
        var template = fs.readFileSync(partialsDir + '/' + filename, 'utf8');
        handlebars.registerPartial(name, template);
    });
    
    const { configureAxe, toHaveNoViolations } = require('jest-axe');
    expect.extend(toHaveNoViolations);
    
    const axe = configureAxe({
        rules: {
            region: { enabled: false }
        }
    });
    
    test('Example data matches schema', () => {
        ajv.addSchema(formSchema, 'formSchema');
        ajv.addSchema(inputSchema, 'inputSchema');
        ajv.addSchema(errorSummarySchema, 'errorSummarySchema');
        ajv.addSchema(errorMessageSchema, 'errorMessageSchema');
        ajv.addSchema(labelSchema, 'labelSchema');
        ajv.addSchema(hintSchema, 'hintSchema');
        ajv.addSchema(buttonSchema, 'buttonSchema');
        ajv.addSchema(navigationSchema, 'navigationSchema');
        ajv.addSchema(accordionSchema, 'accordionSchema');
        
        const validate = ajv.compile(schema);
        const valid = validate(exampleJson);
    
        if (!valid) {
            // Extract and format the Ajv validation errors
            const errors = validate.errors.map((error) => ajv.errorsText([error])).join('\n');
            throw new Error(errors);
        }
    
        expect(valid).toBeTruthy();
    });
    
    test('Template can be compiled and rendered', () => {
        document.body.innerHTML = renderedHeader;
        const container = document.querySelector('*');
        expect(container).toBeDefined();
    });
    
    test('Template passes basic HTML accessibility', async () => {
        expect(await axe(renderedHeader)).toHaveNoViolations();
    });
});
