var b=class{static async step(i,t){let{name:e,action:a,value:n,strategies:o,options:r={timeout:1e4},enrichedData:s}=t;console.log(`[Zibby] \u26A1 Executing step: ${e}`),await this.waitForPageStability(i,r.timeout);let c=await this.findBestMatch(i,o,e);if(!c)throw new Error(`[Zibby] \u274C Failed to find "${e}" using ${o.length} strategies`);s?.position&&await this.verifyPosition(c,s.position),await this.performActionWithRetry(i,c,a,n,3),console.log(`[Zibby] \u2728 Step "${e}" completed.`)}static async waitForPageStability(i,t=1e4){try{await i.waitForLoadState("networkidle",{timeout:3e3}).catch(()=>{}),await i.evaluate(()=>new Promise(e=>{let a,n=new MutationObserver(()=>{clearTimeout(a),a=setTimeout(()=>{n.disconnect(),e()},500)});n.observe(document.body,{childList:!0,subtree:!0,attributes:!0}),a=setTimeout(()=>{n.disconnect(),e()},500)})).catch(()=>{})}catch{}}static async verifyPosition(i,t){try{let e=await i.boundingBox();if(!e)return;let a=Math.abs(e.x-t.x),n=Math.abs(e.y-t.y);(a>50||n>50)&&(console.log(`[Zibby] \u26A0\uFE0F  Element moved: expected (${t.x}, ${t.y}), got (${e.x}, ${e.y})`),await new Promise(o=>setTimeout(o,500)))}catch{}}static async performActionWithRetry(i,t,e,a,n=3){for(let o=1;o<=n;o++)try{e==="click"?await t.click():e==="fill"?await t.fill(a||""):e==="type"?await t.pressSequentially(a||""):e==="selectOption"&&await t.selectOption(a||"");return}catch(r){if(o===n)throw r;console.log(`[Zibby] \u26A0\uFE0F  Action failed (attempt ${o}/${n}), retrying...`),await new Promise(s=>setTimeout(s,1e3*o))}}static async findBestMatch(i,t,e){let n=Date.now(),o=t.map(async(c,u)=>{try{let f=await this.getLocator(i,c).all();return f.length===0?[]:(await Promise.all(f.map(async(l,d)=>{try{if(!await l.isVisible({timeout:100}))return null;let m=await this.scoreCandidate(l,c,i);return{element:l,strategy:c,score:m,strategyIdx:u,elIdx:d}}catch{return null}}))).filter(l=>l!==null)}catch{return[]}}),r=(await Promise.all(o)).flat();if(r.length===0)return console.log(`[Zibby] \u274C No visible candidates found for "${e}"`),null;r.sort((c,u)=>u.score-c.score);let s=r[0];return console.log(`[Zibby] \u2705 Found element using ${s.strategy.type} (score: ${s.score.toFixed(2)}, ${r.length} candidates)`),s.element}static async scoreCandidate(i,t,e){let a=0;if(a+={testid:120,id:110,role:100,label:90,class:85,placeholder:85,text:80,css:50}[t.type]||50,t.priority==="high"&&(a+=20),t.priority==="medium"&&(a+=10),t.fuzzy&&(a-=15),t.parent)try{await i.locator("xpath=ancestor::*").first().evaluate((s,c)=>s.matches(c),t.parent)&&(a+=30)}catch{}if(t.sibling)try{await i.evaluate((r,s)=>r.parentElement?.querySelector(s)!==null,t.sibling)&&(a+=20)}catch{}try{let o=await i.boundingBox();o&&o.y<1e3&&(a+=10)}catch{}try{let o=await i.evaluate(r=>{let s=0,c=r;for(;c;)c.tagName==="IFRAME"&&s++,c=c.parentElement;return s});a-=o*5}catch{}return a}static getLocator(i,t){let e;switch(t.type){case"testid":e=i.getByTestId(t.value);break;case"id":e=i.locator(`#${t.value}`);break;case"class":e=i.locator(`.${t.value.replace(/\./g,".")}`);break;case"text":t.fuzzy?e=i.getByText(new RegExp(t.text,"i")):e=i.getByText(t.text,{exact:!1});break;case"role":e=i.getByRole(t.role,{name:t.name,exact:!1});break;case"label":e=i.getByLabel(t.label,{exact:!1});break;case"placeholder":e=i.getByPlaceholder(t.placeholder);break;default:{let a=t.value?.replace(/aria-ref=e\d+ >> /g,"")||t.css;e=i.locator(a);break}}return t.parent&&(e=e.filter({has:i.locator(t.parent)})),e}};export{b as ZibbyRuntime};
