"use client";
import { useEffect, useRef } from "react";
import { matchWildcardPattern } from "@open-mercato/shared/lib/patterns/wildcard";
const APP_EVENT_DOM_NAME = "om:event";
function matchesPattern(pattern, eventId) {
  return matchWildcardPattern(eventId, pattern);
}
function useAppEvent(eventPattern, handler, deps = []) {
  const handlerRef = useRef(handler);
  handlerRef.current = handler;
  useEffect(() => {
    const listener = (e) => {
      const detail = e.detail;
      if (!detail || typeof detail.id !== "string") return;
      if (matchesPattern(eventPattern, detail.id)) {
        handlerRef.current(detail);
      }
    };
    window.addEventListener(APP_EVENT_DOM_NAME, listener);
    return () => {
      window.removeEventListener(APP_EVENT_DOM_NAME, listener);
    };
  }, [eventPattern, ...deps]);
}
export {
  APP_EVENT_DOM_NAME,
  matchesPattern,
  useAppEvent
};
//# sourceMappingURL=useAppEvent.js.map
