"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 use_builder_connection_ping_exports = {};
__export(use_builder_connection_ping_exports, {
  useBuilderConnectionPing: () => useBuilderConnectionPing
});
module.exports = __toCommonJS(use_builder_connection_ping_exports);
var import_react = require("react");
var import_actions = require("../../../../state/builder-api/actions");
var import_shared_api = require("../../../../state/shared-api");
const CONNECTION_PING_INTERVAL_MS = 20;
function useBuilderConnectionPing({ appOrigin }) {
  (0, import_react.useEffect)(() => {
    let connectionInterval;
    if (window.parent !== window) {
      window.addEventListener("message", messageHandler);
      window.parent.postMessage((0, import_shared_api.makeswiftConnectionInit)(), { targetOrigin: appOrigin });
    }
    return () => {
      window.removeEventListener("message", messageHandler);
      if (connectionInterval != null) {
        window.clearInterval(connectionInterval);
        connectionInterval = null;
      }
    };
    function messageHandler(event) {
      if (event.origin === appOrigin && event.data.type === import_shared_api.SharedActionTypes.MAKESWIFT_CONNECTION_INIT) {
        if (connectionInterval != null) {
          window.clearInterval(connectionInterval);
          connectionInterval = null;
        }
        connectionInterval = window.setInterval(() => {
          window.parent.postMessage(
            (0, import_actions.makeswiftConnectionCheck)({ currentUrl: window.location.href }),
            { targetOrigin: appOrigin }
          );
        }, CONNECTION_PING_INTERVAL_MS);
      }
    }
  }, [appOrigin]);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useBuilderConnectionPing
});
//# sourceMappingURL=use-builder-connection-ping.js.map