var d=class{async generate(e){throw new Error("TestGenerationStrategy.generate() must be implemented")}canGenerate(e){throw new Error("TestGenerationStrategy.canGenerate() must be implemented")}getName(){throw new Error("TestGenerationStrategy.getName() must be implemented")}getPriority(){return 0}};import{readFileSync as m,writeFileSync as I}from"node:fs";var y=class extends d{constructor(){super("stable-id","Stable ID Injection (Experimental)",300)}canGenerate(e){let a=e.eventsPath||`${e.sessionPath}/execute_live/events.json`;try{return JSON.parse(m(a,"utf-8")).some(o=>o.stableId||o.data?.stableId)?(console.log("[StableIdStrategy] \u2705 Stable IDs available"),!0):(console.log("[StableIdStrategy] \u274C No stable IDs found in events"),!1)}catch(s){return console.log("[StableIdStrategy] \u274C Failed to read events:",s.message),!1}}getName(){return"Stable ID Injection (Experimental)"}getPriority(){return 300}async generate(e){let{testFilePath:a,sessionPath:s,state:i}=e,o=`${s}/execute_live/events.json`,h=i?.title||"Generated Test";console.log("[StableIdStrategy] \u{1F3AF} Generating test using stable IDs"),console.log(`[StableIdStrategy]    events: ${o}`),console.log(`[StableIdStrategy]    output: ${a}`);let g=JSON.parse(m(o,"utf-8")).filter(c=>["navigate","type","fill","click","select_option","select"].includes(c.type)),n=`import { test, expect } from '@playwright/test';
`;n+=`import { StableIdRuntime } from '@zibby/core';

`,n+=`test('${h}', async ({ page }) => {
`;let p=!1,u=null;for(let c=0;c<g.length;c++){let t=g[c],r=t.stableId||t.data?.stableId,b=t.data?.element||t.data?.params?.element||"element";if(t.type==="click"&&r&&r===u){console.log(`[StableIdStrategy] Skipping duplicate click on ${r}`);continue}if(t.type==="navigate"){let l=t.data?.url||t.data?.params?.url;l&&!p&&(n+=`  await page.goto('${l}');
`,n+=`  await StableIdRuntime.injectStableIds(page);

`)}else if(t.type==="click")if(p=!0,u=r,r)n+=`  await StableIdRuntime.clickWithRetry(page, '${r}');
`;else{let l=this._generateSemanticSelector(b);n+=`  await ${l}.click();
`,n+=`  await StableIdRuntime.afterNavigation(page);
`}else if(t.type==="fill"||t.type==="type"){p=!1;let l=t.data?.text||t.data?.params?.text||"";r?n+=`  await StableIdRuntime.fillWithRetry(page, '${r}', '${this._escapeString(l)}');
`:n+=`  await page.getByPlaceholder('${this._escapeString(b)}').fill('${this._escapeString(l)}');
`}else if(t.type==="select_option"||t.type==="select"){p=!1;let l=t.data?.values||t.data?.params?.values,S=Array.isArray(l)?l[0]:l||"";r?n+=`  await StableIdRuntime.selectWithRetry(page, '${r}', '${this._escapeString(S)}');
`:n+=`  await page.locator('select').selectOption('${this._escapeString(S)}');
`}}return n+=`});
`,I(a,n),console.log(`[StableIdStrategy] \u2705 Generated test with ${g.length} actions using stable IDs`),{success:!0,testPath:a,method:"Stable ID Injection (Experimental)",actionsGenerated:g.length}}_escapeString(e){return e.replace(/'/g,"\\'").replace(/\n/g,"\\n")}_generateSemanticSelector(e){let a=e.toLowerCase(),s="locator",i=e;return a.includes("button")?(s="button",i=e.replace(/\s*button\s*/gi,"").trim()):a.includes("link")?(s="link",i=e.replace(/\s*link\s*/gi,"").trim()):a.includes("textbox")?(s="textbox",i=e.replace(/\s*textbox\s*/gi,"").trim()):a.includes("checkbox")?(s="checkbox",i=e.replace(/\s*checkbox\s*/gi,"").trim()):(a.includes("combobox")||a.includes("dropdown")||a.includes("select"))&&(s="combobox",i=e.replace(/\s*(combobox|dropdown|select)\s*/gi,"").trim()),s!=="locator"&&i?`page.getByRole('${s}', { name: '${this._escapeString(i)}' })`:`page.getByText('${this._escapeString(e)}')`}};export{y as StableIdStrategy};
