"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 proxy_exports = {};
__export(proxy_exports, {
  BuilderAPIProxy: () => BuilderAPIProxy
});
module.exports = __toCommonJS(proxy_exports);
var import_host_api = require("../host-api");
var import_actions = require("./actions");
var import_message_channel = require("./message-channel");
class BuilderAPIProxy {
  messageChannel;
  constructor({ appOrigin }) {
    this.messageChannel = new import_message_channel.MessageChannel({ appOrigin });
  }
  // low-level, action-based API
  setup({ onHostAction }) {
    this.messageChannel.setup((event) => {
      const action = event.data;
      if (!(0, import_host_api.isHostAction)(action)) {
        console.warn("Unexpected host action", action);
        return;
      }
      onHostAction(action);
    });
  }
  execute(action) {
    if ((0, import_actions.hasTransferables)(action)) {
      const { transferables, ...forwardedAction } = action;
      this.messageChannel.postMessage(forwardedAction, transferables);
      return;
    }
    this.messageChannel.postMessage(action);
  }
  dispatchBuffered() {
    this.messageChannel.dispatchBuffered();
  }
  teardown() {
    this.messageChannel.teardown();
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  BuilderAPIProxy
});
//# sourceMappingURL=proxy.js.map