window.addEventListener("load", function () {
    const url = new URL(window.location.href);

    // ====================
    // Pattern Library Handler
    // ====================

    if (url.searchParams.has("sp_pcp_pattern_library")) {
        function tryClickPatternButton() {
            const patternButton = document.querySelector(
                "#smart-post-library-modal-button"
            );
            if (patternButton) {
                patternButton.click();
                url.searchParams.delete("sp_pcp_pattern_library");
                history.replaceState(null, "", url.toString());
                return true;
            }
            return false;
        }

        // Try every 100ms for up to 2 seconds
        let patternAttempts = 0;
        const patternInterval = setInterval(() => {
            patternAttempts++;
            if (tryClickPatternButton() || patternAttempts > 20) {
                clearInterval(patternInterval);
            }
        }, 100);
    }

    // ====================
    // Block Inserter Handler
    // ====================

    if (!url.searchParams.has("spblock_inserter")) return;

    function tryScroll() {
        const headings = document.querySelectorAll(".block-editor-inserter__panel-title");
        const smartPostHeading = Array.from(headings).find(
            (h) => h.textContent.trim() === "SMART POST"
        );
        if (smartPostHeading) {
            smartPostHeading.scrollIntoView({
                behavior: "smooth",
                block: "start",
                inline: "nearest",
            });
            return true;
        }
        return false;
    }

    function tryClick() {
        const btn = document.querySelector(".editor-document-tools__inserter-toggle");
        if (btn) {
            btn.click();
            url.searchParams.delete("spblock_inserter");
            history.replaceState(null, "", url.toString());

            // Try to scroll every 100ms for up to 3 seconds
            let scrollAttempts = 0;
            const scrollInterval = setInterval(() => {
                scrollAttempts++;
                if (tryScroll() || scrollAttempts > 30) {
                    clearInterval(scrollInterval);
                }
            }, 100);

            return true;
        }
        return false;
    }

    // Try every 100ms for up to 2 seconds
    let attempts = 0;
    const interval = setInterval(() => {
        attempts++;
        if (tryClick() || attempts > 20) {
            clearInterval(interval);
        }
    }, 100);
});
