export class Preloader {
    constructor(selectorOrElement, options = {}) {
        this.isHidden = false;
        this.element =
            typeof selectorOrElement === "string"
                ? document.querySelector(selectorOrElement)
                : selectorOrElement;
        this.options = {
            timeout: options.timeout ?? 500,
            hiddenClass: options.hiddenClass ?? "preloader--hidden",
            minDisplayTime: options.minDisplayTime ?? 0,
            onHide: options.onHide ?? (() => { }),
        };
        this.startTime = Date.now();
        if (!this.element) {
            console.warn("[Stylescape] Preloader element not found");
            return;
        }
        this.init();
    }
    show() {
        if (!this.element)
            return;
        this.isHidden = false;
        this.startTime = Date.now();
        this.element.classList.remove(this.options.hiddenClass);
        this.element.setAttribute("aria-hidden", "false");
    }
    hide() {
        if (!this.element || this.isHidden)
            return;
        const elapsed = Date.now() - this.startTime;
        const remaining = Math.max(0, this.options.minDisplayTime - elapsed);
        setTimeout(() => {
            if (!this.element)
                return;
            this.element.classList.add(this.options.hiddenClass);
            this.element.setAttribute("aria-hidden", "true");
            this.isHidden = true;
            this.options.onHide();
        }, remaining);
    }
    destroy() {
        this.hide();
        this.element = null;
    }
    init() {
        this.element?.setAttribute("role", "progressbar");
        this.element?.setAttribute("aria-busy", "true");
        this.element?.setAttribute("aria-hidden", "false");
        if (document.readyState === "complete") {
            this.scheduleHide();
        }
        else {
            window.addEventListener("load", () => this.scheduleHide());
        }
    }
    scheduleHide() {
        setTimeout(() => this.hide(), this.options.timeout);
    }
}
export default Preloader;
//# sourceMappingURL=Preloader.js.map