var $1ZV4u$ariahidden = require("aria-hidden");


function $parcel$export(e, n, v, s) {
  Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
}

$parcel$export(module.exports, "watchModals", function () { return $6118749fa9fb2cf5$export$11dddb85aae73809; });
/*
 * Copyright 2020 Adobe. All rights reserved.
 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy
 * of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */ 
const $6118749fa9fb2cf5$var$currentDocument = typeof document !== 'undefined' ? document : undefined;
function $6118749fa9fb2cf5$export$11dddb85aae73809(selector = 'body', { document: document1 = $6118749fa9fb2cf5$var$currentDocument } = {}) {
    /**
   * Listen for additions to the child list of the selected element (defaults to body). This is
   * where providers render modal portals. When one is added, see if there is a modal inside it, if
   * there is, then hide everything else from screen readers. If there was already a modal open and
   * a new one was added, undo everything that the previous modal had hidden and hide based on the
   * new one.
   *
   * If a modal container is removed, then undo the hiding based on the last hide others. Check if
   * there are any other modals still around, and hide based on the last one added.
   */ if (!document1) return ()=>{};
    let target = document1.querySelector(selector);
    if (!target) return ()=>{};
    let config = {
        childList: true
    };
    let modalContainers = [];
    let undo;
    let observer = new MutationObserver((mutationRecord)=>{
        const liveAnnouncer = document1.querySelector('[data-live-announcer="true"]');
        for (let mutation of mutationRecord){
            if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
                let addNode = Array.from(mutation.addedNodes).find((node)=>node.querySelector?.('[aria-modal="true"], [data-ismodal="true"]'));
                if (addNode) {
                    modalContainers.push(addNode);
                    let modal = addNode.querySelector('[aria-modal="true"], [data-ismodal="true"]');
                    undo?.();
                    let others = [
                        modal,
                        ...liveAnnouncer ? [
                            liveAnnouncer
                        ] : []
                    ];
                    undo = (0, $1ZV4u$ariahidden.hideOthers)(others);
                }
            } else if (mutation.type === 'childList' && mutation.removedNodes.length > 0) {
                let removedNodes = Array.from(mutation.removedNodes);
                let nodeIndexRemove = modalContainers.findIndex((container)=>removedNodes.includes(container));
                if (nodeIndexRemove >= 0) {
                    undo?.();
                    modalContainers = modalContainers.filter((val, i)=>i !== nodeIndexRemove);
                    if (modalContainers.length > 0) {
                        let modal = modalContainers[modalContainers.length - 1].querySelector('[aria-modal="true"], [data-ismodal="true"]');
                        let others = [
                            modal,
                            ...liveAnnouncer ? [
                                liveAnnouncer
                            ] : []
                        ];
                        undo = (0, $1ZV4u$ariahidden.hideOthers)(others);
                    } else undo = undefined;
                }
            }
        }
    });
    observer.observe(target, config);
    return ()=>{
        undo?.();
        observer.disconnect();
    };
}


//# sourceMappingURL=ariaModalPolyfill.cjs.map
