{% assign content_type = content_type | default: 'body' %} 

{% if content_type == 'body' %}

    <shx-loading-spinner/>

{% elsif content_type == 'head' %}

    <script>
        class SHX_LoadingSpinner extends HTMLElement {
            constructor() {
                super();

                this.container = null;

                this.hideTimeout = null;
            }

            connectedCallback() {
                this.resetStyles();
                this.container.classList.remove('shx-loading-spinner-container-done');
                this.isLoading = true;
            }

            resetStyles() {
                this.innerHTML = `
                    <div class="shx-loading-spinner-container shx-loading-spinner-container-done">
                        {% render 'shx-logo', class_name:"shx-loading-spinner-mainIcon shx-loading-spinner-unclickable", width:"200px"%}        
                        <p class="shx-loading-spinner-unclickable">Lade Vorschau</p>
                    </div>
                `;

                this.container = this.querySelector('.shx-loading-spinner-container');
            }

            showError(text) {
                if(this.hideTimeout) {
                    clearTimeout(this.hideTimeout);
                }

                this.isLoading = false;
                this.resetStyles();

                this.container.innerHTML = `
                      <dotlottie-player
                        id="SHX-feedback-modal-lottie2"
                        src="{{ 'error.lottie' | asset_url }}"
                        background="transparent"
                        speed="1"
                        autoplay
                        loop="true"
                        style="width: 200px; height: 200px; margin: 0 auto;"
                        direction="1"
                        mode="bounce"></dotlottie-player>
                    <p class="shx-loading-spinner-unclickable" style="margin-top: 0;">${text}</p>
                `;
                this.container.classList.remove('shx-loading-spinner-container-done');
            }

            showErrorCharLottie(text) {
                if(this.hideTimeout) {
                    clearTimeout(this.hideTimeout);
                }

                this.isLoading = false;
                this.resetStyles();

                this.container.innerHTML = `
                      <dotlottie-player
                        id="SHX-feedback-modal-lottie2"
                        src="{{ 'box_length.lottie' | asset_url }}"
                        background="transparent"
                        speed="1"
                        autoplay
                        loop="true"
                        style="width: 200px; height: 200px; margin: 0 auto;"
                        direction="1"
                        mode="bounce"></dotlottie-player>
                    <p class="shx-loading-spinner-unclickable" style="margin-top: 0;">${text}</p>
                `;
                this.container.classList.remove('shx-loading-spinner-container-done');
            }

            show() { 
                if(this.isLoading) {
                    return;
                }

                this.isLoading = true;

                if(this.hideTimeout) {
                    clearTimeout(this.hideTimeout);
                }

                this.resetStyles();

                this.hideTimeout = setTimeout(() => {
                    this.container.classList.remove('shx-loading-spinner-container-done');
                }, 10);
            }

            hide() {
                this.isLoading = false;
                
                this.container.classList.add('shx-loading-spinner-container-done');

                if (this.hideTimeout) {
                    clearTimeout(this.hideTimeout);
                }

                this.hideTimeout = setTimeout(() => {
                    this.container.style.display = 'none';
                }, 1000);
            }
        }

        customElements.define('shx-loading-spinner', SHX_LoadingSpinner);
    </script>

    <style>
        @keyframes shx-spinner-pulse {
            0% {
                transform: scale(0.85);
            }
            50% {
                transform: scale(1);
            }
            100% {
                transform: scale(0.85);
            }
        }

        .shx-loading-spinner-container {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.2);
            z-index: 9999;
            display: flex;
            flex-direction: column; /* Add this line */
            justify-content: center;
            align-items: center;

            backdrop-filter: blur(10px);

            transition: all 0.5s;
        }

        .shx-loading-spinner-container-done {
            opacity: 0;
            pointer-events: none;
        }

        .shx-loading-spinner-unclickable {
            pointer-events: none;
        }

        .shx-loading-spinner-mainIcon {
            animation: shx-spinner-pulse 0.65s cubic-bezier( 0.59, 0.23, 0.28, 0.73) infinite;
            
            filter: drop-shadow(0px 0px 5px rgba(255,255,255, 0.5)), drop-shadow(0px 0px 20px rgba(255,255,255, 0.5));
        }

        .shx-loading-spinner-container p {
            color: white;
            font-size: 28px;
            margin: 0 50px 50px 50px;

            text-align: center;
        }

        @media (max-width: 768px) {
            .shx-loading-spinner-container p {
                font-size: 20px;
            }
        }

    </style>        

{% endif %}