import { isHostAction } from "../host-api";
import { hasTransferables } from "./actions";
import { MessageChannel } from "./message-channel";
class BuilderAPIProxy {
  messageChannel;
  constructor({ appOrigin }) {
    this.messageChannel = new MessageChannel({ appOrigin });
  }
  // low-level, action-based API
  setup({ onHostAction }) {
    this.messageChannel.setup((event) => {
      const action = event.data;
      if (!isHostAction(action)) {
        console.warn("Unexpected host action", action);
        return;
      }
      onHostAction(action);
    });
  }
  execute(action) {
    if (hasTransferables(action)) {
      const { transferables, ...forwardedAction } = action;
      this.messageChannel.postMessage(forwardedAction, transferables);
      return;
    }
    this.messageChannel.postMessage(action);
  }
  dispatchBuffered() {
    this.messageChannel.dispatchBuffered();
  }
  teardown() {
    this.messageChannel.teardown();
  }
}
export {
  BuilderAPIProxy
};
//# sourceMappingURL=proxy.js.map