"use strict";(()=>{var l=()=>{},u=()=>{},d={discover:"",discovered:"",ping:"",pong:"",walletCacheChanged:""};window.addEventListener("message",e=>{if(e.source!==window||e.data?.type!=="707326fe4e02419f")return;let t=e.data.cfg;t&&(d.discover=t.discover||"",d.discovered=t.discovered||"",d.ping=t.ping||"",d.pong=t.pong||"",d.walletCacheChanged=t.walletCacheChanged||"",l("[pvb] Config received from ISOLATED world"),window.postMessage({type:"6873fa1595d84d2a"},"*"))});var y=Storage.prototype.setItem;Storage.prototype.setItem=function(e,t){y.call(this,e,t),e==="padreV2-walletsCache"&&d.walletCacheChanged&&(l("[pvb] Detected localStorage write to padreV2-walletsCache"),queueMicrotask(()=>{try{let r=JSON.parse(t),n=[];if(typeof r=="object"&&!Array.isArray(r))for(let s of Object.keys(r)){let a=r[s];if(Array.isArray(a))for(let i of a){let o=i.publicAddress||i.address||i.publicKey;typeof o=="string"&&n.push(o)}}n.length>0&&window.postMessage({type:d.walletCacheChanged,addresses:n},"*")}catch{}}))};window.addEventListener("message",e=>{e.source===window&&(e.data?.type===d.discover&&(l("[pvb] Discovery requested"),queueMicrotask(()=>v())),e.data?.type===d.ping&&window.postMessage({type:d.pong},"*"))});function v(){try{let e=I(),t=h(),r=e.velvetState?.solSubOrgId||t.find(s=>s.subOrgId&&s.walletType==="SOL")?.subOrgId||"",n=m(e.bundles,t);if(!e.stamper){u("[pvb] No VelvetContext stamper found in fiber tree"),window.postMessage({type:d.discovered,error:"No VelvetContext stamper found",wallets:n,organizationId:r},"*");return}l(`[pvb] Found stamper: ${e.stamper.apiPublicKey.slice(0,20)}...`),l(`[pvb] Bundles: ${e.bundles.length}, Wallets: ${n.length}, OrgId: ${r.slice(0,12)}...`),window.postMessage({type:d.discovered,apiPublicKey:e.stamper.apiPublicKey,apiPrivateKey:e.stamper.apiPrivateKey,bundles:e.bundles,wallets:n,organizationId:r},"*")}catch(e){u("[pvb] Discovery failed:",e),window.postMessage({type:d.discovered,error:String(e)},"*")}}var _=50;function I(){let e={stamper:null,bundles:[],velvetState:null},t=document.getElementById("root")||document.getElementById("__next");if(!t)return u("[pvb] No React root element found"),e;let r=null;for(let s of Object.keys(t))if(s.startsWith("__reactContainer$")||s.startsWith("__reactFiber$")){r=s;break}if(!r)return u("[pvb] No React fiber key found on root element"),e;let n=t[r];return n&&b(n,e,0),e}function b(e,t,r){if(!e||r>_||t.stamper&&t.bundles.length>0&&t.velvetState)return;let n=e.memoizedProps;if(n?.value&&typeof n.value=="object"){let o=n.value.apiStamper;o?.apiPublicKey&&o?.apiPrivateKey&&!t.stamper&&(t.stamper={apiPublicKey:String(o.apiPublicKey),apiPrivateKey:String(o.apiPrivateKey)},l("[pvb] Found VelvetContext stamper"))}let s=e.memoizedState,a=0;for(;s&&a<30;){let i=s.memoizedState;if(i&&typeof i=="object"&&!Array.isArray(i)){let o=i;if(o.bundles&&typeof o.bundles=="object"&&t.bundles.length===0){let f=o.bundles,w=Object.keys(f);if(w.length>0&&w.every(c=>/^[1-9A-HJ-NP-Za-km-z]{20,50}$/.test(c)||c.startsWith("0x"))){for(let[c,g]of Object.entries(f))if(g&&typeof g=="object"){let p=g;p.exportBundle&&typeof p.exportBundle=="string"&&t.bundles.push({address:c,exportBundle:String(p.exportBundle)})}t.bundles.length>0&&l(`[pvb] Found ${t.bundles.length} pre-exported bundle(s) in hook state`)}}o.solSubOrgId&&typeof o.solSubOrgId=="string"&&!t.velvetState&&(t.velvetState={solSubOrgId:String(o.solSubOrgId),ethSubOrgId:o.ethSubOrgId?String(o.ethSubOrgId):""},l(`[pvb] Found Velvet state: solSubOrgId=${t.velvetState.solSubOrgId.slice(0,12)}...`))}s=s.next,a++}e.child&&b(e.child,t,r+1),e.sibling&&b(e.sibling,t,r)}function h(){try{let e=localStorage.getItem("padreV2-walletsCache");if(!e)return[];let t=JSON.parse(e),r=[];if(typeof t=="object"&&!Array.isArray(t))for(let n of Object.keys(t)){let s=t[n];if(Array.isArray(s)){for(let a of s)if(a&&typeof a=="object"){let i=a.publicAddress||a.address||a.publicKey;typeof i=="string"&&r.push({address:i,walletType:a.walletType||void 0,walletId:a.walletId||void 0,walletName:a.walletName||void 0,subOrgId:a.subOrgId||void 0,isImported:a.isImported})}}}return r}catch{return[]}}function m(e,t){let r=new Map;for(let n of e)n.address&&r.set(n.address,{address:n.address,exportBundle:n.exportBundle,walletId:n.walletId});for(let n of t){if(!n.address)continue;let s=r.get(n.address);s?(s.walletType=n.walletType,s.walletId=s.walletId||n.walletId,s.subOrgId=n.subOrgId):r.set(n.address,{address:n.address,walletType:n.walletType,walletId:n.walletId,subOrgId:n.subOrgId})}return Array.from(r.values())}l("[pvb] Padre velvet bridge installed");})();
