'use strict';

import {expect} from "chai";
import {FocusManager} from "../../../source/dom/focusmanager.mjs";
import {cleanupDOMFromTesting, initMutationObserverForTesting} from "../../util/cleanupdom.mjs";
import {initJSDOM} from "../../util/jsdom.mjs";

describe('FocusManager', function () {
    before(function (done) {
        initJSDOM().then(() => {
            done()
        });
    });

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

    afterEach(() => {
        cleanupDOMFromTesting();
        document.getElementById('mocks').innerHTML = ''
    })

    describe('new', function () {

        it('should instance of FocusManager', function () {
            expect(new FocusManager()).is.instanceof(FocusManager);
        })

    })

    describe('store and restore', function () {
        let manager;

        beforeEach(() => {
            manager = new FocusManager();
            document.getElementById('mocks').innerHTML = `
            <input class="testit" id="tx1">
            <input class="testit" id="tx2">
            <input class="testit" id="tx3" disabled>
            <input class="testit" id="tx4">
            <p class="testit" tabindex="2" id="tx5">hello</p>
            <input class="testit" id="tx6" aria-hidden="true">
            <button class="testit" id="tx7"></button>
            `;

            for (const [, element] of Object.entries(document.querySelectorAll('.testit'))) {
                element.getBoundingClientRect = () => ({
                    width: 200,
                    height: 200,
                    top: 0,
                    left: 0,
                    right: 200,
                    bottom: 200,
                });
            }

        })

        it('run ist', function () {

            manager.focus(document.getElementById('tx1'));
            manager.storeFocus();

            const e1 = manager.getActive();
            expect(e1.getAttribute('id'), 'error1').to.be.equal('tx1');

            manager.focus(document.getElementById('tx2'));

            manager.storeFocus();
            expect(manager.getActive().getAttribute('id'), 'error2').to.be.equal('tx2');
            manager.focusNext();
            expect(manager.getActive().getAttribute('id'), 'error3').to.be.equal('tx4');
            manager.focusNext();
            expect(manager.getActive().getAttribute('id'), 'error4').to.be.equal('tx5');
            manager.restoreFocus();
            expect(manager.getActive().getAttribute('id'), 'error5').to.be.equal('tx2');
            manager.restoreFocus();
            expect(manager.getActive().getAttribute('id'), 'error6').to.be.equal('tx1');
            manager.restoreFocus();
            expect(manager.getActive().getAttribute('id'), 'error7').to.be.equal('tx1');
        })


    })

    describe('returntype', function () {

        let focusmanager;

        beforeEach(() => {
            focusmanager = new FocusManager();
        })

        it('check returns', function () {

            expect(focusmanager.getFocusable()).is.instanceof(Array);
            expect(focusmanager.getActive()).is.instanceof(HTMLElement);
            expect(focusmanager.focusNext()).is.instanceof(FocusManager);
            expect(focusmanager.focusPrev()).is.instanceof(FocusManager);
            expect(focusmanager.storeFocus()).is.instanceof(FocusManager);
            expect(focusmanager.focus(document.body)).is.instanceof(FocusManager);
            expect(focusmanager.restoreFocus()).is.instanceof(FocusManager);

        })

    })


});