const A=["permissions"];function O(i){const p=i.shellOrigin,g=new Set([...A,"user"]),v=Object.keys(i.capabilities??{}).filter(e=>!g.has(e)),c=512,M=32,n=e=>Promise.reject({code:"bad_request",message:e}),l=e=>e===void 0||typeof e=="string"&&e.length<=c,u=e=>e===void 0||Array.isArray(e)&&e.length<=M&&e.every(r=>typeof r=="string"&&r.length<=c),_="state takes no arguments or one capability name",d="request takes no arguments or an array of capability names";if(v.length===0){const e=i.pipe("permissions");i.mount("permissions",{state:e.wrap("state",r=>l(r)?Promise.resolve(r===void 0?{}:"unavailable"):n(_)),request:e.wrap("request",r=>u(r)?Promise.resolve(r===void 0?{}:Object.fromEntries(r.map(s=>[s,"unavailable"]))):n(d))});return}let T=0;const o=new Map;addEventListener("message",e=>{if(e.source!==parent||e.origin!==p)return;const r=e.data;if(!r)return;const s=typeof r.id=="string"?r.id:null;if(!s)return;const t=o.get(s);if(t){if(r.__frame_cap_ack===!0){clearTimeout(t.timer),t.timer=setTimeout(()=>{o.delete(s),t.reject({code:"upstream_error",message:"no verdict from shell after prompt"})},9e5);return}r.__frame_cap_r===!0&&(o.delete(s),clearTimeout(t.timer),r.error!==null&&r.error!==void 0?t.reject(r.error):t.resolve(r.result))}});const m=(e,r)=>new Promise((s,t)=>{const a="p"+ ++T,E=setTimeout(()=>{o.delete(a),t({code:"upstream_error",message:"no reply from shell"})},13e4);o.set(a,{resolve:s,reject:t,timer:E}),parent.postMessage({__frame_cap:!0,cap:"permissions",id:a,method:e,args:r},p)}),f=i.pipe("permissions");i.mount("permissions",{state:f.wrap("state",e=>l(e)?m("state",e===void 0?[]:[e]):n(_)),request:f.wrap("request",e=>u(e)?m("request",e===void 0?[]:[e]):n(d))})}export{O as install};
