import {expect} from "chai"
import {parseLocale} from "../../../source/i18n/locale.mjs";
import {Embed} from "../../../source/i18n/providers/embed.mjs";
import {
    Translations,
    getDocumentTranslations
    
} from "../../../source/i18n/translations.mjs";
import {initJSDOM} from "../../util/jsdom.mjs";

describe('Translations', function () {

    describe('Instance and Init', function () {

        let translation;

        beforeEach(() => {
            translation = new Translations(parseLocale('en-GB'));

            translation.assignTranslations({
                test1: "abc",
                test2: {'other': 'xyz'}
            });
        })

        it('create instance', function () {
            expect(new Translations(parseLocale('en-GB'))).is.instanceof(Translations);
        });

        it('init translations', function () {
            expect(translation.assignTranslations({
                test1: "abc",
                test2: {'other': 'xyz'}
            })).is.instanceof(Translations);
        });

    });

    describe('getText', function () {

        let translation;

        beforeEach(() => {
            translation = new Translations(parseLocale('en-GB'));

            translation.assignTranslations({
                test1: "abc",
                test2: {'other': 'xyz'}
            });
        })

        it('create, set and get en-GB', function () {
            expect(translation.getText('test1')).is.equal('abc');
        });

        it('create, set and get  en-GB with pluralrules', function () {
            expect(translation.getText('test2')).is.equal('xyz')
        });

    });

    /**
     * initDocumentTranslation
     */

    describe("test initDocumentTranslation ", function () {


        let html1 = `<div id="mock-translations"></div>
    
<script type="application/json" data-monster-role="translations">
    {
        "test1": "abc",
        "test2": {
            "other": "xyz"
        }
    }
</script>

<script type="application/json" data-monster-role="translations">
    {
        "test1": "xyz",
        "test3": {
            "other": "xyz"
        }
    }
</script>    
    
`;

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

        })

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

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


        it('Init translations', function (done) {

            let elem = document.getElementById('mock-translations');
            Embed.assignTranslationsToElement(elem).then((o) => {

                let mocks = document.getElementById('mocks');
                
                // no exception because of default
                expect(getDocumentTranslations(elem).getText('no-key','with-default'))
                    .is.equal('with-default');
                
                expect(getDocumentTranslations(elem).getText('test1'))
                    .is.equal('xyz');

                done();

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


        });
    })


});