"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var function_serialization_exports = {};
__export(function_serialization_exports, {
  deserializeFunction: () => deserializeFunction,
  isSerializedFunction: () => isSerializedFunction,
  serializeFunction: () => serializeFunction
});
module.exports = __toCommonJS(function_serialization_exports);
function isSerializedFunction(value) {
  return value instanceof MessagePort;
}
function serializeFunction(func) {
  const messageChannel = new MessageChannel();
  messageChannel.port1.onmessage = ({ data: [callId, args] }) => {
    Promise.resolve().then(() => func.apply(null, args)).then((result) => messageChannel.port1.postMessage([callId, result]));
  };
  return messageChannel.port2;
}
function deserializeFunction(serializedFunction) {
  let nextCallId = 0;
  const calls = /* @__PURE__ */ new Map();
  serializedFunction.onmessage = ({ data: [callId, result] }) => {
    calls.get(callId)?.(result);
    calls.delete(callId);
  };
  return function deserializedFunction(...args) {
    return new Promise((resolve) => {
      const callId = nextCallId++;
      calls.set(callId, resolve);
      serializedFunction.postMessage([callId, args]);
    });
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  deserializeFunction,
  isSerializedFunction,
  serializeFunction
});
//# sourceMappingURL=function-serialization.js.map