'use strict';

import * as chai from 'chai';
import {Script} from "../../../../source/dom/resource/script.mjs";
import {DataUrl} from "../../../../source/types/dataurl.mjs";
import {ID} from "../../../../source/types/id.mjs";
import {chaiDom} from "../../../util/chai-dom.mjs";
import {cleanupDOMFromTesting, initMutationObserverForTesting} from "../../../util/cleanupdom.mjs";
import {initJSDOM} from "../../../util/jsdom.mjs";

let expect = chai.expect;

chai.use(chaiDom);

let html1 = `

`;


describe('Script', function () {

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

    beforeEach(() => {
        initMutationObserverForTesting()
    })

    afterEach(() => {
        cleanupDOMFromTesting();
    })

    describe('Script()', function () {
        it('setEventTypes()', function (done) {

            const script = new Script({
                src: new DataUrl('', 'text/javascript').toString(),
                async: true,
                crossOrigin: 'anonymous',
                defer: false,
                integrity: undefined,
                nomodule: false,
                nonce: undefined,
                referrerpolicy: undefined,
                type: 'text/javascript'
            });

            script.connect().available().then(() => {

                const id = script.getOption('id')
                done()
            }).catch(e => done(e));

        })
    });

    describe('External JS', () => {

        let id = new ID('script').toString();
        let server, script, url = new DataUrl('console.log(1);', 'text/javascript').toString();

        beforeEach(() => {

            script = new Script({
                src: url,
                async: true,
                crossOrigin: 'anonymous',
                defer: false,
                integrity: undefined,
                nomodule: false,
                nonce: undefined,
                referrerpolicy: undefined,
                type: 'text/javascript',
                id: id
            });

        });

        const triggerLoad = () => {
            const node = document.getElementById(id);
            if (node) {
                node.dispatchEvent(new window.Event("load"));
            }
        };

        it('append and remove script ', (done) => {

            expect(script.isConnected()).to.be.false;

            script.connect();
            triggerLoad();
            script.available().then(() => {
                expect(script.isConnected()).to.be.true;
                expect(document.querySelector('[src="' + url + '"]')).to.exist;

                document.getElementById(id).remove();
                expect(script.isConnected()).to.be.false;
                expect(document.querySelector('[src="' + url + '"]')).not.to.exist;

                script.connect();
                triggerLoad();
                script.available().then(() => {
                    expect(script.isConnected()).to.be.true;
                    expect(document.querySelector('[src="' + url + '"]')).to.exist;

                    document.getElementById(id).remove();
                    expect(document.querySelector('[src="' + url + '"]')).not.to.exist;
                    expect(script.isConnected()).to.be.false;

                    done()
                }).catch(e => done(e));


            }).catch(e => done(e));


        });


    });


});
