var p=class{static async beforeEach(e){await this.injectStableIds(e),e.on("load",async()=>{await this.injectStableIds(e)})}static async afterNavigation(e){await e.waitForLoadState("domcontentloaded").catch(()=>{}),await this.injectStableIds(e)}static async clickWithRetry(e,u,h={}){let s=h.timeout||1e4,l=`[data-zibby-id="${u}"]`,d=Date.now();for(;Date.now()-d<s;){await this.injectStableIds(e);let c=e.locator(l);if(await c.count()>0)try{await c.click({timeout:2e3});return}catch(t){if(t.message.includes("intercepts pointer")){await c.click({force:!0});return}}await e.waitForTimeout(200)}throw new Error(`Element ${l} not found after ${s}ms`)}static async fillWithRetry(e,u,h,s=1e4){let l=`[data-zibby-id="${u}"]`,d=Date.now();for(;Date.now()-d<s;){await this.injectStableIds(e);let c=e.locator(l);if(await c.count()>0){await c.fill(h);return}await e.waitForTimeout(200)}throw new Error(`Element ${l} not found after ${s}ms`)}static async selectWithRetry(e,u,h,s=1e4){let l=`[data-zibby-id="${u}"]`,d=Date.now();for(;Date.now()-d<s;){await this.injectStableIds(e);let c=e.locator(l);if(await c.count()>0){await c.selectOption(h);return}await e.waitForTimeout(200)}throw new Error(`Element ${l} not found after ${s}ms`)}static async injectStableIds(e){try{await e.evaluate(()=>{function u(t){if(t.getAttribute("aria-label"))return t.getAttribute("aria-label").trim();let o=t.getAttribute("aria-labelledby");if(o){let n=document.getElementById(o);if(n)return n.textContent.trim()}if(t.id){let n=document.querySelector(`label[for="${t.id}"]`);if(n)return n.textContent.trim()}let i=t.closest("label");if(i){let n=i.cloneNode(!0);n.querySelectorAll("input, select, textarea").forEach(f=>f.remove());let m=n.textContent.trim();if(m)return m}if(t.placeholder)return t.placeholder;let r=t.tagName.toLowerCase();return r==="button"||r==="a"||t.getAttribute("role")==="button"?(t.textContent||"").trim().slice(0,50):t.title?t.title:r==="input"&&(t.type==="submit"||t.type==="button")&&t.value||""}function h(t){let o=[],i=t.closest("form");if(i)if(i.id)o.push(`form#${i.id}`);else if(i.name)o.push(`form[name=${i.name}]`);else if(i.action)try{let a=new URL(i.action,window.location.origin).pathname;o.push(`form[action=${a}]`)}catch{o.push(`form[action=${i.getAttribute("action")}]`)}else{let a=document.querySelectorAll("form"),b=Array.from(a).indexOf(i);o.push(`form:nth(${b})`)}let r=t.closest('header, nav, main, footer, aside, [role="banner"], [role="navigation"], [role="main"], [role="contentinfo"]');if(r){let a=r.tagName.toLowerCase(),b=r.getAttribute("role");o.push(b||a)}let n=t.closest('section, article, [role="region"]');if(n){let a=n.querySelector("h1, h2, h3, h4, h5, h6");a&&o.push(`section:${a.textContent.trim().slice(0,30)}`)}let m=t.closest("fieldset");if(m){let a=m.querySelector("legend");a&&o.push(`fieldset:${a.textContent.trim()}`)}let f=t.closest('dialog, [role="dialog"], [role="alertdialog"]');if(f){let a=f.querySelector('[role="heading"], h1, h2, h3');a?o.push(`dialog:${a.textContent.trim().slice(0,30)}`):o.push("dialog")}return o.join("/")}function s(t){let o=t.tagName.toLowerCase(),i=t.id||"",r=t.name||"",n=t.type||"",m=t.getAttribute("role")||"",f="";if(t.href)try{f=new URL(t.href,window.location.origin).pathname.slice(0,50)}catch{f=t.getAttribute("href")?.slice(0,50)||""}let a=u(t).slice(0,50).replace(/\s+/g," "),b=h(t),g=[o,i,r,n,m,f,a,b].join("|"),w=5381;for(let y=0;y<g.length;y++)w=(w<<5)+w^g.charCodeAt(y);return`zibby-${(w>>>0).toString(36)}`}let l=["button","a","input","select","textarea","label[for]",'[role="button"]','[role="link"]','[role="textbox"]','[role="checkbox"]','[role="radio"]','[role="combobox"]','[role="menuitem"]','[role="tab"]','[role="option"]','[role="switch"]','[role="slider"]',"[onclick]","[data-action]"].join(", "),d=new Map,c=0;document.querySelectorAll(l).forEach(t=>{let o=window.getComputedStyle(t);if(o.display==="none"||o.visibility==="hidden")return;let i=s(t),r=i,n=d.get(r)||0;n>0&&(i=`${r}-${n}`),d.set(r,n+1),t.setAttribute("data-zibby-id",i),c++}),console.log(`[Zibby] Injected ${c} stable IDs`)})}catch{}}};export{p as StableIdRuntime};
