import { useCallback as l, useEffect as f } from "react";
import { useUpdatingRef as c } from "./useUpdatingRef.js";
const d = (e) => e !== null && typeof e == "object" && "type" in e && typeof e.type == "string", o = (e) => typeof e == "function", g = (e) => typeof e == "object" && "origin" in e && typeof e.origin == "string" && "handler" in e && o(e.handler), u = (e) => typeof e == "object" && "handlers" in e && e.handlers !== null && typeof e.handlers == "object", a = (e, r) => r === "*" || e.origin === r, p = (e) => {
  const r = c(e), i = l((n) => {
    const s = r.current;
    if (o(s))
      s(n);
    else if (g(s))
      a(n, s.origin) && s.handler(n);
    else if (u(s)) {
      if (!a(n, s.origin ?? "*") || !d(n.data))
        return;
      const t = s.handlers[n.data.type];
      t && t(n.data.payload);
    }
  }, []);
  f(() => (window.addEventListener("message", i, !1), () => {
    window.removeEventListener("message", i, !1);
  }), []);
};
export {
  p as default
};
//# sourceMappingURL=useIncomingPostMessages.js.map
