var z = Object.defineProperty;
var n = (e, t) => z(e, "name", { value: t, configurable: !0 });
const X = void 0;
/**
 * @license
 * Copyright 2019 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
const x = Symbol("Comlink.proxy"), N = Symbol("Comlink.endpoint"), H = Symbol("Comlink.releaseProxy"), P = Symbol("Comlink.finalizer"), w = Symbol("Comlink.thrown"), T = /* @__PURE__ */ n((e) => typeof e == "object" && e !== null || typeof e == "function", "isObject"), _ = {
  canHandle: /* @__PURE__ */ n((e) => T(e) && e[x], "canHandle"),
  serialize(e) {
    const { port1: t, port2: r } = new MessageChannel();
    return R(e, t), [r, [r]];
  },
  deserialize(e) {
    return e.start(), W(e);
  }
}, v = {
  canHandle: /* @__PURE__ */ n((e) => T(e) && w in e, "canHandle"),
  serialize({ value: e }) {
    let t;
    return e instanceof Error ? t = {
      isError: !0,
      value: {
        message: e.message,
        name: e.name,
        stack: e.stack
      }
    } : t = { isError: !1, value: e }, [t, []];
  },
  deserialize(e) {
    throw e.isError ? Object.assign(new Error(e.value.message), e.value) : e.value;
  }
}, A = /* @__PURE__ */ new Map([
  ["proxy", _],
  ["throw", v]
]);
function V(e, t) {
  for (const r of e)
    if (t === r || r === "*" || r instanceof RegExp && r.test(t))
      return !0;
  return !1;
}
n(V, "isAllowedOrigin");
function R(e, t = globalThis, r = ["*"]) {
  t.addEventListener("message", /* @__PURE__ */ n(function g(s) {
    if (!s || !s.data)
      return;
    if (!V(r, s.origin)) {
      console.warn(`Invalid origin '${s.origin}' for comlink proxy`);
      return;
    }
    const { id: o, type: y, path: l } = Object.assign({ path: [] }, s.data), u = (s.data.argumentList || []).map(d);
    let a;
    try {
      const i = l.slice(0, -1).reduce((c, E) => c[E], e), f = l.reduce((c, E) => c[E], e);
      switch (y) {
        case "GET":
          a = f;
          break;
        case "SET":
          i[l.slice(-1)[0]] = d(s.data.value), a = !0;
          break;
        case "APPLY":
          a = f.apply(i, u);
          break;
        case "CONSTRUCT":
          {
            const c = new f(...u);
            a = G(c);
          }
          break;
        case "ENDPOINT":
          {
            const { port1: c, port2: E } = new MessageChannel();
            R(e, E), a = U(c, [c]);
          }
          break;
        case "RELEASE":
          a = void 0;
          break;
        default:
          return;
      }
    } catch (i) {
      a = { value: i, [w]: 0 };
    }
    Promise.resolve(a).catch((i) => ({ value: i, [w]: 0 })).then((i) => {
      const [f, c] = M(i);
      t.postMessage(Object.assign(Object.assign({}, f), { id: o }), c), y === "RELEASE" && (t.removeEventListener("message", g), C(t), P in e && typeof e[P] == "function" && e[P]());
    }).catch((i) => {
      const [f, c] = M({
        value: new TypeError("Unserializable return value"),
        [w]: 0
      });
      t.postMessage(Object.assign(Object.assign({}, f), { id: o }), c);
    });
  }, "callback")), t.start && t.start();
}
n(R, "expose");
function I(e) {
  return e.constructor.name === "MessagePort";
}
n(I, "isMessagePort");
function C(e) {
  I(e) && e.close();
}
n(C, "closeEndPoint");
function W(e, t) {
  const r = /* @__PURE__ */ new Map();
  return e.addEventListener("message", /* @__PURE__ */ n(function(s) {
    const { data: o } = s;
    if (!o || !o.id)
      return;
    const y = r.get(o.id);
    if (y)
      try {
        y(o);
      } finally {
        r.delete(o.id);
      }
  }, "handleMessage")), k(e, r, [], t);
}
n(W, "wrap");
function h(e) {
  if (e)
    throw new Error("Proxy has been released and is not useable");
}
n(h, "throwIfProxyReleased");
function O(e) {
  return m(e, /* @__PURE__ */ new Map(), {
    type: "RELEASE"
  }).then(() => {
    C(e);
  });
}
n(O, "releaseEndpoint");
const b = /* @__PURE__ */ new WeakMap(), p = "FinalizationRegistry" in globalThis && new FinalizationRegistry((e) => {
  const t = (b.get(e) || 0) - 1;
  b.set(e, t), t === 0 && O(e);
});
function j(e, t) {
  const r = (b.get(t) || 0) + 1;
  b.set(t, r), p && p.register(e, t, e);
}
n(j, "registerProxy");
function D(e) {
  p && p.unregister(e);
}
n(D, "unregisterProxy");
function k(e, t, r = [], g = function() {
}) {
  let s = !1;
  const o = new Proxy(g, {
    get(y, l) {
      if (h(s), l === H)
        return () => {
          D(o), O(e), t.clear(), s = !0;
        };
      if (l === "then") {
        if (r.length === 0)
          return { then: /* @__PURE__ */ n(() => o, "then") };
        const u = m(e, t, {
          type: "GET",
          path: r.map((a) => a.toString())
        }).then(d);
        return u.then.bind(u);
      }
      return k(e, t, [...r, l]);
    },
    set(y, l, u) {
      h(s);
      const [a, i] = M(u);
      return m(e, t, {
        type: "SET",
        path: [...r, l].map((f) => f.toString()),
        value: a
      }, i).then(d);
    },
    apply(y, l, u) {
      h(s);
      const a = r[r.length - 1];
      if (a === N)
        return m(e, t, {
          type: "ENDPOINT"
        }).then(d);
      if (a === "bind")
        return k(e, t, r.slice(0, -1));
      const [i, f] = S(u);
      return m(e, t, {
        type: "APPLY",
        path: r.map((c) => c.toString()),
        argumentList: i
      }, f).then(d);
    },
    construct(y, l) {
      h(s);
      const [u, a] = S(l);
      return m(e, t, {
        type: "CONSTRUCT",
        path: r.map((i) => i.toString()),
        argumentList: u
      }, a).then(d);
    }
  });
  return j(o, e), o;
}
n(k, "createProxy");
function F(e) {
  return Array.prototype.concat.apply([], e);
}
n(F, "myFlat");
function S(e) {
  const t = e.map(M);
  return [t.map((r) => r[0]), F(t.map((r) => r[1]))];
}
n(S, "processArguments");
const L = /* @__PURE__ */ new WeakMap();
function U(e, t) {
  return L.set(e, t), e;
}
n(U, "transfer");
function G(e) {
  return Object.assign(e, { [x]: !0 });
}
n(G, "proxy");
function $(e, t = globalThis, r = "*") {
  return {
    postMessage: /* @__PURE__ */ n((g, s) => e.postMessage(g, r, s), "postMessage"),
    addEventListener: t.addEventListener.bind(t),
    removeEventListener: t.removeEventListener.bind(t)
  };
}
n($, "windowEndpoint");
function M(e) {
  for (const [t, r] of A)
    if (r.canHandle(e)) {
      const [g, s] = r.serialize(e);
      return [
        {
          type: "HANDLER",
          name: t,
          value: g
        },
        s
      ];
    }
  return [
    {
      type: "RAW",
      value: e
    },
    L.get(e) || []
  ];
}
n(M, "toWireValue");
function d(e) {
  switch (e.type) {
    case "HANDLER":
      return A.get(e.name).deserialize(e.value);
    case "RAW":
      return e.value;
  }
}
n(d, "fromWireValue");
function m(e, t, r, g) {
  return new Promise((s) => {
    const o = Y();
    t.set(o, s), e.start && e.start(), e.postMessage(Object.assign({ id: o }, r), g);
  });
}
n(m, "requestResponseMessage");
function Y() {
  return new Array(4).fill(0).map(() => Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toString(16)).join("-");
}
n(Y, "generateUUID");
export {
  N as createEndpoint,
  R as expose,
  P as finalizer,
  G as proxy,
  x as proxyMarker,
  H as releaseProxy,
  X as then,
  U as transfer,
  A as transferHandlers,
  $ as windowEndpoint,
  W as wrap
};
