import {expect} from "chai"
import {ATTRIBUTE_OBJECTLINK} from "../../../source/dom/constants.mjs";
import {getLinkedObjects} from "../../../source/dom/attributes.mjs";
import {Provider} from "../../../source/i18n/provider.mjs";
import {initJSDOM} from "../../util/jsdom.mjs";
import {getDocumentTranslations, Translations} from "../../../source/i18n/translations.mjs";

describe('Provider', function () {


    let html1 = `
    <div id="test1">
    </div>
`;

    beforeEach(() => {
        let mocks = document.getElementById('mocks');
        mocks.innerHTML = html1;


    })

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

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

    describe('Provider and Dom', function () {

        const translationsLinkSymbol = Symbol.for("@schukai/monster/i18n/translations@@link");

        it('assignToElement', function (done) {
            const element = document.getElementById('test1');
            const p = new Provider();
            const r = p.assignToElement(undefined, element);

            r.then((e) => {
                const s = element.getAttribute(ATTRIBUTE_OBJECTLINK);
                if (s === null) {
                    done(new Error("Attribute not set"));
                    return;
                }

                const i = getLinkedObjects(element, translationsLinkSymbol)
                if (i === null) {
                    done(new Error("No linked object found"));
                    return;
                }
                let counter = 0;

                for (let v of i) {
                    counter++;
                }

                if (counter !== 1) {
                    done(new Error("No linked object found"));
                    return;
                }

                const docTrans = getDocumentTranslations(element)
                expect(docTrans).is.instanceof(Translations);


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

        });

    });


});