{"size": 2767, "ext": "js", "max_stars_repo_path": "loader/app-loader/app-loader.js", "max_stars_repo_name": "TiagoVenceslau/trust-loader", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "loader/app-loader/app-loader.js", "max_issues_repo_name": "TiagoVenceslau/trust-loader", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "loader/app-loader/app-loader.js", "max_forks_repo_name": "TiagoVenceslau/trust-loader", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.1274509804, "max_line_length": 89, "alphanum_fraction": 0.5966750994}
/**
 * @module loader
 */

/**
 *
 */

import {getNavigatorUtils, ServiceWorkerConstants} from "../../bundles/trust-loader.js";
import env from "../../environment.js"
const paths = window.location.pathname.split("/iframe/");
const myIdentity = paths[1];
const swName = "swBoot.js";
let loadingInterval;

const NavigatorUtils = getNavigatorUtils();
console.log(`NavigatorUtils: ${NavigatorUtils}`);

function startLoadingProgressInterval(initialLoadingProgress) {
    sendLoadingProgress(initialLoadingProgress, `Loading ${initialLoadingProgress}%`);
    let loadingProgress = initialLoadingProgress;
    loadingInterval = setInterval(() => {
        let increment = 10;
        if (loadingProgress >= 90) {
            increment = 1;
        }

        loadingProgress += increment;

        if (loadingProgress >= 100) {
            clearInterval(loadingInterval);
            return;
        }
        sendLoadingProgress(loadingProgress, `Loading ${loadingProgress}%`);
    }, 1000);
}

window.frameElement.setAttribute("app-placeholder","true");
startLoadingProgressInterval(10);

if(NavigatorUtils.canUseServiceWorkers()) {
    window.document.addEventListener(myIdentity, (e) => {
        const data = e.detail || {};

        if (data.seed) {
            const seed = data.seed;
            const swConfig = {
                name: swName,
                path: `../${swName}`,
                scope: myIdentity
            };

            NavigatorUtils.loadDistributedApp(seed, swConfig, (err) => {
                if (err) {
                    clearInterval(loadingInterval);
                    console.error(err);
                    return sendMessage({
                        status: ServiceWorkerConstants.STATE.ERROR
                    });
                }
                sendMessage({
                    status: ServiceWorkerConstants.STATE.COMPLETED
                });
            })

        }
    });
    
    sendMessage({
        query: ServiceWorkerConstants.QUERIES.SEED
    });
} else {
    console.log(`Skipping registering ${swName} due to service workers being disabled`);
}

function sendMessage(message) {
    const event = new CustomEvent(myIdentity, {
        detail: message
    });
    window.parent.document.dispatchEvent(event);
}

function sendLoadingProgress(progress, status) {
    if (env.showLoadingProgress === false)
        return;

    let currentWindow = window;
    let parentWindow = currentWindow.parent;

    while (currentWindow !== parentWindow) {
        currentWindow = parentWindow;
        parentWindow = currentWindow.parent;
    }

    parentWindow.document.dispatchEvent(new CustomEvent(ServiceWorkerConstants.LOADING, {
        detail: {
            progress,
            status
        }
    }));
}
