{"version":3,"file":"wait-for-element.cjs","sources":["../../src/modules/wait-for-element/wait-for-element.js"],"sourcesContent":["/**\n * Wait for an element to be added to the DOM or shadow DOM.\n * @param {ShadowRoot | Document} root - The root to observe.\n * @param {string} selector - The selector of the element to wait for.\n * @param {number} [timeout] - The time in milliseconds to wait before rejecting the promise.\n * @returns {Promise<HTMLElement | Node>} - A promise that resolves with the element when it is added to the DOM or shadow DOM.\n */\nfunction waitForElement(root, selector, timeout = 1000) {\n  let timeoutId;\n  return new Promise((resolve, reject) => {\n    let element = root.querySelector(selector);\n    if (element) {\n      resolve(element);\n      return;\n    }\n    const observer = new MutationObserver((mutations) => {\n      mutations.forEach((mutation) => {\n        if (mutation.type !== \"childList\") {\n          return;\n        }\n        mutation.addedNodes.forEach((node) => {\n          if (node.nodeType !== Node.ELEMENT_NODE) {\n            return;\n          }\n          if (\n            node.nodeType === Node.ELEMENT_NODE &&\n            node instanceof Element &&\n            node.matches(selector)\n          ) {\n            observer.disconnect();\n            clearTimeout(timeoutId);\n            resolve(node);\n            return;\n          }\n          let shadowElement = null;\n          if (node instanceof Element && node.shadowRoot) {\n            shadowElement = node.shadowRoot.querySelector(selector);\n          }\n\n          if (shadowElement) {\n            observer.disconnect();\n            clearTimeout(timeoutId);\n            resolve(shadowElement);\n            return;\n          }\n          if (node instanceof Element && node.shadowRoot) {\n            observer.observe(node.shadowRoot, {\n              childList: true,\n              subtree: true,\n            });\n          }\n          element = root.querySelector(selector);\n          if (element) {\n            observer.disconnect();\n            clearTimeout(timeoutId);\n            resolve(element);\n          }\n        });\n      });\n    });\n\n    observer.observe(root, { childList: true, subtree: true });\n\n    timeoutId = setTimeout(() => {\n      observer.disconnect();\n      reject();\n    }, timeout);\n  });\n}\n\nexport default waitForElement;\n"],"names":["waitForElement","root","selector","timeout","timeoutId","resolve","reject","element","observer","mutations","mutation","node","shadowElement"],"mappings":"aAOA,SAASA,EAAeC,EAAMC,EAAUC,EAAU,IAAM,CACtD,IAAIC,EACJ,OAAO,IAAI,QAAQ,CAACC,EAASC,IAAW,CACtC,IAAIC,EAAUN,EAAK,cAAcC,CAAQ,EACzC,GAAIK,EAAS,CACXF,EAAQE,CAAO,EACf,MACF,CACA,MAAMC,EAAW,IAAI,iBAAkBC,GAAc,CACnDA,EAAU,QAASC,GAAa,CAC1BA,EAAS,OAAS,aAGtBA,EAAS,WAAW,QAASC,GAAS,CACpC,GAAIA,EAAK,WAAa,KAAK,aACzB,OAEF,GACEA,EAAK,WAAa,KAAK,cACvBA,aAAgB,SAChBA,EAAK,QAAQT,CAAQ,EACrB,CACAM,EAAS,WAAU,EACnB,aAAaJ,CAAS,EACtBC,EAAQM,CAAI,EACZ,MACF,CACA,IAAIC,EAAgB,KAKpB,GAJID,aAAgB,SAAWA,EAAK,aAClCC,EAAgBD,EAAK,WAAW,cAAcT,CAAQ,GAGpDU,EAAe,CACjBJ,EAAS,WAAU,EACnB,aAAaJ,CAAS,EACtBC,EAAQO,CAAa,EACrB,MACF,CACID,aAAgB,SAAWA,EAAK,YAClCH,EAAS,QAAQG,EAAK,WAAY,CAChC,UAAW,GACX,QAAS,EACvB,CAAa,EAEHJ,EAAUN,EAAK,cAAcC,CAAQ,EACjCK,IACFC,EAAS,WAAU,EACnB,aAAaJ,CAAS,EACtBC,EAAQE,CAAO,EAEnB,CAAC,CACH,CAAC,CACH,CAAC,EAEDC,EAAS,QAAQP,EAAM,CAAE,UAAW,GAAM,QAAS,GAAM,EAEzDG,EAAY,WAAW,IAAM,CAC3BI,EAAS,WAAU,EACnBF,EAAM,CACR,EAAGH,CAAO,CACZ,CAAC,CACH"}