import { newE2EPage } from "@stencil/core/testing";
import { haveSameStructure } from "./same-structure.e2e";
const COMPONENT_PREFIX = "gx-ide-";
const languages = [
    { name: "chinese", attr: "zh" },
    { name: "english", attr: "en" },
    { name: "japanese", attr: "ja" }
];
const testLocale = (componentNameWithPrefix) => {
    describe(`[locale][${componentNameWithPrefix}]`, () => {
        let page;
        const componentName = componentNameWithPrefix.replace(COMPONENT_PREFIX, "");
        // This implementation is a WA since we can't evaluate JS classes in the page.
        // TODO: Implement the locale utility as a function, which can be evaluated
        // in the page
        const getTranslations = () => page.evaluate(component => {
            var _a, _b;
            const ASSETS_FOLDER = "gx-ide-assets/";
            const DEFAULT_LANGUAGE = "en";
            const LANGS_FOLDER = "langs/";
            const LANG_PREFIX = ".lang.";
            const LANG_EXTENSION = ".json";
            const lang = (_b = (_a = document
                .getElementsByTagName("html")[0]
                .getAttribute("lang")) === null || _a === void 0 ? void 0 : _a.valueOf()) !== null && _b !== void 0 ? _b : DEFAULT_LANGUAGE;
            const folder = `${component}/`;
            const path = `/build/${ASSETS_FOLDER}${folder}${LANGS_FOLDER}${component}${LANG_PREFIX}${lang}${LANG_EXTENSION}`;
            return new Promise(resolve => {
                fetch(path)
                    .then(async (langFile) => {
                    if (langFile.ok) {
                        const fileObject = await langFile.json();
                        resolve(fileObject);
                    }
                    else {
                        resolve(undefined);
                    }
                })
                    .catch(() => resolve(undefined));
            });
        }, componentName);
        beforeEach(async () => {
            page = await newE2EPage({
                failOnConsoleError: true,
                html: "" // Necessary
            });
        });
        it("should work without specifying the language (lang attr) in the browser", async () => {
            const languageFile = await getTranslations();
            expect(languageFile).toBeTruthy();
        });
        it("should default to english language when there is no language (lang attr) in the browser", async () => {
            const languageFile = await getTranslations();
            expect(languageFile).toBeTruthy();
            await page.evaluate(() => document.documentElement.setAttribute("lang", "en"));
            const languageEnglishFile = await getTranslations();
            expect(languageEnglishFile).toBeTruthy();
            expect(languageFile).toEqual(languageEnglishFile);
        });
        languages.forEach(language => {
            it(`should work with ${language.name} language`, async () => {
                await page.evaluate((attr) => document.documentElement.setAttribute("lang", attr), language.attr);
                const languageFile = await getTranslations();
                expect(languageFile).toBeTruthy();
            });
        });
        for (let index = 0; index < languages.length - 1; index++) {
            const language1 = languages[index];
            const language2 = languages[index + 1];
            // TODO: Complete all translations. At this moment, Japanese and Chinese
            // translations have missing literals
            it(`should translate the same literals (${language1.name} and ${language2.name})`, async () => {
                await page.evaluate((attr) => document.documentElement.setAttribute("lang", attr), language1.attr);
                const language1File = await getTranslations();
                await page.evaluate((attr) => document.documentElement.setAttribute("lang", attr), language2.attr);
                const language2File = await getTranslations();
                expect(language1File).toBeTruthy();
                expect(language2File).toBeTruthy();
                expect(haveSameStructure(language1File, language2File)).toBe(true);
            });
        }
    });
};
// TypeScript does not have a built-in type to exhaust a list with union types
const allGeneXusIdeUIComponents = {
    "gx-ide-about": 0,
    "gx-ide-bpm-app-declaration": 0,
    "gx-ide-bpm-assign-roles": 0,
    "gx-ide-bpm-import-files": 0,
    "gx-ide-bpm-import-gxpm": 0,
    "gx-ide-bpm-objects-selector": 0,
    "gx-ide-bpm-sync-gam-roles": 0,
    "gx-ide-bpm-task-documents": 0,
    "gx-ide-bpm-timer-duration": 0,
    "gx-ide-chat-container": 0,
    "gx-ide-chat-welcome": 0,
    "gx-ide-sc-chat-container": 0,
    "gx-ide-connect-gx-server": 0,
    "gx-ide-container": 0,
    "gx-ide-create-kb-from-server": 0,
    "gx-ide-current-user-info": 0,
    "gx-ide-data-selector": 0,
    "gx-ide-data-type-selector": 0,
    "gx-ide-deployment-tool": 0,
    "gx-ide-details-acknowledgements": 0,
    "gx-ide-design-import": 0,
    "gx-ide-edit-module-server": 0,
    "gx-ide-empty-state": 0,
    "gx-ide-entity-selector": 0,
    "gx-ide-file-item": 0,
    "gx-ide-file-uploader": 0,
    "gx-ide-gam-installation-settings": 0,
    "gx-ide-json-import": 0,
    "gx-ide-kb-manager-export": 0,
    "gx-ide-kb-manager-import": 0,
    "gx-ide-list-selector": 0,
    "gx-ide-list-selector-item": 0,
    "gx-ide-loader": 0,
    "gx-ide-manage-module-references": 0,
    "gx-ide-multi-select": 0,
    "gx-ide-name-picker": 0,
    "gx-ide-new-environment": 0,
    "gx-ide-new-kb": 0,
    "gx-ide-new-object": 0,
    "gx-ide-new-version": 0,
    "gx-ide-object-selector": 0,
    "gx-ide-open-api-import": 0,
    "gx-ide-progress-bar": 0,
    "gx-ide-plugin-explorer": 0,
    "gx-ide-plugin-details": 0,
    "gx-ide-references": 0,
    "gx-ide-server-selector": 0,
    "gx-ide-select-kb-items": 0,
    "gx-ide-select-user-team": 0,
    "gx-ide-sign-in": 0,
    "gx-ide-sign-in-team": 0,
    "gx-ide-share-kb": 0,
    "gx-ide-start-page": 0,
    "gx-ide-sign-in-result": 0,
    "gx-ide-status-buttons": 0,
    "gx-ide-team-dev-bring-changes": 0,
    "gx-ide-team-dev-bring-all-changes-selector": 0,
    "gx-ide-team-dev-commit": 0,
    "gx-ide-team-dev-history": 0,
    "gx-ide-team-dev-preview": 0,
    "gx-ide-team-dev-select-recent-comment": 0,
    "gx-ide-team-dev-update": 0,
    "gx-ide-team-dev-update-partial-selection": 0,
    "gx-ide-team-dev-update-to-revision": 0,
    "gx-ide-starting-template": 0,
    "gx-ide-test": 0,
    "gx-ide-wf-settings": 0,
    "gx-ide-ww-images": 0,
    "gx-ide-ww-attributes": 0,
    "gx-ide-ww-files": 0,
    "gx-ide-navigation-report": 0,
    "gx-ide-splash": 0,
    "gx-ide-welcome-page": 0,
    "gx-ide-bpm-export-xpdl": 0,
    "gx-ide-curl-inspector": 0,
    "gx-ide-launchpad": 0,
    "gx-ide-launchpad-web-apps": 0,
    "gx-ide-launchpad-native-apps": 0,
    "gx-ide-launchpad-apis": 0,
    "gx-ide-launchpad-api": 0,
    "gx-ide-comments-viewer": 0,
    "gx-ide-restore-version": 0,
    "gx-ide-search": 0,
    "gx-ide-breadcrumb": 0,
    "gx-ide-breadcrumb-item": 0
};
const componentsWithoutTranslations = new Set([
    "gx-ide-chat-container",
    "gx-ide-container",
    "gx-ide-sc-chat-container",
    "gx-ide-empty-state",
    "gx-ide-list-selector",
    "gx-ide-list-selector-item",
    "gx-ide-loader",
    "gx-ide-test",
    "gx-ide-file-item",
    "gx-ide-breadcrumb",
    "gx-ide-breadcrumb-item",
    "gx-ide-multi-select"
]);
const componentsWithTranslations = Object.keys(allGeneXusIdeUIComponents).filter(component => !componentsWithoutTranslations.has(component));
componentsWithTranslations.forEach(testLocale);
//# sourceMappingURL=locale.e2e.js.map
