import{ariaHandler}from"./AriaQueryHandler.js";function makeQueryHandler(e){const r={};return e.queryOne&&(r.queryOne=async(r,n)=>{const t=await r.evaluateHandle(e.queryOne,n),a=t.asElement();return a||(await t.dispose(),null)},r.waitFor=(r,n,t)=>r.waitForSelectorInPage(e.queryOne,n,t)),e.queryAll&&(r.queryAll=async(r,n)=>{const t=await r.evaluateHandle(e.queryAll,n),a=await t.getProperties();await t.dispose();const o=[];for(const e of a.values()){const r=e.asElement();r&&o.push(r)}return o},r.queryAllArray=async(r,n)=>{const t=await r.evaluateHandle(e.queryAll,n);return await t.evaluateHandle((e=>Array.from(e)))}),r}const _defaultHandler=makeQueryHandler({queryOne:(e,r)=>e.querySelector(r),queryAll:(e,r)=>e.querySelectorAll(r)}),pierceHandler=makeQueryHandler({queryOne:(e,r)=>{let n=null;const t=e=>{const a=document.createTreeWalker(e,NodeFilter.SHOW_ELEMENT);do{const e=a.currentNode;e.shadowRoot&&t(e.shadowRoot),e instanceof ShadowRoot||!n&&e.matches(r)&&(n=e)}while(!n&&a.nextNode())};return e instanceof Document&&(e=e.documentElement),t(e),n},queryAll:(e,r)=>{const n=[],t=e=>{const a=document.createTreeWalker(e,NodeFilter.SHOW_ELEMENT);do{const e=a.currentNode;e.shadowRoot&&t(e.shadowRoot),e instanceof ShadowRoot||e.matches(r)&&n.push(e)}while(a.nextNode())};return e instanceof Document&&(e=e.documentElement),t(e),n}}),_builtInHandlers=new Map([["aria",ariaHandler],["pierce",pierceHandler]]),_queryHandlers=new Map(_builtInHandlers);export function registerCustomQueryHandler(e,r){if(_queryHandlers.get(e))throw new Error(`A custom query handler named "${e}" already exists`);if(!/^[a-zA-Z]+$/.test(e))throw new Error("Custom query handler names may only contain [a-zA-Z]");const n=makeQueryHandler(r);_queryHandlers.set(e,n)}export function unregisterCustomQueryHandler(e){_queryHandlers.has(e)&&!_builtInHandlers.has(e)&&_queryHandlers.delete(e)}export function customQueryHandlerNames(){return[..._queryHandlers.keys()].filter((e=>!_builtInHandlers.has(e)))}export function clearCustomQueryHandlers(){customQueryHandlerNames().forEach(unregisterCustomQueryHandler)}export function getQueryHandlerAndSelector(e){if(!/^[a-zA-Z]+\//.test(e))return{updatedSelector:e,queryHandler:_defaultHandler};const r=e.indexOf("/"),n=e.slice(0,r),t=e.slice(r+1),a=_queryHandlers.get(n);if(!a)throw new Error(`Query set to use "${n}", but no query handler of that name was found`);return{updatedSelector:t,queryHandler:a}}