

<dialog id="components-reconnect-modal" data-nosnippet>
    <div class="components-reconnect-container">
        <div class="components-rejoining-animation" aria-hidden="true">
            <div></div>
            <div></div>
        </div>
        <p class="components-reconnect-first-attempt-visible">
            Rejoining the server...
        </p>
        <p class="components-reconnect-repeated-attempt-visible">
            Rejoin failed... trying again in <span id="components-seconds-to-next-attempt"></span> seconds.
        </p>
        <p class="components-reconnect-failed-visible">
            Failed to rejoin.<br />Please retry or reload the page.
        </p>
        <button id="components-reconnect-button" class="components-reconnect-failed-visible">
            Retry
        </button>
        <p class="components-pause-visible">
            The session has been paused by the server.
        </p>
        <p class="components-resume-failed-visible">
            Failed to resume the session.<br />Please retry or reload the page.
        </p>
        <button id="components-resume-button" class="components-pause-visible components-resume-failed-visible">
            Resume
        </button>
    </div>
</dialog>

<style>
    .components-reconnect-first-attempt-visible,
    .components-reconnect-repeated-attempt-visible,
    .components-reconnect-failed-visible,
    .components-pause-visible,
    .components-resume-failed-visible,
    .components-rejoining-animation {
        display: none;
    }

    #components-reconnect-modal.components-reconnect-show .components-reconnect-first-attempt-visible,
    #components-reconnect-modal.components-reconnect-show .components-rejoining-animation,
    #components-reconnect-modal.components-reconnect-paused .components-pause-visible,
    #components-reconnect-modal.components-reconnect-resume-failed .components-resume-failed-visible,
    #components-reconnect-modal.components-reconnect-retrying,
    #components-reconnect-modal.components-reconnect-retrying .components-reconnect-repeated-attempt-visible,
    #components-reconnect-modal.components-reconnect-retrying .components-rejoining-animation,
    #components-reconnect-modal.components-reconnect-failed,
    #components-reconnect-modal.components-reconnect-failed .components-reconnect-failed-visible {
        display: block;
    }


    #components-reconnect-modal {
        background-color: white;
        width: 20rem;
        margin: 20vh auto;
        padding: 2rem;
        border: 0;
        border-radius: 0.5rem;
        box-shadow: 0 3px 6px 2px rgba(0, 0, 0, 0.3);
        opacity: 0;
        transition: display 0.5s allow-discrete, overlay 0.5s allow-discrete;
        animation: components-reconnect-modal-fadeOutOpacity 0.5s both;

        &[open] {
            animation: components-reconnect-modal-slideUp 1.5s cubic-bezier(.05, .89, .25, 1.02) 0.3s, components-reconnect-modal-fadeInOpacity 0.5s ease-in-out 0.3s;
            animation-fill-mode: both;
        }
    }

        #components-reconnect-modal::backdrop {
            background-color: rgba(0, 0, 0, 0.4);
            animation: components-reconnect-modal-fadeInOpacity 0.5s ease-in-out;
            opacity: 1;
        }

    @@keyframes components-reconnect-modal-slideUp {
        0% {
            transform: translateY(30px) scale(0.95);
        }

        100% {
            transform: translateY(0);
        }
    }

    @@keyframes components-reconnect-modal-fadeInOpacity {
        0% {
            opacity: 0;
        }

        100% {
            opacity: 1;
        }
    }

    @@keyframes components-reconnect-modal-fadeOutOpacity {
        0% {
            opacity: 1;
        }

        100% {
            opacity: 0;
        }
    }

    .components-reconnect-container {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 1rem;
    }

    #components-reconnect-modal p {
        margin: 0;
        text-align: center;
    }

    #components-reconnect-modal button {
        border: 0;
        background-color: #6b9ed2;
        color: white;
        padding: 4px 24px;
        border-radius: 4px;
    }

        #components-reconnect-modal button:hover {
            background-color: #3b6ea2;
        }

        #components-reconnect-modal button:active {
            background-color: #6b9ed2;
        }

    .components-rejoining-animation {
        position: relative;
        width: 80px;
        height: 80px;
    }

        .components-rejoining-animation div {
            position: absolute;
            border: 3px solid #0087ff;
            opacity: 1;
            border-radius: 50%;
            animation: components-rejoining-animation 1.5s cubic-bezier(0, 0.2, 0.8, 1) infinite;
        }

            .components-rejoining-animation div:nth-child(2) {
                animation-delay: -0.5s;
            }

    @@keyframes components-rejoining-animation {
        0% {
            top: 40px;
            left: 40px;
            width: 0;
            height: 0;
            opacity: 0;
        }

        4.9% {
            top: 40px;
            left: 40px;
            width: 0;
            height: 0;
            opacity: 0;
        }

        5% {
            top: 40px;
            left: 40px;
            width: 0;
            height: 0;
            opacity: 1;
        }

        100% {
            top: 0px;
            left: 0px;
            width: 80px;
            height: 80px;
            opacity: 0;
        }
    }

</style>

<script>
        // Set up event handlers
    const reconnectModal = document.getElementById("components-reconnect-modal");
    reconnectModal.addEventListener("components-reconnect-state-changed", handleReconnectStateChanged);

    const retryButton = document.getElementById("components-reconnect-button");
    retryButton.addEventListener("click", retry);

    const resumeButton = document.getElementById("components-resume-button");
    resumeButton.addEventListener("click", resume);

    function handleReconnectStateChanged(event) {
        if (event.detail.state === "show") {
            reconnectModal.showModal();
        } else if (event.detail.state === "hide") {
            reconnectModal.close();
        } else if (event.detail.state === "failed") {
            document.addEventListener("visibilitychange", retryWhenDocumentBecomesVisible);
        } else if (event.detail.state === "rejected") {
            location.reload();
        }
    }

    async function retry() {
        document.removeEventListener("visibilitychange", retryWhenDocumentBecomesVisible);

        try {
            // Reconnect will asynchronously return:
            // - true to mean success
            // - false to mean we reached the server, but it rejected the connection (e.g., unknown circuit ID)
            // - exception to mean we didn't reach the server (this can be sync or async)
            const successful = await Blazor.reconnect();
            if (!successful) {
                // We have been able to reach the server, but the circuit is no longer available.
                // We'll reload the page so the user can continue using the app as quickly as possible.
                const resumeSuccessful = await Blazor.resumeCircuit();
                if (!resumeSuccessful) {
                    location.reload();
                } else {
                    reconnectModal.close();
                }
            }
        } catch (err) {
            // We got an exception, server is currently unavailable
            document.addEventListener("visibilitychange", retryWhenDocumentBecomesVisible);
        }
    }

    async function resume() {
        try {
            const successful = await Blazor.resumeCircuit();
            if (!successful) {
                location.reload();
            }
        } catch {
            reconnectModal.classList.replace("components-reconnect-paused", "components-reconnect-resume-failed");
        }
    }

    async function retryWhenDocumentBecomesVisible() {
        if (document.visibilityState === "visible") {
            await retry();
        }
    }

</script>

