agent-device
Version:
Open source device layer for AI coding agents: a CLI, MCP server, and typed Node.js API to control mobile, web, TV, and desktop apps on devices and simulators, locally or in the cloud.
1 lines • 30.2 kB
JavaScript
import{Ai as e,Ci as t,Di as n,Oi as r,Si as i,_i as a,bi as o,fi as s,li as c,wi as l}from"./sdk-batch.js";import{it as u}from"./sdk-contracts.js";import{n as d}from"./rect-center.js";import{n as f,t as p}from"./scroll-indicator.js";import{i as m,n as h,t as g}from"./ios-snapshot-planning.js";import{_,a as v,c as y,d as ee,f as b,g as te,h as ne,i as x,l as re,m as S,n as C,o as w,p as T,r as E,t as D,u as ie}from"./tree.js";import{a as ae,i as oe,o as O,s as se}from"./react-native-overlay.js";function ce(e,t){let n=v(t.sourceNodesByIndex.values());for(let r of e)le(r,t.sourceNodesByIndex,n)&&S(t.replacements,r,{type:`Cell`})}function le(e,t,n){return!(a(e.type??``)!==`other`||e.enabled===!1||!e.rect||!fe(e.label)||!w(e,t,de)||!ue(e)||ie(e.rect,n))}function ue(e){return e.rect?e.rect.height>=44&&e.rect.height<=160&&e.rect.width>=120:!1}function de(e){let t=a(e.type??``);return t===`scrollview`||t===`scrollarea`}function fe(e){let t=e?.trim();return!(!t||/^(toolbar|window|application)$/i.test(t)||t.startsWith(`!,`)||/debugger|fast refresh/i.test(t))}function pe(e,t){let n=new Set;for(let r of e)me(t.replacements.get(r.index)??r)&&he(r,t.sourceNodesByIndex,t,n);ye(e,t,n)}function me(e){let t=e.label?.trim();return!!(t&&f(t))}function he(e,t,n,r){let i=!b(e.type)||n.isSuppressed(e),a=p(e.label?.trim()??``,e.value),o=a?ve(e,t):void 0;i&&n.suppressNode(e,o?[o]:[]),o&&a&&De(n,o,e,a)&&r.add(o.index)}function ge(e,t){return typeof e.parentIndex==`number`?t.get(e.parentIndex):void 0}function _e(e,t){return e.rect!=null&&t.rect!=null&&k(e.rect,t.rect)}function ve(e,t){if(b(e.type))return null;let n=ge(e,t);for(;n&&!b(n.type);){let e=ge(n,t);if(!e||!_e(n,e))return null;n=e}return n??null}function ye(e,t,n){let r=new Map;for(let i of e){let e=r.get(i.parentIndex??-1),a=e?.clip,o=t.replacements.get(i.index)??i,s=Ee(o.rect,a),c=be(o,a,s),l=xe(e,c,o);Se(t,i,o,a,s,c,l),r.set(i.index,Ce(i,o,a,s,l,n))}}function be(e,n,r){return!!(n&&t(e.rect)&&!t(r))}function xe(e,t,n){return!!(e?.projectedOut||t&&Te(n))}function Se(e,t,n,r,i,a,o){o||a?e.suppressNode(t,[]):r&&i&&!k(n.rect,i)&&S(e.replacements,t,{rect:i})}function Ce(e,t,n,r,i,a){return{projectedOut:i,...we(e,t,n,r,a)?{clip:r}:n?{clip:n}:{}}}function we(e,n,r,i,a){return!!(t(i)&&(a.has(e.index)||r!==void 0&&b(n.type)))}function Te(e){return e.type?.trim().toLowerCase()===`cell`||b(e.type)}function Ee(e,t){if(!e||!t)return e;let n=Math.max(e.x,t.x),r=Math.max(e.y,t.y),i=Math.min(e.x+e.width,t.x+t.width),a=Math.min(e.y+e.height,t.y+t.height);return{x:n,y:r,width:Math.max(0,i-n),height:Math.max(0,a-r)}}function k(e,t){return!!(e&&e.x===t.x&&e.y===t.y&&e.width===t.width&&e.height===t.height)}function De(e,t,n,r){let i=Oe((e.replacements.get(t.index)??t).rect,n.rect);return _(e.replacements,t,e=>({rect:i??e.rect,hiddenContentAbove:A(e.hiddenContentAbove,r.above),hiddenContentBelow:A(e.hiddenContentBelow,r.below)})),!!i}function A(e,t){return e===!0||t?!0:void 0}function Oe(e,t){if(!(!e||!t)&&!(t.height<=0||t.height>=e.height)&&!(t.y<e.y||t.y>e.y+e.height))return{...e,y:t.y,height:Math.min(t.height,e.y+e.height-t.y)}}function j(e,t){for(let n=0;n<e.length;n+=1){let r=e[n],i=r?.label?.trim();r&&i&&a(r.type??``)===`other`&&t(r,i,n)}}function ke(e,t){j(e,(n,r,i)=>{if(!O(r)||!n.rect)return;let a=x(e,i,e=>!!e.rect&&se(e.label?.trim()??``));if(!a?.rect)return;let o=je(n.rect,a.rect);if(!o)return;let s=Ae(e,i,o);S(t,n,{rect:s}),y(e,i,e=>{O(e.label?.trim()??``)&&S(t,e,{rect:s})})})}function Ae(e,t,i){let a=i;return y(e,t,e=>{let t=e.label?.trim()??``;!e.rect||!O(t)||r(i,e.rect)&&n(e.rect)<n(a)&&(a=e.rect)}),a}function je(e,t){let n=e.x+e.width,r=t.x+t.width,i={x:t.x,y:e.y,width:n-t.x,height:e.height};if(t.x>e.x&&d(t,i))return{...e,width:t.x-e.x};let a={x:e.x,y:e.y,width:r-e.x,height:e.height};if(r<n&&d(t,a))return{...e,x:r,width:n-r}}function Me(e,t){j(e,(n,r,i)=>{oe(n)&&ae(n,M(e,i))&&t.suppressNode(n,M(e,i))})}function M(e,t){let n=[];return y(e,t,e=>{n.push(e)}),n}function Ne(e,t){let n=v(e),r=n?n.y+n.height:null;if(r!==null)for(let n=0;n<e.length;n+=1){let i=e[n];!i||!Pe(i,r)||(t.suppressNode(i,[]),Fe(i,t,r),y(e,n,e=>{t.suppressNode(e,[])}))}}function Pe(e,t){return!e.rect||a(e.type??``)!==`keyboard`?!1:e.rect.y>=t}function Fe(e,t,n){let r=typeof e.parentIndex==`number`?t.sourceNodesByIndex.get(e.parentIndex):void 0;for(;r?.rect&&r.rect.y>=n;)t.suppressNode(r,[]),r=typeof r.parentIndex==`number`?t.sourceNodesByIndex.get(r.parentIndex):void 0}function Ie(e,t){for(let n=0;n<e.length;n+=1){let r=e[n];if(!r)continue;if(Le(r)){ze(e,n,r,t);continue}if(!Re(r))continue;let i=x(e,n,e=>a(e.type??``)===`searchfield`&&e.label===`Search`);i&&(t.suppressNode(r,[i]),Be(r,i,t),ze(e,n,i,t))}}function Le(e){return a(e.type??``)===`searchfield`&&e.label===`Search`}function Re(e){let t=a(e.type??``);return e.label===`Toolbar`&&(t===`toolbar`||t===`searchfield`)}function ze(e,t,n,r){y(e,t,e=>{e.index!==n.index&&Ve(e)&&r.suppressNode(e,[n])})}function Be(e,t,n){let r=e;for(;typeof r.parentIndex==`number`;){let e=n.sourceNodesByIndex.get(r.parentIndex);if(!e||e.label!==`Toolbar`)return;n.suppressNode(e,[t]),r=e}}function Ve(e){let t=a(e.type??``);return t===`button`?!1:t===`image`||e.label===`Search`}function He(e,t){for(let n=0;n<e.length;n+=1){let r=e[n],i=r?.label?.trim();!r||t.isSuppressed(r)||!i||Ue(e,n,r,i,t)}}function Ue(e,t,n,r,i){let o=a(n.type??``);if(o===`statictext`||o===`link`){N(e,t,r,n,i);return}if(o!==`other`)return;let s=!c(n)&&We(e,t,r);if(s){i.suppressNode(n,[s]);return}N(e,t,r,n,i)}function We(e,t,n){return x(e,t,e=>{let t=a(e.type??``);return(t===`link`||t===`searchfield`||b(e.type))&&e.label?.trim()===n})}function N(e,t,n,r,i){y(e,t,e=>{!i.semanticRepresentativeIndexes.has(e.index)&&ee(e,n)&&i.suppressNode(e,[r])})}function Ge(e,t){j(e,(n,r,i)=>{if(c(n))return;let a=x(e,i,e=>T(e)&&e.label?.trim()===r&&(d(e.rect,n.rect)||Ke(n,e)));a&&t.suppressNode(n,[a])})}function Ke(e,t){if(t.label?.trim()!==e.label?.trim())return!1;let n=a(t.type??``);return qe(e,n)||n===`textfield`||Je(e,n,t)}function qe(e,t){let n=e.label?.trim();return t===`button`&&(n===`Dismiss`||n===`Back`)}function Je(e,t,n){return t!==`button`||!e.rect||!n.rect?!1:e.rect.x===0&&e.rect.y===0&&e.rect.width>=300&&e.rect.height>=600&&n.rect.width<e.rect.width}function Ye(e,t){let n=D(e);for(let r of e){if(a(r.type??``)!==`other`||r.hittable===!0||r.label?.trim()||r.value?.trim()||!r.identifier?.trim())continue;let e=E(r,n);(e.length!==0||r.hittable===!1)&&t.suppressNode(r,e)}}function Xe(e,t){Ne(e,t),Ye(e,t),pe(e,t),Ie(e,t),Ge(e,t),ke(e,t.replacements),Me(e,t),He(e,t)}function Ze(e,t){for(let n=0;n<e.length;n+=1){let r=e[n],i=r?Qe(e,n,r,t):void 0;!r?.rect||!i||tt(e,n,r,i,t)}}function Qe(e,t,n,r){let i=n.label?.trim();if(a(n.type??``)!==`cell`||i&&!$e(i,n.identifier)&&!f(i))return i;let o=C(e,t).find(et),s=o?.label?.trim();return!o||!s?i:(S(r.replacements,n,{label:s}),r.suppressNode(o,[n]),s)}function $e(e,t){return t?.trim()===e&&/^[A-Z_$][A-Za-z0-9_$]*Cell$/.test(e)}function et(e){if(!e.label?.trim())return!1;let t=a(e.type??``);return t===`statictext`||t===`text`||t===`textview`}function tt(e,t,n,r,i){let o=a(n.type??``);if(o===`button`){P(C(e,t),r,i,n);return}if(o!==`cell`)return;let s=C(e,t);nt(s,n,r,i)||rt(s,n,r,i)}function nt(e,t,n,r){let i=e.find(e=>ot(e,t,n));if(!i)return!1;let a=e.find(e=>F(e,t,n)),o=i.identifier?void 0:a?.identifier??t.identifier;return o&&S(r.replacements,i,{identifier:o}),r.suppressNode(t,[i]),it(e,t,n,i,r),!0}function rt(e,t,n,r){let i=e.find(e=>F(e,t,n));if(!i){e.some(re)&&P(e,n,r,t);return}!t.identifier&&i.identifier&&S(r.replacements,t,{identifier:i.identifier}),r.suppressNode(i,[t]),P(e.filter(e=>e.index!==i.index),n,r,t)}function it(e,t,n,r,i){for(let a of e)a.index!==r.index&&(F(a,t,n)||at(a,t)||st(a,r)||te(a,n))&&i.suppressNode(a,[r])}function P(e,t,n,r){for(let i of e)(te(i,t)||r&&at(i,r))&&n.suppressNode(i,r?[r]:[])}function F(e,t,n){if(a(e.type??``)!==`button`)return!1;let r=t.identifier?.trim(),i=e.identifier?.trim();return r&&i&&r===i?!0:e.label?.trim()===n&&d(e.rect,t.rect)}function at(e,t){return a(e.type??``)===`button`&&!e.label?.trim()&&!e.value?.trim()&&d(e.rect,t.rect)}function ot(e,t,n){if(a(e.type??``)!==`switch`)return!1;let r=t.identifier?.trim(),i=e.identifier?.trim();return r&&i&&r===i?!0:e.label?.trim()===n}function st(e,t){if(a(e.type??``)!==`switch`||e.index===t.index)return!1;let n=e.label?.trim();return n===t.value?.trim()||n===`0`||n===`1`}const I=1.15;function ct(e,t,n){let r=new Map(e.map((e,t)=>[e.index,t]));for(let[i,a]of e.entries()){let o=lt(e,r,t,a,n);if(!o)continue;R(e,i,n);let s=z(o.shelfRect,2);for(let t of o.intermediateSiblings){if(vt(t)||!t.rect||!l(t.rect,s))continue;let i=r.get(t.index);i!==void 0&&R(e,i,n)}}}function lt(e,t,n,r,i){let a=ut(r,n,i.sourceNodesByIndex);if(!a)return;let o=pt(e,t,a.siblings,a.position,a.parent);if(!(o<0))return{shelfRect:a.rect,intermediateSiblings:a.siblings.slice(a.position+1,o)}}function ut(e,t,n){let r=mt(e,L(t,e.index));if(!r)return;let i=ft(e,n);if(!i||!i.rect)return;let a=L(t,i.index),o=a.findIndex(t=>t.index===e.index);if(dt(a,o,e,r))return{rect:r.rect,parent:i,siblings:a,position:o}}function L(e,t){return e.get(t)??[]}function dt(e,t,n,r){if(t<1)return!1;let i=ht(e,t,n);return i?!r.childActionsPresented||!gt(i,r.actionButtons):!1}function ft(e,t){return typeof e.parentIndex==`number`?t.get(e.parentIndex):void 0}function pt(e,t,n,r,i){return n.findIndex((n,a)=>a>r&&_t(e,t.get(n.index),n,i))}function mt(e,t){if(!b(e.type)||!e.rect)return;let n=e.rect,i=t.filter(e=>a(e.type??``)===`button`&&e.rect);if(i.length===0&&e.hiddenContentBelow===!0)return{rect:n,childActionsPresented:!1,actionButtons:i};if(i.length<3)return;let o=i.map(e=>e.rect.x+e.rect.width/2);if(o.every((e,t)=>t===0||e>o[t-1]))return{rect:n,childActionsPresented:i.some(e=>r(n,e.rect)),actionButtons:i}}function ht(e,t,n){if(!(!n.rect||t<1))for(let r=t-1;r>=0;--r){let t=e[r];if(!(!t.rect||!T(t))&&t.rect.x+t.rect.width/2<n.rect.x&&yt(t.rect,z(n.rect,2)))return t}}function gt(e,t){if(!e.rect||t.length===0)return!1;let n=Math.max(...t.map(e=>e.rect?.width??0)),r=Math.max(...t.map(e=>e.rect?.height??0));return e.rect.width>n*I||e.rect.height>r*I}function _t(e,t,n,r){return t!==void 0&&a(n.type??``)===`other`&&d(n.rect,r.rect)&&!!x(e,t,T)}function R(e,t,n){let r=e[t];if(r){n.suppressNode(r,[]);for(let r of C(e,t))n.suppressNode(r,[])}}function vt(e){let t=a(e.type??``);return t===`textfield`||t===`securetextfield`||t===`searchfield`||t===`textview`}function z(e,t){return{x:e.x-t,y:e.y-t,width:e.width+t*2,height:e.height+t*2}}function yt(e,t){return Math.max(e.y,t.y)<=Math.min(e.y+e.height,t.y+t.height)}function bt(e,t){let n=D(e);ct(e,n,t),xt(e,n,t)}function xt(t,n,r){for(let i of t){if(a(i.type??``)!==`navigationbar`||!i.rect)continue;let t=n.get(i.index)??[],o=t.flatMap(e=>St(e,t,i.rect));if(o.length!==1)continue;let{field:s,title:c,image:l,label:u}=o[0];S(r.replacements,s,{type:`Button`,label:u,enabled:!0,hittable:void 0,rect:e([l.rect,s.rect,c.rect])}),r.semanticRepresentativeIndexes.add(s.index),r.suppressNode(c,[s]),r.suppressNode(l,[s])}}function St(e,t,n){let r=s(e);if(!Ct(e,r))return[];let i=B(t,e=>wt(e,r)),a=B(t,Tt);return i?.rect&&a?.rect&&Et(a.rect,e.rect,i.rect,n)?[{field:e,title:i,image:a,label:r}]:[]}function Ct(e,t){return a(e.type??``)===`textfield`&&e.enabled===!1&&!!e.rect&&!!t&&e.value?.trim()===t}function wt(e,t){return a(e.type??``)===`statictext`&&e.label?.trim()===t&&!!e.rect}function Tt(e){return a(e.type??``)===`image`&&!!e.identifier?.trim()&&!!e.rect}function B(e,t){let n=e.filter(t);return n.length===1?n[0]:void 0}function Et(e,t,n,i){let a=t.x-(e.x+e.width),o=n.x-(t.x+t.width);return r(i,e)&&r(i,t)&&r(i,n)&&a>=0&&a<=12&&o>=0&&o<=12&&V(e,t)&&V(t,n)}function V(e,t){return Math.max(e.y,t.y)<=Math.min(e.y+e.height,t.y+t.height)}function Dt(e,t){let n=D(e);for(let r of e){if(a(r.type??``)===`element(58)`){S(t.replacements,r,{type:`WebView`});continue}let e=Ot(r,t.sourceNodesByIndex,n);if(e?.kind===`suppress`){t.suppressNode(r,[]);continue}e&&(S(t.replacements,r,{type:e.type,...e.type===`Heading`?{value:void 0}:{}}),t.semanticRepresentativeIndexes.add(r.index))}}function Ot(e,t,n){if(a(e.type??``)!==`other`)return null;let r=w(e,t,jt),i=e.label?.trim();return!r||!i?null:At(e,r,i)?{kind:`suppress`}:kt(e,i,n)?{kind:`semantic`,type:H(e.value)?`Heading`:`StaticText`}:null}function kt(e,t,n){let r=n.get(e.index);if(r?.length!==1)return!1;let i=r[0];return a(i.type??``)===`statictext`&&i.label?.trim()===t&&!n.has(i.index)}function At(e,t,n){return e.parentIndex===t.index&&t.label?.trim()===n&&!H(e.value)}function jt(e){let t=a(e.type??``);return t===`webview`||t===`element(58)`}function H(e){return/^[1-6]$/.test(e?.trim()??``)}const Mt=[Dt,bt,ce,Ze,Xe];function U(e){if(e.length===0)return{nodes:e,presentedIndexesBySourceIndex:new Map};let t=new Map,n=new Map,r=new Set,i=new Map(e.map(e=>[e.index,e])),a=new Set,o={replacements:t,semanticRepresentativeIndexes:r,sourceNodesByIndex:i,isSuppressed:e=>a.has(e.index),suppressNode(e,t){if(a.add(e.index),t.length===0)return;let r=n.get(e.index)??new Set;for(let e of t)r.add(e.index);n.set(e.index,r)}};for(let t of Mt)t(e,o);if(a.size===0&&t.size===0)return{nodes:e,presentedIndexesBySourceIndex:new Map(e.map(e=>[e.index,[e.index]]))};let s=e.filter(e=>!a.has(e.index)).map(e=>t.get(e.index)??e),c=ne(s,a,e),l=new Map(s.map((e,t)=>[e.index,c[t].index]));return{nodes:c,presentedIndexesBySourceIndex:new Map(e.map(e=>[e.index,W(e.index,l,n)]))}}function W(e,t,n,r=new Set){let i=t.get(e);if(i!==void 0)return[i];if(r.has(e))return[];r.add(e);let a=new Set;for(let i of n.get(e)??[])for(let e of W(i,t,n,r))a.add(e);return[...a].sort((e,t)=>e-t)}var G=class extends Error{code=`IOS_SNAPSHOT_ENGINE_FAILED`;reason;details;constructor(e,t,n={}){super(t),this.name=`IosSnapshotEngineError`,this.reason=e,this.details=n}};function Nt(e){return{reason:e.reason,...e.details}}function K(e){let t=new Map;for(let[n,r]of e.entries()){if(!Number.isInteger(r.index)||r.index<0||t.has(r.index))throw new G(`malformed-graph`,`iOS snapshot graph contains a duplicate or invalid node index`,{index:r.index});if(t.set(r.index,n),r.depth!==void 0&&(!Number.isInteger(r.depth)||r.depth<0))throw new G(`malformed-graph`,`iOS snapshot graph contains an invalid node depth`,{index:r.index,field:`depth`})}for(let[n,r]of e.entries()){if(r.parentIndex===void 0)continue;let e=t.get(r.parentIndex);if(e===void 0||e>=n)throw new G(`malformed-graph`,`iOS snapshot graph parent must precede its child`,{index:r.index,parentIndex:r.parentIndex})}}const q=new Set([`collectionview`,`scrollview`,`table`]),Pt=new Set([`application`,`window`]),Ft=new Set([`UITextEffectsWindow`,`UIRemoteKeyboardWindow`]),J=new Map([[`Sheet`,5],[`Drawer`,6],[`Alert`,7],[`Dialog`,8]]);function It(){return{projectedOut:!1}}function Lt(e,n,r,i,o,s){let c=s===`cursor-projected`?n.ancestorClip:void 0,l=Rt(e.rect,r,c),u=t(Y(e.rect)),d=t(l),f=a(e.type??``),p=zt(s,n.projectedOut,u,d,f,o),m=Kt(e,Bt(s,n.projectedOut,u,d)&&!Vt(e,u,s),i,s),h=Gt(f,m,d,o),g=Ut(s,n.projectedOut,u,d,e);return{isIncluded:m,descendants:Ht(s,p,c,l,h),effectiveRect:l,...g?{hiddenContentFrame:g}:{},establishesScrollAnchor:h}}function Rt(e,t,n){let r=Y(e);if(!r)return;let i=X(r,t);return n&&(i=X(i,n)),i}function zt(e,t,n,r,i,a){return e===`plain-viewport`?!r:t||n&&!r&&Wt(i,a)}function Bt(e,t,n,r){return e===`plain-viewport`?r:!t&&(!n||r)}function Vt(e,t,n){return n!==`cursor-projected`||e.parentIndex===void 0||qt(e)||!t?!1:Jt(e.rect)<=1||Yt(e.rect)<=1}function Ht(e,t,n,r,i){return{projectedOut:t,...e===`cursor-projected`&&i?{ancestorClip:r}:{ancestorClip:n}}}function Ut(e,t,n,r,i){if(!(e!==`cursor-projected`||t||!n||r))return Y(i.rect)}function Wt(e,t){return t&&(e===`cell`||q.has(e))}function Gt(e,t,n,r){return t&&n&&r&&q.has(e)}function Kt(e,t,n,r){if(e.parentIndex===void 0)return!0;let i=a(e.type??``);return r===`plain-viewport`&&n&&!t&&i!==`application`?!1:Pt.has(i)||t}function qt(e){return[e.label,e.identifier,e.value].some(e=>typeof e==`string`&&e.trim().length>0)}function Y(e){if(e&&[e.x,e.y,e.width,e.height].every(Number.isFinite)&&!(e.width<0||e.height<0))return{...e,width:Math.max(0,e.width),height:Math.max(0,e.height)}}function Jt(e){return Y(e)?.width??0}function Yt(e){return Y(e)?.height??0}function X(e,t){let n=Math.max(e.x,t.x),r=Math.max(e.y,t.y),i=Math.min(e.x+e.width,t.x+t.width),a=Math.min(e.y+e.height,t.y+t.height);return i<=n||a<=r?{x:e.x,y:e.y,width:0,height:0}:{x:i>n?n:e.x,y:a>r?r:e.y,width:i-n,height:a-r}}function Xt(e){let t=D(e),n=new Set;for(let e of t.values()){let r=Zt(e,t);if(r){for(let i of e.slice(0,-1))if(Qt(r,i)){n.add(i.index);for(let e of E(i,t))n.add(e.index)}}}return n}function Zt(e,n){let r=e.at(-1);if(!r||!Z(r))return;let i=(n.get(r.index)??[]).find($t);if(!(i?.userInteractionEnabled!==!0||!t(i.rect)))return{container:r,dimmingRect:i.rect}}function Qt(e,n){return Z(n)&&t(n.rect)&&r(e.dimmingRect,n.rect)}function Z(e){return en(e,`UITransitionView`)}function $t(e){return en(e,`UIDimmingView`)}function en(e,t){return a(e.role??``)===a(t)}function tn(e,t){let n=un(e),r=dn(e,n);if(e.some((e,t)=>r.has(t)&&nn(e)))return`presented`;if(t!==`complete`||n.length===0||!e.every(e=>e.role!==void 0)||!ln(e))return`unknown`;let i=n.filter(t=>!cn(e[t]));return i.length===1&&pn(e[i[0]],e).length===1?`absent`:`unknown`}function nn(e){return $t(e)||rn(e)||an(e)}function rn(e){let t=e.role;return t!==void 0&&a(t).startsWith(a(`_UIAlertController`))}function an(e){let t=e.type;if(!t)return!1;if(J.has(t))return!0;let n=on.exec(t);return n!==null&&sn.has(Number(n[1]))}const on=/^Element\((\d+)\)$/,sn=new Set(J.values());function cn(e){let t=e.role;if(t===void 0)return!1;let n=a(t);for(let e of Ft)if(a(e)===n)return!0;return!1}function ln(e){return new Set(e.map(e=>e.index)).size===e.length}function un(e){let t=[];for(let[n,r]of e.entries())r.parentIndex===void 0&&t.push(n);if(t.length!==1)return[];let n=e[t[0]].index,r=[];for(let[t,i]of e.entries())i.parentIndex===n&&r.push(t);return r}function dn(e,t){let n=new Map;for(let[t,r]of e.entries()){if(typeof r.parentIndex!=`number`)continue;let e=n.get(r.parentIndex)??[];e.push(t),n.set(r.parentIndex,e)}let r=new Set(e.map((e,t)=>t));for(let i of t)if(cn(e[i]))for(let t of fn(i,e,n))r.delete(t);return r}function fn(e,t,n){let r=[e],i=new Set([e]);for(let e=0;e<r.length;e+=1)for(let a of n.get(t[r[e]].index)??[])i.has(a)||(i.add(a),r.push(a));return r}function pn(e,t){return t.filter(t=>t.parentIndex===e.index&&Z(t))}function mn(e,n,r,i,a={}){K(e);let o=Xt(e),s=e.filter(e=>!o.has(e.index)),c=hn(s),l=new Map,u=[],d=new Map,f=0,p=0,m=0;for(let e of s){let o=gn(e,l,()=>{f+=1}),s=o?.traversal??It(),h=i===`cursor-projected`?o?.anchor:void 0,g=Lt(e,s,n,r,c.has(e.index),i);t(e.rect)&&(g.isIncluded&&g.effectiveRect&&!Cn(e.rect,g.effectiveRect)?p+=1:!g.isIncluded&&!t(g.effectiveRect)&&(m+=1)),_n(g,h,d);let _=vn(e,g,o,u,n,a),v=bn(g,h,_.keptIndex);l.set(e.index,{traversal:g.descendants,anchor:v,keptIndex:_.keptIndex,keptDepth:_.keptDepth})}let h=Sn(d,u);return{nodes:h,...p+m>0?{qualityNotes:[{code:`regular-geometry-clipped`,clippedNodeCount:p,droppedNodeCount:m}]}:{},stats:{presentedNodeCount:h.length,sourceNodeCount:e.length,parentClipLookups:f,modalContainedNodeCount:o.size}}}function hn(e){let t=new Set;for(let n of e)n.parentIndex!==void 0&&t.add(n.parentIndex);return t}function gn(e,t,n){if(e.parentIndex!==void 0)return n(),t.get(e.parentIndex)}function _n(e,t,n){e.hiddenContentFrame&&t&&xn(e.hiddenContentFrame,t,n)}function vn(e,t,n,r,i,a){let o=n?.keptIndex,s=n?.keptDepth??-1;if(!t.isIncluded)return{keptIndex:o,keptDepth:s};let c=r.length;s+=1;let{hittable:l,...u}=e;return r.push({raw:{...u,index:c,depth:s,parentIndex:o,...yn(l,e.parentIndex!==void 0,t.effectiveRect,e.enabled!==!1,i,a)},sourceIndex:e.index,...t.effectiveRect?{effectiveRect:t.effectiveRect}:{}}),o=c,{keptIndex:o,keptDepth:s}}function yn(e,t,n,r,a,o){return o.hittabilityAvailable===!1?e===!1||!r?{hittable:!1}:{}:{hittable:t&&e===!0&&i(r,n,a)}}function bn(e,t,n){return e.establishesScrollAnchor&&n!==void 0&&e.effectiveRect?{index:n,rect:e.effectiveRect}:t}function xn(e,t,n){let r=n.get(t.index)??{above:!1,below:!1};e.y+e.height<=t.rect.y?r.above=!0:e.y>=t.rect.y+t.rect.height&&(r.below=!0),n.set(t.index,r)}function Sn(e,t){return t.map(t=>{let n=e.get(t.raw.index);if(!n)return t;let r=t.raw;return{...t,raw:{...r,hiddenContentAbove:r.hiddenContentAbove===!0||n.above?!0:void 0,hiddenContentBelow:r.hiddenContentBelow===!0||n.below?!0:void 0}}})}function Cn(e,t){return e.x===t.x&&e.y===t.y&&e.width===t.width&&e.height===t.height}const wn=new Set([`collectionview`,`scrollview`,`table`]);function Tn(e){return En(e.viewport)}function En(e){if(e.kind===`missing`)throw new G(e.reason===`invalid`?`invalid-viewport`:`missing-viewport`,`regular iOS snapshot presentation requires a valid viewport`,{field:`viewport`});if(!t(e.rect))throw new G(`invalid-viewport`,`regular iOS snapshot presentation requires a positive finite viewport`,{field:`viewport`});return e.rect}function Dn(e,n,r,i=!0){K(e);let a=new Map,o=0;for(let s of e){let e=On(s,a,n,()=>{o+=1}),c=Pn(s.rect);if(kn(s,i),!c||!t(c)){An(s,c),a.set(s.index,e);continue}jn(s,c,e),Mn(s,c,n),a.set(s.index,Nn(s,c,e,r))}return{parentClipLookups:o}}function On(e,t,n,r){if(e.parentIndex===void 0)return n;r();let i=t.get(e.parentIndex);if(!i)throw new G(`invalid-presented-payload`,`regular iOS snapshot payload refers to a parent outside the payload`,{index:e.index,parentIndex:e.parentIndex});return i}function kn(e,t){if(!(e.hittable!==!0||t))throw new G(`invalid-presented-payload`,`iOS snapshot payload marked a node actionable without hittability evidence`,{index:e.index})}function An(e,t){if(e.hittable===!0)throw new G(`regular-degenerate-actionable-node`,`regular iOS snapshot node with a missing or degenerate frame is actionable`,{index:e.index,frame:t??e.rect})}function jn(e,t,n){if(!Fn(t,n))throw new G(`regular-node-outside-cumulative-clip`,`regular iOS snapshot node escaped its cumulative clip`,{index:e.index,frame:t,clip:n})}function Mn(e,t,n){if(!(e.hittable!==!0||e.enabled!==!1&&o(n,t.x+t.width/2,t.y+t.height/2)))throw new G(`invalid-presented-payload`,`regular iOS snapshot payload marked a disabled or off-viewport node actionable`,{index:e.index,frame:t})}function Nn(e,t,n,r){return r===`cursor-projected`&&wn.has(a(e.type??``))?t:n}function Q(e,t,n,r,i=!0){if(t===`raw`)return K(e),{parentClipLookups:0};if(!n)throw new G(`missing-viewport`,`regular iOS snapshot payload cannot be validated without a viewport`,{field:`viewport`});try{return Dn(e,n,r,i)}catch(e){throw e instanceof G&&e.code===`IOS_SNAPSHOT_ENGINE_FAILED`&&e.reason===`malformed-graph`?new G(`invalid-presented-payload`,e.message,e.details):e}}function Pn(e){if(!(!e||![e.x,e.y,e.width,e.height].every(Number.isFinite))&&!(e.width<0||e.height<0))return{...e,width:Math.max(0,e.width),height:Math.max(0,e.height)}}function Fn(e,t){let n=1e-4;return r({x:t.x-n,y:t.y-n,width:t.width+n*2,height:t.height+n*2},e)}const In=new Set([`button`,`cell`,`checkbox`,`collectionview`,`keyboard`,`link`,`menuitem`,`picker`,`searchfield`,`securetextfield`,`segmentedcontrol`,`slider`,`scrollview`,`stepper`,`switch`,`tabbar`,`table`,`textfield`,`textview`,`webview`]);function Ln(e){let t=Xn(e.nodes),n=Bn(e,t);return e.projection===`raw`?Vn(n,e.depth,t):Hn(n,e.depth)}function Rn(e){let t=Xn(e.nodes);return e.projection===`raw`?Vn(e.nodes,e.depth,t):Hn(e.nodes,e.depth)}function zn(e){return e.parentIndex===void 0||In.has(a(e.type??``))||Zn(e)}function Bn(e,t){let n=e.scope?.trim().toLowerCase();if(!n)return[...e.nodes];for(let r=0;r<e.nodes.length;r+=1){let i=e.nodes[r];if(!i||!Jn(i.raw,n))continue;let a=Yn(e.nodes,r,t);if(!(e.projection===`raw`||a.some(t=>zn(e.nodes[t].raw))))continue;let o=a.map(t=>e.nodes[t]);return qn(e.projection===`raw`&&e.depth!==null?o.filter(n=>$(n,t)-$(i,t)<=e.depth):o,$(i,t),t)}return[]}function Vn(e,t,n){let r=t===null?[...e]:e.filter(e=>$(e,n)<=t);return{nodes:r.map(e=>({...e.raw,rect:e.raw.rect})),sourceIndexes:r.map(e=>e.sourceIndex)}}function Hn(e,t){let n=[],r=[],i=new Map;for(let a of e){let e=Un(a,i),o=Wn(a,e,t,n.length);if(!o){Kn(a,e,i);continue}n.push(o),r.push(a.sourceIndex),i.set(a.raw.index,o)}return{nodes:n,sourceIndexes:r}}function Un(e,t){return e.raw.parentIndex===void 0?void 0:t.get(e.raw.parentIndex)}function Wn(e,t,n,r){if(!zn(e.raw))return;let i=t?(t.depth??0)+1:0;if(n!==null&&i>n)return;let a=Gn(e);return{...e.raw,index:r,depth:i,parentIndex:t?.index,...e.effectiveRect?{rect:e.effectiveRect}:{rect:void 0},...a===void 0?{}:{hittable:a}}}function Gn(e){if(e.raw.hittable!==void 0)return!!(e.raw.hittable===!0&&e.effectiveRect&&e.effectiveRect.width>0&&e.effectiveRect.height>0)}function Kn(e,t,n){t&&n.set(e.raw.index,t)}function qn(e,t,n){let r=new Map(e.map((e,t)=>[e.raw.index,t]));return e.map((e,i)=>({...e,raw:{...e.raw,index:i,depth:Math.max(0,$(e,n)-t),parentIndex:e.raw.parentIndex===void 0?void 0:r.get(e.raw.parentIndex)}}))}function Jn(e,t){return[e.label,e.identifier,e.value].some(e=>typeof e==`string`&&e.toLowerCase().includes(t))}function Yn(e,t,n){let r=$(e[t],n),i=[];for(let a=t;a<e.length&&!(a>t&&$(e[a],n)<=r);a+=1)i.push(a);return i}function $(e,t){return Math.max(0,e.raw.depth??t?.get(e.raw.index)??0)}function Xn(e){let t=new Map;for(let n of e){let e=(n.raw.parentIndex===void 0?-1:t.get(n.raw.parentIndex)??-1)+1;t.set(n.raw.index,n.raw.depth===void 0?e:Math.max(0,n.raw.depth))}return t}function Zn(e){return[e.label,e.identifier,e.value].some(e=>typeof e==`string`&&e.trim().length>0)}function Qn(e,t,n=`cursor-projected`){$n(e,t);let r=e.validation.presentationKey.projection,i=r===`regular`?En(e.validation.viewport):void 0,a=e.validation.hittability.kind===`available`,o=er(e,r,i,n,a),s=tr(e.presentation.payload.nodes,r,t),c=s.compacted?nr(s.nodes,r,i,n,a,o):o;return{nodes:s.nodes,...i?{validatedViewport:i}:{},...e.presentation.qualityPayload?{qualityNodes:[...e.presentation.qualityPayload.nodes]}:{},presentedIndexesBySourceIndex:s.presentedIndexesBySourceIndex,stats:{presentedNodeCount:s.nodes.length,sourceNodeCount:e.presentation.payload.nodes.length,parentClipLookups:c.parentClipLookups,modalContainedNodeCount:0}}}function $n(e,t){if(!rr(h(t),e.validation.presentationKey))throw new G(`projection-mismatch`,`presented iOS snapshot does not match the requested presentation key`,{projection:e.validation.presentationKey.projection});if(e.presentation.intent!==t.acquisitionIntent)throw new G(`projection-mismatch`,`presented iOS snapshot does not match the requested acquisition intent`,{field:`acquisitionIntent`})}function er(e,t,n,r,i){let a=Q(e.presentation.payload.nodes,t,n,r,i);if(e.presentation.qualityPayload)try{Q(e.presentation.qualityPayload.nodes,t,n,r,i)}catch(e){throw e instanceof G?new G(`invalid-quality-payload`,e.message,e.details):e}return a}function tr(e,t,n){return t===`regular`&&n.interactiveOnly?{...U([...e]),compacted:!0}:{nodes:[...e],presentedIndexesBySourceIndex:ir(e),compacted:!1}}function nr(e,t,n,r,i,a){return t!==`regular`||!n?a:Q(e,t,n,r,i)}function rr(e,t){return e.projection===t.projection&&e.interactiveOnly===t.interactiveOnly&&e.depth===t.depth&&e.scope===t.scope&&e.customActions===t.customActions}function ir(e){return new Map(e.map(e=>[e.index,[e.index]]))}function ar(e,t,n={}){let r=sr(e,t,n),i=e.stage===`presented`?e.validation.presentationKey:h(t),a=e.stage===`acquired`?e.acquisition.truncated:e.presentation.payload.truncated;return{payload:{nodes:u(r.nodes),...a===void 0?{}:{truncated:a}},presentationKey:i,...r.qualityNotes?{qualityNotes:r.qualityNotes}:{},comparisonIdentity:g(e,t),residue:e.stage===`acquired`?[...e.acquisition.residue]:[...e.validation.residue],...r.validatedViewport?{validatedViewport:r.validatedViewport}:{},...e.stage===`acquired`?{presentedSurface:tn(e.acquisition.nodes,or(e.acquisition))}:{}}}function or(e){return e.truncated===void 0?`unstated`:e.truncated?`capped`:`complete`}function sr(e,t,n={}){let r=n.foldPolicy??`cursor-projected`;return e.stage===`acquired`?cr(e.acquisition,t,r):Qn(e,t,r)}function cr(e,t,n){if(ur(e,m(t)),t.projection===`raw`){K(e.nodes);let r=e.nodes.map(e=>({raw:e,sourceIndex:e.index})),i=Ln({nodes:r,projection:`raw`,scope:t.scope,depth:t.depth,foldPolicy:n}),a=t.scope===null?void 0:Rn({nodes:r,projection:`raw`,depth:null,foldPolicy:n}).nodes;return{nodes:i.nodes,...a?{qualityNodes:a}:{},presentedIndexesBySourceIndex:fr(e.nodes,i.nodes,i.sourceIndexes,dr(i.nodes)),stats:{presentedNodeCount:i.nodes.length,sourceNodeCount:e.nodes.length,parentClipLookups:0,modalContainedNodeCount:0}}}let r=Tn(e),i=!lr(e.residue),a=mn(e.nodes,r,t.interactiveOnly,n,{hittabilityAvailable:i}),o=Ln({nodes:a.nodes,projection:`regular`,scope:t.scope,depth:t.depth,foldPolicy:n}),s=t.interactiveOnly?U(o.nodes):{nodes:o.nodes,presentedIndexesBySourceIndex:dr(o.nodes)},c=Q(s.nodes,`regular`,r,n,i),l=t.scope===null?void 0:Rn({nodes:a.nodes,projection:`regular`,depth:null,foldPolicy:n}).nodes;return{nodes:s.nodes,validatedViewport:r,...a.qualityNotes?{qualityNotes:a.qualityNotes}:{},...l?{qualityNodes:l}:{},presentedIndexesBySourceIndex:fr(e.nodes,o.nodes,o.sourceIndexes,s.presentedIndexesBySourceIndex),stats:{presentedNodeCount:s.nodes.length,sourceNodeCount:e.nodes.length,parentClipLookups:a.stats.parentClipLookups+c.parentClipLookups,modalContainedNodeCount:a.stats.modalContainedNodeCount}}}function lr(e){return e.some(e=>e.kind===`unavailable-fact`&&e.fact===`hittability`)}function ur(e,t){let n=e.hint;if(n.projection!==t.projection||n.rawTraversalDepth!==t.rawTraversalDepth||n.regularPresentedDepth!==t.regularPresentedDepth||n.interactiveOnly!==t.interactiveOnly||n.customActions!==t.customActions||n.acquisitionIntent!==t.acquisitionIntent)throw new G(`projection-mismatch`,`acquired iOS snapshot does not match the requested capture hint`,{projection:n.projection})}function dr(e){return new Map(e.map(e=>[e.index,[e.index]]))}function fr(e,t,n,r){let i=new Map(t.map((e,t)=>[e.index,n[t]])),a=new Map(e.map(e=>[e.index,[]]));for(let[e,t]of r){let n=i.get(e);n!==void 0&&a.set(n,t)}return a}export{U as a,Nt as i,Qn as n,G as r,ar as t};