import * as i0 from '@angular/core';
import { Injectable } from '@angular/core';

class SdLoadingService {
    loadingId = 'L8d556b9b-f6dd-46e9-9710-757e65d82839';
    renderer;
    loadingMap = new WeakMap();
    constructor(rendererFactory) {
        this.renderer = rendererFactory.createRenderer(null, null);
    }
    start = (element = 'body') => {
        const ele = document.querySelector(element);
        if (ele && !this.loadingMap.has(ele)) {
            const loadingElement = this.#createLoading();
            ele.appendChild(loadingElement);
            this.loadingMap.set(ele, loadingElement);
        }
    };
    isLoading = (element = 'body') => {
        const ele = document.querySelector(element);
        return ele && this.loadingMap.has(ele);
    };
    stop = (element = 'body') => {
        const ele = document.querySelector(element);
        if (ele && this.loadingMap.has(ele)) {
            const loadingElement = this.loadingMap.get(ele);
            loadingElement?.remove();
            this.loadingMap.delete(ele);
        }
    };
    #createLoading = () => {
        const renderer = this.renderer;
        const container = this.renderer.createElement('div');
        const spinner = this.renderer.createElement('div');
        const containerStyle = {
            position: 'absolute',
            top: 0,
            left: 0,
            height: '100%',
            width: '100%',
            opacity: 0.6,
            background: '#FFFFFF',
            'z-index': 99999,
        };
        const spinnerStyle = {
            position: 'absolute',
            top: 'calc(50% - 2.5rem)',
            left: 'calc(50% - 2.5rem)',
            width: '5rem',
            height: '5rem',
            border: '0.5rem solid var(--sd-primary)',
            'border-top': '0.5rem solid var(--sd-black200)',
            'border-radius': '50%',
            animation: 'spin 1s linear infinite',
        };
        const keyframes = `
      @keyframes spin {
        0% { transform: rotate(0deg); }
        100% { transform: rotate(360deg); }
      }
    `;
        renderer.addClass(container, 'sd-loading');
        renderer.setAttribute(container, 'id', this.loadingId);
        Object.entries(containerStyle).forEach(entry => {
            renderer.setStyle(container, entry[0], entry[1]);
        });
        renderer.addClass(spinner, 'sd-loading-spinner');
        Object.entries(spinnerStyle).forEach(entry => {
            renderer.setStyle(spinner, entry[0], entry[1]);
        });
        const styleElement = this.renderer.createElement('style');
        this.renderer.appendChild(styleElement, this.renderer.createText(keyframes));
        document.head.appendChild(styleElement);
        container.appendChild(spinner);
        return container;
    };
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdLoadingService, deps: [{ token: i0.RendererFactory2 }], target: i0.ɵɵFactoryTarget.Injectable });
    static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdLoadingService, providedIn: 'root' });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdLoadingService, decorators: [{
            type: Injectable,
            args: [{
                    providedIn: 'root',
                }]
        }], ctorParameters: () => [{ type: i0.RendererFactory2 }] });

/**
 * Generated bundle index. Do not edit.
 */

export { SdLoadingService };
//# sourceMappingURL=sd-angular-core-services-loading.mjs.map
