/**
 * EZ Horoscope – Tarot 3 Card Spread: Frontend Loader
 * This file ensures tarot.js picks up the correct language
 * and initializes only when this block is present on the page.
 */

document.addEventListener("DOMContentLoaded", () => {

    const tarotBlocks = document.querySelectorAll(".ez-tarot-spread");

    if (!tarotBlocks.length) {
        return; // no tarot block on the page
    }

    tarotBlocks.forEach(block => {
        const lang = block.dataset.ezhpLang || "en";

        // Ensure tarot.js sees the correct ?language=
        const url = new URL(window.location.href);

        if (!url.searchParams.get("language")) {
            url.searchParams.set("language", lang);
            window.history.replaceState({}, "", url.toString());
        }
    });

    // tarot.js is already globally enqueued in ez-horoscope.php
    // This ensures tarot.js initializes AFTER markup is present
    if (window.initializeTarotUI) {
        try {
            window.initializeTarotUI();
        } catch (err) {
            console.error("Tarot initialization error:", err);
        }
    }
});
