"use strict";var e=require("debug"),t=require("postcss-selector-parser"),o=require("fs");function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n=r(e),a=r(t),s=r(o);const i=n.default("automator:devtool");function c(e){e.walk((e=>{if("tag"===e.type){const t=e.value;e.value="page"===t?"uni-page-body":"uni-"+t}}))}const l=["Page.getElement","Page.getElements","Element.getElement","Element.getElements"];function u(e){try{return require(e)}catch(t){return require(require.resolve(e,{paths:[process.cwd()]}))}}/^win/.test(process.platform);const p=["chromium","firefox","webkit"];let f=!1;try{f=!!u("playwright")}catch(e){}const h=new Map;function d(e="chromium"){const t=e&&p.includes(e)?e:p[0];let o=h.get(t);return o||(o=function(e){if("webkit"===e)return w("webkit");if("firefox"===e)return w("firefox");if(f)return w("chromium");throw new Error("Playwright dependency not found, please install playwright!")}(t),h.set(t,o)),o}function w(e){const t=u("playwright");let o,r;return{type:e,provider:"playwright",async open(n,a,s){o=await t[e].launch(a.options),"firefox"===e&&(a.contextOptions.isMobile=!1),i(`browser.newContext ${JSON.stringify(a.contextOptions)}`);const c=await o.newContext(a.contextOptions);r=await c.newPage(),r.on("console",(e=>{s.emit("App.logAdded",{type:e.type(),args:[e.text()]})})),r.on("pageerror",(e=>{s.emit("App.exceptionThrown",e)})),await r.goto(a.url||n),await r.waitForTimeout(1e3)},close:()=>o.close(),screenshot:(e=!1)=>r.screenshot({fullPage:e}).then((e=>e.toString("base64")))}}let g;const m={"Tool.close":{reflect:async()=>{await g.close()}},"App.exit":{reflect:async()=>{}},"App.enableLog":{reflect:()=>Promise.resolve()},"App.captureScreenshot":{reflect:async(e,t)=>{const o=await g.screenshot(!!t.fullPage);return i(`App.captureScreenshot ${o.length}`),{data:o}}}};!function(e){l.forEach((t=>{e[t]=function(e){return{reflect:async(t,o)=>t(e,o,!1),params:e=>(e.selector&&(e.selector=a.default(c).processSync(e.selector)),e)}}(t)}))}(m);const y={devtools:{name:"browser",paths:[],validate:async function(e){return e.options=e.options||{},e.executablePath&&!e.options.executablePath&&(e.options.executablePath=e.executablePath),e.contextOptions={viewport:Object.assign({width:375,height:667},e.options.defaultViewport||{}),hasTouch:!0,isMobile:!0,deviceScaleFactor:2},e.options.defaultViewport=Object.assign({width:375,height:667,deviceScaleFactor:2,hasTouch:!0,isMobile:!0},e.options.defaultViewport||{}),e.teardown||(e.teardown=!1===e.options.headless?"disconnect":"close"),e},create:async function(e,t,o){t.executablePath?await new Promise(((o,r)=>{const{exec:n}=require("node:child_process");if(/^win/.test(process.platform)){const r="C:\\Users\\Public\\AppData\\Local\\chrome";s.default.existsSync(r)||s.default.mkdirSync(r,{recursive:!0}),n(`start ${t.executablePath} --user-data-dir=${r} ${e}`,(e=>{if(e)throw console.error(`open ${t.executablePath} fail, ${e}`),Error(e)})),setTimeout((()=>{o(null)}),1e3)}else n(`open -a "${t.executablePath}" ${e}`,(e=>{e&&(console.error(`open ${t.executablePath} fail, ${e}`),r(e)),o(null)}))})):(g=d(process.env.BROWSER),i("createDevtools "+(g.provider+" "+g.type+" "+JSON.stringify(t))),await g.open(e,t,o))}},shouldCompile:(e,t)=>!t.url,adapter:m};module.exports=y;
