"use strict";var e=require("debug"),t=require("postcss-selector-parser"),o=require("fs");function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n=a(e),r=a(t),i=a(o);const s=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 p=["Page.getElement","Page.getElements","Element.getElement","Element.getElements"];function l(e){try{return require(e)}catch(t){return require(require.resolve(e,{paths:[process.cwd()]}))}}/^win/.test(process.platform);const u=["chromium","firefox","webkit"];let w=!1;try{w=!!l("playwright")}catch(e){}const h=new Map;function d(e="chromium"){const t=e&&u.includes(e)?e:u[0];let o=h.get(t);return o||(o=function(e){if("webkit"===e)return f("webkit");if("firefox"===e)return f("firefox");if(w)return f("chromium");throw new Error("Playwright dependency not found, please install playwright!")}(t),h.set(t,o)),o}function f(e){const t=l("playwright");let o,a,n={width:375,height:667};return{type:e,provider:"playwright",async open(r,i,c){var p;o=await t[e].launch(i.options),"firefox"===e&&(i.contextOptions.isMobile=!1),s(`browser.newContext ${JSON.stringify(i.contextOptions)}`),(null===(p=i.contextOptions)||void 0===p?void 0:p.viewport)&&(n=i.contextOptions.viewport);const l=await o.newContext(i.contextOptions);a=await l.newPage(),a.on("console",(e=>{c.emit("App.logAdded",{type:e.type(),args:[e.text()]})})),a.on("pageerror",(e=>{c.emit("App.exceptionThrown",e)})),await a.goto(i.url||r),await a.waitForTimeout(1e3)},close:()=>o.close(),screenshot(e=!1){let t={fullPage:e};return"true"===process.env.UNI_AUTOMATOR_WEB_SCREENSHOT_NO_NAVIGATION_BAR&&(t={fullPage:e,clip:{x:0,y:44,width:Number.MAX_VALUE,height:Number.MAX_VALUE}}),a.screenshot(t).then((e=>e.toString("base64")))},swipe:e=>new Promise((async t=>{const{startPoint:o,endPoint:n}=e;await a.evaluate((([e,t])=>{window.scrollBy({left:e.x-t.x,top:e.y-t.y,behavior:"smooth"})}),[o,n]),t("swipe success")})),tap:e=>new Promise((async t=>{const{x:o,y:n,duration:r}=e;await a.mouse.move(o,n),await a.mouse.down(),await a.waitForTimeout(r||0),await a.mouse.up(),t("tap success")})),setScreenOrientation:e=>new Promise((async t=>{const o=a.viewportSize()||n;await a.setViewportSize(function(e,t){const o=Math.min(e.width,e.height),a=Math.max(e.width,e.height);switch(t){case"portrait":case"portraitUpsideDown":return{width:o,height:a};case"landscapeLeft":case"landscapeRight":return{width:a,height:o};default:throw Error(`setScreenOrientation fail: invalid orientation "${t}". Expected portrait, portraitUpsideDown, landscapeLeft, or landscapeRight.`)}}(o,e)),t("setScreenOrientation success")})),keyboardInput:e=>new Promise((async t=>{await a.keyboard.type(e.text),t("keyboardInput success")}))}}let y;const g={"Tool.close":{reflect:async()=>{await y.close()}},"App.start":{reflect:async()=>{}},"App.exit":{reflect:async()=>{}},"App.enableLog":{reflect:()=>Promise.resolve()},"App.captureScreenshot":{reflect:async(e,t)=>{const o=await y.screenshot(!!t.fullPage);return s(`App.captureScreenshot ${o.length}`),{data:o}}},"App.swipe":{reflect:async(e,t)=>{const o=await y.swipe(t);return s(`App.swipe ${o.length}`),{data:o}}},"App.tap":{reflect:async(e,t)=>{const o=await y.tap(t);return s(`App.tap ${o.length}`),{data:o}}},"App.setScreenOrientation":{reflect:async(e,t)=>{const o=await y.setScreenOrientation(t);return s(`App.setScreenOrientation ${o.length}`),{data:o}}},"App.keyboardInput":{reflect:async(e,t)=>{const o=await y.keyboardInput(t);return s(`App.keyboardInput ${o.length}`),{data:o}}}};!function(e){p.forEach((t=>{e[t]=function(e){return{reflect:async(t,o)=>t(e,o,!1),params:e=>(e.selector&&(e.selector=r.default(c).processSync(e.selector)),e)}}(t)}))}(g);const m={devtools:{name:"browser",paths:[],validate:async function(e){var t;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:(null===(t=e.options.defaultViewport)||void 0===t?void 0:t.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,a)=>{const{exec:n}=require("node:child_process");if(/^win/.test(process.platform)){const a="C:\\Users\\Public\\AppData\\Local\\chrome";i.default.existsSync(a)||i.default.mkdirSync(a,{recursive:!0}),n(`start ${t.executablePath} --user-data-dir=${a} ${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}`),a(e)),o(null)}))})):(y=d(process.env.BROWSER),s("createDevtools "+(y.provider+" "+y.type+" "+JSON.stringify(t))),await y.open(e,t,o))}},shouldCompile:(e,t)=>!t.url,adapter:g};module.exports=m;