"use strict";var d=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var F=Object.prototype.hasOwnProperty;var N=(t,e)=>{for(var r in e)d(t,r,{get:e[r],enumerable:!0})},_=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of q(e))!F.call(t,o)&&o!==r&&d(t,o,{get:()=>e[o],enumerable:!(n=w(e,o))||n.enumerable});return t};var k=t=>_(d({},"__esModule",{value:!0}),t);var f=(t,e,r)=>{if(!e.has(t))throw TypeError("Cannot "+r)};var g=(t,e,r)=>(f(t,e,"read from private field"),r?r.call(t):e.get(t)),m=(t,e,r)=>{if(e.has(t))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(t):e.set(t,r)},x=(t,e,r,n)=>(f(t,e,"write to private field"),n?n.call(t,r):e.set(t,r),r);var E={};N(E,{DOProxy:()=>y});module.exports=k(E);function h(t,e,r){return{type:t,prop:String(e),args:r}}async function P(t){let e=[];for(let r of t)e.push(await Promise.resolve(r));return e}function i(t,e,r){return{_stub:null,_mode:"execute",get mode(){return this._mode},get stub(){return this._stub},setStub(o){this._stub=o},getStub(){},setMode(o){this._mode=o},get methods(){if(this._methods)return this._methods;let o={};return e.forEach(s=>{o[s]=async(...a)=>{let u=h(t,s,a);return this.mode==="batch"?u:r(this.stub,u)}}),this._methods=o,this._methods}}}function O(t,e){return i("function",Array.from(t),e)}function S(t){let e=["constructor","fetch"],r=new Set,n=t;for(;n!==null;){for(let o of Object.getOwnPropertyNames(n))!e.includes(o)&&typeof t[o]=="function"&&r.add(o);n=Object.getPrototypeOf(n),Object.prototype===n&&(n=null)}return r}var A=["delete","deleteAlarm","deleteAll","get","getAlarm","list","put","setAlarm","sync"];function D(t){return i("storage",A,t)}function j(t){let e=S(t),r=D(p),n=O(e,p);return o=>{let s=Object.create(r),a=Object.create(n);return s.setStub(o),a.setStub(o),I(o,a,s)}}function I(t,e,r){let n=Object.keys(e.methods).length>0;return new Proxy({id:t.id,stub:t},{get:(o,s)=>{if(s==="batch")return async function(u){r.setMode("batch"),e.setMode("batch");let b=await u();if(!Array.isArray(b))throw Error(`\`batch\` callback should return an array, got: ${typeof b}`);let l=await P(b);return l.forEach((R,M)=>{if(!R?.type)throw Error(`Returned array has invalid job at index ${M}. Only \`storage\` and \`class\` methods supported.`)}),r.setMode("execute"),e.setMode("execute"),p(t,l)};if(s==="class"&&n)return e.methods;if(s==="storage")return r.methods;let a=o[s];return typeof a=="function"?a.bind(o):a},set(o,s,a){return o[s]=a,!0}})}async function p(t,e){return(await t.fetch(new Request("https://do-proxy/",{method:"POST",body:JSON.stringify(e)})).then(n=>n.text()).then(n=>JSON.parse(n,function(s,a){return typeof a=="object"&&a!==null&&a.__dataType==="Map"?new Map(a.value):a}))).data}var v="do-proxy";async function B(t,e){return t.storage[e.prop](...e.args)}var T=new Map;function C(t){let e=T.get(t);if(e)return e;let r=j(t);return T.set(t,r),r}var c,y=class{constructor(e,r){m(this,c,void 0);x(this,c,e)}async fetch(e){let r=null,n=async s=>{if(s.type==="function")return await this[s.prop](...s.args);if(s.type==="storage")return await B(g(this,c),s)};try{r=await e.json()}catch{}if(!r)throw Error(`You can't directly call stub's fetch method if you're using ${v}. Use class methods instead.`);let o;if(Array.isArray(r)){o=[];for(let s of r)o.push(await n(s))}else o=await n(r);return new Response(JSON.stringify({data:o},function(a,u){return u instanceof Map?{__dataType:"Map",value:Array.from(u.entries())}:u}),{headers:{"content-type":"application/json"}})}static wrap(e){let r=C(this.prototype);return new Proxy(e,{get:(n,o)=>{if(o==="get")return function(s){let a=e.get(s);return r(a)};if(o==="getByName")return function(s){let a=e.get(e.idFromName(s));return r(a)};if(o==="getById")return function(s){let a=e.get(e.idFromString(s));return r(a)};if(o in n){let s=n[o];return typeof s=="function"?s.bind(n):s}}})}static from(e){let r=C(this.prototype);return{get(n){let o=e.get(e.idFromName(n));return r(o)},getById(n){let o=e.get(n);return r(o)},getByString(n){let o=e.get(e.idFromString(n));return r(o)}}}};c=new WeakMap;0&&(module.exports={DOProxy}); //# sourceMappingURL=do-proxy.cjs.map