"use strict";

import * as Locutus from "locutus";

function resize() {
    document.documentElement.style.setProperty('--vh', `${window.innerHeight * 0.01}px`);
}

resize();
window.addEventListener("resize", resize);

window.addEventListener("DOMContentLoaded", () => {
    // Remove <noscript> tags
    for (const noscript of document.getElementsByTagName("noscript")) {
        noscript.remove();
    }

    const locutus = new Locutus.App();

    if (isMobilePlatform()) {
        document.addEventListener("visibilitychange", () => {
            if (document.visibilityState === 'hidden') {
                locutus.sleep();
            } else if (document.visibilityState === 'visible') {
                locutus.wakeup().then();
            }
        });
    }

    window.addEventListener("beforeunload", () => {
        locutus.quit();
    });
});

function isMobilePlatform(): boolean {
    if (window.matchMedia("(any-pointer:coarse)").matches) return true;
    const ua = navigator.userAgent;
    if (/(tablet|ipad|playbook|silk)|(android(?!.*mobi))/i.test(ua)) {
        return true;
    } else if (/Mobile|Android|iP(hone|od)|IEMobile|BlackBerry|Kindle|Silk-Accelerated|(hpw|web)OS|Opera M(obi|ini)/.test(ua)) {
        return true;
    }
    return false;
}
