import { t } from "../../services/i18n.js";
import BasicWidget from "../basic_widget.js";
import utils from "../../services/utils.js";
import UpdateAvailableWidget from "./update_available.js";
import options from "../../services/options.js";
import { Tooltip, Dropdown } from "bootstrap";
const TPL = /*html*/`
`;
export default class GlobalMenuWidget extends BasicWidget {
    private updateAvailableWidget: UpdateAvailableWidget;
    private isHorizontalLayout: boolean;
    private tooltip!: Tooltip;
    private dropdown!: Dropdown;
    private $updateToLatestVersionButton!: JQuery;
    private $zoomState!: JQuery;
    private $toggleZenMode!: JQuery;
    constructor(isHorizontalLayout: boolean) {
        super();
        this.updateAvailableWidget = new UpdateAvailableWidget();
        this.isHorizontalLayout = isHorizontalLayout;
    }
    doRender() {
        this.$widget = $(TPL);
        if (!this.isHorizontalLayout) {
            this.$widget.addClass("dropend");
        }
        const $globalMenuButton = this.$widget.find(".global-menu-button");
        if (!this.isHorizontalLayout) {
            $globalMenuButton.prepend(
                $(`\
                
                    
                         
                 `)
            );
            this.tooltip = new Tooltip(this.$widget.find("[data-bs-toggle='tooltip']")[0], { trigger: "hover" });
        } else {
            $globalMenuButton.toggleClass("bx bx-menu");
        }
        this.dropdown = Dropdown.getOrCreateInstance(this.$widget.find("[data-bs-toggle='dropdown']")[0], {
            popperConfig: {
                placement: "bottom"
            }
        });
        this.$widget.find(".show-about-dialog-button").on("click", () => this.triggerCommand("openAboutDialog"));
        const isElectron = utils.isElectron();
        this.$widget.find(".toggle-pin").toggle(isElectron);
        if (isElectron) {
            this.$widget.on("click", ".toggle-pin", (e) => {
                const $el = $(e.target);
                const remote = utils.dynamicRequire("@electron/remote");
                const focusedWindow = remote.BrowserWindow.getFocusedWindow();
                const isAlwaysOnTop = focusedWindow.isAlwaysOnTop();
                if (isAlwaysOnTop) {
                    focusedWindow.setAlwaysOnTop(false);
                    $el.removeClass("active");
                } else {
                    focusedWindow.setAlwaysOnTop(true);
                    $el.addClass("active");
                }
            });
        }
        this.$widget.find(".logout-button").toggle(!isElectron);
        this.$widget.find(".logout-button-separator").toggle(!isElectron);
        this.$widget.find(".open-dev-tools-button").toggle(isElectron);
        this.$widget.find(".switch-to-mobile-version-button").toggle(!isElectron && utils.isDesktop());
        this.$widget.find(".switch-to-desktop-version-button").toggle(!isElectron && utils.isMobile());
        this.$widget.on("click", ".dropdown-item", (e) => {
            if ($(e.target).parent(".zoom-buttons")) {
                return;
            }
            this.dropdown.toggle();
        });
        if (utils.isMobile()) {
            this.$widget.on("click", ".dropdown-submenu .dropdown-toggle", (e) => {
                const $submenu = $(e.target).closest(".dropdown-item");
                $submenu.toggleClass("submenu-open");
                $submenu.find("ul.dropdown-menu").toggleClass("show");
                e.stopPropagation();
                return;
            });
        }
        this.$widget.on("click", ".dropdown-submenu", (e) => {
            if ($(e.target).children(".dropdown-menu").length === 1 || $(e.target).hasClass("dropdown-toggle")) {
                e.stopPropagation();
            }
        });
        this.$widget.find(".global-menu-button-update-available").append(this.updateAvailableWidget.render());
        this.$updateToLatestVersionButton = this.$widget.find(".update-to-latest-version-button");
        if (!utils.isElectron()) {
            this.$widget.find(".zoom-container").hide();
        }
        this.$zoomState = this.$widget.find(".zoom-state");
        this.$toggleZenMode = this.$widget.find('[data-trigger-command="toggleZenMode"');
        this.$widget.on("show.bs.dropdown", () => this.#onShown());
        if (this.tooltip) {
            this.$widget.on("hide.bs.dropdown", () => this.tooltip.enable());
        }
        this.$widget.find(".zoom-buttons").on(
            "click",
            // delay to wait for the actual zoom change
            () => setTimeout(() => this.updateZoomState(), 300)
        );
        this.updateVersionStatus();
        setInterval(() => this.updateVersionStatus(), 8 * 60 * 60 * 1000);
    }
    #onShown() {
        this.$toggleZenMode.toggleClass("active", $("body").hasClass("zen"));
        this.updateZoomState();
        if (this.tooltip) {
            this.tooltip.hide();
            this.tooltip.disable();
        }
    }
    updateZoomState() {
        if (!utils.isElectron()) {
            return;
        }
        const zoomFactor = utils.dynamicRequire("electron").webFrame.getZoomFactor();
        const zoomPercent = Math.round(zoomFactor * 100);
        this.$zoomState.text(`${zoomPercent}%`);
    }
    async updateVersionStatus() {
        await options.initializedPromise;
        if (options.get("checkForUpdates") !== "true") {
            return;
        }
        const latestVersion = await this.fetchLatestVersion();
        this.updateAvailableWidget.updateVersionStatus(latestVersion);
        // Show "click to download" button in options menu if there's a new version available
        this.$updateToLatestVersionButton.toggle(utils.isUpdateAvailable(latestVersion, glob.triliumVersion));
        this.$updateToLatestVersionButton.find(".version-text").text(`Version ${latestVersion} is available, click to download.`);
    }
    async fetchLatestVersion() {
        const RELEASES_API_URL = "https://api.github.com/repos/TriliumNext/Trilium/releases/latest";
        const resp = await fetch(RELEASES_API_URL);
        const data = await resp.json();
        return data?.tag_name?.substring(1);
    }
    downloadLatestVersionCommand() {
        window.open("https://github.com/TriliumNext/Trilium/releases/latest");
    }
    activeContextChangedEvent() {
        this.dropdown.hide();
    }
    noteSwitchedEvent() {
        this.dropdown.hide();
    }
}