/**
 * Searches for an element with the given ID in parent shadow roots
 */
export function findElementInShadowRoots(id, from) {
    // First try to find in document
    const rootElm = document.getElementById(id);
    if (rootElm) {
        return rootElm;
    }
    // If not found in document, search in parent shadow roots
    let current = from;
    const queryId = `#${id}`;
    while (current) {
        // Check if current element has a shadow root and search within it
        if (current.shadowRoot) {
            const element = current.shadowRoot.querySelector(queryId);
            if (element) {
                return element;
            }
        }
        // Move up the DOM tree
        if (current.parentNode) {
            if (current.parentNode.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
                // We're inside a shadow root, move to the shadow host
                const shadowRoot = current.parentNode;
                current = shadowRoot.host;
            }
            else {
                // Regular DOM traversal
                current = current.parentElement;
            }
        }
        else {
            // No more parents
            current = null;
        }
    }
    return null;
}
const adoptedStyles = new WeakSet();
/**
 * Adopts the provided styles into the document.
 *
 * @param styles - The styles to adopt.
 */
export function adoptStyles(...styles) {
    if (typeof document === 'undefined' || !('adoptedStyleSheets' in document)) {
        // Constructable stylesheets are not supported in this environment.
        return;
    }
    const sheets = [];
    for (const style of styles) {
        if (adoptedStyles.has(style)) {
            continue;
        }
        if (style instanceof CSSStyleSheet) {
            adoptedStyles.add(style);
            sheets.push(style);
        }
        else if (style && typeof style === 'object' && 'styleSheet' in style) {
            const styleSheet = style.styleSheet;
            if (styleSheet instanceof CSSStyleSheet) {
                adoptedStyles.add(style);
                sheets.push(styleSheet);
            }
        }
    }
    if (sheets.length === 0) {
        return;
    }
    document.adoptedStyleSheets = [...document.adoptedStyleSheets, ...sheets];
}
//# sourceMappingURL=Dom.js.map