// Import the required libraries
import { expect } from 'chai';
//import { JSDOM } from 'jsdom';
import { generateUniqueConfigKey } from '../../../../source/components/host/util.mjs';
import {initJSDOM} from "../../../util/jsdom.mjs";

// Create a JSDOM instance to simulate the browser environment
//const dom = new JSDOM();


// Test suite for the generateUniqueConfigKey function
describe('generateUniqueConfigKey', () => {

    //let originalWindow;

    // before(() => {
    //     // Store the original window object
    //     originalWindow = globalThis.window;
    //
    //     // Create a JSDOM instance to simulate the browser environment
    //     const dom = new JSDOM();
    //     globalThis.window = dom.window;
    // });


    before(function (done) {
        initJSDOM().then(() => {
            done();
        });
    })    

    // ... (same test cases as before)

    after(() => {
        // Restore the original window object
       // globalThis.window = originalWindow;
    });    
    
    it('should generate a unique key with the given parameters', () => {
        const componentName = 'MyComponent';
        const id = '123';
        const prefix = 'myPrefix';

        const uniqueKey = generateUniqueConfigKey(componentName, id, prefix);

        // Ensure the unique key contains the given parameters and follows the expected format
        expect(uniqueKey).to.include(prefix);
        expect(uniqueKey).to.include(componentName);
        expect(uniqueKey).to.include(id);
        expect(uniqueKey).to.match(/^[a-zA-Z0-9_]+$/);
    });

    it('should replace special characters and spaces with underscores', () => {
        const componentName = 'My$Component';
        const id = '12#3';
        const prefix = 'my Prefix';

        const uniqueKey = generateUniqueConfigKey(componentName, id, prefix);

        // Ensure the unique key does not contain any special characters or spaces
        expect(uniqueKey).to.match(/^[a-zA-Z0-9_]+$/);
    });

    it('should include the browser location without parameters', () => {
        const componentName = 'MyComponent';
        const id = '123';
        const prefix = 'myPrefix';

        const uniqueKey = generateUniqueConfigKey(componentName, id, prefix);

        // Ensure the unique key contains the browser location without parameters
        const urlWithoutParams = window.location.href.split('?')[0];
        const sanitizedUrl = urlWithoutParams.replace(/[^\w\s]/gi, '_').replace(/\s+/g, '_');
        expect(uniqueKey).to.include(sanitizedUrl);
    });

    
    
})