const schema = require('./data/eds-c-navigation.schema.json');
const exampleJson = require('./data/eds-c-navigation.example.json');
const fs = require('fs');
const path = require('path');
const componentsDir = path.join(__dirname, '..');
const partialsDir = __dirname;
const filenames = fs.readdirSync(partialsDir);
const componentTemplate = fs.readFileSync(path.resolve(__dirname, 'eds-c-navigation.hbs'), 'utf8');
const accordionTemplatePath = path.resolve(componentsDir, 'eds-c-accordion', 'eds-c-accordion.hbs');
const accordionTemplate = fs.readFileSync(accordionTemplatePath, 'utf8');
const Ajv = require("ajv");
const ajv = new Ajv();
const handlebars = require('handlebars');

handlebars.registerPartial('eds-c-accordion', accordionTemplate);

const componentHbs = handlebars.compile(componentTemplate);
const renderedComponent = componentHbs(exampleJson);

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

describe('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', () => {
        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 = renderedComponent;
        const container = document.querySelector('*');
        expect(container).toBeDefined();
    });
    
    test('Template passes basic HTML accessibility', async () => {
        expect(await axe(renderedComponent)).toHaveNoViolations();
    });
});