const re4_topicIdsToRemove = [
    "RenditionPlugIn",
    "Rendition-UsingOverrides",
    "Rendition-CreatingandEditingInstances",
    "Rendition-CreatingaRendition",
    "Rendition-UsingTextBoxes"
];

function re4_findElementById() {
    // Look for elements with data-id="RenditionPlugIn"
    const elements = document.querySelectorAll('[data-id="RenditionPlugIn"]');
    console.log('Searching for elements with data-id="RenditionPlugIn":', elements);

    if (elements.length > 0) {
        console.log(`Found ${elements.length} matching elements`);
        elements.forEach(element => {
            // console.log('Found element:', element);

            // Hide the element
            element.style.display = 'none';

            // Hide the parent toc entry if it exists
            const tocEntry = element.closest('.wh_main_page_toc_entry');
            if (tocEntry) {
                // console.log('Hiding parent toc entry:', tocEntry);
                tocEntry.style.display = 'none';
            }
        });
    } else {
        console.log('No elements found with data-id="RenditionPlugIn"');
    }

    // Look for topicref elements with data-id="RenditionPlugIn"
    const topicRefs = document.querySelectorAll('[class*="topicref"][data-id="RenditionPlugIn"]');
    console.log('Searching for topicref elements with data-id="RenditionPlugIn":', topicRefs);

    if (topicRefs.length > 0) {
        console.log(`Found ${topicRefs.length} matching topicref elements`);
        topicRefs.forEach(topicRef => {
            console.log('Found topicref element:', topicRef);

            // Hide the topicref element
            topicRef.style.display = 'none';

            // Hide the parent toc entry if it exists
            const tocEntry = topicRef.closest('.wh_main_page_toc_entry');
            if (tocEntry) {
                console.log('Hiding parent toc entry for topicref:', tocEntry);
                tocEntry.style.display = 'none';
            }
        });
    } else {
        console.log('No topicref elements found with data-id="RenditionPlugIn"');
    }
}

function re4_removeTopicsByDataId(topicIds) {
    console.log("Starting removeTopicsByDataId with topicIds:", topicIds);

    topicIds.forEach((id) => {
        // First try to find the element directly
        const elements = document.querySelectorAll(`[data-id="${id}"]`);
        if (elements.length > 0) {
            elements.forEach(el => {
                console.log(`Processing element:`, el);

                // Try to find and remove the toc entry
                const tocEntry = el.closest("div.wh_main_page_toc_entry");
                if (tocEntry) {
                    console.log("Hiding toc entry:", tocEntry);
                    tocEntry.style.display = 'none';
                }

                // Try to find and hide the parent li with role='treeitem'
                const treeItem = el.closest("li[role='treeitem']");
                if (treeItem) {
                    console.log("Hiding treeitem:", treeItem);
                    treeItem.style.display = 'none';
                    return; // Exit early since the treeitem is hidden
                }

                // Try to find and hide the parent li
                const parentLi = el.closest("li");
                if (parentLi) {
                    console.log("Hiding parent li:", parentLi);
                    parentLi.style.display = 'none';
                }
            });
        }
    });
}

function re4_handleTopicRefElements(dataId) {
    console.log(`Executing re4_handleTopicRefElements for data-id="${dataId}"`);

    // Look for topicref elements with the specified data-id
    const topicRefs = document.querySelectorAll(`[data-id="${dataId}"]`);
    console.log(`Searching for topicref elements with data-id="${dataId}":`, topicRefs);

    if (topicRefs.length > 0) {
        console.log(`Found ${topicRefs.length} matching topicref elements`);
        topicRefs.forEach(topicRef => {
            console.log('Found topicref element:', topicRef);

            // Hide the topicref element
            topicRef.style.display = 'none';

            // Hide the parent li with role='treeitem' if it exists
            const treeItem = topicRef.closest("li[role='treeitem']");
            if (treeItem) {
                console.log("Hiding treeitem for topicref:", treeItem);
                treeItem.style.display = 'none';
            }

            // Hide the parent toc entry if it exists
            const tocEntry = topicRef.closest('.wh_main_page_toc_entry');
            if (tocEntry) {
                console.log('Hiding parent toc entry for topicref:', tocEntry);
                tocEntry.style.display = 'none';
            }
        });
    } else {
        console.log(`No topicref elements found with data-id="${dataId}"`);
    }
}

// Export the functions to the global scope
window.re4_removeTopicsByDataId = re4_removeTopicsByDataId;
window.re4_findElementById = re4_findElementById;
window.re4_topicIdsToRemove = re4_topicIdsToRemove;

// Add a check function to verify if the module is loaded
window.re4_isModuleLoaded = function() {
    return typeof window.re4_removeTopicsByDataId === 'function' &&
           typeof window.re4_findElementById === 'function' &&
           Array.isArray(window.re4_topicIdsToRemove);
};

// Execute the functions
re4_findElementById();
re4_removeTopicsByDataId(re4_topicIdsToRemove);

// Call the new function for each topic ID
re4_topicIdsToRemove.forEach((id) => {
    re4_handleTopicRefElements(id);
});