"use strict"; var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); var _a; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const t = Symbol("nextP"), e = /* @__PURE__ */ new WeakMap(); _a = t; class n { constructor(t2) { __publicField(this, _a, null); o(t2) && e.set(this, t2); } getNext() { return this[t]; } setNext(e2) { this[t] = e2; } hasNext() { return null != this[t]; } isNil() { return null == e.get(this); } isHandleAs(t2) { return e.get(this) === t2; } isOperation() { return a(e.get(this)); } bind(t2, n2) { return this.isNil() ? () => { } : e.get(this).bind(null, t2, n2); } call(t2, n2) { if (!this.isNil()) return e.get(this).call(null, t2, n2); } destroy() { e.delete(this); } } const r = Symbol("__is_Opeartion"); function s(t2, e2, r2) { const o2 = /* @__PURE__ */ new Set(), c2 = (t3, ...u2) => { if (t3.isNil()) return Promise.resolve(); if (o2.has(t3)) return Promise.reject(new Error("next called multiple times")); o2.add(t3); const l2 = function(t4) { const e3 = new n(); let r3 = e3; for (const e4 of t4) a(e4) && (r3.setNext(new n(e4)), r3 = r3.getNext()); return e3.getNext(); }(u2), h2 = t3.getNext() ?? new n(t3.isHandleAs(r2) ? void 0 : r2); return Promise.resolve(l2 ? s(l2, e2, t3.bind(e2, c2.bind(void 0, h2))) : t3.call(e2, c2.bind(void 0, h2))).finally(i.bind(void 0, t3, r2)); }; return c2(t2); } function i(t2, e2) { ((t2 == null ? void 0 : t2.isNil()) || (t2 == null ? void 0 : t2.isHandleAs(e2)) || (t2 == null ? void 0 : t2.isOperation())) && (t2 == null ? void 0 : t2.destroy()); } const o = (t2) => "function" == typeof t2, a = (t2) => o(t2) && true === t2[r], c = (t2) => null == t2, u = /* @__PURE__ */ new WeakMap(), l = /* @__PURE__ */ new WeakMap(); class h { constructor(t2, e2 = true) { var r2; u.set(this, new n(async (t3, e3) => { const n2 = await e3(); return c(n2) ? (t3 == null ? void 0 : t3.res) ?? t3 : n2; })), l.set(this, u.get(this)), r2 = t2, !c(r2 == null ? void 0 : r2.request) && o(r2 == null ? void 0 : r2.request) && function(t3, e3, n2 = true) { const r3 = t3.request; t3.request = async function() { const n3 = Array.prototype.slice.call(arguments), s2 = { args: n3, cfg: t3.defaults }; return await e3.handle(s2, async (t4, e4) => await r3.apply(this, n3).then((e5) => (t4.res = e5, e5)).catch((e5) => (t4.res = (e5 == null ? void 0 : e5.response) ?? e5, Promise.reject(e5))).finally(() => e4())); }, n2 && function(t4) { ["delete", "get", "head", "options"].forEach((e4) => { t4[e4] = function(n3, r4) { return t4.request({ url: n3, method: e4, ...r4 }); }; }), ["post", "put", "patch"].forEach((e4) => { t4[e4] = function(n3, r4, s2) { return t4.request({ url: n3, data: r4, method: e4, ...s2 }); }, t4[e4 + "Form"] = function(n3, r4, s2) { return t4.request({ url: n3, data: r4, method: e4, headers: { "Content-Type": "multipart/form-data" }, ...s2 }); }; }); }(t3); }(t2, this, e2); } use(...t2) { return t2.forEach((t3) => { var _a2, _b; const e2 = t3; if (!o(e2)) throw new TypeError("middleware or intercept must be a function!"); (_a2 = l.get(this)) == null ? void 0 : _a2.setNext(new n(e2)), l.set(this, (_b = l.get(this)) == null ? void 0 : _b.getNext()); }), this; } handle(t2, e2) { return s(u.get(this), t2, e2); } } function d(t2, e2 = true) { return new h(t2, e2); } exports.OnionInterceptor = h, exports.createFetchInterceptor = function(...t2) { const e2 = d(), { fetch: n2 } = window; return window.fetch = async function(t3, r2) { const s2 = { input: t3, init: r2 }; return await e2.handle(s2, async (e3, s3) => await n2.call(this, t3, r2).then((t4) => (e3.res = t4, t4)).catch((t4) => Promise.reject(t4)).finally(() => s3())); }, t2.length && e2.use(...t2), e2; }, exports.createInterceptor = d, exports.operate = function(t2) { if (!o(t2)) throw new TypeError("operate must be a function!"); return t2[r] = true, t2; };